Graph Traversals
Graph Traversals
PRESENTED BY
D.ANANDHASILAMBARASAN
KIT - CBE
GRAPH TRAVERSALS
Graph traversal is technique used for searching a vertex in a graph. The graph traversal
is also used to decide the order of vertices to be visit in the search process.
A graph traversal finds the edges to be used in the search process without creating
loops that means using graph traversal we visit all vertices of graph without getting
into looping path.
There are two graph traversal techniques and they are as follows...
BFS (breadth first search)
We use queue data structure with maximum size of total number of vertices in the
graph to implement BFS traversal of a graph.
Step 4: When there is no new vertex to be visit from the vertex at front of the queue then
delete that vertex from the queue.
Step 6: When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph.
DFS (DEPTH FIRST SEARCH)
DFS traversal of a graph, produces a spanning tree as final result. Spanning tree is a
graph without any loops.
We use stack data structure with maximum size of total number of vertices in the graph
to implement DFS traversal of a graph.
Step 2: select any vertex as starting point for traversal. Visit that vertex and push it on to
the stack.
Step 3: visit any one of the adjacent vertex of the vertex which is at top of the stack
which is not visited and push it on to the stack.
Step 4: repeat step 3 until there are no new vertex to be visit from the vertex on top of the
stack.
Step 5: when there is no new vertex to be visit then use back tracking and pop one
vertex from the stack.
Step 7: when stack becomes empty, then produce final spanning tree by removing unused
edges from the graph.
Back tracking is coming back to the vertex from which we came to current vertex.