Unit-3 �Non-Linear Data Structure Graph
pradyuman.jadeja@darshan.ac.in
+91 9879461848
Computer Engineering Department
Dr. Pradyumansinh Jadeja
Data Structures (DS)
GTU # 3130702
Darshan Institute of Engineering & Technology, Rajkot
Graphs
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
2
Adjacency matrix
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
3
Adjacency matrix
V1
V2
V3
V4
0 | 1 | 0 | 1 |
1 | 0 | 0 | 0 |
1 | 1 | 0 | 1 |
0 | 1 | 0 | 0 |
V1
V1
V2
V2
V3
V3
V4
V4
A =
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
4
Adjacency matrix
V1
V2
V3
V4
0 | 1 | 0 | 1 |
1 | 0 | 0 | 0 |
1 | 1 | 0 | 1 |
0 | 1 | 0 | 0 |
V1
V1
V2
V2
V3
V3
V4
V4
A =
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
5
Power of Adjacency matrix
0 | 1 | 0 | 1 |
1 | 0 | 0 | 0 |
1 | 1 | 0 | 1 |
0 | 1 | 0 | 0 |
A =
A2 = A x A =
1 | 1 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 2 | 0 | 1 |
1 | 0 | 0 | 0 |
1 | 1 | 0 | 1 |
1 | 1 | 0 | 0 |
2 | 2 | 0 | 1 |
0 | 1 | 0 | 1 |
A3 =
1 | 2 | 0 | 1 |
1 | 1 | 0 | 1 |
2 | 3 | 0 | 2 |
1 | 1 | 0 | 0 |
A4 =
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
6
Path matrix or reachability matrix
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
7
Adjacency List Representation
0
1
3
2
4
0 |
1 |
2 |
3 |
4 |
1
2
3
4
0
3
0
3
4
0
1
2
4
0
2
3
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
8
Graph Traversal
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
9
Depth First Search (DFS)
1
2
3
4
5
6
7
8
DFS (G, 1) is given by
Step 1: Visit (1)
Step 2: DFS (G, 2)
DFS (G, 3)
DFS (G, 4)
DFS (G, 5)
DFS (G, 2):
Step1: Visit(2)
Step 2: DFS (G, 6)
DFS (G, 6):
Step1: Visit(6)
Step 2: DFS (G, 3)
DFS (G, 8)
DFS of given graph starting from node 1 is given by
1
2
6
3
8
7
4
5
✓
✓
✓
✓
✓
✓
✓
✓
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit @ – Non Linear Data Structures (Graph)
10
Depth First Search (DFS)
A
B
C
D
E
F
G
H
A
B
C
D
E
F
R
M
Q
N
P
O
A
B
D
H
E
C
F
G
✓
✓
✓
✓
✓
✓
✓
✓
A
B
D
C
F
E
✓
✓
✓
✓
✓
✓
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
11
Breadth First Search (BFS)
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
12
Breadth First Search (BFS)
1
3
4
5
6
7
8
2
A
B
C
D
E
F
G
H
V0
V1
V3
V2
V4
V5
V6
✓
1
2
3
4
5
6
7
8
|
|
|
✓
✓
✓
✓
✓
✓
✓
A
| B C
| D E F G
| H
✓
✓
✓
✓
✓
✓
✓
✓
V0| V1 V2 | V4 V6 V3 | V5
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
13
Write DFS & BFS of following Graphs
A
B
C
D
E
F
G
H
A
B
C
D
E
F
R
M
Q
N
P
O
0
1
4
3
5
2
A
B
C
D
E
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
14
Procedure : DFS (vertex V)
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
15
Procedure : DFS (vertex V)
1. [Initialize TOP and Visited]
visited[] 🡨 0
TOP 🡨 0
2. [Push vertex into stack]
PUSH (V)
3. [Repeat while stack is not Empty]
Repeat Step 3 while stack is not empty
v 🡨 POP()
if visited[v] is 0
then visited [v] 🡨 1
for all W adjacent to v
if visited [w] is 0
then PUSH (W)
end for
end if
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
16
Procedure : BFS (vertex V)
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
17
Procedure : BFS (vertex V)
1. [Initialize Queue & Visited]
visited[] 🡨 0
F 🡨 R 🡨 0
2. [Marks visited of V as 1]
visited[v] 🡨 1
3. [Add vertex v to Q]
InsertQueue(V)
4. [Repeat while Q is not Empty]
Repeat while Q is not empty
v 🡨 RemoveFromQueue()
For all vertices W adjacent to v
If visited[w] is 0
Then visited[w] 🡨 1
InsertQueue(w)
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
18
Spanning Tree
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
19
Construct Spanning Tree
A
B
C
D
E
F
G
H
A
B
C
D
E
F
G
H
DFS Spanning Tree
BFS Spanning Tree
A
B
C
D
E
F
G
H
A
B
C
D
E
F
A
B
C
D
E
F
A
B
C
D
E
F
DFS Spanning Tree
BFS Spanning Tree
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non-Linear Data Structure (Graph)
20
Minimum Cost Spanning Tree
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
21
Prims Algorithm
A
B
C
D
E
4
2
1
7
5
3
6
6
5
A – B | 4
A – E | 5
A – C | 6
A – D | 6
B – E | 3
B – C | 2
C – E | 5
C – D | 1
D – E | 7
Let X be the set of nodes
explored, initially X = { A }
A
Step 1: Taking minimum Weight edge of all Adjacent edges of X={A}
A
B
4
X = { A , B }
Step 2: Taking minimum weight edge of all Adjacent edges of X = { A , B }
A
B
4
2
C
X = {A ,B,C}
Step 3: Taking minimum weight edge of all Adjacent edges of X = { A , B , C }
A
B
4
2
C
D
1
X = {A,B,C,D}
A
B
4
2
C
D
1
X = {A,B,C,D,E}
Step 4: Taking minimum weight edge of all Adjacent edges of X = {A ,B ,C ,D }
E
3
We obtained minimum spanning tree of cost:
4 + 2 + 1 + 3 = 10
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non-Linear Data Structure (Graph)
22
Kruskal’s Algorithm
A
B
C
D
E
4
2
7
5
3
6
6
5
Step 1: Taking min edge (C,D)
1
C
D
1
Step 2: Taking next min edge (B,C)
B
C
D
2
1
Step 3: Taking next min edge (B,E)
B
C
D
E
2
3
1
Step 4: Taking next min edge (A,B)
B
C
D
E
2
3
1
A
4
so we obtained minimum
spanning tree of cost:
4 + 2 + 1 + 3 = 10
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
23
Construct Minimum Spanning Tree
0
1
3
4
5
2
3
6
5
5
3
6
6
4
1
2
0
1
3
4
5
2
3
3
4
1
2
3
1
6
4
2
7
5
1
4
1
2
3
2
2
3
3
3
1
4
2
5
1
1
2
2
2
3
7
6
4
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
24
Shortest Path Algorithm
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
25
Dijkstra Algorithm – Shortest Path
A
B
C
D
E
F
1
1
5
2
4
5
7
6
2
A | B | C | D | E | F |
0 | ∞ | ∞ | ∞ | ∞ | ∞ |
0 | 0 | 0 | 0 | 0 | 0 |
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4
5
7
6
2
A | B | C | D | E | F |
0 | | | ∞ | ∞ | ∞ |
| | | 0 | 0 | 0 |
Distance
Visited
0
1st Iteration: Select Vertex A with minimum distance
0
∞
1
1
0
0
∞
5
1
5
∞
∞
∞
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
26
Dijkstra Algorithm – Shortest Path
A
B
C
D
E
F
1
1
5
2
4
7
6
2
0
2nd Iteration: Select Vertex B with minimum distance
1
2
Cost of going to C via B = dist[B] + cost[B][C] = 1 + 1 = 2
Cost of going to D via B = dist[B] + cost[B][D] = 1 + 2 = 3
Cost of going to E via B = dist[B] + cost[B][E] = 1 + 4 = 5
Cost of going to F via B = dist[B] + cost[B][F] = 1 + ∞ = ∞
A | B | C | D | E | F |
0 | 1 | 5 | ∞ | ∞ | ∞ |
1 | 0 | 0 | 0 | 0 | 0 |
Distance
Visited
3
5
∞
A | B | C | D | E | F |
0 | 1 | 2 | 3 | 5 | ∞ |
| | | 0 | 0 | 0 |
Distance
Visited
1
1
0
5
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
27
Dijkstra Algorithm – Shortest Path
3rd Iteration: Select Vertex C via B with minimum distance
A | B | C | D | E | F |
0 | 1 | 2 | 3 | 5 | ∞ |
1 | 1 | 0 | 0 | 0 | 0 |
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4
7
6
2
0
1
3
2
5
∞
Cost of going to D via C = dist[C] + cost[C][D] = 2 + ∞ = ∞
Cost of going to E via C = dist[C] + cost[C][E] = 2 + 5 = 7
Cost of going to F via C = dist[C] + cost[C][F] = 2 + ∞ = ∞
A | B | C | D | E | F |
0 | 1 | 2 | 3 | 5 | ∞ |
| | | 0 | 0 | 0 |
Distance
Visited
1
1
1
5
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
28
Dijkstra Algorithm – Shortest Path
4th Iteration: Select Vertex D via path A - B with minimum distance
A | B | C | D | E | F |
0 | 1 | 2 | 3 | 5 | ∞ |
1 | 1 | 1 | 0 | 0 | 0 |
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4
7
6
2
1
3
2
5
9
Cost of going to E via D = dist[D] + cost[D][E] = 3 + 7 = 10
Cost of going to F via D = dist[D] + cost[D][F] = 3 + 6 = 9
A | B | C | D | E | F |
0 | 1 | 2 | 3 | 5 | 9 |
| | | 1 | 0 | 0 |
Distance
Visited
1
1
1
5
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
29
Dijkstra Algorithm – Shortest Path
4th Iteration: Select Vertex E via path A – B – E with minimum distance
A | B | C | D | E | F |
0 | 1 | 2 | 3 | 5 | 9 |
1 | 1 | 1 | 1 | 0 | 0 |
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4
7
6
2
1
3
2
5
7
Cost of going to F via E = dist[E] + cost[E][F] = 5 + 2 = 7
A | B | C | D | E | F |
0 | 1 | 2 | 3 | 5 | 7 |
| | | 1 | 1 | 0 |
Distance
Visited
1
1
1
Shortest Path from A to F is
A 🡪 B 🡪 E 🡪 F = 7
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
30
Shortest Path
Find out shortest path from node 0 to all other nodes using Dijkstra Algorithm
0
1
2
3
4
10
100
30
60
50
10
20
0
1
2
3
4
10
100
30
60
50
10
20
0
10
50
60
30
Dr. Pradyumansinh U. Jadeja
#3130702 (DS) ⬥ Unit 3 – Non Linear Data Structures (Graph)
31
pradyuman.jadeja@darshan.ac.in
+91 9879461848
Computer Engineering Department
Dr. Pradyumansinh Jadeja
Data Structures (DS)
GTU # 3130702
Darshan Institute of Engineering & Technology, Rajkot
Thank
You