0% found this document useful (0 votes)
13 views50 pages

S Um Eah

Uploaded by

eiragefatima999
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)
13 views50 pages

S Um Eah

Uploaded by

eiragefatima999
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/ 50

Graph Theory

Dr. Muhammad Asim Ali1


Fall 2024
1 Sukkur IBA University
Shortest Path Search Algorithms
Shortest Path Search Algorithms

The problem of finding shortest path in a network is called the shortest path problem.
This lecture traverses two well-known SP algorithms. Using the algorithm, we can obtain
SP and SD from a fixed vertex v source to every other vertex u provided that there is a
path in the network from u to v .

3
Learning Outcomes

• Dijkstra’s Algorithm • Steiner Network Problem


• Floyd-Warshall’s Algorithm • Bellman-Ford Algorithm

4
Dijkstra’s Algorithm- Simply Put

Some Basics
1. Greedy Algorithm i.e. at every stage it finds local
optimal solution.
2. Not suitable for graphs with -ve weights.
3. Iteratively finds shortest path from source vertex to
all other vertices of graph.
Edsger W. Dijkstra
1931-2002

5
Dijkstra’s Algorithm - Simply Put

Basic Rules

1. Ensure there are no -ve edges. Set distance to source vertex as zero and all other
vertex distance to ∞.
2. Relax all vertices adjacent to current vertex as .
if (d[u]+C(u,v)<d[v])
d[v]=d[u]+C(u,v)
3. Choose the closest vertex as next vertex .
4. Repeat (2) and (3) until destination is reached or end of queue.

6
Dijkstra’s Algorithm

Let G = (V , E ), where V = {1, 2, . . . , n} is a directed network. This algorithm can find


the SP and SD from any fixed vertex i to any other vertex j. If there is no arc from i to
j, a(i, j) is +∞. Each vertex i is assigned a label that is either permanent or tentative.
The permanent label L(i) is the SD from 1 to i. The tentative label L′ (i) is an upper
bound of L(i). At each stage of the algorithm, P is the set of vertices with permanent
labels and T is the set of vertices with tentative labels. Initially, P is the set {1} with
L(1) = 0 and L′ (j) = a(1, j) for all j. The procedure terminates when P = V . Each
iteration consists of two steps:
• Find a vertex k in T for which L′ (k) is finite and minimum. If there is no k, stop;
there is no path from 1 to any unlabeled vertex. Otherwise, declare k to be
permanently labeled and adjoin k to P. Stop if P = V . Label the arc (i, k), where i
is the labeled vertex that determines the minimum value of L′ (k).
• Replace L′ (j) by the smaller value of L′ (j) and L(k) + α(k, j) for every j in T . Go 7
Dijkstra’s Algorithm - Example A

Q B C D E F
B 4 D A 2 4 ∞ ∞ ∞
2 2 B 2 3 6 4 ∞
A 1 2 3 F C 3 6 4 ∞
4 2 E 6 4 6
C 3 E D 6 6
F 6

8
Dijkstra’s Algorithm - Example B

B
Q B C D E F G
1 A 2 1 3 ∞ ∞ ∞
2
C 2 1 3 9 10
E

A
2 4
B 2 3 3 10 ∞
3
D 8 G
E 3 3 10 7
1 5
4
F
D 3 10 7
9
C
F 10 7

9
Dijkstra’s Algorithm-Example C

C
4
E V B C D E F
1
3 A 2 1 ∞ ∞ ∞
A C 2 1 4 5 ∞
D
2
B 2 4 5 7
2
D 4 5 5
3 1
E 5 5
F 5
B 5 F

10
Dijkstra’s Algorithm - Example D
B 8 D 7 G

4 2 9

A 11 E 4 14 I

8 7 6 10

C 1 F 2 H

Q B C D E F G H I
A 4 8 ∞ ∞ ∞ ∞ ∞ ∞
B 4 8 12 ∞ ∞ ∞ ∞ ∞
C 4 8 12 15 9 ∞ ∞
F 12 15 9 ∞ 11 ∞
H 12 15 25 11 21
D 12 14 19 21
E 14 19 21
G 19 21
I 21
11
Dijkstra’s Algorithm - Example C [1/3]

Iteration 1:
1 4 2
• Step 1 P = {1} and L(1) = 0, L′ (2) = 4 and L′ (3) = 6 and L′ (4) = 8.
Adjoin vertex 2 to P, Arc {1, 2} is labeled.
• Step 2 P = {1, 2}, and L(2) = 4, L′ (3) = min{6, L(2) + α(2, 3)}, 8 6 1 7
L′ (4) = min{8, L(2) + α(2, 4)}, L′ (5) = min{∞, L(2) + α(2, 5)},
L′ (6) = min{∞, L(2) + α(2, 6)}, and L′ (7) = min{6, L(2) + α(2, 7)}
Iteration 2: 4 2 3 5 5
• Step 1 P = {1, 2} and L(1) = 0, L(2) = 4, L′ (3) = 5, L′ (4) = 8 and
L′ (5) = 11.
Adjoin vertex 3 to P, Arc {2, 3} is labeled. 5 4 1 6

• Step 2 P = {1, 2, 3}, and L(3) = 5, L′ (4) = min{8, L(3) + α(3, 4)},
L′ (5) = min{11, L(3) + α(3, 5)}, L′ (6) = min{∞, L(3) + α(3, 6)},
6 8 7
L′ (7) = min{∞, L(3) + α(3, 7)}, and L′ (7) = min{6, L(2) + α(2, 7)}

12
Dijkstra’s Algorithm -Example C [2/3]

1 4 2
Iteration 3:
• Step 1 P = {1, 2, 3} and L(1) = 0, L(2) = 4, L(3) = 5, L′ (4) = 7,
L′ (5) = 10 and L′ (6) = 9. Adjoin vertex 4 to P, Arc {3, 4} is labeled. 8 6 1 7
• Step 2 P = {1, 2, 3, 4}, and L(4) = 7, L′ (5) = min{10, L(4) + α(4, 5)},
L′ (6) = min{9, L(4) + α(4, 6)}, L′ (7) = min{∞, L(4) + α(4, 7)}.
4 2 3 5 5
Iteration 4:
• Step 1 P = {1, 2, 3, 4} and L(1) = 0, L(2) = 4, L(3) = 5, L(4) = 7,
L′ (5) = 10 and L′ (6) = 9. Adjoin vertex 6 to P, Arc {3, 6} is labeled. 5 4 1 6
• Step 2 P = {1, 2, 3, 4, 6}, and L(6) = 9, L′ (5) = min{10, L(6) + α(6, 5)},
L′ (7) = min{∞, L(6) + α(6, 7)}.
6 8 7

13
Dijkstra’s Algorithm - Example C [3/3]

1 4 2
Iteration 5:
• Step 1 P = {1, 2, 3, 4, 6} and L(1) = 0, L(2) = 4, L(3) = 5, L(4) = 7,
8 6 1 7
L(6) = 9 and L′ (5) = 10. Adjoin vertex 5 to P, Arc {3, 5} is labeled.
• Step 2 P = {1, 2, 3, 4, 6, 5}, and L(5) = 10,
L′ (7) = min{17, L(5) + α(5, 7)}. 4 2 3 5 5
Iteration 6:
• Step 1 P = {1, 2, 3, 4, 6, 5} and L(1) = 0, L(2) = 4, L(3) = 5, L(4) = 7,
L(6) = 9, L(5) = 10, L(7) = 16. Adjoin vertex 7 to P, Arc {5, 7} is labeled. 5 4 1 6

• Step 2 P = {1, 2, 3, 4, 6, 5, 7}, and L(7) = 16. At this stage P = V .

6 8 7

14
Dijkstra’s Algorithm-Example A

A 5 C 6 E

2 6 32 3 7 5 2

B 2 D 9 F 1 G

15
The Floyd Warshall Algorithm

This algorithm is a special case of Dijkstra’s algorithm, where the weight function need
not be non-negative. The initial path matrix P is the n × n matrix [p(i, j)] where
p(i, j) = j. There are n-iterations in the execution of algorithm. Iteration j (based at
vertex 1) begins with two matrices Aj−1 and Pj−1 and ends with Aj and Pj . Initially,
A0 = A and P0 = P. The (u, v ) enteries in Aj and Pj are denoted by aj (u, v ) and
pj (u, v ) respectively. For a fixed j, the matrices Aj and Pj are obtained from Aj−1 and
Pj−1 by applying the following rules, known as the triangle (triple) operation: If
aj−1 (u, v ) ≤ aj−1 (u, j) + aj−1 (j, v ), aj (u, v ) = aj−1 (u, v ) and pj (u, v ) = pj−1 (u, v )
otherwise aj (u, v ) = aj−1 (u, j) + aj−1 (j, v ) and pj (u, v ) = pj−1 (u, j).
The algorithm terminates with SD in matrix An and SP in matrix Pn . The (u, v ) entry
in the first matrix is the shortest distance between these two vertices and the (u, v ) entry
in the second matrix in the first vertex after u in the shortest path from u to v .

16
Floyd Warshall Algorithm - Example A

1 2
0 3 ∞ 7
 
8 0 2 ∞
3
A0 = 
 
5 ∞ 0 1 

7 2 5 2
2 ∞ ∞ 0

4 1 3

17
Floyd Warshall Algorithm - Example A [0/4]

1 2

0 3 ∞ 7
 
7 2 5 2
8 0 
A1 = 
 
5 ∞ 0


4 1 3
2 0
0 3 ∞ 7
 
8 0 2 ∞
A0 = 
 
5 ∞ 0 1 

2 ∞ ∞ 0

18
Floyd Warshall Algorithm - Example A [1/4]

1 2

0 3
 
7 2 5 2
 8 0 2 15
A2 = 
 
8 0

 
4 1 3
5 0
0 3 ∞ 7
 
8 0 2 15 
A1 = 
 
5 8 0 1


2 5 7 0

19
Floyd Warshall Algorithm - Example A [1/4]

1 2

0 3
 
7 2 5 2
 8 0 2 15
A2 = 
 
8 0

 
4 1 3
5 0
0 3 ∞ 7
 
8 0 2 15
A1 = 
 
5 8 0 1

2 5 7 0

20
Floyd Warshall Algorithm- Example A [2/4]

1 2

0 5
 
7 2 5 2
 0 
A3 = 
 
5 8 0 1

4 1 3
7 0
0 3 5 7
 
8 0 2 15
A2 = 
 
5 8 0 1

2 5 7 0

21
Floyd Warshall Algorithm - Example A [3/4]

1 2

0 6
 
7 2 5 2
 0 8
A4 = 
 
0 1


4 1 3
2 5 7 0
0 3 5 6
 
7 0 2 8
A3 = 
 
5 8 0 1

2 5 7 0

22
Floyd Warshall Algorithm - Example B [0/4]

b
2
a

0 ∞ 3 ∞
 

7
 2 0 ∞ ∞
A0 = 
3
 
∞ 7 0 1 

6

6 ∞ ∞ 0
c
1
d

23
Floyd-Warshal Algorithm - Example B [1/4]

b
2
a

7
3
0 ∞ 3 ∞
 
6

2 0 
A1 = 
 
c
∞ 0

1

d
6 0
0 ∞ 3 ∞
 
 2 0 ∞ ∞
A0 = 
 
∞ 7 0 1 

6 ∞ ∞ 0

24
Floyd-Warshal Algorithm- Example B [2/4]

b
2
a

7
3
0 ∞
 
6

 2 0 5 ∞
A2 = 
 
c
7 0

1
 
d
∞ 0
0 ∞ 3 ∞
 
 2 0 5 ∞
A1 = 
 
∞ 7 0 1 

6 ∞ 9 0

25
Floyd-Warshal Algorithm - Example B [3/4]

b
2
a

7
3
0 3
 
6

 0 5 
A3 = 
 
c
9 7 0 1

1
d
9 0
0 ∞ 3 ∞
 
2 0 5 ∞
A2 = 
 
9 7 0 1 

6 ∞ 9 0

26
Floyd-Warshal Algorithm - Example B [4/4]

b
2
a

7
3
0 4
 
6

 0 6
A4 = 
 
c
0 1

1

d
6 16 9 0
0 10 3 4
 
2 0 5 6
A3 = 
 
9 7 0 1

6 16 9 0

27
Floyd Warshall Algorithm - Example C

1 2
-3
5 6
   
0 4 −3 ∞ 1 2 3 4
4 −3 0 −7 ∞ 1 2 3 4
A= and P =
   
∞ 10 0 3 1 2 3 4
 
10 5 6 6 0 1 2 3 4

-3 3 6 -7

28
Floyd Warshall Algorithm- Example

1 2
-3 Iteration 1: We begin with A1 and
5 6
P1 and obtain the matrices A2 and
4
P2 , after
 applying the triangle
 operation  
0 4 1 2 3 4
10 −3 ∞
−3 0 −7 ∞  1 2 3 4
A1 =  and P1 =
  
∞ 10 0 3 1 2 3 4
  

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

29
Floyd Warshall Algorithm- Example

1 2
-3 Iteration 2: A1 = A and P0 = P. In
5 6
performing the triangle operation based at
4
vertex 1. the only change is at (4, 3) entry
10
a1 (4, 3) = min{a0 (4, 3), a0 (4, 1) + a0 (1, 3)} =

-3 3 6 -7 min{6, 5−3} = 2. Then p1 (4, 3)


  = p0 (4, 1) = 1  
0 4 −3 ∞ 1 2 3 4
−3 0 −7 ∞ 1 2 3 4
A2 =  and P2 =
   
 7 10 0 3 2 2 3 4
  

3 3 6 −1 0 2 2 2 4
   
0 4 −3 ∞ 1 2 3 4
−3 0 −7 ∞ 1 2 3 4
A1 =  and P1 = 
   
∞ 10 0 3 1 2 3 4
 
5 6 2 0 1 2 1 4

30
Floyd Warshall Algorithm- Example

1 2
-3
5 6
4
Iteration 3:
10
  
0 4 −3 ∞ 1 2 3 3
−3 0 −7 −4 1 2 3 3
A3 =  and P3 = 
   
 7 10 0 3  2 2 3 4
 

-3 3 6 -7 3 6 −1 0 2 2 2 4

3
   
0 4 −3 ∞ 1 2 3 4
−3 0 −7 ∞ 1 2 3 4
A2 =  and P2 = 
   
 7 10 0 3 2 2 3 4
 
3 6 −1 0 2 2 2 4

31
Floyd Warshall Algorithm- Example

1 2
-3
5 6
4
Iteration 4: 
10
 
0 4 −3 0 1 2 3 3
−3 0 −7 −4 1 2 3 3
A4 =  and P4 = 
   
 6 9 0 3  4 4 3 4
 

-3 3 6 -7 3 6 −1 0 2 2 2 4

3
   
0 4 −3 ∞ 1 2 3 3
−3 0 −7 −4 1 2 3 3
A3 =  and P3 = 
   
 7 10 0 3  2 2 3 4
 
3 6 −1 0 2 2 2 4

32
Floyd Warshall Algorithm- Further Activity 1

B
4
A
8

2 5 E

4
4
C 3 D

33
Floyd Warshall Algorithm- Further Activity 2

3 4

5 8 2
7 1

-4 2 -5

4 6 3
34
The Bellman Ford’s Algorithm

Bellman-Ford is a single source shortest path algorithm that determines the shortest path
between a given source vertex and every other vertex in a graph. This algorithm can be
used on both weighted and unweighted graphs.
A Bellman-Ford algorithm is also guaranteed to find the shortest path in a graph, similar
to Dijkstra’s algorithm.
Although Bellman-Ford is slower than Dijkstra’s algorithm, it is capable of handling
graphs with negative edge weights, which makes it more versatile.
The shortest path cannot be found if there exists a negative cycle in the graph.

35
Bellman-Ford Algorithm Example

(1,2) (1,3) (1,4) (2,5) (3,2) (3,5) (4,3) (4,6) (5,7) (6,7)
2 -1 5 6 5 5 5 3 5 3 4 8 7
6
1
-2 1 3 3 3 5 2 1 2 3 4 5 5
5

1 3 5 0 1 0 3 4 3 3
0 7
5
1 3 5 0 1 0 3 4 3 3
-2 3

4 -1 6

36
Bellman-Ford Algorithm Example

1 3 3
(0,1) (0,2) (2,1) (1,3) (2,4) (3,4)
4

0 -5 2

2 1 4

37
Bellman-Ford Algorithm Example

1 4 2 (3,2) (4,3) (1,4) (1,2) (2,4)

5 5 -10

4 3 3

38
Steiner Network Problem

Steiner network problem is the optimization problem of finding a tree T = (W ′ , F ) in G


such that W ⊂ W ′ and the weight w (F ) is a small as possible, given a set W of vertices
in an undirected graph G = (V , E ) with a non-negative weight function w defined on E .
The tree T is a Steiner tree of the set W . The vertices in (W ′ − W ) are steiner
points of the W with respect to T . A steiner tree w.r.t to a pair of vertices in an
undirected network is any shortest path P between them, and steiner points of this pair
with respect to P are intermediate vertices of this path.
On the other extreme, any minimum spanning tree in the network is a steiner tree of V .
So both SP and MST can be considered as a special case of steiner.

39
Steiner Network Problem

Weight of minimum spanning tree of sub-


7 3 2 2 3

1 2
graph induced by set W = {1, 2, 3, 4} is
5 5 4 8 3 9.
5 1
But tree T that spans W =
6 2 1 4 4

{1, 2, 3, 4, 5} is 8.
7

Theorem (Steiner Theorem)


Let {1, 2, · · · , n} be the set of vertices of a complete graph G with non-negative
weight function w satisfying the triangle property i.e. w (i, j) ≤ w (i, k) + w (k, j) for all
vertices i,j,k. If W is a set of m vertices in network G, there exists a Steiner tree for W
in netowrk that contains at most n − 2 steiner points.
40
Steiner Network Problem

0 3 5 4 1 2 2
6
 

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

4 5 3 0 5 6 6
 
5 1
D= 
1 2 4 5 0 3 1
 
6 2 1 4 4

2 5 7 6 3 0 4
 
7

2 3 5 6 1 4 0

41
Steiner Network Problem

The input is a set W of m vertices in a connected graph G = (V , E ) of order n in which


a non-negative weights is defined on each edge. Construct the complete network
G ′ = (V , F ) where weights of an edge joining vertices is SD between them. Let
S = {S ⊂ (V − W ) : |S| ≤ (m − 2)}. For each subset S in S. find the MST of the
subgraph G ′ induced by W ∪ S of G ′ . Among the trees thus obtained, select a tree T ′
of minimum weights. Construct a tree T from T ′ by replacing each edge joining two
vertices by the set of edges in a shortest path between them. Those vertices of T that
are not in W will form set of steiner points for W .

42
Spanning Tree Enumeration Algorithm

Find steiner trees with respect to W = {3, 6, 7} in the network.

0 3 5 4 1 2 2
  6

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

4 5 3 0 5 6 6
 
D=  5 1

1 2 4 5 0 3 1
  6 2 1 4 4

2 5 7 6 3 0 4
 
7

2 3 5 6 1 4 0
Since m = 3, S is any subset of {1, 2, 4, 5} with at most one element. There are five choices for W ∪ S

W1 = {3, 6, 7}, W2 = {3, 6, 7, 1} , W3 = {3, 6, 7, 2}, , W4 = {3, 6, 7, 4}, and , W5 = {3, 6, 7, 5}

43
Steiner Network Problem

7 5 3 7 3

4 2 5

0 3 5 4 1 2 2
 
6 6 3 1
3 0 2 5 2 5 3
 
5 2 0 3 4 7 5
 
w1 = 9 w2 = 9

4 5 3 0 5 6 6
 
D=  7 3 2 2 3 7 5 3

1 2 4 5 0 3 1
 
4 4 3
2 5 7 6 3 0 4
 

2 3 5 6 1 4 0 6 6 4

w3 = 9 w4 = 12

44
Steiner Network Problem

7 3

1 4

0 3 5 4 1 2 2
 
3
3 0 2 5 2 5 3
  6

5 2 0 3 4 7 5
 
weight of edge between 3-5 is 4 and SP is 5-2-3

4 5 3 0 5 6 6
 
D=  weight of edge between 5-6 is 3 and SP is 5-1-6
1 2 4 5 0 3 1 7 2 2 3
 
2 5 7 6 3 0 4
 
1 2

2 3 5 6 1 4 0 5

6 2 1
minimum weight spanning tree of subgraph of G ′ induced by these sets have weights 9,9,9,12 and 8 respectively

45
Facility Location Problems

The problem of deciding the exact place in a community where the facility (school,
mosque or post office) should be located to serve the community as economically and
efficiently as possible. Such location problems can often be modeled as a networks.
It is desirable to locate it such that the sum of distances from the facility is as small as
possible. The aim is to minimize the sum of several weights, is aptly called a minsum
problem.
It is always desirable to locate it such that the distance from a station to the farthest
point is minimized. This category, in which the aim is to minimize the maximum shortest
distance, is known as minmax problem.

46
Median (Minsum) Problem

Let G = (V , E ) where V = {1, 2, · · · , n} is weighted network with non-negative weight


function defined on E and let D = [d(i, j)] be the SD matrix where d(i, j) is the SD
from i to j in the network.
for each i, let s(i) be the sum of all elements in row i of D. Vertex j is called a median
vertex if s(j) ≤ s(i) for every vertex i of the network. The median is the set of all
median vertices.
In more general setting,

s ′ (i) = w (1)d(i, 1) + w (2)d(i, 2) + · · · + w (n)d(i, n)

The vertex j is a weighted median vertex if s ′ (j) ≤ s ′ (i) for every vertex i of the network,
and the weighted median is the set of all weighted median vertices.

47
Center (Minmax) Problem

For each vertex i, define the eccentricities e(i ) as the largest entry in a row i of the SD
matrix. Vertex j called the center vertex if e(j) ≤ e(i) for every vertex i in network.
The set of all center vertices is called the center of the network.

48
0 1 2 3 4 4 4
 
1 0 1 2 3 3 3
 
2 1 0 1 2 2 2
 

3 2 1 0 1 1 1
 
5 D= 
4 3 2 1 0 2 2
 
4 3 2 1 2 0 2
 
1 2 3 4 7
4 3 2 1 2 2 0

6 a. The row sums are 18,13, 10, 9, 14, 14. Row


4 has the lowest sum median is the set {4}.
b. The eccentricities of seven vertices are
4,3,2,3,4,4; the respective center is set {3}.

49
1
1

2
4
2 4

3
3

50

You might also like