CH-6
CH-6
Instance 5:
A B Moves
3 2 I takes 1 from A.
2 2 You take 1 from B
2 1 I takes 2 from A
0 1 You take 1 from B
I
Example
• These moves can be represented by the graph as follows-
1,0
Concept of graph
• Graph is a collection of two sets V and E where V is a set of vertices
and E is a set of edges.
• Vertices are the nodes of the graph and Edge is used to connect two
nodes.
• So any Graph is denoted as G = { V , E }
G ={ {V1, V2, V3, V4, V5,V6},
V1
E1 {E1, E2, E3, E4, E5, E6} }
V2 V3
V4
Concept of graph
• Types of Graph :
1. Directed Graph
2. Undirected Graph
1) Directed Graph :
• In the directed Graph directions are shown on the edges.
V1
E1 E3
V2 V3
E2 E4
V4
Concept of graph
2) Undirected Graph
In undirected Graph directions are not given on the edges.
V1
E1 E3
V2 V3
E2 E4
V4
Concept of graph
• Complete Graph: If an undirected Graph of n vertices consists of
n(n-1)/2 number of edges then that Graph is known as a complete
graph.
Path : 1 , 2 , 3 , 4 , 5
Concept of graph
• Cycle :- A close walk through the Graph with reaped vertices, mostly
having the same starting and ending vertex is called a cycle.
Path : 1 , 2 , 3 , 4 , 5, 1
OR
2,3,4,2
Concept of graph
• In-Degree and Out-Degree :
Vertices In-degree Out-degree
V1 1 1
V2 2 0
V3 0 2
V4 1 1
Concept of graph
• Self Loop : Self Loop is an edge that connects the same vertex to it
self.
Representation of graph
• There are two representation of Graph :-
1. Adjacency Matrix
2. Adjacency List
1. Adjacency Matrix : 1 2 3 4 5
1 0 1 1 0 1
2 1 0 1 0 0
3 1 1 0 1 0
4 0 0 1 0 1
5 1 0 0 1 0
Cont…
• If Graph is Weighted Graph
1 2 3 4 5
1 0 10 5 0 12
2 10 0 20 0 0
3 5 20 0 6 0
4 0 0 6 0 8
5 12 0 0 8 0
Adjacency List Representation
In place of array if we can use Link List to represent Graph then we can
obtain all the advantage of Link List.
Traversing a Graph
• Searching a graph means traversing a graph from given vertex. There
are two commonly used graph searching algorithms.
• Breadth First Search
• Queue Data structure is used
• Depth First Search
• Stack Data Structure is used
Breadth first search
• BFS is a traversing algorithm where you should start traversing from
a any node and traverse the graph layer wise thus exploring the
neighbour nodes (nodes which are directly connected to source node).
You must then move towards the next-level neighbour nodes.
• As the name BFS suggests, you are required to traverse the graph
breadth wise as follows:
• First move horizontally and visit all the nodes of the current layer
• Move to the next layer
• Note : Queue Data structure is used for BFS.
Breadth first search
1. Visit Source Node. It can be any node of the graph
2. Store Source node in Queue (FIFO operation in Queue)
3. Perform De-Queue operation with Queue.
4. Print deleted node and explore them and store all neighbour
node of it in Queue.
5. Repeat Step 4 and 5 until we visit all node of graph.
Q ueu
Example e
B C D
A
C D E
D E F
B C D
E F G
H
F G H
E F G H
G H
H
O utp
ut
A B C D E F G H
Algorithm
BFS (G, s)
{
let Q be queue.
Q.enqueue( s )
mark s as visited.
while ( Q is not
empty)
{ v = Q.dequeue( )
for all neighbors w of v in Graph
G
{ if w is not visited
Q.enqueue( w )
mark w as visited.
}
}
}
Depth First Search (DFS) :
In Depth First Search the algorithm starts at root node and
explores as far as possible along each branch before
backtracking.
1. Select Any Node as Starting node and Mark it as visited and
then Store it in Stack.
2. Identify Neighbour node of it and select any one node.
3. Marks that selected node as visited node and store it in Stack.
4. Repeat step 2 and 3 until we get neighbour node.
5. Once there is no any neighbour of selected node then perform
POP operation and repeat step 3 and 4 again.
⚫ Stop when there is no element in Stack.
Exam
ple
S
A B C
D
DFS Tree
O utp
ut
S A B C
D
Depth First Search (DFS)
The recursive algorithm for implementing the depth first traversal is
as given below :
Algorithm DFS(v)
{
visit[v] = 1;
for(each vertex x adjacent from v)
{
if(visit[x] = = 0) then
DFS(x)
}
}
Applications
Applications of Breadth First Search
1. For finding the connected components in the graph.
2. For checking if any cycle exist in the given graph.
3. To obtain shortest path between two vertices.
A
A
B C
B C
D
D
It is a DAG
It is not a DAG
Topological sort
• Based on the principle of DAG, specific ordering of vertices is possible.
This method of arranging the vertices in some specific manner is called
topological sort.
• There are two commonly used algorithms for sorting the vertices using
topological sort method.
DFS
Based
Topological Sort Algorith
Topological
Sort m
Source
Removal
Algorithm
Different methods for Topological Sort
DFS Based Algorithm
1) Perform DFS algorithm.
2) Store POP Off content of Stack separately.
3) At the end reverse the Pop off Content. It will give one
topological sort.
Pop-off contents of
Stack
C D
A A A
E C D A
A
B
B B B B B
Reverse the
content.
Apply DFS based algorithm to solve the topological
sorting problem for following graph.
1.From a given graph find a vertex with no incoming edges. Delete it along
with all the edges outgoing from it. If there are more than one such vertices
then break the tie randomly.
A C
C
A C
D
A C
A Again From a given graph
E B find a vertex with no incoming
edges. Delete it along with
all the edges outgoing from
D it.
E B ,A,D, B ,A,D,C,
C E
AA C
B D
B ,A,D,C,
E
Find Topological Sort of following
Graph using Source Removal
Algorithm
A B
C D E
F G
D,A,B,C,F,G,E
D,A,C,B,F,G,E
Articulation Point
⚫ Articulation Point:
◦ Let G=(V, E) be a connected undirected graph, then an
articulation point of Graph G is a vertex whose removal
disconnects graph G.
F
A D
B C
Articulation Point
⚫ Articulation Point:
◦ Let G=(V, E) be a connected undirected graph,
then an articulation point of Graph G is a vertex
whose removal disconnects graph G.
F
A
B C
What is purpose to identify Articulation
Point???
⚫ If graph represent some network such as Computer
network or any road network, then Graph must not have
articulation point.
What is purpose to identify Articulation
Point???
⚫ If graph represent some network such as Computer
network or any road network, then Graph must not have
articulation point.
City-1 City-4
City-8 City-7
City-3
Steps to find articulation point
⚫ Find DFS tree of graph and give DFN to each vertex
⚫ Add back edges (those edges who are not present in graph. And direction
of back edge must be from greater DFN to lesser DFN).
⚫ Find out low of each vertex
⚫ If dfn[u]<=low[v] then u is a articulation point (here u parent, v child)
(check for every vertex except root node and root can be articulation point
is it has two or more than two child).
Example E
F
A D
B C
A D dfn = 4 F dfn = 6
dfn = 1
G dfn = 7
dfn = 2 B C dfn = 3
dfn = 1
F dfn = 6
A D dfn = 4
G dfn = 7
B C dfn = 3
dfn = 2
dfn = 1 dfn =
E
4 F dfn = 6
A D
G dfn = 7
dfn = 5
dfn = 2 B C dfn = 3
Example E dfn = 5
F dfn = 6
dfn = 1 A D dfn = 4
G dfn = 7
dfn = 2 B C dfn = 3
G dfn = 7 dfn = 6
Low[G]= 4 Low[F]= 4
dfn = 2 dfn = 3
B C
Low[B]= 1
Low[C]= 1
Step-4 if dfn [u] <= low[v] then u is an Articulation Point (here u
parent, v child) (check for every vertex except root node)
F
A D
B C
Examp
le
A
A
A C
B D
A
A C
B D