0% found this document useful (0 votes)
51 views23 pages

CS214 DS2022 Lec 13 - Graphs Part2

The document discusses shortest path problems and algorithms, including Dijkstra's algorithm, for finding the shortest route between two nodes in a graph. It provides an example application of finding the shortest driving route between cities. It also explains modeling other problems, like connecting electronic circuit pins with the least amount of wire, as finding a minimum spanning tree in a graph.

Uploaded by

azer elsaied
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)
51 views23 pages

CS214 DS2022 Lec 13 - Graphs Part2

The document discusses shortest path problems and algorithms, including Dijkstra's algorithm, for finding the shortest route between two nodes in a graph. It provides an example application of finding the shortest driving route between cities. It also explains modeling other problems, like connecting electronic circuit pins with the least amount of wire, as finding a minimum spanning tree in a graph.

Uploaded by

azer elsaied
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/ 23

Data Structures

SCS 214
2nd Term 2021-2022

Graphs
Shortest Path
Minimum Spanning Tree
1
8.3 Shortest Path Problem
• It is the problem of finding the shortest route
connecting a source node to a destination node.
• Applications?
• Transportation / communication
• Finding the shortest road from a city to another
(navigator devices / Mapquest / Google Maps).
• Finding the shortest path to route to direct a
data packet between two computers or a phone
call between two mobile phones.
Shortest Path Problem

• 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 5
Complexity of Dijkstra’s Algorithm

• The algorithm’s complexity is O(|V2|)


• The first for loop and while loop and executed
|V| times.
• .. .
Dijkstra's Shortest Path Algorithm

• Find shortest path from s to t.

24
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44
7
Dijkstra's Shortest vertex weight previous visited
s 0 s F
Path Algorithm 2 ∞ F
3 ∞ F
4 ∞ F
tobeChecked = { s, 2, 3, 4, 5, 6, 7, t } 5 ∞ F
6 ∞ F
7 ∞ F
t ∞ F

8
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s F
3 ∞ F
4 ∞ F
tobeChecked = { 2, 3, 4, 5, 6, 7, t } 5 ∞ F
6 14 s F
7 15 s F
t ∞ F

9
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 33 2 F
4 ∞ F
tobeChecked = { 3, 4, 5, 6, 7, t } 5 ∞ F
6 14 s F
7 15 s F
t ∞ F

10
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 32 6 F
4 ∞ F
tobeChecked = { 3, 4, 5, 7, t } 5 44 6 F
6 14 s T
7 15 s F
t ∞ F

11
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 32 6 F
4 ∞ F
tobeChecked = { 3, 4, 5, t } 5 35 7 F
6 14 s T
7 15 s T
t 59 7 F

12
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 32 6 T
4 ∞ F
tobeChecked = { 4 , 5 , t } 5 34 3 F
6 14 s T
7 15 s T
t 51 3 F

13
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 32 6 T
4 45 5 F
tobeChecked = { 4 , t } 5 34 3 T
6 14 s T
7 15 s T
t 50 5 F

14
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 32 6 T
4 45 5 T
tobeChecked = { t } 5 34 3 T
6 14 s T
7 15 s T
t 50 5 F

15
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 32 6 T
4 45 5 T
tobeChecked = { } 5 34 3 T
6 14 s T
7 15 s T
t 50 5 T

16
Dijkstra's Shortest vertex weight previous visited
s 0 s T
Path Algorithm 2 9 s T
3 32 6 T
4 45 5 T
shortestPath = 50 5 34 3 T
Nodes = { s, 6, 3, 5, t }
6 14 s T
7 15 s T
t 50 5 T

17
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

• Can a graph have more


than one spanning tree? c v f

• Can a disconnected graph d


have a spanning tree?

Greedy/Prims 18
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.

Modeling the Problem


• The graph is a complete, undirected graph
G = ( V, E ,W ), where V is the set of pins, E is the set
of all possible interconnections between the pairs of
pins and w(e) is the length of the wire needed to
connect the pair of vertices.
• Find a minimum spanning tree.

Greedy/Prims 19
Greedy Choice

There are two ways to build a minimum spanning tree.


• A MST can be grown from the current spanning tree
by adding the nearest vertex. (Prim's algorithm)

• A MST can be grown from a forest of spanning trees


by adding the smallest edge. (Kruskal's algorithm)

Greedy/Prims 20
Starting with
node f
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 23
f (x*)= 18

You might also like