Skip to main content
Analysis and Design of Algorithms
Graph Algorithms
Analysis and Design of Algorithms
Graph
Directed vs Undirected Graph
Acyclic vs Cyclic Graph
Backedge
Search vs Traversal
Breadth First Traversal
Depth First Traversal
Detect Cycle in a Directed Graph
Analysis and Design of Algorithms
 Graph data structure consists of a
finite set of vertices or nodes. The
interconnected objects are
represented by points termed as
vertices, and the links that connect
the vertices are called edges.
A
B C
D E
F
Node or Vertices
Edge
Analysis and Design of Algorithms
 Vertex: Each node of the graph is
represented as a vertex.
 V={A,B,C,D,E,F}
 Edge: Edge represents a path
between two vertices or a line
between two vertices.
 E={AB,AC,BD,BE,CE,DE,DF,EF}
A
B C
D E
F
Node or Vertices
Edge
Analysis and Design of Algorithms
A
B C
A
B C
Directed Graph Undirected Graph
Analysis and Design of Algorithms
A
B C
Acyclic Graph Cyclic Graph
A
B C
Analysis and Design of Algorithms
Backedge is an edge that is from node to itself
(selfloop), or one to its ancestor.
A
B
A
Analysis and Design of Algorithms
Search: Look for a given node
Traversal: Always visit all nodes
Analysis and Design of Algorithms
Breadth First Traversal
Analysis and Design of Algorithms
Breadth First Traversal (BFT) algorithm traverses all nodes
on graph in a breadthward motion and uses a queue to
remember to get the next vertex.
Analysis and Design of Algorithms
Layers A
B C
D E
F
Layer1
Layer2
Layer3
Layer4
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print:
A
B C
D E
F
0 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= A
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= B C
 Print: A
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C
 Print: A B
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= C D E
 Print: A B
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= D E
 Print: A B C
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= D E
 Print: A B C
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= E F
 Print: A B C D
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= F
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= F
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue= F
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A B C D E
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
 Visited=
 Queue=
 Print: A B C D E F
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
Breadth First on Tree
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 V={0,1,2,3,4,5}
 E={01,02,13,14,24,34,35,45}
0
1 2
3 4
5
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Another Example:
Analysis and Design of Algorithms
Depth First Traversal
Analysis and Design of Algorithms
Depth First Traversal (DFT) algorithm traverses a graph in
a depthward motion and uses a stack to remember to get
the next vertex to start a search.
Analysis and Design of Algorithms
depthward motion A
B C
D E
F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
0 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
0 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
Analysis and Design of Algorithms
 Visited=
Stack=
 Print:
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 0 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 0 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 0 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 0
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 0 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
C
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
E
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
A
Analysis and Design of Algorithms
 Visited=
Stack=
 Print: A B D E F C
A
B C
D E
F
1 1 1 1 1 1
A B C D E F
Analysis and Design of Algorithms
Depth First on tree
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 V={0,1,2,3,4,5}
 E={01,02,13,14,24,34,35,45}
0
1 2
3 4
5
Analysis and Design of Algorithms
Analysis and Design of Algorithms
 Another Example:
Analysis and Design of Algorithms
Given a directed graph, check whether the graph
contains a cycle or not. Your function should return
true if the given graph contains at least one cycle,
else return false.
Analysis and Design of Algorithms
Depth First Traversal can be used to detect cycle in a
Graph. There is a cycle in a graph only if there is a
back edge present in the graph.
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
0 0 0 0
A B C D
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
0 0 0 0
A B C D
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 0 0 0
A B C D
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 0 0 0
A B C D
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 0 0 0
A B C D
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 0 0
A B C D
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 0 0
A B C D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 0 0
A B C D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
C
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
C
B
A
Analysis and Design of Algorithms
 Visited=
Stack=
A
C
B
D
1 1 1 0
A B C D
C
B
A
Analysis and Design of Algorithms
Graph contains cycle
A
C
B
D
Analysis and Design of Algorithms