Answer to Question #12890 in C++ for JX

Question #12890
How can i implement the Depth First Search graph traversal algorithm by using the pseudocode below?

dfs(in graph:Graph, in v:Vertex)
s.createStack( )
s.push(v)
Mark v as visited
while (!s.isEmpty( ))
{
if (no unvisited vertices are adjacent to the vertex on the top of the stack)
{
s.pop( ) // backtrack
}
else
{
Select an unvisited vertex u adjacent to the vertex on the top of the stack
s.push(u)
Mark u as visited
}
}
1
Expert's answer
2012-08-21T09:26:00-0400
void dfs(int** adjacentMatrix, int *visited, int vertex, int count){

cout << vertex << " ";
visited[vertex] = 1;
for
(int i=0; i<cout; i++){
if ((adjacentMatrix[vertex][i] == 1)
&& ( visited[i] == 0)){
dfs(adjacentMatrix, visited, i,
count);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS