Graph - 2020 - Pr1 and 2
Graph - 2020 - Pr1 and 2
Elena COJUHARI
MS I, 2020
SEERVADA PARK has recently been set aside for a limited amount of sightseeing and backpack hiking. Cars
are not allowed into the park, but there is a narrow, winding road system for trams and for jeeps driven by the
park rangers. This road system is shown (without the curves) in the figure
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
where location O is the entrance into the park; other letters designate the locations of ranger stations (and other
limited facilities). The numbers give the distances of these winding roads in miles.
The park contains a scenic wonder at station T . A small number of trams are used to transport sightseers
from the park entrance to station T and back.
Closest
Solved Nodes Total nth Mini- Last
Connected
n Directly Connected Distance Nearest mum Con-
Unsolved
to Unsolved Nodes Involved Node Distance nection
Node
1 O A 2 A 2 OA
O C 4 C 4 OC
2,3
A B 2+2=4 B 4 AB
A D 2+7=9
4 B E 4+3=7 E 7 BE
C E 4+4=8
A D 2+7=9
5 B D 4+4=8 D 8 BD
E D 7+1=8 D 8 ED
D T 8 + 5 = 13 T 13 DT
6
E T 7 + 7 = 14
A
2 2 7
T
5
5 4
O B D
1 3 1
4 7
C E
4
Applying Dijkstra’s algorithm to the next graph, find shortest paths from vertex 1
to vertices 2, 3, 4.
8
1 2
5 1 7 2
3 4
0 8
8
1 2
5 1 7 2
5 7
3 4
0 8
8
1 2
5 1 7 2
5 7
3 4
0 6
8
1 2
5 1 7 2
5 7
3 4
0 6
8
1 2
5 1 7 2
5 7
3 4
0 6
8
1 2
5 1 7 2
5 7
3 4
0 6
8
1 2
5 1 7 2
5 7
3 4
2. L4 = 7, k = 4 PL = {1, 2, 3, 4}, TL =∅
0 6
8
1 2
5 1 7 2
5 7
3 4
8
1 2
5 1 7 2
3 4
Path 1 → 3
Path 1 → 2
Path 1 → 4
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
G = (V , E), V = 1, 7, lij , i = 1, 6, j = 2, 7
1 VO , L1 = 0
l1j , (1, j) ∈ G
j = 2, . . . , 7 , TL : L̃j =
∞, (1, j) ∈ /G
2
2
A ∞
0 5 ∞ T
O B D
4 ∞
C E
1 2 3 4 5 6 7
PL = {O} T L = {A; B; C; D; E; T }
E. Cojuhari Mathematics in Computer Sciences 14 / 31
Dijkstra’s Algorithm for Seervada Park example
2
L2 = min{L̃2 , . . . , L̃7 } = min{2, 5, 4, ∞, ∞, ∞} = 2 2
L2 = 2 for A, k = 2 ∞
1 2 3 4 5 6 7 A
PL = {O; A} T L = {B; C; D; E; T } 0 4 9 T
O B D
4 ∞
3 C E
L̃j = mink {L̃j , Lk + lkj }
B L̃3 = min{L̃3 , L2 + l23 } = min{5, 2 + 2} = 4 L̃3 =4
C L̃4 = min{L̃4 , L2 + l24 } = min{4, 2 + ∞} = 4 L̃4 =4
D L̃5 = min{L̃5 , L2 + l25 } = min{∞, 2 + 7} = 9 L̃5 =9
E L̃6 = min{L̃6 , L2 + l26 } = min{∞, ∞} = ∞ L̃6 =∞
T L̃7 = min{L̃7 , L2 + l27 } = min{∞, ∞} = ∞ L̃7 =∞
2
k −? min{4, 4, 9, ∞, ∞}
Lk = L̃3(B) = 4 B C 2
Lk = L̃min
k =4 (3 < 4) ∞
Lk = L̃4(C) = 4 A
0 4 8 T
L3 = L̃3 = 4 for B, k = 3
1 2 3 4 5 6 7 O B D
PL = {O; A; B} T L = {C; D; E; T } 4 7
C E
3
L̃j = mink {L̃j , Lk + lkj }
C L̃4 = min{L̃4 , L3 + l34 } = min{4, 4 + 1} = 4 L̃4 =4
D L̃5 = min{L̃5 , L3 + l35 } = min{9, 4 + 4} = 8 L̃5 =8
E L̃6 = min{L̃6 , L3 + l36 } = min{∞, 4 + 3} = 7 L̃6 =7
T L̃7 = min{L̃7 , L3 + l37 } = min{∞, 4 + ∞} = ∞ L̃7 =∞
E. Cojuhari Mathematics in Computer Sciences 16 / 31
Dijkstra’s Algorithm for Seervada Park example
2
k −? min{4, 8, 7, ∞} = 4 2
L4 = L̃4 = 4, for C, k = 4 ∞
1 2 3 4 5 6 7 A
PL = {O; A; B; C} T L = {D; E; T } 0 4 8 T
O B D
4 7
3 C E
L̃j = mink {L̃j , Lk + lkj }
D L̃5 = min{L̃5 , L4 + l45 } = min{8, 4 + ∞} = 8 L̃5 = 8
E L̃6 = min{L̃6 , L4 + l46 } = min{7, 4 + 4} = 7 L̃6 = 7
T L̃7 = min{L̃7 , L4 + l47 } = min{∞, 4 + ∞} = ∞ L̃7 = ∞
2
k −? min{8, 7, ∞} = 7 2
L6 = L̃6 = 7, for E, k = 6 14
1 2 3 4 6 5 7 A
PL = {O; A; B; C; E} T L = {D; T } 0 4 8 T
O B D
4 7
3 C E
L̃j = mink {L̃j , Lk + lkj }
D L̃5 = min{L̃5 , L6 + l65 } = min{8, 7 + 1} = 8 L̃5 = 8
T L̃7 = min{L̃7 , L6 + l67 } = min{∞, 7 + 7} = 14 L̃7 = 14
2
k −? min{8, 14} = 8 2
L5 = L̃5 = 8, for D, k = 5 13
1 2 3 4 6 5 7 A
PL = {O; A; B; C; E; D} T L = {T } 0 4 8 T
O B D
4 7
3 C E
L̃j = mink {L̃j , Lk + lkj }
T L̃7 = min{L̃7 , L5 + l57 } = min{14, 8 + 5} = 13 L̃7 = 13
2
k −? min{13} = 13
L7 = L̃7 = 13, for T , k = 7
1 2 3 4 6 5 7
PL = {O; A; B; C; E; D; T } TL=∅
STOP
2
A 13
0 4 8 T
O B D
4 7
C E
2
A
13
2 2
5 T
4 8
4
O B D
3 1
4 4 7
C E
Nodes and distances for the problem are summarized below, where the thin lines
now represent potential links.
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
A
2 2
7
T
5
5 4
O B D
1 3 1
4 7
C E
4
Dijkstra’s algorithm (or Dijkstra’s Shortest Path First algorithm, SPF algorithm) is an algorithm for finding the shortest paths between nodes in a
graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years
later. The algorithm exists in many variants. Dijkstra’s original algorithm found the shortest path between two given nodes, but a more common variant
fixes a single node as the "source" node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree.
1
Dijkstra’s algorithm is shown on the page , where a connected graph G is a graph in which,
7
for any two vertices v and w in G, there is a path v → w. The algorithm is a labeling procedure. At
each stage of the computation, each vertex v gets a label, either
(PL) a permanent label = length Lv of a shortest path 1 → v
or
(TL) a temporary label = upper bound L̃v for the length of a shortest path 1 → v .
We denote by PL and T L the sets of vertices with a permanent label and with a temporary
label, respectively. The algorithm has an initial step in which vertex 1 gets the permanent label
L1 = 0 and the other vertices get temporary labels, and then the algorithm alternates between Steps
2 and 3. In Step 2 the idea is to pick k "minimally". In Step 3 the idea is that the upper bounds will in
general improve (decrease) and must be updated accordingly. Namely, the new temporary label L̃j
of vertex j will be the old one if there is no improvement or it will be Lk + lkj if there is.
1
EDSGER WYBE DIJKSTRA (1930–2002), Dutch computer scientist, 1972 recipient of the ACM
Turing Award. His algorithm appeared in Numerische Mathematik 1 (1959), 269–271.
E. Cojuhari Mathematics in Computer Sciences 31 / 31