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

Chapter 4 - Nonlinear Data Structures - Graphs

Uploaded by

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

Chapter 4 - Nonlinear Data Structures - Graphs

Uploaded by

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

BIOMEDICAL ENGINEERING

DATA STRUCTURES AND ALGORITHMS


CHAPTER 4
NONLINEAR DATA STRUCTURES : GRAPHS
LEARNING OBJECTIVES

At the end of this chapter, you will be able to:


▪ Explain the basic concepts and definitions on graphs.
▪ Discuss the methods of graph representation: adjacency matrix and adjacency list.
▪ Traverse graphs using the algorithms depth-first search and breadth-first search.
▪ Get the minimum cost spanning tree for undirected graphs using Prim's algorithm
and Kruskal's algorithms.
▪ Solve the single-source shortest path problem using Dijkstra's algorithm.
▪ Solve the all-pairs shortest path problem using Floyd's algorithms.
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 3
GRAPHS

 A graph can be defined as group of vertices and edges


that are used to connect these vertices. A graph can be
seen as a cyclic tree, where the vertices (Nodes) maintain
any complex relationship among them instead of having
parent child relationship. A graph G can be defined as an
ordered set G(V, E) where V(G) represents the set of
vertices and E(G) represents the set of edges which are
used to connect these vertices.
 Can be directed and undirected graph.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 4


BASIC TERMINOLOGY
 Path
 Closed Path
 Simple Path
 Cycle.
 Connected Graph
 Complete Graph
 Weighted Graph
 Digraph
 Loop
 Adjacent Nodes
 Degree of a node
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 5
GRAPH REPRESENTATION

 Sequential Representation.
Adjacency matrix was
used to store the mapping
represented by vertices
and edges. In adjacency
matrix, the rows and
columns are represented
by the graph vertices.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 6


GRAPH REPRESENTATION CONT…

 Linked Representation. Adjacency list


is used to store the Graph into the
computer's memory. An adjacency list
is maintained for each node present in
the graph which stores the node value
and a pointer to the next adjacent
node to the respective node. If all the
adjacent nodes are traversed then
store the NULL in the pointer field of
last node of the list. The sum of the
lengths of adjacency lists is equal to
the twice of the number of edges
present in an undirected graph.
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 7
BREADTH FIRST SEARCH (BFS)

 Breadth first search is a graph traversal algorithm that starts traversing the graph
from root node and explores all the neighboring nodes. Then, it selects the nearest
node and explore all the unexplored nodes. The algorithm follows the same process
for each of the nearest node until it finds the goal.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 8


THE BFS ALGORITHM

 Step 1: SET STATUS = 1 (ready state) for each node in G


 Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
 Step 3: Repeat Steps 4 and 5 until QUEUE is empty
 Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
 Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS =
1) and set their STATUS = 2 (waiting state)
 Step 6: EXIT

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 9


EXAMPLE: BFS TRAVERSAL

 Consider the graph G shown in


the following image, calculate the
minimum path p from node A to
node E. Given that each edge has a
length of 1.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 10


SOLUTION: BFS TRAVERSAL

Queue1 Queue2
A A
BD B
Now, backtrack from E to A, using the
nodes available in QUEUE2.
DCF D
The minimum path will be: CF C
A → B → C → E. FEG F
EG E
G
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 11
DEPTH FIRST SEARCH (DFS)

 Depth first search (DFS) algorithm starts with the initial node of the graph G, and
then goes to deeper and deeper until we find the goal node or the node which has
no children. The algorithm, then backtracks from the dead end towards the most
recent node that is yet to be completely unexplored.
 The data structure which is being used in DFS is stack. The process is similar to BFS
algorithm. In DFS, the edges that leads to an unvisited node are called discovery
edges while the edges that leads to an already visited node are called block edges.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 12


THE DFS ALGORITHM

 Step 1: SET STATUS = 1 (ready state) for each node in G


 Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
 Step 3: Repeat Steps 4 and 5 until STACK is empty
 Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
 Step 5: Push on the stack all the neighbours of N that are in the ready state (whose
STATUS = 1) and set their STATUS = 2 (waiting state) [END OF LOOP]
 Step 6: EXIT

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 13


EXAMPLE: DFS TRAVERSAL

 Consider the graph G along with


its adjacency list, given in the
figure below. Calculate the order
to print all the nodes of the graph
starting from node H, by using
depth first search (DFS)
algorithm.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 14


SOLUTION: DFS TRAVERSAL

Stack Data Structure


H
A
BD
BF H –A – D – F – B – C – G – E
B
C
EG
E
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 15
EXERCISES: BFS AND DFS TRAVERSAL

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 16


SPANNING TREES

 A connected non-cyclic graph


 A minimal connected subgraph (by minimal, it means one with the fewest number of
edges.)
 Any tree consisting solely of edges in the graph including all vertices in the graph.
 DF spanning tree
 BF spanning tree

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 17


EXAMPLE: DFS AND BFS SPANNING TREE

DF Spanning Tree: Starting Node at 1 BF Spanning Tree: Starting Node at 1


CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 18
MINIMUM COST SPANNING TREE FOR
UNDIRECTED GRAPHS

 A minimum cost spanning tree (MST), as defined previously, is a subgraph of a given graph G,
in which all the vertices are connected and has the lowest cost. This is particularly useful in
finding the cheapest way to connect computers in a network, and in similar applications.
 Finding the MST for a given undirected graph using brute-force approach is not advisable
since the number of spanning trees for n distinct vertices is nn-2. It is therefore imperative
to use another approach in finding the minimum cost spanning tree and in this lesson, we will
cover algorithms that use the greedy approach. In this approach, a sequence of opportunistic
choices succeeds in finding a global optimum. To solve the MST problem, we shall use Prim's
and Kruskal's algorithms, which are both greedy algorithms.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 19


PRIM’S ALGORITHM

 This algorithm finds the edge of least cost connecting some vertex u in U to some
vertex v in (V – U) at each step of the algorithm:
 Let G = (V, E) be a connected, weighted, undirected graph. Let U denote the set of
vertices chosen and T denote the set of edges already taken in at any instance of the
algorithm.
− Step 1: Choose initial vertex from V and place it in U.
− Step 2: From among the vertices in V - U choose that vertex, say v, which is connected to some
vertex, say u, in U by an edge of least cost.Add vertex v to U and edge (u, v) to T.
− Step 3. Repeat Step 2 until U = V, in which case,T is a minimum cost spanning tree for G.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 20


EXAMPLE: PRIM’S ALGORITHM

 Having D as the start vertex,


Compute for MCST and draw the
final Spanning Tree using Prims
Algorithm.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 21


Initialize array
2

K dv pv
3

10
F C A F  −
B F  −
A 7 3
8 4 C F  −
18
4 D F  −
9
B D
10 E F  −
H 25 F F  −
2
3 G F  −
G 7
E H F  −

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 22


Start with any node, say D
2

3 K dv pv

10
F C A
B
A 7
4
3
8 C
18
4 D T 0 −
9
B D
10 E
H 25 F
2
3 G
G 7
E H

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 23


Update distances of adjacent, unselected nodes
2

K dv pv
3
A
10
F C B

A 7
4
3 C 3 D
8
18 D T 0 −
4
9
B D E 25 D
10
F 18 D
H 25
2 G 2 D
3
H
G 7
E
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 24
Select node with minimum distance
2

K dv pv
3
A
10
F C
B
A 7
4
3
C 3 D
8
18 D T 0 −
4
9
B D E 25 D
10
H 25
F 18 D
2 G T 2 D
3
H
G E
7
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 25
Update distances of adjacent, unselected nodes
2 K dv pv

3 A
10
F C B

A 7
4
3 C 3 D
8
18 D T 0 −
4
9
B D E 7 G
10
H 25
F 18 D
2
3
G T 2 D

G 7
E H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 26


Select node with minimum distance
2
K dv pv

3 A
F C
10 B
7 3
A 4
C T 3 D
8
18 D T 0 −
4
9
B D E 7 G
10
H 25
F 18 D
2
3
G T 2 D

G 7
E H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 27


Update distances of adjacent, unselected nodes
2

K dv pv
3
A
10
F C
B 4 C
7 3
A 4 C T 3 D
8
18
4 D T 0 −
9
B D E 7 G
10
H 25 F 3 C
2
3 G T 2 D

G 7
E H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 28


Select node with minimum distance
2
K dv pv

3 A

10
F C B 4 C

7 3
A 4
C T 3 D
8
18 D T 0 −
4
9
B D E 7 G
10
H 25 F T 3 C
2
3 G T 2 D
G 7
E H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 29


Update distances of adjacent, unselected nodes
2 K dv pv

A 10 F
3

10
F C B 4 C

C T 3 D
A 7
4
3
8 D T 0 −
18
4
9
B D E 2 F
10
H 25
F T 3 C
2
G T 2 D
3
G 7
E H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 30


Select node with minimum distance
2
K dv pv

3 A 10 F

10
F C B 4 C

C T 3 D
A 7
4
3
8
18 D T 0 −
4
9
B D E T 2 F
10
H 25
F T 3 C
2
G T 2 D
3
G 7
E H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 31


Update distances of adjacent, unselected nodes;
2
K dv pv

3 A 10 F

10
F C B 4 C

7 3 C T 3 D
A 4
8 25 D T 0 −
18
4
B E T 2 F
9 D
10 F T 3 C
H
2 G T 2 D
3 H 3 G
G 7
E
Table entries unchanged
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 32
Select node with minimum distance
2
K dv pv

3 A 10 F

10
F C B 4 C

A 7
4
3 C T 3 D
8
18 D T 0 −
4
9
B D E T 2 F
10
H 25
F T 3 C
2
3
G T 2 D

G 7
E H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 33


Update distances of adjacent, unselected nodes
2

K dv pv
3
A 4 H
10
F C B 4 C

A 7
4
3
C T 3 D
8
18 25 D T 0 −
4
9
B D E T 2 F
10
H F T 3 C
2
3 G T 2 D
G 7
E H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 34


Select node with minimum distance
2 K dv pv

A T 4 H
3

10
F C B 4 C

7 3 C T 3 D
A 4
8
18 D T 0 −
4
9
B D E T 2 F
10
H 25
F T 3 C
2
G T 2 D
3
G 7
E H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 35


Update distances of adjacent, unselected nodes
2
K dv pv

3 A T 4 H

10
F C B 4 C

7 3 C T 3 D
A 4
8 D T 0 −
18
4
9
B D E T 2 F
10
F T 3 C
H 25
2 G T 2 D
3
H T 3 G
G 7
E
Table entries unchanged
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 36
Select node with minimum distance
2 K dv pv

A T 4 H
3

10
F C B T 4 C

C T 3 D
A 7
4
3
8 D T 0 −
18
4
9
B D E T 2 F
10
H 25
F T 3 C
2
G T 2 D
3
G 7
E H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 37


Cost of Minimum Spanning Tree =  dv = 21
2 K dv pv

A T 4 H
3
F C B T 4 C

A 4
3 C T 3 D

D T 0 −
4
B D E T 2 F
H F T 3 C
2
3 G T 2 D
G E H T 3 G

Minimum Cost Spanning Tree Done


CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 38
KRUSKAL’S ALGORITHM

 The other greedy algorithm that is used to find the MST was developed by Kruskal. In this
algorithm, the vertices are listed in non-decreasing order of the weights. The first edge to be
added to T, which is the MST, is the one that has the lowest cost. An edge is considered if at
least one of the vertices is not in the tree found so far.
 Let G = (V, E) be a connected, weighted, undirected graph on n vertices. The minimum cost
spanning tree, T, is built edge by edge, with the edges considered in non-decreasing order of
their cost.
1. Choose the edge with the least cost as the initial edge.
2. The edge of least cost among the remaining edges in E is considered for inclusion in T. If cycle will be
created, the edge in T is rejected.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 39


EXAMPLE: KRUSKAL’S ALGORITHM

 Consider an undirected and weighted


graph below, compute for MCST and
draw the Spanning Tree using Kruskal's
Algorithm.

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 40


Sort the edges by increasing edge weight
3 edge dv edge dv

10
F C (D,E) 1 (B,E) 4

4 3
A 4
(D,G) 2 (B,F) 4
8
6 (E,G) 3 (B,H) 4
5

4
B D (C,D) 3 (A,H) 5
4
H 1 (G,H) 3 (D,F) 6
2
3 (C,F) 3 (A,B) 8
G 3
E
(B,C) 4 (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 41


Select first |V|–1 edges which do not generate a cycle
3 edge dv edge dv

10
F C (D,E) 1  (B,E) 4

4 3
A 4
(D,G) 2 (B,F) 4
8
6 (E,G) 3 (B,H) 4
5

4
B D (C,D) 3 (A,H) 5
4
H 1 (G,H) 3 (D,F) 6
2
3 (C,F) 3 (A,B) 8
G 3
E
(B,C) 4 (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 42


Select first |V|–1 edges which do not generate a cycle
edge dv edge dv
3

10
F C (D,E) 1  (B,E) 4

4 3 (D,G) 2 
A 4
(B,F) 4
8
6 (E,G) 3 (B,H) 4
5
4
B D (C,D) 3 (A,H) 5
4
H 1 (G,H) 3 (D,F) 6
2
3 (C,F) 3 (A,B) 8
G 3
E
(B,C) 4 (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 43


Select first |V|–1 edges which do not generate a cycle

3 edge dv edge dv

10
F C (D,E) 1  (B,E) 4

4 3
A 4
(D,G) 2  (B,F) 4
8
6 (E,G) 3  (B,H) 4
5
4
B D (C,D) 3 (A,H) 5
4
H 1
(G,H) 3 (D,F) 6
2
3 (C,F) 3 (A,B) 8
G 3
E (B,C) 4 (A,F) 10

Accepting edge (E,G) would create a cycle


CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 44
Select first |V|–1 edges which do not generate a cycle

3
edge dv edge dv

10
F C (D,E) 1  (B,E) 4

4 3 (D,G) 2  (B,F) 4
A 4
8
6 (E,G) 3  (B,H) 4
5

4
B D (C,D) 3  (A,H) 5
4
H 1
(G,H) 3 (D,F) 6
2
3 (C,F) 3 (A,B) 8

G 3
E (B,C) 4 (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 45


Select first |V|–1 edges which do not generate a cycle
3 edge dv edge dv

10
F C (D,E) 1  (B,E) 4

4 3
A 4
(D,G) 2  (B,F) 4
8
6 (E,G) 3  (B,H) 4
5

4
B D (C,D) 3  (A,H) 5
4
H 1
(G,H) 3  (D,F) 6
2
3 (C,F) 3 (A,B) 8

G 3
E (B,C) 4 (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 46


Select first |V|–1 edges which do not generate a cycle

3
edge dv edge dv

10
F C (D,E) 1  (B,E) 4

4 3 (D,G) 2  (B,F) 4
A 4
8
6 (E,G) 3  (B,H) 4
5

4
B D (C,D) 3  (A,H) 5
4
H 1
(G,H) 3  (D,F) 6
2
3 (C,F) 3  (A,B) 8

G 3
E (B,C) 4 (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 47


Select first |V|–1 edges which do not generate a cycle

3 edge dv edge dv

10
F C (D,E) 1  (B,E) 4

4 3 
A 4
(D,G) 2 (B,F) 4
8
6 (E,G) 3  (B,H) 4
5

4
B D (C,D) 3  (A,H) 5
4
H 1 (G,H) 3  (D,F) 6
2
3 (C,F) 3  (A,B) 8
G 3
E (B,C) 4  (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 48


Select first |V|–1 edges which do not generate a cycle
edge dv edge dv
3

10
F C (D,E) 1  (B,E) 4 

4 3 (D,G) 2  (B,F) 4
A 4
8
6 (E,G) 3  (B,H) 4
5

4
B D (C,D) 3  (A,H) 5
4
H 1
(G,H) 3  (D,F) 6
2
3 (C,F) 3  (A,B) 8

G 3
E (B,C) 4  (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 49


Select first |V|–1 edges which do not generate a cycle
3 edge dv edge dv

10
F C (D,E) 1  (B,E) 4 

4 3 (D,G) 2  (B,F) 4 
A 4
8
6 (E,G) 3  (B,H) 4
5

4
B D (C,D) 3  (A,H) 5
4
H 1
(G,H) 3  (D,F) 6
2
(C,F) 3  (A,B) 8
3

G 3
E (B,C) 4  (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 50


Select first |V|–1 edges which do not generate a cycle

edge dv edge dv
3

10
F C (D,E) 1  (B,E) 4 

4 3 (D,G) 2  (B,F) 4 
A 4
8
6 (E,G) 3  (B,H) 4 
5

4
B D (C,D) 3  (A,H) 5
4
H 1
(G,H) 3  (D,F) 6
2
3 (C,F) 3  (A,B) 8

G 3
E (B,C) 4  (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 51


Select first |V|–1 edges which do not generate a cycle

3 edge dv edge dv

10
F C (D,E) 1  (B,E) 4 

4 3 (D,G) 2  (B,F) 4 
A 4
8
6 (E,G) 3  (B,H) 4 
5

4
B D (C,D) 3  (A,H) 5 
4
H 1
(G,H) 3  (D,F) 6
2
3 (C,F) 3  (A,B) 8

G 3
E (B,C) 4  (A,F) 10

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 52


Cost of Minimum Spanning Tree = dv= 21
3 edge dv edge dv

F C (D,E) 1  (B,E) 4 

3 (D,G) 2  (B,F) 4 
A 4
(E,G) 3  (B,H) 4 
5
B D (C,D) 3  (A,H) 5 

}
H 1
(G,H) 3  (D,F) 6
2 not
3
(C,F) 3  (A,B) 8
considered
G E (B,C) 4  (A,F) 10

Minimum Cost Spanning Tree Done


CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 53
SHORTEST PATH PROBLEMS FOR
DIRECTED GRAPHS

 Another classic set of problems in graphs is finding the shortest path given a
weighted graph. In finding the shortest path, there is a need to get the length which,
in this case, is the sum of the nonnegative cost of each edge in the path.
 There are two path problems on weighted graphs:
− Single Source Shortest Paths (SSSP) Problem that determines the cost of the shortest path from a
source vertex u to a destination vertex v, where u and v are elements of V.
− All-Pairs Shortest Paths (APSP) Problem that determines the cost of the shortest path from each
vertex to every other vertex in V

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 54


DIJKSTRA'S ALGORITHM FOR
THE SSSP PROBLEM

 Just like Prim's and Kruskal's, Dijkstra's algorithm uses the greedy approach. In this algorithm,
each vertex is assigned a class and a value, where:
 Class 1 vertex is a vertex whose shortest distance from the source vertex, say k, has already
been found; Its value is equal to its distance from vertex k along the shortest path.
 Class 2 vertex is a vertex whose shortest distance from k has yet to be found. Its value is its
shortest distance from vertex k found thus far.
 Let vertex u be the source vertex and vertex v be the destination vertex. Let pivot be the
vertex that is most recently considered to be a part of the path. Let path of a vertex be its
direct source in the shortest path.
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 55
THE DIJKSTRA'S ALGORITHM
 Step 1: Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for
all other nodes.
 Step 2: Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes
called the unvisited set.
 Step 3: For the current node, consider all of its neighbors and calculate their tentative distances. Compare
the newly calculated tentative distance to the current assigned value and assign the smaller one.
 Step 4: When we are done considering all of the neighbors of the current node, mark the current node as
visited and remove it from the unvisited set.A visited node will never be checked again.
 Step 5: If the destination node has been marked visited (when planning a route between two specific nodes)
or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a
complete traversal; occurs when there is no connection between the initial node and remaining unvisited
nodes), then stop.The algorithm has finished.
 Step 6: Otherwise, select the unvisited node that is marked with the smallest tentative distance, set it as the
new "current node", and go back to step 3.
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 56
EXAMPLE: DIJKSTRA'S ALGORITHM

 Given the following weighted, directed


graph, find single-source shortest path
using Dijkstra's algorithm starting at
node G. Identify shortest path from G
to C? G to E?

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 57


Initialize array
2 K dv pv

A F  −
3
5 F C B F  −

10 C F  −
A 7 3
4
8
18 D F  −
4
B D
9 E F  −
10
H
9 25 F F  −
2
3 G F  −
G E
7 H F  −

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 58


Start with G
2 K dv pv

A
3
5 F C B
10
A 7 3 C
4
8
18 D
4
B D
9 E
10
H F
9 25
2
3 G T 0 −
G E
7 H

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 59


Update unselected nodes
2 K dv pv

A
3
5 F C B
10
A 7 3 C
4
8
18 D 2 G
4
B D
9 E
10
H F
9 25
2
3 G T 0 −
G E
7 H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 60


Select minimum distance

2 K dv pv

A
3
5 F C B

10 C
A 7 3
4
8 25
18 D T 2 G
4
B D
9 E
10
H F
9
2
3 G T 0 −
G E
7 H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 61


Update unselected nodes
2 K dv pv

A
3
5 F C B
10
A 7 3 C
4
8
18 25 D T 2 G
4
B D
9 E 27 D
10
H F 20 D
9
2
3 G T 0 −
G E
7 H 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 62


Select minimum distance
2 K dv pv

A
3
5 F C B
10
A 7 3 C
4
8
18 D T 2 G
4
B D
9 E 27 D
10
H F 20 D
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 63


Update unselected nodes
2
K dv pv

3 A 7 H
5 F C B 12 H
10
A 7 3
4
C
8
18
4 D T 2 G
B D
9
10
E 27 D
H
9 25 F 20 D
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 64


Select minimum distance
2 K dv pv

A T 7 H
3
5 F C B 12 H
10
A 7 3 C
4
8
18 D T 2 G
4
B D
9 E 27 D
10
H F 20 D
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 65


Update unselected nodes
2 K dv pv

A T 7 H
3
5 F C B 12 H
10
A 7 3 C
4
8
18 D T 2 G
4
B D
9 E 27 D
10
H F 17 A
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 66


Select minimum distance
2 K dv pv

A T 7 H
3
5 F C B T 12 H

10 C
A 7 3
4
8
18 D T 2 G
4
B D E 27 D
9
10
H F 17 A
9 25
2
3 G T 0 −
G E H T 3 G
7

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 67


Update unselected nodes
2 K dv pv

A T 7 H
3
5 F C B T 12 H
10
A 7 3 C 16 B
4
8
18 D T 2 G
4
B D
9 E 22 B
10
H F 17 A
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 68


Select minimum distance
2 K dv pv

A T 7 H
3
5 F C B T 12 H
10
A 7 3 C T 16 B
4
8
18 D T 2 G
4
B D
9 E 22 B
10
H F 17 A
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 69


Update unselected nodes
2 K dv pv

A T 7 H
3
5 F C B T 12 H
10
A 7 3 C T 16 B
4
8
18 D T 2 G
4
B D
9 E 22 B
10
H F 17 A
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 70


Select minimum distance
K dv pv
2

A T 7 H
3
5 F B T 12 H
C
10
7 3
C T 16 B
A
4
8 D T 2 G
18
4
B D E 22 B
9
10
H F T 17 A
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 71


Update unselected nodes
2 K dv pv

A T 7 H
3
5 F C B T 12 H
10
A 7 C T 16 B
4
8
18 D T 2 G
4
B D
9 E 19 F
10
H F T 17 A
9 25
2
3 G T 0 −
G E
7 H T 3 G

CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 72


Select minimum distance
K dv pv
2
A T 7 H

B T 12 H
F C C T 16 B
10
A D T 2 G
4

4 E T 19 F
B D
9 F T 17 A
H
G T 0 −
2
3 H T 3 G
G E
Shortest path from G to C: 3+9+4 = 16
Minimum Cost Spanning Tree Shortest path from G to E: 3+4+10+2 = 19
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 73
SUMMARY

 A graph can be seen as a cyclic tree, where the vertices (Nodes) maintain any
complex relationship among them instead of having parent child relationship.
 A graph can be represented in terms of sequential and linked representation.
 For sequential representation a graph can be written using adjacency matrix
otherwise for linked representation it can be written using adjacency list.
 Breadth first search is a graph traversal algorithm that starts traversing the graph
from root node and explores all the neighboring nodes. Then, it selects the nearest
node and explore all the unexplored nodes. The algorithm follows the same process
for each of the nearest node until it finds the goal.
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 74
SUMMARY CONT…

 Depth first search (DFS) algorithm starts with the initial node of the graph G, and then goes
to deeper and deeper until we find the goal node or the node which has no children. The
algorithm, then backtracks from the dead end towards the most recent node that is yet to
be completely unexplored.
 A minimum cost spanning tree (MCST), as defined previously, is a subgraph of a given graph
G, in which all the vertices are connected and has the lowest cost. MCST can be computed
using Prim’s and Kruskal’s Algorithm.
 Single Source Shortest Paths (SSSP) determines the cost of the shortest path from a source
vertex u to a destination vertex v, where u and v are elements of V.
 All-Pairs Shortest Paths (APSP) determines the cost of the shortest path from each vertex
to every other vertex in V.
CHAPTER 4: NONLINEAR DATA STRUCTURES - GRAPHS Sunday, 19 February 2023 75

You might also like