0% found this document useful (0 votes)
123 views38 pages

Graph - 2020 - Pr1 and 2

The document summarizes three problems faced by the management of Seervada Park and provides examples of algorithms that could help solve them. The first problem is finding the shortest route from the park entrance to station T, which is an example of the shortest path problem. The second problem is installing telephone lines between stations at minimum cost, which relates to the minimum spanning tree problem. The third problem involves maximizing the number of tram trips per day within road capacity limits, representing the maximum flow problem.
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)
123 views38 pages

Graph - 2020 - Pr1 and 2

The document summarizes three problems faced by the management of Seervada Park and provides examples of algorithms that could help solve them. The first problem is finding the shortest route from the park entrance to station T, which is an example of the shortest path problem. The second problem is installing telephone lines between stations at minimum cost, which relates to the minimum spanning tree problem. The third problem involves maximizing the number of tram trips per day within road capacity limits, representing the maximum flow problem.
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/ 38

Graphs

Seervada Park example

Elena COJUHARI

Technical University of Moldova

MS I, 2020

E. Cojuhari Mathematics in Computer Sciences 1 / 31


PROTOTYPE EXAMPLE // Seervada Park

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.

E. Cojuhari Mathematics in Computer Sciences 2 / 31


PROTOTYPE EXAMPLE // Seervada Park

The park management currently faces three problems.


One is to determine which route from the park entrance to station T has the smallest total
distance for the operation of the trams. (This is an example of the shortest-path problem to be
discussed on page 4 ) 7
A second problem is that telephone lines must be installed under the roads to establish telephone
communication among all the stations (including the park entrance). Because the installation is both
expensive and disruptive to the natural environment, lines will be installed under just enough roads
to provide some connection between every pair of stations. The question is where the lines should
be laid to accomplish this with a minimum total number of miles of line installed. (This is an example
of the minimum spanning tree problem to be discussed on page 28 )
The third problem is that more people want to take the tram ride from the park entrance to station
T than can be accommodated during the peak season. To avoid unduly disturbing the ecology and
wildlife of the region, a strict ration has been placed on the number of tram trips that can be made on
each of the roads per day. (These limits differ for the different roads, as we shall describe in detail on
page 36 ) Therefore, during the peak season, various routes might be followed regardless of
distance to increase the number of tram trips that can be made each day. The question pertains to
how to route the various trips to maximize the number of trips that can be made per day without
violating the limits on any individual road. (This is an example of the maximum flow problem to be
discussed on page 36 .)

E. Cojuhari Mathematics in Computer Sciences 3 / 31


Shortest-path algorithm

Algorithm for the Shortest-Path Problem


Objective of nth iteration: Find the nth nearest node to the origin (to be repeated for
n = 1, 2, . . . until the nth nearest node is the destination.
Input for nth iteration: n − 1 nearest nodes to the origin (solved for at the previous
iterations), including their shortest path and distance from the origin. (These
nodes, plus the origin, will be called solved nodes; the others are unsolved
nodes.)
Candidates for nth nearest node: Each solved node that is directly connected by a link
to one or more unsolved nodes provides one candidate — the unsolved
node with the shortest connecting link. (Ties provide additional candidates.)
Calculation of nth nearest node: For each such solved node and its candidate, add the
distance between them and the distance of the shortest path from the origin
to this solved node. The candidate with the smallest such total distance is
the nth nearest node (ties provide additional solved nodes), and its shortest
path is the one generating this distance.

E. Cojuhari Mathematics in Computer Sciences 4 / 31


Applying the shortest-path algorithm to the Seervada Park problem

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

E. Cojuhari Mathematics in Computer Sciences 5 / 31


Applying the shortest-path algorithm to the Seervada Park problem

A
2 2 7
T
5
5 4
O B D
1 3 1
4 7

C E
4

E. Cojuhari Mathematics in Computer Sciences 6 / 31


Dijkstra’s Algorithm for Shortest Paths

ALGORITHM DIJKSTRA [G = (V , E), V = 1, · · · , n, lij for all (i, j) in E ]


Given a connected graph G = (V , E) with vertices 1, · · · , n and edges (i, j) having lengths lij > 0, this
algorithm determines the lengths of shortest paths from vertex 1 to the vertices 2, · · · , n.
INPUT: Number of vertices n, edges (i, j), and lengths lij
OUTPUT: Lengths Lj of shortest paths 1 → j, j = 2, · · · , n
1. Initial step
Vertex 1 gets PL: L1 = 0. 
l1j , (1, j) ∈ G;
Vertex j(= 2, · · · , n) gets TL: L̃j =
∞, if there is no edge (1, j) in G.
Set PL = {1}, T L = {2, 3, · · · , n}.
2. Fixing a permanent label
Find a k in T L for which L̃k is minimum, set Lk = L̃k . Take the smallest k if there are several. Delete k
from T L and include it in PL .
If T L = ∅ (that is, T L is empty) then
OUTPUT L2 , · · · , Ln . Stop
Else continue (that is, go to Step 3).
3. Updating temporary labels
For all j in T L , set L̃j = mink {L̃j , Lk + lkj } (that is, take the smaller of L̃j and Lk + lkj as your new L̃j).
Go to Step 2.
End DIJKSTRA

E. Cojuhari Mathematics in Computer Sciences 7 / 31


Dijkstra’s Algorithm for Shortest Paths

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

E. Cojuhari Mathematics in Computer Sciences 8 / 31


Dijkstra’s Algorithm for Shortest Paths

Solution. We list the steps and computations.


1. L1 = 0, L̃2 = 8, L̃3 = 5, L̃4 = 7, PL = {1}, T L = {2, 3, 4}
2. L3 = min{L̃2 , L̃3 , L̃4 } = 5, k = 3, PL = {1, 3}, T L = {2, 4}
3. L̃2 = min{8, L3 + l32 } = min{8, 5 + 1} = 6
L̃4 = min{7, L3 + l34 } = min{7, ∞} = 7
2. L2 = min{L̃2 , L̃4 } = min{6, 7} = 6, k = 2, PL = {1, 2, 3}, T L = {4}
3. L̃4 = min{7, L2 + l24 } = min{7, 6 + 2} = 7
2. L4 = 7, k = 4 PL = {1, 2, 3, 4}, TL =∅

E. Cojuhari Mathematics in Computer Sciences 9 / 31


Dijkstra’s Algorithm for Shortest Paths

1. L1 = 0, L̃2 = 8, L̃3 = 5, L̃4 = 7, PL = {1}, T L = {2, 3, 4}

0 8
8
1 2
5 1 7 2
5 7
3 4

E. Cojuhari Mathematics in Computer Sciences 10 / 31


Dijkstra’s Algorithm for Shortest Paths

2. L3 = min{L̃2 , L̃3 , L̃4 } = 5, k = 3, PL = {1, 3}, T L = {2, 4}

0 8
8
1 2
5 1 7 2
5 7
3 4

E. Cojuhari Mathematics in Computer Sciences 10 / 31


Dijkstra’s Algorithm for Shortest Paths

3. L̃2 = min{8, L3 + l32 } = min{8, 5 + 1} = 6

0 6
8
1 2
5 1 7 2
5 7
3 4

E. Cojuhari Mathematics in Computer Sciences 10 / 31


Dijkstra’s Algorithm for Shortest Paths

L̃4 = min{7, L3 + l34 } = min{7, ∞} = 7

0 6
8
1 2
5 1 7 2
5 7
3 4

E. Cojuhari Mathematics in Computer Sciences 10 / 31


Dijkstra’s Algorithm for Shortest Paths

2. L2 = min{L̃2 , L̃4 } = min{6, 7} = 6, k = 2, PL = {1, 2, 3}, T L = {4}

0 6
8
1 2
5 1 7 2
5 7
3 4

E. Cojuhari Mathematics in Computer Sciences 10 / 31


Dijkstra’s Algorithm for Shortest Paths

3. L̃4 = min{7, L2 + l24 } = min{7, 6 + 2} = 7

0 6
8
1 2
5 1 7 2
5 7
3 4

E. Cojuhari Mathematics in Computer Sciences 10 / 31


Dijkstra’s Algorithm for Shortest Paths

2. L4 = 7, k = 4 PL = {1, 2, 3, 4}, TL =∅

0 6
8
1 2
5 1 7 2
5 7
3 4

E. Cojuhari Mathematics in Computer Sciences 10 / 31


Dijkstra’s Algorithm for Shortest Paths

Shortest paths in given graph

8
1 2
5 1 7 2

3 4
Path 1 → 3
Path 1 → 2
Path 1 → 4

E. Cojuhari Mathematics in Computer Sciences 11 / 31


Dijkstra’s Algorithm for Seervada Park example

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

E. Cojuhari Mathematics in Computer Sciences 12 / 31


Dijkstra’s Algorithm for Seervada Park example

n=7 (1, 2) OA (2, 5) AD (5, 6) DE (5, 7) DT


(1, 3) OB (2, 3) AB (3, 5) BD (6, 7) ET
(1, 4) OC (4, 3) CB (3, 6) BE
(4, 6) CE
l12 = 2 l25 = 7 l56 = 1 l57 = 5
l13 = 5 l23 = 2 l35 = 4 l67 = 7
l14 = 4 l43 = 1 l36 = 3
l46 = 4

E. Cojuhari Mathematics in Computer Sciences 13 / 31


Dijkstra’s Algorithm for Seervada Park example

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 =∞

E. Cojuhari Mathematics in Computer Sciences 15 / 31


Dijkstra’s Algorithm for Seervada Park example

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 = ∞

E. Cojuhari Mathematics in Computer Sciences 17 / 31


Dijkstra’s Algorithm for Seervada Park example

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

E. Cojuhari Mathematics in Computer Sciences 18 / 31


Dijkstra’s Algorithm for Seervada Park example

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

E. Cojuhari Mathematics in Computer Sciences 19 / 31


Dijkstra’s Algorithm for Seervada Park example

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

E. Cojuhari Mathematics in Computer Sciences 20 / 31


Dijkstra’s Algorithm for Seervada Park example

2
A
13
2 2
5 T
4 8
4
O B D
3 1
4 4 7
C E

E. Cojuhari Mathematics in Computer Sciences 21 / 31


Spanning tree

Algorithm for the Minimum Spanning Tree Problem


1. Select any node arbitrarily, and then connect it (i.e., add a link) to the nearest
distinct node.
2. Identify the unconnected node that is closest to a connected node, and then
connect these two nodes (i.e., add a link between them). Repeat this step
until all nodes have been connected.
3. Tie breaking: Ties for the nearest distinct node (step 1) or the closest
unconnected node (step 2) may be broken arbitrarily, and the algorithm must
still yield an optimal solution. However, such ties are a signal that there may
be (but need not be) multiple optimal solutions. All such optimal solutions can
be identified by pursuing all ways of breaking ties to their conclusion.
The fastest way of executing this algorithm manually is the graphical approach
illustrated next.

E. Cojuhari Mathematics in Computer Sciences 22 / 31


Spanning tree

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

E. Cojuhari Mathematics in Computer Sciences 23 / 31


Spanning tree

Arbitrarily select node O to start. The unconnected node closest to node O is


node A. Connect node A to node O.

A
2 2
7
T
5
5 4
O B D
1 3 1
4 7

C E
4

E. Cojuhari Mathematics in Computer Sciences 23 / 31


Spanning tree

The unconnected node closest to either node O or node A is node B (closest to


A). Connect node B to node A.

A
2 2
7
T
5
5 4
O B D
1 3 1
4 7

C E
4

E. Cojuhari Mathematics in Computer Sciences 23 / 31


Spanning tree

The unconnected node closest to node O, A, or B is node C (closest to B).


Connect node C to node B.

A
2 2
7
T
5
5 4
O B D
1 3 1
4 7

C E
4

E. Cojuhari Mathematics in Computer Sciences 23 / 31


Spanning tree

The unconnected node closest to node O, A, B, or C is node E (closest to B).


Connect node E to node B.

A
2 2
7
T
5
5 4
O B D
1 3 1
4 7

C E
4

E. Cojuhari Mathematics in Computer Sciences 23 / 31


Spanning tree

The unconnected node closest to node O, A, B, C, or E is node D (closest to E).


Connect node D to node E.

A
2 2
7
T
5
5 4
O B D
1 3 1
4 7

C E
4

E. Cojuhari Mathematics in Computer Sciences 23 / 31


Spanning tree

The only remaining unconnected node is node T . It is closest to node D. Connect


node T to node D.

A
2 2
7
T
5
5 4
O B D
1 3 1
4 7

C E
4

E. Cojuhari Mathematics in Computer Sciences 23 / 31


Bibliography

E. Kreyszig, Advanced Engineering Mathematics, 10th ed.

Frederick S. Hillier, Introduction to operations research, 9th ed.

V. Beşliu, Matematica Discretă, UTM.

E. Cojuhari Mathematics in Computer Sciences 29 / 31


Notes

RICHARD BELLMAN (1920–1984), American mathematician, known for his


work in dynamic programming.
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.
JOSEPH BERNARD KRUSKAL (1928–2010), American mathematician who
worked at Bell Laboratories. He is known for his contributions to graph theory and
statistics.
ROBERT CLAY PRIM (1921– ), American computer scientist at General
Electric, Bell Laboratories, and Sandia National Laboratories.
LESTER RANDOLPH FORD Jr. (1927–2017) and DELBERT RAY
FULKERSON (1924–1976), American mathematicians known for their pioneering
work on flow algorithms.

E. Cojuhari Mathematics in Computer Sciences 30 / 31


Notes // Dijkstra’s Algorithm for Shortest Paths

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

You might also like