# 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

}

}

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

}

}

Expert's answer

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);

}

}

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);

}

}

## Comments

## Leave a comment