CP4151-ADS Unit Iii
CP4151-ADS Unit Iii
•Adjacency Matrices
•Adjacency Lists
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.
•Depth-First Traversal
•Breadth-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)
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
b c
e f
b c
e f
c
b c
e f
e f
d
b
e f
d
b
e
d
e f
d
b
e
d
e f
b,d,e
e f
b,d,e
f
b
e f
b,d,e
f
b
e f
b,d,e,f
e f
b,d,e,f
b
d
e f
𝑂 ( 𝐸 +𝑉 )
b c
0 - Finished.
e f
1 -
b c
e f
1 -
b 2 - c
e f
1 -
b 2 3 c
e f
1 -
b 2 3 c
e f
1 -
b 4 - 2 3 c
e f
1 -
b 4 - 2 3 c
5 -
e f
1 -
b 4 - 2 3 c
5 -
e 6 - f
1 -
b 4 - 2 3 c
5 -
e 6 - f
1 -
b 4 - 2 3 c
5 -
e 6 - f
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
1 -
b 4 - 2 3 c
5 -
e 6 - 7 - f
1 -
b 4 - 2 3 c
5 -
e 6 - 7 8 f
1 -
b 4 - 2 3 c
5 -
e 6 9
- 7 8 f
1 -
b 4 - 2 3 c
5 -
e 6 9
- 7 8 f
1 -
b 4 - 2 3 c
5 -
e 6 9
- 7 8 f
1 -
b 4 - 2 3 c
5 10
-
e 6 9
- 7 8 f
1 -
b 4 - 2 3 c
5 10
-
e 6 9
- 7 8 f
1 -
b 4 - 2 3 c
5 10
-
e 6 9
- 7 8 f
1 -
b 4 11
- 2 3 c
5 10
-
e 6 9
- 7 8 f
1 12
-
b 4 11
- 2 3 c
5 10
-
e 6 9
- 7 8 f
1 12
- { 𝑎 , 𝑏, 𝑑 ,𝑒 , 𝑓 , 𝑐 }
b 4 11
- 2 3 c
5 10
-
e 6 9
- 7 8 f
b c
e f
b c
e f
b c
e f
b c
e f
b c
e f
b c
e f
b c
e f
b c
e f
𝑂 ( 𝐸 +𝑉 )
Initialize all
the distances
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
-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