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

CH 06

The document discusses algorithms for finding shortest paths in graphs, including Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd's algorithm. Dijkstra's algorithm finds shortest paths from a single source node in graphs with non-negative edge weights. Bellman-Ford handles graphs that may have negative edge weights but no negative cycles. Floyd's algorithm finds shortest paths between all pairs of nodes, handling graphs with negative edge weights but no negative cycles. Examples are provided to demonstrate how each algorithm is applied.

Uploaded by

Tấn Phát
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)
15 views32 pages

CH 06

The document discusses algorithms for finding shortest paths in graphs, including Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd's algorithm. Dijkstra's algorithm finds shortest paths from a single source node in graphs with non-negative edge weights. Bellman-Ford handles graphs that may have negative edge weights but no negative cycles. Floyd's algorithm finds shortest paths between all pairs of nodes, handling graphs with negative edge weights but no negative cycles. Examples are provided to demonstrate how each algorithm is applied.

Uploaded by

Tấn Phát
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

FINDING THE SHORTEST PATH

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

2
State the 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 :
– Use to find the shortest path from one vertex s to the other vertices of the graph
– directed graphs with non-negative weights.
• Idea:
– Temporarily label the vertices
– The label of each vertex indicates the upper bound of the shortest path length to that vertex
– These labels will be transformed (recomputed ) by an iterative
– 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 . Route: 1-2 .
– Shortest path from vertex 1 to vertex 3: 4 . Route: 1-2-3 .
– Shortest path from vertex 1 to vertex 4:10 . Route: 1-2-3- 4 .
– Shortest path from vertex 1 to vertex 5: 8 . Route: 1-2-3-7-6-5 .
– Shortest path from vertex 1 to vertex 6: 7 . Route: 1-2-3-7-6 .
– Shortest path from vertex 1 to vertex 7: 5 . Route: 1-2-3-7 .
– The shortest path from vertex 1 to vertex 8: 7 . Route: 1-2-3-7-8 .
– Shortest path from vertex 1 to vertex 9:15 . Route: 1-2-3-7-6-9 .
– The shortest path from vertex 1 to vertex 10: 21 . Route: 1-2-3-7-6-9-10 .
– The shortest path from vertex 1 to vertex 11:18 . Route: 1-2-3-7-8-12-13-11 .
– The shortest path from vertex 1 to vertex 12: 18 . Route: 1-2-3-7-8-12 .
– Shortest path from vertex 1 to vertex 13:11 . Route: 1-2-3-7-8-12-13 .

13
Dijkstra 's algorithm implementation
• See code demonstration

14
Bellman-Ford Algorithm
Algorithm description
• Purpose
– Use to find the shortest path from a vertex s to the remaining vertices of the graph
– Applies to directed graphs and no negative cycles (possibly with negative edges).
• Thought
– Temporarily label 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
– 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)
Test 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 demonstration

22
Floyd's algorithm
Algorithm description
• Purpose
– Used to find the shortest path between all pairs of vertices of the graph.
– Applies to directed graphs and no negative cycles (possibly with negative edges).
• Thought
– 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 checking (1/3)
• Apply Floyd algorithm to find the shortest path between all pairs of vertices of the graph.

26
Algorithm
checking (2/3)

27
Algorithm
checking (3/3)

28
Floyd algorithm Implementation
• See code demonstration

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.
Requires specifying intermediate results during algorithm implementation.

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? Specify the result for each execution step of the algorithm?

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

32

You might also like