Hello everyone,
I am new here. I use this website excessively everyday for reference. I hit a blocker while coding this simple program and I guess I can use some help from the experts here.
I am trying to create a graph or a logical implementation of it (which I did successfully). Then I tried traversing it using 'Depth First Search'. It worked for the first iteration of the node from where I started and after that, it just goes haywire.
I am using graphNode.h and dfs.cpp (which includes graphNode.h). Following is graphNode.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <list>
using namespace std;
class graphNode {
public:
string value;
list<graphNode> adjacents;
bool isVisited;
public:
graphNode () {
isVisited = false;
}
void addNode (string str);
void addAdjacent (graphNode g);
};
void graphNode :: addNode (string str) {
value.assign(str);
}
void graphNode :: addAdjacent (graphNode g) {
adjacents.push_back (g);
}
|
Following is dfs.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <stack>
#include </home/graphs/graphNode.h>
using namespace std;
class dfs : public graphNode {
public:
stack<graphNode> st;
public:
void traverseGraphIter (graphNode node);
};
void dfs :: traverseGraphIter (graphNode node) {
list<graphNode>::iterator it;
st.push(node);
do {
node.isVisited=true;
st.pop();
cout<<"\n #### Popped node "<<node.value;
// Go over node's adjacents and add them to the stack if they
// were not visited earlier
for (it=node.adjacents.begin(); it!=node.adjacents.end(); it++) {
if (it->isVisited == false) {
st.push(*it);
}
}
/* Have to put this check to avoid a SEGV because
compiler does not thrown an error when the stack is empty
and a method call like pop or top is being called on it. */
if (!st.empty()) {
node=st.top();
cout<<"\n Node "<<node.value<<"'s adjacent list contains "<<(int)node.adjacents.size()<<" elements";
}
} while (!st.empty());
cout<<"\n Exiting the do-while loop";
}
int main () {
int size=4;
string s;
graphNode *node = new graphNode[size];
for (int i=0; i<size; i++) {
cout<<"\n Enter the value for the node : ";
cin>>s;
node[i].addNode(s);
}
//Hard coding the adjacents for now
node[0].addAdjacent (node[1]);
node[0].addAdjacent (node[2]);
node[1].addAdjacent (node[2]);
node[2].addAdjacent (node[3]);
node[2].addAdjacent (node[0]);
//Check if adjacents are added correctly
list<graphNode>::iterator it;
for (int i=0; i<4; i++) {
if (node[i].adjacents.empty()) {
cout<<"\n Node "<<node[i].value<<"'s adjacent list is empty. Continuing from the top";
continue;
}
for (it=node[i].adjacents.begin(); it!=node[i].adjacents.end(); it++ ) {
cout<<"\n Node "<<node[i].value<<"'s adjacent is "<<it->value;
}
}
dfs d;
d.traverseGraphIter (node[0]);
//Destructor
delete[] node;
cout<<"\n\n";
return 0;
}
|
I left the debugging stuff there in case you guys want to run it to verify if things are in right place to begin with. I have hard coded the number of graphNodes and the adjacents. So for nodes A, B, C and D (using node: adjacents format), I am doing:
A: B, C
B: C
C: D, A
D: <EMPTY>
Idea: I am just creating 4 graphNodes and storing each graphNode's adjacent list with the node. The list is of type graphNode, so it should store the graphNode object itself. Starting from the graphNode object that I pass to the function traverseGraphIter(graphNode):
1) Add each node to the stack, mark it visited, pop it and print it.
2) Then explore its unvisited adjacents, add them to the stack. Then for each adjacent continue from step 1 .....while stack is not empty.
Problem: Let us say that I enter node A, B, C and D. I pass object for node A to the function traverseGraphIter(graphNode). A contains 2 adjacents (B and C). Node B and Node C get added to the stack. When the program print the number of elements in the adjacent list of B and C, it is 0 and indeed B and C adjacent list is empty. I thought that the root cause was that I was sending a copy of A which may not be copying B & C's list information. Don't know why that should happen because B and C's values and isVisited fields print just fine. I tried passing by reference. That did not work either.
What am I doing wrong here? I have spent hours debugging this. Help please!!
Thanks much!