0% found this document useful (0 votes)
5 views32 pages

ch06

The document discusses the shortest path problem in graph theory, detailing algorithms such as Dijkstra's, Bellman-Ford, and Floyd's for finding shortest paths in graphs. It outlines the problem statement, specific cases, and provides algorithm descriptions along with examples for each method. Additionally, it includes exercises for applying these algorithms to find shortest paths in given graphs.
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)
5 views32 pages

ch06

The document discusses the shortest path problem in graph theory, detailing algorithms such as Dijkstra's, Bellman-Ford, and Floyd's for finding shortest paths in graphs. It outlines the problem statement, specific cases, and provides algorithm descriptions along with examples for each method. Additionally, it includes exercises for applying these algorithms to find shortest paths in given graphs.
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/ 32

The shortest path Problem

Discrete Math 2
Content
• Problem of finding the shortest path
• Dijkstra algorithm
• Bellman-Ford algorithm
• Floyd algorithm

2
Problem of finding the shortest
path
Problem statement
• Consider the graph G=<V, E>:
– For each edge (u, v)  E, we set corresponding to it a real number A[u][v] called the
weight of the edge.
– We will set A[u,v] = if (u, v)  E . If the sequence v 0 , v 1 , . . . , v p is a path on G then
the length of its path is .

• General form problem :


– Find the shortest path from a starting vertex s  V (source vertex) to the final vertex t
 V (destination vertex).
– Such a path is called the shortest path from s to t.
– The length of the path d (s,t) is called the shortest distance from s to t (in the general
case d(s,t) can be negative).
– If there is no path from s to t, the length of the path d(s,t)= .

4
Some specific expressions of the problem
• Case 1. If s is fixed and t is variable:
– shortest path from s to all remaining vertices on the graph.
– graphs with non-negative weights, the problem always has a solution using Dijkstra 's
algorithm.
– graphs with negative weights but no negative cycles, the problem is solved by Bellman-
Ford algorithm .
– In case the graph has a negative cycle, the problem has no solution.
• Case 2. If s changes and t also changes :
– shortest path between all pairs of vertices in a graph.
– The problem always has a solution on a graph without negative cycles.
– graphs with non-negative weights, the problem is solved by repeating n times Dijkstra's
algorithm .
– graphs without negative cycles, the problem can be solved by Floyd 's algorithm.

5
Dijkstra's algorithm
Algorithm description
• Purpose :
– To find the shortest path from one vertex s to the other vertices of the graph
– directed graphs with non-negative weights.
• Idea:
– Temporarily labeling the vertices
– The label of each vertex indicates the upper bound of the shortest path length to that vertex
– These labels will be recomputed by an iterative procedure
– At each iteration, there will be a temporary label that becomes a fixed label (that label is the
shortest path length from s to that vertex).

7
Dijkstra's algorithm

8
Example 1: Dijkstra (1/2)
• Apply Dijkstra 's
algorithm to find the
shortest path from
vertex 1 to the remaining
vertices of the graph.

9
Example 1: Dijkstra (2/2)

If d[v]>d[u]+A[u][v] then d[v]>d[u]+A[u][v], before[v]=u 10


Example 2: Dijkstra (1/3)
• Apply Dijkstra's algorithm
to find the shortest path
from vertex 1 to the
remaining vertices of the
graph represented in the
form of a weight matrix as
shown below.

11
Example 2: Dijkstra (2/3)
Steps to implement Dijkstra's algorithm at s = 1

12
Example 2: Dijkstra (3/3)
• Result:
– Shortest path from vertex 1 to vertex 2: 2 . Path: 1-2 .
– Shortest path from vertex 1 to vertex 3: 4 . Path : 1-2-3 .
– Shortest path from vertex 1 to vertex 4:10 . Path : 1-2-3- 4 .
– Shortest path from vertex 1 to vertex 5: 8 . Path : 1-2-3-7-6-5 .
– Shortest path from vertex 1 to vertex 6: 7 . Path : 1-2-3-7-6 .
– Shortest path from vertex 1 to vertex 7: 5 . Path : 1-2-3-7 .
– The shortest path from vertex 1 to vertex 8: 7 . Path : 1-2-3-7-8 .
– Shortest path from vertex 1 to vertex 9:15 . Path : 1-2-3-7-6-9 .
– The shortest path from vertex 1 to vertex 10: 21 . Path : 1-2-3-7-6-9-10 .
– The shortest path from vertex 1 to vertex 11:18 . Path : 1-2-3-7-8-12-13-11 .
– The shortest path from vertex 1 to vertex 12: 18 . Path : 1-2-3-7-8-12 .
– Shortest path from vertex 1 to vertex 13:11 . Path : 1-2-3-7-8-12-13 .

13
Dijkstra 's algorithm implementation
• See code

14
Bellman-Ford Algorithm
Algorithm description
• Purpose
– To find the shortest path from a vertex s to the remaining vertices of the graph
– Directed graphs with no negative cycles (possibly with negative edges).
• Idea
– Temporarily labeling the vertices
– The label of each vertex indicates the upper bound of the shortest path length to that
vertex
– These labels will be progressively better (recalculated ) by an iterative procedure
– Every time d[v] > d[u] + A[u][v] is detected, update d[v] = d[u] + A[u][v].

16
Bellman-Ford Algorithm

17
Example 1: Bellman-Ford (1/2)
• Apply the Bellman-
Ford algorithm to find
the shortest path from
vertex 1 to the
remaining vertices of
the graph.

18
Example 1: Bellman-Ford (2/2)

If d[v]>d[u]+A[u][v] then d[v]=d[u]+A[u][v], before[v]=u 19


Example 2: Bellman-Ford (1/2)
• Apply the Bellman-Ford
algorithm to find the shortest
path from vertex 1 to the
remaining vertices of the
weighted graph as shown in the
figure below.

20
Example 2: Bellman-Ford (2/2)
Results according to
Bellman-Ford algorithm

s=1 d[1], before d[2],before[2] d[3], before [3] d[4], before [4] d[5],before[5] d[6], before
[1] [6]

Initialization 0.1 1.1 ,1 ,1 ,1 ,1

first 0.1 1.1 3.2 ,1 -1.2 -4.5

2 0.1 1.1 3.2 2.5 -1.2 -4.5

3 0.1 1.1 1.4 2.5 -1.2 -4.5

4 0.1 1.1 1.4 2.5 -1.2 -4.5

21
Bellman-Ford algorithm implementation
• See code

22
Floyd's algorithm
Algorithm description
• Purpose
– To find the shortest path between all pairs of vertices of the graph.
– directed graphs with no negative cycles (possibly with negative edges).
• Idea
– Perform the iterative process
• Consider each vertex, for all paths (between any two vertices)
• If the current path is greater than the path through the current vertex, change it back to
the path through this vertex.

24
Floyd algorithm
Each pair of vertices (i,j) assigns two values:
d[i][j]: shortest path length from
vertex i to vertex j.

p[i][j] : vertex immediately preceding


vertex j on the shortest path from i to j.

▪ Initial assignment: ∀(i,j) d[i][j]=a[i][j] or ∞ if there is no


arc(i,j) and p[i][j]=i;
▪ Consider intermediate vertices k=1,..,n in turn and for
all pairs of vertices (i,j) through k
▪ If (d[i][j]>d[i][k]+d[k][j]) then update d[i][j]=d[i][k]+d[k][
j]; and p[i][j]=p[k][j];

25
Algorithm - Ex (1/3)
• Apply Floyd algorithm to find the shortest path between all pairs of vertices of the graph.

26
Algorithm – Ex
(2/3)

27
Algorithm - Ex
(3/3)

28
Floyd algorithm Implementation
• See code

29
Exercise 1
• Given a graph of 7 vertices given by the weight matrix.
• Using Dijkstra's algorithm, find the shortest path from vertex 1 to vertex 7. Show
steps in details.

30
Exercise 2
• Let the weighted directed graph be represented as a weight matrix as shown below.
• Using Bellman-Ford algorithm, find the shortest path from vertex 1 to all other
vertices of the graph? Show steps in details.

31
Exercise 3
• Using Floyd algorithm, find the shortest path between all pairs of vertices of
the following graph:

32

You might also like