![]() Both S and D are adjacent to A but we are concerned for unvisited nodes only. Explore any unvisited adjacent node from A. Mark A as visited and put it onto the stack. ![]() For this example, we shall take the node in an alphabetical order. We have three nodes and we can pick any of them. Explore any unvisited adjacent node from S. Mark S as visited and put it onto the stack. Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty. (It will pop up all the vertices from the stack, which do not have adjacent vertices.) Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. Rule 1 − Visit the adjacent unvisited vertex. ![]() Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.Īs in the example given above, DFS algorithm traverses from S to A to D to G to E to B first, then to F and lastly to C.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |