0% found this document useful (0 votes)
11 views

CH-6

CH 6 OF ADA

Uploaded by

Eva Watts'
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

CH-6

CH 6 OF ADA

Uploaded by

Eva Watts'
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Unit 6 Exploring Graphs

Introduction using graphs and games


• Various problems can be formulated using graphs.
• Graphs is a non linear data structure. Game playing application can be
represented by using graphs.
• To understand the strategy for this game consider following rules-
• Initially there is a heap of objects between two players.
• Each player can pick up the objects alternatively.
• Each player can pick up at the most twice the number of objects than
its opponent and not more than that.
• Any object is removed from one row at a time.
• The person who picks up lastly looses. And the opponent wins.
• There is no draw between two players.
Example
• Consider that there are two players: I and you. Heap of object is
arranged in two rows namely A and B. following are instances which
shows winning and loosing of two players.
Instance 1: Instance 2:
A B Moves A B Moves
3 2 I takes 1 from B. 3 2 I takes 2 from B.
3 1 You take 2 from A 3 0 You take 3 from A
I win and You loose
1 1 I takes 1 from A
0 1 You takes 1 from B
I win and You loose
Example
Instance 3: Instance 4:
A B Moves A B Moves
3 2 I takes 2 from B. 3 2 I takes 2 from B.
3 0 You take 2 from A 3 0 You take 3 from A
I win and You loose
1 0 I takes 1 from A
I loose and You win

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-

3,2 3,1 1,1 0,1

3,0 2,2 2,1

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.

• Sub Graph: A Sub-Graph G’ of Graph G is a graph such that the set


of vertices and set of edges of G’ are proper subset of the set of edges
of G.
Concept of graph
• Connected Graph :An undirected Graph is said to be connected if for
every pair of distinct vertices Vi and Vj in V(G) there is an edge Vi to
Vj in G.

• Weighted Graph : Weighted graph is a graph which consists of


weights along its edges.
Concept of graph
• Dense Graph : The graph which contains maximum number of edges
for connecting vertices of a graph is called the dense graph.

• Sparse Graph : The Sparse graph is a kind of graph having minimum


number of edges within it.
Concept of graph
• Path :- A path is denoted using sequence of vertices and there exists
an edge from one vertex to the next vertex.

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.

Applications of Depth First Search


4. DFS is used for checking connectivity of a graph.
5. For checking if any cycle exist in the given graph.
If the DFS forest does not have back edge then the graph is said to be
acyclic.
3. DFS is used to find articulation point.
Topological sort
• One of the application of DFS is that it helps us to find whether a cycle
exists in the graph or not. Let us define one commonly used terminology
in Depth First search of a digraph. ie. DAG.
• Definition of DAG:
• A directed acyclic graph is a directed graph with no cycles.

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.

Note : Same graph may have multiple Topological Sort.


One graph may have many topological order . E has no any
unvisited
neighbor node.
Example So P O P(E)
Sort the digraph for topological sort using DFS based
algorithm.
E
A C
A C C C
E
A A A
B B B B
B D
B
N o unvisited neighbor
D
node.
So POP(C)

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.

⚫ Popping off the stack : e , f ,g , b , c , a ,d


⚫ Reverse of it : d , a , c , b , g , f , e
Topological Sort
(Using Source Removal Algorithm)
Steps for Source Removal
Algorithm
⚫ This is a direct implementation of decrease and conquer method. Following are the
steps to be followed in this algorithm.

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

2. Note the vertices that are deleted.

3.All these recorded vertices give topologically sorted list.


Example
⚫ Sort the digraph for topological sort using source
removal algorithm.
From a given graph find a
A C
vertex with no incoming edges.
E
Delete it along with all the
edges outgoing from it.
B D
B

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.

Again From a given graph


CC
find a vertex with no incoming
B , edges. Delete it along with
E
all the edges outgoing from
A it.
D
D
Again From a given graph
find a vertex with no incoming
C edges. Delete it along with
C B ,A, all the edges outgoing from
E D it.
Final Topological Sort

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-2 City-5 City-6

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

Step-1 Find DFS Tree and


give DFN to each vertex
E dfn = 5

A D dfn = 4 F dfn = 6
dfn = 1
G dfn = 7

dfn = 2 B C dfn = 3

DFN number is number in the order in which they are


Example E dfn = 5

dfn = 1
F dfn = 6
A D dfn = 4

G dfn = 7

B C dfn = 3
dfn = 2

Step-2 Add Back edges


Back edge is edge which is present in original graph but not present in DFS
tree. Direction of this edge must be from greater DFN vertex to Lesser DFN
vertex E

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

Step-3 Find out Low of each vertex


It is the lowest dfn number that can be reached from a vertex
by
taking one back edge. Low[E]= 4
E dfn = 5
Low[D]= 1
dfn = 1 F dfn = 6
A D dfn = 4
Low[F]= 4
G dfn = 7
Low[G]= 4
dfn = 2 dfn = 3
B C
Low[B]= 1
Low[C]= 1
Low[E]= 4
Example E dfn = 5
Low[D]= 1
dfn = 1
A D dfn = 4 F

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)

dfn[B] <= low[C] = 2 <= 1 Condition false


dfn[C] <= low[D] = 3 <= 1 Condition
false dfn[D] <= low[E] = 4 <= 4
Condition True
It means that D is an articulation
point
So, D is Articulation Point

F
A D

B C
Examp
le
A

A
A C

B D

⚫ Here removal of C vertex makes disconnected graph.


⚫ So C is known as articulation point.
• Bi-Connected Graph:
⚫ A graph G is said to be bi-connected if it contains no
articulation points.
Examp
le
A

A
A C

B D

⚫ Here removal of C vertex makes disconnected graph.


⚫ So C is known as articulation point.
• Bi-Connected Graph:
⚫ A graph G is said to be bi-connected if it contains no
articulation points.
Example
⚫ Example of Bi-Connected Graph :
1
Shown in figure if we
2 3 remove any single node we
do not get disjoint graph.
It is called Bi-
So
4 5
graph.
directed
6

• If there exists any articulation point in the given graph then it is an


undesirable feature of that graph. For example in communication network
the nodes or vertices represent the communication station x.
• If this communication station x is an articulation point then failure of this
station makes the entire communication system down. And this is surely
not desirable feature.
Thank You

You might also like