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

Graph

Uploaded by

roopitaryaman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Graph

Uploaded by

roopitaryaman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Graph

Madhvi Gaur (Assistant Professor)


Introduction

• A graph is another important non-linear data structure.


• In a tree data structure, there is a hierarchical relationship between parent and
children i.e. one parent and many children.
• A tree is a special type of graph. In a graph the relationship is less restricted.

Applications:
Airlines, analysis of electrical circuits, source and destination networks, finding
shortest routes etc.

Madhvi Gaur (Assistant Professor)


Graph Definition
A graph G is a set vertices V and a set of edges E. It can be represented as:
G= (V,E) where V is the set of vertices and E is the set of edges.
The set of vertices is non-empty and the set of edges contain pair of vertices.

A Graph can be of two types:


1. Undirected Graph
2. Directed Graph

Madhvi Gaur (Assistant Professor)


Undirected Graph
• If the pair of vertices are unordered then graph G is called an unordered graph.
• It means if there is an edge between v1 and v2 then it can be represented as
(v1,v2) or (v2,v1).

1 2

3 4
V(G)={1,2,3,4,5}
E(G)={(1,2),(1,3),(2,4),(3,4),(2,5),(4,5)}

Madhvi Gaur (Assistant Professor)


Directed Graph
If the pair of vertices are ordered then graph G is called an ordered graph.

1 2

3 4

V(G)={1,2,3,4,5}
E(G)={(1,2),(3,1),(3,4),(2,4),(2,5),(4,5)}

Madhvi Gaur (Assistant Professor)


Weighted Graph: A graph is said to be weighted if all its edges are labelled with
some numbers/ values.

2
6

4
7

Self Loop: If there is an edge whose starting and ending vertices are same that is
(v2,v2) is an edge then it is called a self loop or simply a loop.

Parallel Edges: If there are more than one edge between the same pair of vertices
then they are known as parallel edges.

Madhvi Gaur (Assistant Professor)


Adjacent vertices: A vertex u is adjacent to (or neighbour of) other vertex v if there is an
edge from u to v.

Degree of a vertex: Degree of a vertex is the number of edges incident to that vertex. In an
undirected graph, the number of edges connected to a vertex/node is called the degree of
that node.
In a digraph/directed graph, there are two types of degrees for every node:
1. In Degree: it is the number of edges coming to that vertex or the number of edges
incident to it.
2. Out Degree: it is the number of edges going outside from that vertex or the number of
edges incident from it.

Simple Graph: A graph or directed graph which does not have self-loop or parallel edges is
called a simple graph.

Multi Graph: A graph or directed graph which has either a self-loop or parallel edges or
both is called a multi graph.
Madhvi Gaur (Assistant Professor)
Complete Graph: A graph G is said to be complete if every vertex in G is
connected to every other vertex in G. Thus a complete graph G must be connected.
The complete graph with n vertices is denoted by Kn.

Madhvi Gaur (Assistant Professor)


Connected Graph: A graph is said to be connected if every pair of vertices in the
graph is connected.
Graph G is said to be connected if any pair of vertices (Vi, Vj) of a graph G is
reachable from one another.

Strongly Connected Graph: A directed graph is called strongly connected if there


is a path from each vertex in the graph to every other vertex. In particular, this
means paths in each direction; a path from a to b and also a path from b to a.

Cycle: If there is a path containing one or more edges which starts from a vertex
and terminates into the same vertex then the path is called as a cycle.

Madhvi Gaur (Assistant Professor)


Representation of a Graph in Memory

A graph can be represented in two ways in memory:


(i) Sequential Representation/ Adjacency Matrix
(ii) Linked Representation/ Adjacency List

Madhvi Gaur (Assistant Professor)


Sequential Representation/ Adjacency Matrix
Adjacency matrix is the matrix which keeps the information of adjacent nodes.
The entries in the matrix are placed according to the following rule:
Aij = 1 if there is an edge from vi to vj
0 otherwise

1
1 2 3 4
1 0 1 1 0
2 2 1 0 1 0
4
3 1 1 0 1
4 1 0 0 0
3

Madhvi Gaur (Assistant Professor)


Linked Representation/ Adjacency List
In a linked representation of graphs, the number of list depends on the number of
vertices in the graph.
The header node in each list maintains a list of all adjacent vertices of that node.
The adjacency list representation of this graph is as follows:

1 2 4 X 1 2 3
2 5 X
3 6 5 X
4 2 X
5 4 X 4 5 6
6 6 X

Madhvi Gaur (Assistant Professor)


Comparison of Representations
Representation Advantages Disadvantages
Adjacency Matrix 1. Manipulation of graph is 1. Space is wasted.
easy.
2. It allows storing of parallel 2. Insertion and Deletion
edges. operations are easy.
Adjacency List 1. It is a space saving method. 1. Pointer fields are used as
extra fields in addition to the
data fields.
2. It is dynamic in nature. 2. There is no faster way other
than to search the entire list for
the presence of the edge.
3. It allows storing of parallel
edges.
4. Insertion, Deletion and other
operations are easy to perform.
Madhvi Gaur (Assistant Professor)
Connected Component
Connected component in an undirected graph refers to a group of vertices that are
connected to each other through edges, but not connected to other vertices outside
the group.
For example in the graph shown below, {0, 1, 2} form a connected component and
{3, 4} form another connected component.

Madhvi Gaur (Assistant Professor)


Characteristics of Connected Component:
• A connected component is a set of vertices in a graph that are connected to each
other.
• A graph can have multiple connected components.
• Inside a component, each vertex is reachable from every other vertex in that
component.

Madhvi Gaur (Assistant Professor)


How to identify Connected Component:
• There are several algorithms to identify Connected Components in a graph. The
most popular ones are:
• Depth-First Search (DFS)
• Breadth-First Search (BFS)

Madhvi Gaur (Assistant Professor)


Traversing a Graph
As we know that traversing is nothing but visiting each node in some systematic
manner.

There are two standard ways to traverse a Graph:


Breadth First Search (BFS) : uses a queue for keeping the nodes for next
processing.
Depth First Search (DFS) : uses stack which keeps the node for next processing.

Madhvi Gaur (Assistant Professor)


BFS
This algorithm executes a breadth-first search on a graph G beginning at starting node A.
1. Initialize all nodes to the ready state.
2. Put the starting node A in QUEUE.
3. Repeat step 4 and 5 until QUEUE is empty:
4. Remove the FRONT node N of QUEUE. Process N.
5. Add to the REAR of QUEUE all the adjacent nodes of N which have not been
traversed yet.
6. Exit.

Note: The following algorithm will process only those nodes which are reachable from the
starting node A.

Madhvi Gaur (Assistant Professor)


BFS Example
A B

C D

Madhvi Gaur (Assistant Professor)


The adjacency list representation of the graph is as follows:
Vertex Adjacency List
A B,C,D
B D,E
C D
D E
E -

Suppose A is the source vertex.


Step 1: Initially push A to the queue.
0 1 2 3 4
A Q: A
F R Origin : ø
Madhvi Gaur (Assistant Professor)
Step 2: Remove the Front element A from the Queue Then push all the adjacent vertices of
A to the Queue (By incrementing Rear=Rear +1) if it is not in Queue.
0 1 2 3 4
F R
B C D
A B C D
Visited Node : A ø A A A

Step 3: Remove the Front element B from the Queue. Then push all the adjacent vertices of
B to the Queue if it is not in Queue.
0 1 2 3 4

C D E
F R
Visited Node : A, B
A B C D E
Madhvi Gaur (Assistant Professor) ø A A A B
Step 4: Remove the Front element C from the Queue. Then push all the adjacent
vertices of C to the Queue if it is not in Queue.
0 1 2 3 4
D E
F R A B C D E
Visited Node: A, B, C ø A A A B
Step 5: Remove the Front element D from the Queue. Then push all the adjacent
vertices of D to the Queue if it is not in Queue.

0 1 2 3 4
E
F R
Visited Node: A,B,C,D
Q A B C D E
ø A A A B
Madhvi Gaur (Assistant Professor) O
Step 6: Remove the Front element E from the Queue. Then push all the adjacent
vertices of E to the Queue if it is not in Queue. As there is no adjacent vertex of E.
We directly remove the front element E.
0 1 2 3 4

F=-1
R=-1
Visited Node: A,B,C,D,E
So A,B,C,D,E is the BFS traversal of the given Graph.

Madhvi Gaur (Assistant Professor)


DFS
This algorithm executes a depth-first search on a graph G beginning at starting node
A.
1. Initialize all nodes to the ready state.
2. Push the starting node A onto STACK.
3. Repeat step 4 and 5 until STACK is empty:
4. POP the top node N of STACK i.e. top=top - 1. Process N.
5. PUSH onto STACK all the adjacent nodes of N which have not been
traversed yet.
6. Exit.

Madhvi Gaur (Assistant Professor)


DFS Example
1 2

5 4

Madhvi Gaur (Assistant Professor)


Suppose the starting node is 1.
Step 1: Push node 1 into stack.
Now, Top=0, and STACK = 1
Step 2: Pop node 1 from stack and insert all the adjacent nodes of 1 into stack.
Now, Top=2, and STACK=2,5,4 Visited Node: 1
Step 3: Pop node 4 from stack and insert all the adjacent nodes of 4 into stack.
Now, Top=2, and STACK=2,5,3 Visited Node: 1,4
Step 4: Pop node 3 from stack and insert all the adjacent nodes of 3 into stack.
Now, Top=1, and STACK=2,5 Visited Node: 1,4,3
Step 5: Pop node 5 from stack and insert all the adjacent nodes of 5 into stack.
Now, Top=0, and STACK=2 Visited Node: 1,4,3,5
Step 6: Pop node 2 from stack and insert all the adjacent nodes of 2 into stack.
Now, Top=-1, and STACK is empty. Visited Node: 1,4,3,5,2
Madhvi Gaur (Assistant Professor)
Spanning Tree
A spanning tree of a connected graph G is a sub graph that contains all the vertices
and has edges which connects all the vertices.

A connected, Spanning trees of the graph


undirected graph

Madhvi Gaur (Assistant Professor)


Minimum Spanning Tree
• Suppose you have a connected undirected graph with a weight (or cost)
associated with each edge
• The cost of a spanning tree would be the sum of the costs of its edges
• A minimum-cost spanning tree is a spanning tree that has the lowest cost
• Common application is in network design (e.g. computer network , electrical
network etc.)
16 16
A B A B
21 11 6 11 6

19 5 5
F C F C
33 14
10
E 18 D E 18 D
A connected, undirected graph A minimum-cost spanning tree

Madhvi Gaur (Assistant Professor)


There are two famous greedy algorithms for creating a MST for a weighted graph :
1. Kruskal’s Algorithm
• Create a list of all the edges of G in ascending order of their weights.
• Repetitively add edges to G in ascending order unless the addition of the edge
forms a cycle.
2. Prim’s Algorithm
• Consider any vertex V of G and grow a tree T from V.
• Repetitively add cheapest edge to T such that the edge has only one end point in T.

Madhvi Gaur (Assistant Professor)


Kruskal Algorithm
MST_KRUSKAL(G,W)
1. A <- ø
2. For each vertex v ɛ V[G]
do MAKE_SET (v)
3. Sort the edges of E into ascending order by weight W.
4. For each edge (u,v) ɛ E taken in ascending order
do if FIND_SET(u) != FIND_SET(v)
then A <- A U {u,v}
UNION(u,v)
5. Return A.

Madhvi Gaur (Assistant Professor)


MAKE-SET(x)
[Here p[x] denote parent of x and initially rank is initialized to 0]
1. p[x] ← x
2. rank[x] ← 0

UNION(x, y)
LINK(FIND-SET(x), FIND-SET(y))

LINK(x, y)
1. if key[x] > key[y]
2. then p[y] ← x
3. else p[x] ← y
4. if key[x] = key[y]
5. then key[y] ← key[y] + 1

FIND-SET(x)
1. if x != p[x]
2. then p[x] ← FIND-SET(p[x])
3. return p[x]
Madhvi Gaur (Assistant Professor)
Kruskal Algorithm

• Sort all the edges in ascending order of their weight.


• Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so
far. If the cycle is not formed, include this edge. Else, discard it.
• Repeat step#2 until there are (V-1) edges in the spanning tree.

Madhvi Gaur (Assistant Professor)


Example 8 7

b c d 9

11 i 14 e
a
6
7
10
8
h g f

1 2

Madhvi Gaur (Assistant Professor)


Edge Weight
(h,g) 1 8 7
(g,f) 2 c 9
b d
(a,b) 4
(i,g) 6 4
(h,i) 7
(c,d) 7 11 i 14 e
(b,c) 8 a
(a,h) 8 6
(d,e) 9 7
(e,f) 10 10
(b,h) 11 8
h g f
(d,f) 14
1 2

• Note: we should select safe edge so that it will not create a cycle.

Madhvi Gaur (Assistant Professor)


Equivalent MST 8 7

b c d 9

i e
a
6

8
h g f

1 2

• Total Cost = 45

Madhvi Gaur (Assistant Professor)


Solve using Kruskal’s Algorithm

Madhvi Gaur (Assistant Professor)


