Skip to content
Snippets Groups Projects
Commit f9af5a80 authored by Lukas Garbas's avatar Lukas Garbas Committed by p-hamann
Browse files

Added indexing of edges to graph class

parent 8e338172
Branches
1 merge request!7Incremental bfs
......@@ -7,6 +7,31 @@ unsigned int graph::num_nodes() const {
return nodes_;
}
unsigned int graph::num_edges() const {
return indexed_edges_.size();
}
const std::vector<unsigned int>& graph::get_adjacency(node_id v) const {
return adjacency_list_[v-1];
}
void graph::index_edges() {
unsigned int nodes = num_nodes();
unsigned int eid = 0;
for (node_id u = 1; u <= nodes; u++) {
const std::vector<node_id> & neighbors = get_adjacency(u);
for (node_id v : neighbors) {
if (u < v) {
auto node_pair = std::make_pair(u, v);
indexed_edges_[node_pair] = eid;
eid++;
}
}
}
}
unsigned int graph::get_edge_id(node_id u, node_id v) const {
return indexed_edges_.at(std::make_pair(u, v));
}
......@@ -2,6 +2,7 @@
#define GRAPH_HPP
#include <vector>
#include <map>
using node_id = unsigned int;
......@@ -18,6 +19,16 @@ public:
* @return Number of nodes
*/
unsigned int num_nodes() const;
/** @brief Returns the number of edges in the graph
* @return Number of edges after have been indexed
*/
unsigned int num_edges() const;
/**
* @brief Indexes edges of the graph
*/
void index_edges();
/** @brief Provides access to the adjacency of a node
* @param Node Id
......@@ -25,9 +36,16 @@ public:
*/
const std::vector<unsigned int>& get_adjacency(node_id v) const;
/** @brief Returns the id of an edge after calling index_edges()
* @param Two Node ids (u, v)
* @return Edge id
*/
unsigned int get_edge_id(node_id u, node_id v) const;
private:
unsigned int nodes_;
std::vector<std::vector<unsigned int>> adjacency_list_;
std::map<std::pair<node_id, node_id>, unsigned int> indexed_edges_;
};
#endif
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment