0% found this document useful (0 votes)
62 views166 pages

CP4151-ADS Unit Iii

The document covers various elementary graph algorithms, including representations of graphs, traversal methods such as Depth-First Search (DFS) and Breadth-First Search (BFS), and concepts like Strongly Connected Components (SCC) and Minimum Spanning Trees. It discusses the use of adjacency matrices and lists for graph representation, along with algorithms for finding shortest paths and connected components. Additionally, it introduces the Kosaraju-Sharir algorithm for identifying strongly connected components in directed graphs.

Uploaded by

joysbai
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)
62 views166 pages

CP4151-ADS Unit Iii

The document covers various elementary graph algorithms, including representations of graphs, traversal methods such as Depth-First Search (DFS) and Breadth-First Search (BFS), and concepts like Strongly Connected Components (SCC) and Minimum Spanning Trees. It discusses the use of adjacency matrices and lists for graph representation, along with algorithms for finding shortest paths and connected components. Additionally, it introduces the Kosaraju-Sharir algorithm for identifying strongly connected components in directed graphs.

Uploaded by

joysbai
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/ 166

P.

Joy Suganthy Bai, AP/CSE Grace College of Engineering


CP4151-ADS
UNIT III
UNIT III GRAPHS

Elementary Graph Algorithms: Representations of Graphs


– Breadth-First Search – Depth-First Search – Topological
Sort – Strongly Connected Components- Minimum
Spanning Trees: Growing a Minimum Spanning Tree –
Kruskal and Prim- Single-Source Shortest Paths: The
Bellman-Ford algorithm – Single-Source Shortest paths in
Directed Acyclic Graphs – Dijkstra‘s Algorithm; Dynamic
Programming - All-Pairs Shortest Paths: Shortest Paths and
Matrix Multiplication – The Floyd-Warshall Algorithm
P.Joy Suganthy Bai, AP/CSE Grace College of Engine
ering
Representations of Graphs

•Adjacency Matrices

•Adjacency Lists

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Adjacency Matrices
Graphs G = (V, E) can be represented by adjacency
matrices
G[v1..v|V |, v1..v|V |], where the rows and columns are indexed by the
nodes, and the entries G[vi, vj] represent the edges. In the case of
unlabeled graphs, the entries are just boolean values.
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 1 0

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Adjacency Matrices
In case of labeled graphs, the labels themselves may be
introduced into the entries.

A B C D
1
A 4 1
0 1
B
5
C 9
D
Adjacency matrices require O(|V |2) space, and so they are space-
efficient only when they are dense (that is, when the graphs have
many edges). Time-wise, the adjacency matrices allow easy addition
and deletion of edges.
P.Joy Suganthy Bai, AP/CSE Grace College of Engine
ering
Adjacency Lists
A representation of the graph consisting of a list of nodes,
with each node containing a list of its neighboring nodes.

This representation takes O(|V | + |E|) space.

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Graph Traversal

•Depth-First Traversal

•Breadth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Depth-First Traversal

 algorithm dft(x)
visit(x)
FOR each y such that (x,y) is an edge DO
IF <y was not visited yet >
THEN dft(y)

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Depth-First Traversal

 A recursive algorithm implicitly recording a “backtracking” path from


the root to the node currently under consideration

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Depth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Depth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Depth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Depth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Depth-First Traversal
•Depth first search is another way of traversing graphs,
which is closely related to preorder traversal of a tree.
•the Breath-first search tree is typically "short and bushy", the DFS tree is
typically "long and stringy".

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Breadth-First Traversal

Visit the nodes at level i before the nodes


of level i+1.

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Breadth-First Traversal
visit(start node)
queue <- start node
WHILE queue is not empty DO
x <- queue
FOR each y such that (x,y) is an edge
and y has not been visited yet
DO
visit(y)
queue <- y
END
END
P.Joy Suganthy Bai, AP/CSE Grace College of Engine
ering
Breadth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Breadth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Breadth-First Traversal

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Breadth-First

Traversal
Each vertex is clearly
• marked at most once,
• added to the list at most once (since that happens only when
it's marked), and
• removed from the list at most once.
Since the time to process a vertex is proportional to the length of
its adjacency list, the total time for the whole algorithm is
O(m).

• A tree T constructed by the algorithm is called a breadth first


search tree.

• The traversal goes a level at a time, left to right within a level


P.Joy Suganthy Bai, AP/CSE Grace College of Engine
ering (where a level is defined simply in terms of distance from the root
Breadth-First Traversal
• Every edge of G can be classified into one of three groups.
• Some edges are in T themselves.
• Some connect two vertices at the same level of T.
• The remaining ones connect two vertices on two adjacent
levels. It is not possible for an edge to skip a level.
• Breadth-first search tree really is a shortest path tree starting
from its root.

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Relation between BFS
dfs(G)
and DFS
bfs(G)
{ {
list L = empty tree list L = empty
T = empty tree T = empty
choose a starting vertex x choose a starting vertex x
search(x) search(x)
while(L nonempty) while(L nonempty)
remove edge (v,w) from start remove edge (v,w) from end of
of L L
if w not yet visited if w not yet visited
{ {
add (v,w) to T add (v,w) to T
search(w) search(w)
} }
} }
search(vertex v) {
visit(v);
for each edge (v,w)
P.Joy Suganthy Bai, AP/CSE Grace College of Engine
add edge (v,w) to end of L
ering }
Relation between BFS and DFS

Both of these search algorithms now keep a list of edges


to explore; the only difference between the two is

while both algorithms adds items to the end of L,

• BFS removes them from the beginning, which results in


maintaining the list as a queue

• DFS removes them from the end, maintaining the list


as a stack.

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
P.Joy Suganthy Bai, AP/CSE Grace College of Engineering
Components (SCC) in a Graph
Strongly Connected
Strongly Connected Components
A subset of a directed graph is called strongly connected

P.Joy Suganthy Bai, AP/CSE Grace College of Engineering


if there is a path in each direction between each pair of vertices of the subset.

2 5
7

1 3 4 6
P.Joy Suganthy Bai, AP/CSE Grace College of Engineering
Path-Based Depth-First Search Algorithm [1]
Path-Based Depth-First Search Alg.
SCCs P

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
a
a

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
a
a

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
a
a

c
b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

d
b

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

d
b
e
d

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

d
b
e
d

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

b,d,e

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

b,d,e

f
b

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

b,d,e

f
b

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

b,d,e,f

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a

b,d,e,f

b
d

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a
{ b , d ,e , f }

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c } a
a
{ b , d ,e , f }
{ a}

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Path-Based Depth-First Search Alg.
SCCs P
{c }
{ b , d ,e , f }
{ a}

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Complexity

𝑂 ( 𝐸 +𝑉 )

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
P.Joy Suganthy Bai, AP/CSE Grace College of Engineering
Kosaraju-Sharir algorithm [2]
Kosaraju-Sharir algorithm [2]
a

0 - Seen, not finished yet.

b c
0 - Finished.

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 2 - c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 2 3 c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 2 3 c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - 7 - f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - 7 - f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - 7 - f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - 7 - f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 - 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 -

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 10
-

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 10
-

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 - 2 3 c

5 10
-

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 -

b 4 11
- 2 3 c

5 10
-

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 12
-

b 4 11
- 2 3 c

5 10
-

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

1 12
- { 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }

b 4 11
- 2 3 c

5 10
-

e 6 9
- 7 8 f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
a
{ 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
SCCs a
{ 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }
{𝑎 }

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
SCCs a
{ 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }
{𝑎 }

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
SCCs a
{ 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }
{𝑎 }
{ 𝑏, 𝑑, 𝑒, 𝑓 }

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
SCCs a
{ 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }
{𝑎 }
{ 𝑏, 𝑑, 𝑒, 𝑓 }

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Kosaraju-Sharir algorithm [2]
SCCs a
{ 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }
{𝑎 }
{ 𝑏, 𝑑, 𝑒, 𝑓 }
{𝑐 }

b c

e f

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
Complexity

𝑂 ( 𝐸 +𝑉 )

P.Joy Suganthy Bai, AP/CSE Grace College of Engine


ering
The Single-Source Shortest Path (SSSP) problem consists of finding the shortest paths between a
given vertex v and all other vertices in the graph. Algorithms such as Breadth-First-Search (BFS) for
unweighted graphs or Dijkstra [1] solve this problem
Bellman-Ford algorithm
Bellman-Ford algorithm

Initialize all
the distances
Bellman-Ford algorithm

iterate over all


edges/vertices
and apply
update rule
Bellman-Ford algorithm
Bellman-Ford algorithm

check for
negative cycles
Negative cycles
What is the shortest
path from a to e?

1
1 B D

5
A
10 -10

C E
3
Bellman-Ford algorithm
Bellman-Ford algorithm
S
1
A How many edges
0
is the shortest
8 1
path from s to:

G -4 B A:
2
1 1

-2
F C

-1 3
E D
-1
Bellman-Ford algorithm
S
1
A How many edges
0
is the shortest
8 1
path from s to:

G -4 B A: 3
2
1 1

-2
F C

-1 3
E D
-1
Bellman-Ford algorithm
S
1
A How many edges
0
is the shortest
8 1
path from s to:

G -4 B A: 3
2
1 1

-2 B:
F C

-1 3
E D
-1
Bellman-Ford algorithm
S
1
A How many edges
0
is the shortest
8 1
path from s to:

G -4 B A: 3
2
1 1

-2 B: 5
F C

-1 3
E D
-1
Bellman-Ford algorithm
S
1
A How many edges
0
is the shortest
8 1
path from s to:

G -4 B A: 3
2
1 1

-2 B: 5
F C

-1 3
E D D:
-1
Bellman-Ford algorithm
S
1
A How many edges
0
is the shortest
8 1
path from s to:

G -4 B A: 3
2
1 1

-2 B: 5
F C

-1 3
E D D: 7
-1
Bellman-Ford algorithm
0 
1
S 0 A Iteration: 0
8 1

 G -4 B
2
1 1

-2 
 F C

-1 3
E D
-1
 
Bellman-Ford algorithm
0 1
1 0
S 0 A Iteration: 1
8 1

8 G -4 B
2
1 1

-2 
 F C

-1 3
E D
-1
 
Bellman-Ford algorithm
0 1
1 0
S 0 A Iteration: 2
8 1

8 G -4 B
2
1 1

-2 
9 F C

-1 3
E D
-1
1 
2
Bellman-Ford algorithm
0 5
1
S 0 A Iteration: 3
8 1
1
0
8 G -4 B A has the
2 correct
1 1 distance and
path
-2 
9 F C

-1 3
E D
-1
8 
Bellman-Ford algorithm
0 5
1
S 0 A Iteration: 4
8 1
6

8 G -4 B
2
1 1

-2 1
9 F C
1
-1 3
E D
-1
7 
Bellman-Ford algorithm
0 5
1
S 0 A Iteration: 5
8 1
5

8 G -4 B B has the
2 correct
1 1 distance and
path
-2 7
9 F C

-1 3
E D
-1
7 1
4
Bellman-Ford algorithm
0 5
1
S 0 A Iteration: 6
8 1
5

8 G -4 B
2
1 1

-2 6
9 F C

-1 3
E D
-1
7 1
0
Bellman-Ford algorithm
0 5
1
S 0 A Iteration: 7
8 1
5

8 G -4 B D (and all other


2 nodes) have
1 1 the correct
distance and
-2 6 path
9 F C

-1 3
E D
-1
7 9
P.Joy Suganthy Bai, AP/CSE Grace College of Engine
ering
P.Joy Suganthy Bai, AP/CSE Grace College of Engine
ering
P.Joy Suganthy Bai, AP/CSE Grace College of Engine
ering

You might also like