Created
February 27, 2020 01:23
-
-
Save kumar-akshay324/36d62facc6f7b611476424d36c6419eb to your computer and use it in GitHub Desktop.
Creating an adjacency list based representation of a graph in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <memory> | |
#include <algorithm> | |
// an edge has a start and an end node | |
namespace adj_list_namespace { | |
using Edge = std::pair<int, int>; | |
struct AdjacencyNode { | |
int data; | |
AdjacencyNode* next_node; | |
double weight; | |
AdjacencyNode() | |
: next_node(nullptr){} | |
}; | |
class AdjacencyList { | |
public: | |
AdjacencyList() = default; | |
~AdjacencyList() = default; | |
void setNumNodes(unsigned int num_nodes); | |
bool addGraph(std::vector<Edge> edges, std::vector<double> weights); | |
bool printGraph(); | |
private: | |
std::vector<std::unique_ptr<AdjacencyNode>> head_nodes_; | |
unsigned int num_nodes_; | |
}; | |
void AdjacencyList::setNumNodes(unsigned int num_nodes) { | |
num_nodes_ = num_nodes; | |
for (unsigned int i = 0; i < num_nodes; i++) { | |
std::unique_ptr<AdjacencyNode> new_node = std::make_unique<AdjacencyNode>(); | |
new_node->data = i; | |
if (!new_node) { | |
continue; | |
} | |
head_nodes_.push_back(std::move(new_node)); | |
} | |
}; | |
bool AdjacencyList::addGraph(std::vector<Edge> edges, std::vector<double> weights) { | |
// if ((edges.size() != weights.size()) || (edges.size() != num_nodes_)) { | |
// return false; | |
// } | |
for (unsigned int i = 0; i < edges.size(); i++) { | |
int start = edges[i].first; | |
int end = edges[i].second; | |
AdjacencyNode* start_node = head_nodes_[start].get(); | |
// Traverse to the end of this node's connections and append the incoming edge nodes | |
while (1) { | |
if (start_node->next_node) { | |
start_node = start_node->next_node; | |
} else { | |
AdjacencyNode* node_to_add = new AdjacencyNode(); | |
node_to_add->data = end; | |
node_to_add->weight = weights[i]; | |
start_node->next_node = node_to_add; | |
break; | |
} | |
} | |
} | |
} | |
bool AdjacencyList::printGraph() { | |
for (unsigned int i = 0; i < head_nodes_.size(); i++) { | |
if (!head_nodes_[i]) { | |
std::cout << "X" << std::endl; | |
continue; | |
} | |
std:: cout << "( Node: " << head_nodes_[i]->data << ", Weight: "<< head_nodes_[i]->weight << " ) ->"; | |
bool result = true; | |
AdjacencyNode* new_node = head_nodes_[i].get(); | |
if (!new_node) { | |
continue; | |
} | |
while (result) { | |
AdjacencyNode* next_node = new_node->next_node; | |
if (!next_node) { | |
result = false; | |
continue; | |
} | |
std::cout << "( Node: " << next_node->data << ", Weight: "<< next_node->weight << " ) ->"; | |
new_node = next_node; | |
} | |
std::cout << " " << std::endl; | |
} | |
return true; | |
} | |
} | |
int main() { | |
adj_list_namespace::AdjacencyList adj_list; | |
adj_list.setNumNodes(5); | |
std::vector<adj_list_namespace::Edge> node_edges_to_add; | |
std::vector<double> nodes_weights; | |
node_edges_to_add.push_back(std::make_pair(0,1)); | |
node_edges_to_add.push_back(std::make_pair(0,2)); | |
node_edges_to_add.push_back(std::make_pair(1,4)); | |
node_edges_to_add.push_back(std::make_pair(2,3)); | |
node_edges_to_add.push_back(std::make_pair(2,5)); | |
node_edges_to_add.push_back(std::make_pair(3,1)); | |
node_edges_to_add.push_back(std::make_pair(1,2)); | |
node_edges_to_add.push_back(std::make_pair(1,5)); | |
node_edges_to_add.push_back(std::make_pair(4,3)); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
nodes_weights.push_back(99.9); | |
adj_list.addGraph(node_edges_to_add, nodes_weights); | |
adj_list.printGraph(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment