Chapter 4 - Nonlinear Data Structures - Graphs
Chapter 4 - Nonlinear Data Structures - Graphs
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.
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.
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.
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.
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.
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 −
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
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
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
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
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
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
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
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
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
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
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
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
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.
10
F C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3 (D,F) 6
2
3 (C,F) 3 (A,B) 8
G 3
E (B,C) 4 (A,F) 10
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
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
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
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
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
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
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
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
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 −
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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