This post is completed by 3 users
|Add to List|
Graph – Depth First Traversal
Objective – Given a graph, do the depth first traversal(DFS).
What is depth-first traversal- Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking. Source - Wiki
- Use Stack.
- First add the Starting Node to the Stack.
- Pop out an element from Stack and add all of its connected nodes to stack.
- Repeat the above two steps until the Stack is empty.
- Below is a walk through of the graph above.
source = 0 is connected to nodes: 2 1 source = 1 is connected to nodes: 3 2 source = 2 is connected to nodes: 3 source = 3 is connected to nodes: 4 source = 4 is connected to nodes: 5 1 0 Depth First Traversal: 0 1 3 4 5 2
- Find the nearest building which has bike | Find nearest specific vertex from source in a graph. Hard