#include #include #include #include #include #include #include using namespace std; #include "Graph.h" //Create a graph object with the given nodes //and initialize the adjacent lists to empty template Graph::Graph (NodeType nodeArray[], int len, bool d) { for (int i=0;i adjacentList; edges.push_back (adjacentList); } directed = d; } //Precondition: from and to nodes are in the vector of nodes // template void Graph::AddEdge (const NodeType & from, const NodeType & to) { int cnt=0; int fromIndex, toIndex; //Find from and to nodes in the nodes vector for (int i=0;i list Graph::AdjacentNodes (const NodeType & u) { for (int i=0;i int Graph::FindNode (const NodeType & u) { for (int i=0;i void Graph::Display() { list::iterator it; if (directed) cout <<"Directed Graph:\n"; else cout <<"Undirected Graph:\n"; cout <<"Vertex: {"; for (int i=0;i"< void Graph::BFS_Explore (NodeType s) { typedef enum Color{ White=0, Gray=1, Black=2} Color; map d; //the depth/hop count of all nodes from s map pred; // pred[u] is the node that leads us to u map color; //color[u] will be White, Gray, Black queue Q; // initialize all nodes to white for (int i=0;i adjNodes = AdjacentNodes (u); for (int j:adjNodes){ NodeType v=nodes[j]; if (color[v]==White) { color[v]=Gray; d[v]=d[u]+1; pred[v]=u; Q.push(v); } } color[u]=Black; } //Display color, depth, predecessor for (int i=0;i