ch06
ch06
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 .
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)
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)
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]
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.
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