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); } }
Comments
Leave a comment