Graph Traversal Techniques
Graph Traversal Techniques
Techniques
Bheekya D
Graph Traversal Definition
● Path finding problems of Graph are called Search And Traversal Techniques.
● Determine there exists a path or not in the given graph G = (V, E)
● A more general form of traversal is to determine a path from a given starting
vertex v to all other vertices u for all u belong into V.
● This problem can be solved by starting at vertex v and systematically
searching the graph G for vertices that can be reached from v.
● We have two search methods known as
○ Breadth First Search(BFS),
○ Depth First Search (DFS)
Breadth First Search (BFS)
● Breadth First Search (BFS) algorithm starts at the root of the graph and visits all
nodes at the current depth level before moving on to the nodes at the next depth
level.
● In graphs, we may come to the same node again. To avoid processing a node
more than once, we divide the vertices into two categories: Visited and Not visited.
● A boolean visited array is used to make the difference between visited and not
visited vertices.
● A BFS uses Queue data structure for traversal. All the adjacent unvisited nodes of
the current level are pushed into the queue and the nodes of the current level are
marked visited and popped from the queue.
BFS: Example
Step1: Initially queue and visited arrays are empty.
Disadvantages:
● It requires lots of memory since each level of the tree must be saved into
memory to expand the next level.
● BFS needs lots of time if the solution is far away from the root node.
Depth First Search (DFS)
● Depth-first search is an algorithm for traversing or searching graph data structures.
● The DFS algorithm starts at the root node (selecting some arbitrary node as the root
node in the case of a graph) and explores as far as possible along each branch before
backtracking.
● In graphs, we may come to the same node again. To avoid processing a node more
than once, we divide the vertices into two categories: Visited and Not visited.
● A boolean visited array is used to make the difference between visited and not visited
vertices.
● A DFS uses Stack data structure for traversal. One adjacent unvisited node of the
current node is pushed into the stack and then explore the current node. If there is no
further explore backtrack to current node parent.
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
1. DFS Spanning tree
Advantage:
● DFS requires very less memory as it only needs to store a stack of the
nodes on the path from root node to the current node.
● It takes less time to reach to the goal node than BFS algorithm (if it
traverses in the right path).
Disadvantage:
● There is the possibility that many states keep reoccurring, and there is no
guarantee of finding the solution.
● DFS algorithm goes for deep down searching and sometime it may go to
the infinite loop
Time and space complexity of DFS and BFS
Connected Graph
● A graph is said to be connected graph if there is a path between every pair of
vertex.
● From every vertex to any other vertex there must be some path to traverse. This is
called the connectivity of a graph.
● A graph is said to be disconnected, if there exists multiple disconnected vertices
and edges.
● Graph connectivity theories are essential in network applications, routing
transportation networks, network tolerance etc.
Properties of connected Graph
● If G is a connected undirected graph, then all vertices of G will get visited on the first
call of DFS or BFS.
● If G is not connected, then at least two calls of BFS or DFS will be needed.
● Hence, DFS and BFS can be used to determine whether G is connected.
Fully Connected Graph
A fully Connected graph, also known as a complete graph, is one with n vertices and
n-1 degrees per vertex. Alternatively said, every vertex connects to every other vertex.
Cut Vertex
● A single vertex whose removal disconnects a graph is called a cut-vertex.
● Let G be a connected graph. A vertex v of G is called a cut vertex of G, if G-v (Remove v from
G) results a disconnected graph.
● When we remove a vertex from a graph then graph will break into two or more graphs. This
vertex is called a cut vertex.
● A connected graph G may have maximum (n-2) cut vertices.
Cut Edge (Bridge)
● A cut- Edge or bridge is a single edge whose removal disconnects a graph.
● Let G be a connected graph. An edge e of G is called a cut edge of G, if G-e
(Remove e from G) results a disconnected graph.
● When we remove an edge from a graph then graph will break into two or more
graphs. This removal edge is called a cut edge or bridge.
● A connected graph G may have at most (n-1) cut edges.
Spanning Tree
● A spanning tree can be defined as the subgraph of an undirected connected graph.
● It includes all the vertices along with the least possible number of edges.
● If any vertex is missed, it is not a spanning tree.
● A spanning tree is a subset of the graph that does not have cycles, and it also cannot
be disconnected.
● Let T(V1,E1) is said to be a Spanning tree of given Connected Graph G(V,E) such that
○ set(V1) is equal to set(V)
○ set(E1) is subset of set(E)
○ Maximum no of edges in spanning tree is |V|-1
Spanning Tree
● All the possible spanning trees created from the given graph G would have the same number of vertices, but the
number of edges in the spanning tree would be equal to the number of vertices in the given graph minus 1.
● A complete undirected graph can have nn-2 number of spanning trees where n is the number of vertices in the graph.
Spanning Tree
● A spanning tree can be defined as the subgraph of an undirected connected graph.
● It includes all the vertices along with the least possible number of edges.
● If any vertex is missed, it is not a spanning tree.
● A spanning tree is a subset of the graph that does not have cycles, and it also cannot
be disconnected.
● Let T(V1,E1) is said to be a Spanning tree of given Connected Graph G(V,E) such that
○ set(V1) is equal to set(V)
○ set(E1) is subset of set(E)
○ Maximum no of edges in spanning tree is |V|-1
Connected Components : Set of vertices which are reachable or is a sub-graph,
in which any two verices are connected by paths and no vertices is connected
with any vertices in the super graph.
Given an undirected graph, it’s important to find out the number of connected components to
analyze the structure of the graph – it has many real-life applications.
We can use either DFS or BFS for this task.