Professional Documents
Culture Documents
SCS 214
2nd Term 2021-2022
Graphs
1
Agenda
1. Graph representation
2. Graph traversal
3. Shortest path
• Dijkstra
• Floyd
4. Minimum spanning tree
• Prim
2
Graph Definitions
Cycle
• A graph G is connected if
there is a path from any vertex
to any other vertex in the
graph
Connected
• A connected graph
cannot have any
isolated vertices
Graph Theory
14 Chapter 9: Sorting
8.1 Graph Representation
• For a multi-graph,
• aij = is the number of edges between vi and vj
0 otherwise
8.1 Graph Representation
20 Chapter 9: Sorting
A Small Airline Service Graph
A Small Airline Service Graph
Path and Cycle
Representing A Graph w an
Adjacency List
Representing A Graph w an
Adjacency Matrix
Adjacency matrix of
an undirected graph
is symmetric across
the diagonal
Directed Graph Representation
Undirected Graph Representation
Representation Costs
Adjacency Matrix:
▪ Space is O(N2)
▪ Finding the neighbors is O(N)
▪ Finding if two nodes are connected is O(1)
Adjacency List:
• Space is O(N x Dmax) or O(N + |E|)
▪ Finding the neighbors is O(Dmax)
▪ Finding if two nodes are connected is O(Dmax)
A Small Airline Service Graph w Costs
8.2 Graph Traversal
• Applications?
• In a graph, vertices can describe states and
edges describe possible transitions.
• Shortest path algorithms can find an optimal
sequence of choices to reach a certain goal
state with the least time or effort.
• E.g., if vertices are the states of a puzzle like
Rubik's Cube and each directed edge is a single
move or turn, shortest path algorithms can be
used to find a solution that uses the minimum
possible number of moves.
Dijkstra’s Algorithm
Dijkstra (weighted simple digraph, vertex first)
for all vertices v
currDist(v) = ;
currDist(first) = 0;
tobeChecked = all vertices;
while tobeChecked is not empty
v = a vertex in tobeChecked with minimal currDist(v);
remove v from tobeChecked;
for all vertices u adjacent to v and in tobeChecked
if currDist(u) > currDist(v) + weight (edge(vu))
currDist(u) = currDist(v) + weight (edge(vu));
predecessor(u) = v;
Chapter 6: Binary Trees 40
Complexity of Dijkstra’s Algorithm
24
2 3
9
s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
42
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
24
2 3
0 9
s
18
14 2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
43
distance label
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin
24
2 3
0 9
s
18
14 2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
44
distance label
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
decrease key
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
45
distance label 15
X
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
delmin
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
46
distance label 15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
47
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
decrease key
33
X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
48
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
33
X
X 9
24
2 3
0 9
delmin
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
49
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
50
15
X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
51
15
X delmin
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
52
15
X
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t } delmin
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
53
15
X
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
54
15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
delmin
t
7 44
55
15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
56
15
X 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5 delmin
5
6
20 16
t
7 44
57
15
X 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
58
15
X 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
59
15
X
delmin 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
60
15
X 50 51
X 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
61
15
X 50 51
X 59
X X
Chapter 6: Binary Trees 62
8.5 Minimum Spanning Tree
• .. ..
Chapter 6: Binary Trees 64
What is A Spanning Tree?
• A spanning tree for an
undirected graph G=(V,E)
is a subgraph of G that is a
tree and contains all the a
vertices of G
b u e
Greedy/Prims 65
Example of a Problem that Translate into
a MST
The Problem
• Several pins of an electronic circuit must be
connected using the least amount of wire.
Greedy/Prims 66
Greedy Choice
Greedy/Prims 67
3. Minimum Spanning Tree Problem
◼ Example of Prim’s Algorithm
2
2 1 2
1 2 3 6
2 3 6 2
1 2 1 2 5 8 4
3 6 5 8 4 3 6
1 3
1 3
5 8 4 5 8 4
7 2
1 3 6 7 4 2
3 1 3 6 7
4
9 3
7 9
7 2 7 2 2
6 4 3 2 6 7
4
9 3 2
7 9 2 8 6 8
6
2 1 2 1
2 8 5 4 6 2 8 5 4
6
1 1
5 4 5 4
Select a node
at random
2
2 1 2
1 2 3 6
2 3 6 2
1 2 1 2 5 8 4
3 6 5 8 4 3 6
1 3
5 8 4 1 3
5 8 4 7 2
1 3 7 2 6 7
4
9 3
6 7
4
9 3 1 3
7 2 2
6 7
4
9 3 2 6 7 4 2
3 2 8
2 8 7 9 6
6
2 2 1
6 2 8 1 2 8
5 4
5 4 6
5 1 4 5 1 4
Soft Computing Lab. WASEDA UNIVERSITY , IPS 71
f (x*)= 18