Madhvi Gaur (Assistant Professor)
Prim’s Algorithm
MST_PRIM(G,w,s)
1. For each u ɛ V[G]
2. do key[u] <- ∞
3. 𝜋[u] <- NIL
4. Key[s] <- 0
5. Q<-V[G]
6. While Q != ø
7. do u <- EXTRACT_MIN(Q) // EXTRACT_MIN selects the minimum
weighted node from the queue Q.
8. for each v ɛ Adj[u]
9. do if v ɛ Q and w(u,v) < key [v]
10. then 𝜋[u] <- u
11. keyMadhvi
[v] Gaur
<- (Assistant
w(u,v) Professor)
Prim’s Algorithm

Step 1: Determine an arbitrary vertex as the starting vertex of the MST.

Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST
(known as fringe vertex).

Step 3: Find edges connecting any tree vertex with the fringe vertices.

Step 4: Find the minimum among these edges.

Step 5: Add the chosen edge to the MST if it does not form any cycle.

Step 6: Return the MST and exit

Madhvi Gaur (Assistant Professor)


b c d
8 7
8 7
b c d ∞ ∞ ∞
4 9 9
4 4
11 2 i 2 4
14 11 14
a i a ∞ ∞ e
e 0
6 6
7 7
8 10 8 10
h g f ∞ ∞ ∞
1 2 1 2
h g f

Madhvi Gaur (Assistant Professor)


8 7
8 7
4 ∞ ∞ 4 ∞ ∞
4 9 9
4 4
11 2 2 4
14 11 14
0 ∞ ∞ ∞
∞ 0
6 6
7 7
8 10 8 10
8 ∞ ∞ 8 ∞ ∞
1 2 1 2

Madhvi Gaur (Assistant Professor)


8 7
8 7
4 8 ∞ 4 8 ∞
4 9 9
4 4
11 2 2 4
14 11 14
0 ∞ ∞ ∞
∞ 0
6 6
7 7
8 10 8 10
8 ∞ ∞ 8 ∞ ∞
1 2 1 2

Madhvi Gaur (Assistant Professor)


8 7
8 7
4 8 7 4 8 7
4 9 9
4 4
11 2 2 4
14 11 14
0 2 2 ∞
∞ 0
6 6
7 7
8 10 8 10
8 ∞ 4 8 ∞ 4
1 2 1 2

Madhvi Gaur (Assistant Professor)


8 7
8 7
4 8 7 4 8 7
4 9 9
4 4
11 2 2 4
14 11 14
0 2 2 ∞
∞ 0
6 6
7 7
8 10 8 10
7 6 4 7 6 4
1 2 1 2

Madhvi Gaur (Assistant Professor)


8 7
8 7
4 8 7 4 8 7
4 9 9
4 4
11 2 2 4
14 11 14
0 2 2 10
10 0
6 6
7 7
8 10 8 10
7 2 4 7 2 4
1 2 1 2

Madhvi Gaur (Assistant Professor)


8 7
8 7
4 8 7 4 8 7
4 9 9
4 4
11 2 2 4
14 11 14
0 2 2 10
10 0
6 6
7 7
8 10 8 10
1 2 4 1 2 4
1 2 1 2

Madhvi Gaur (Assistant Professor)


8 7
8 7
4 8 7 4 8 7
4 9 9
4 4
11 2 2 4
14 11 14
0 2 2 9
10 0
6 6
7 7
8 10 8 10
1 2 4 1 2 4
1 2 1 2

Madhvi Gaur (Assistant Professor)


8 7
4 8 7
4 9
2
Final MST using Prim’s Algorithm is : 11
4
14
2 9
Total Cost = 37 0
6
7
8 10
1 2 4
1 2

Madhvi Gaur (Assistant Professor)


MST: Using Prim’s Algorithm

Madhvi Gaur (Assistant Professor)


MST: Using Prim’s Algorithm Solution

Madhvi Gaur (Assistant Professor)


Shortest Path:
A path from source vertex s to destination vertex t is shortest if there is no other path
from s to t with lower weights.
To find the shortest path from source to destination, There are two algorithms:
(i) Dijkstra’s Algorithm
(ii) Floyd’s Warshall Algorithm

Madhvi Gaur (Assistant Professor)


Dijkstra’s(Dike-stra) Shortest Path Algorithm
• It is a greedy algorithm that solves the single-source shortest path problem for a
directed graph G=(V,E) with non-negative edge weights.
• DIJKSTRA’s uses a greedy approach because it always chooses the lightest or
closest vertex in V-S to insert into set S.

DIJKSTRA(G,w,s)
1. INITIALIZE_SINGLE_SOURCE(G,s)
2. S<-ø
3. Q<-V[G]
4. While Q!= ø
5. do u <- EXTRACT_MIN(Q)
6. S<- S U {u}
7. for each vertex v ɛ Adj[u]
8. do RELAX (u,v,w)
Madhvi Gaur (Assistant Professor)
INITIALIZE_SINGLE_SOURCE(G,s)
1. For each vertex v ɛ V[G]
2. do d[v] <- ∞
3. 𝜋[v] <- NIL
4. d[s] <- 0

where d[v] : represents the shortest distance of v from the source and
𝝅[v] : represents the predecessor of node v.

Madhvi Gaur (Assistant Professor)


RELAX(u,v,w)
1. If d[v] > d[u] + w(u,v)
2. then, d[v] <- d[u] + w(u,v)
3. 𝜋[v] <- u

Madhvi Gaur (Assistant Professor)


Consider A is the source vertex.

2
B D
10

1 4 8 9
7
A

3 C E
2

Madhvi Gaur (Assistant Professor)


Initialise:

2
∞ ∞
10

1 4 8 9
7
0

3 ∞ ∞
2
S={ }

Madhvi Gaur (Assistant Professor)


EXTRACT_MIN(Q) : A Q A B C D E
0 ∞ ∞ ∞ ∞

2
∞ ∞
10

1 4 8 9
7
0

3 ∞ ∞
2
S={ A}

Madhvi Gaur (Assistant Professor)


Relax All Edges Leaving A: Q A B C D E
0 ∞ ∞ ∞ ∞

10 3
2
10 ∞
10

1 4 8 9
7
0

3 3 ∞
2
S={ A}

Madhvi Gaur (Assistant Professor)


EXTRACT _MIN(Q): C Q A B C D E
0 ∞ ∞ ∞ ∞

10 3
2
10 ∞
10

1 4 8 9
7
0

3 3 ∞
2
S={ A,C}

Madhvi Gaur (Assistant Professor)


Q A B C D E
0 ∞ ∞ ∞ ∞
Relax All edges leaving C:
10 3
7 11 5

2
7 11
10

1 4 8 9
7
0

3 3 5
2
S={ A,C}

Madhvi Gaur (Assistant Professor)


Q A B C D E
0 ∞ ∞ ∞ ∞
EXTRACT_MIN(Q): E
10 3
7 11 5

2
7 11
10

1 4 8 9
7
0

3 3 5
2
S={ A,C,E}

Madhvi Gaur (Assistant Professor)


Q A B C D E
0 ∞ ∞ ∞ ∞
Relax All Edges leaving E:
10 3
7 11 5
7 11
2
7 11
10

1 4 8 9
7
0

3 3 5
S={ A,C,E} 2

No changes will be done.

Madhvi Gaur (Assistant Professor)


Q A B C D E
0 ∞ ∞ ∞ ∞
EXTRACT_MIN(Q): B
10 3
7 11 5
7 11
2
7 11
10

1 4 8 9
7
0

3 3 5
2
S={ A,C,E,B}

Madhvi Gaur (Assistant Professor)


Q A B C D E
0 ∞ ∞ ∞ ∞
Relax all edges leaving B.

10 3

2 7 11 5
7 9 7 11
10 9

1 4 8 9
7
0

3 3 5
2
S={ A,C,E,B}

Madhvi Gaur (Assistant Professor)


Q A B C D E
0 ∞ ∞ ∞ ∞
EXTRACT_MIN(Q): D
10 3
7 11 5
7 11
2
7 9 9

10

1 4 8 9
7
0

3 3 5
2
S={ A,C,E,B,D}

Madhvi Gaur (Assistant Professor)


Q A B C D E
0 ∞ ∞ ∞ ∞
Relax all edges leaving D.
10 3

7 11 5
2 7 11
7 9
9
10

1 4 8 9
7
0

3 3 5
2
S={ A,C,E,B,D}
No changes will be done.

Madhvi Gaur (Assistant Professor)


Solve using DIJKSTRA

Madhvi Gaur (Assistant Professor)


Madhvi Gaur (Assistant Professor)
• A=0
• B = 4 (A -> B)
• C = 5 (A -> C)
• D = 4 + 9 = 13 (A -> B -> D)
• E = 5 + 3 = 8 (A -> C -> E)
• F = 5 + 3 + 6 = 14 (A -> C -> E -> F)

Madhvi Gaur (Assistant Professor)


Floyd Warshall Algorithm
The Floyd–Warshall algorithm is a graph analysis algorithm for finding shortest
paths in a weighted graph with positive or negative edge weights (but with no
negative cycles) and also for finding transitive closure of a relation R.
FLOYD_WARSHALL(W)
1. n <- rows[W]
2. D (0) <- W
3. for k <-1 to n
4. do for i <-1 to n
5. do for j <-1 to n
6. do dij(k) <- min(dij(k-1) , dik (k-1) + dkj (k-1) )
7. Return D (n)

Madhvi Gaur (Assistant Professor)


Madhvi Gaur (Assistant Professor)
Madhvi Gaur (Assistant Professor)
If source vertex is given as 1 and destination vertex is 3:
Then the shortest path will be :
Path[1][3] = 1 <- 5 <- 4 <- 3
Similarly if we compute the path from 4 to 5, it will be:
Path[4][5]= 4 <- 1 <- 5
From this way can find the shortest path from any given source vertex to destination
vertex.

Madhvi Gaur (Assistant Professor)


Transitive Closure
The transitive closure T(G) of a given graph G connects vertices u and v if and only
if there is a path in G from u to v. Thus the transitive closure of any connected graph
is complete.
Floyd Warshall Algorithm can be used to find the transitive closure, we can
calculate the distance matrix dist[V][V] using Floyd Warshall, if dist[i][j] is infinite,
then j is not reachable from i, otherwise j is reachable and value of dist[i][j] will be
less than V.
Instead of directly using Floyd Warshall, we can optimize it in terms of space and
time, for this particular problem.
Following are the optimizations:
1) Instead of integer resultant matrix (dist[V][V] in floyd warshall), we can create a
boolean reach-ability matrix reach[V][V] (we save space). The value reach[i][j] will
be 1 if j is reachable from i, otherwise 0.
2) Instead of using arithmetic operations, we can use logical operations. For
arithmetic operation ‘+’, logical AND ‘&&’ is used, and for min, logical OR ‘||’ is
used. (We save time by a constantMadhvi
factor.)
Gaur (Assistant Professor)
Warshall Algorithm for Transitive Closure
• Warshall's algorithm determines whether there is a path between any two nodes in
the graph. It does not give the number of the paths between two nodes.
• Idea: Compute all paths containing node 1, then all paths containing nodes 1 or 2
or 1 and 2, and so on, until we compute all paths with intermediate nodes selected
from the set {1, 2, … n}.

Madhvi Gaur (Assistant Professor)


WARSHALL_TRANSITIVE(A) where A is the Adjacency Matrix
1. n <- rows[A]
2. t (0) <- A
3. for k <-1 to n
4. do for i <-1 to n
5. do for j <-1 to n
6. do tij(k) <- (tij(k-1) OR tik (k-1) AND tkj (k-1) )
7. Return t (n)

Madhvi Gaur (Assistant Professor)


Madhvi Gaur (Assistant Professor)
Heap Sort

Madhvi Gaur (Assistant Professor)


Madhvi Gaur (Assistant Professor)
Strongly Connected Components
• A directed graph is called strongly connected if there is a path from each vertex in
the graph to every other vertex. In particular, this means paths in each direction; a
path from a to b and also a path from b to a that is vertices a and b are reachable
from each other..
• The strongly connected components(SCC) of a directed graph G are its strongly
connected subgraphs.

• G is strongly connected, all vertices can be reached from all other vertices.
• H is not, cannot reach any vertex from a.
Madhvi Gaur (Assistant Professor)
In other words, two vertices of directed graph are in the same component if and only
if they are reachable from each other.

STRONGLY-CONNECTED-COMPONENTS (G)
1. Call DFS(G) to compute finishing times f[u] for all u.
2. Compute GT
3. Call DFS(GT), but in the main loop of DFS, consider vertices in order of
decreasing f[u] (as computed in line 1)
4. Output the vertices of each tree in the depth-first forest formed in line 3 as a
separate SCC.

Madhvi Gaur (Assistant Professor)


Consider a Graph G =(V,E)

a b c d

e f g h

Madhvi Gaur (Assistant Professor)


Consider a graph G = (V, E).
1. Call DFS(G)

2. Compute GT

3. Call DFS(GT) but this time consider the vertices in order to decreasing finish
time.

4. Output the vertices of each tree in the DFS-forest as a separate strongly connected
components.
{a, b, e}, {c, d}, {f, g}, and {h}

Madhvi Gaur (Assistant Professor)

You might also like