Professional Documents
Culture Documents
shortest path
Single-source shortest path
2
Directed Graph
A directed graph G, also known as a digraph, is a graph in which every edge has a
direction assigned to it. An edge of a directed graph is given as an ordered pair (u, v)
of nodes in G. For an edge (u, v),
● The edge begins at u and terminates at v.
● u is known as the origin or initial point of edge e. Correspondingly, v is known as
the destination or terminal point of edge e.
● u is the predecessor of v. Correspondingly, v is the successor of u.
● Nodes u and v are adjacent to each other.
Single-source shortest path
Terminologies 3
Shortest Path
Given a weighted directed graph, one common problem is finding the
shortest path between two given vertices
◦ Recall that in a weighted graph, the length of a path is the sum of the weights of each
of the edges in that path
Given the graph, suppose we wish to find the shortest path from vertex 1 to vertex 13
Single-source shortest path
5
Shortest Path
After some consideration, we may determine that the shortest path is as
follows, with length 14
Applications
one application is circuit design: the time it takes for a change in input to
affect an output depends on the shortest path
Single-source shortest path
7
Applications
The Internet is a collection of interconnected computer networks
◦ Information is passed through packets
Packets are passed from the source, through routers, to their destination
Applications
A visualization of the
graph of the routers
and their various
connections through
a portion of the
Internet
Single-source shortest path
9
Applications
In software engineering, one obvious problem is finding the shortest route
between to points on a map
◦ Shortest path, however, need not refer to distance...
Single-source shortest path
10
Applications
The shortest path using distance as a metric is obvious, however, a driver may be
more interested in minimizing time
For example, using the 407 may be preferable to using the 401 during rush hour,
however, there is an added cost
A company will be interested in minimizing the cost which includes the following factors:
◦ salary of the truck driver (overtime?)
◦ possible tolls and administrative costs
◦ bonuses for being early
◦ penalties for being late
◦ cost of fuel
Single-source shortest path
11
Shortest Path
The goal of this algorithm will be to find the shortest path and its length
We will make the assumption that the weights on all edges is a positive
number
◦ Clearly, if we have negative vertices, it may be possible to end up in a cycle whereby
each pass through the cycle decreases the total length
◦ Thus, a shortest length would be undefined for such a graph
◦ Consider the shortest path
from vertex 1 to 4...
Single-source shortest path
12
Shortest Path
Consider the following graph
◦ All edges have positive weight
◦ There exists cycles—it is not a DAG
Single-source shortest path
13
Algorithms
Algorithms for finding the shortest path include:
◦ Dijkstra’s algorithm
◦ A* search algorithm
◦ Bellman-Ford algorithm
◦ BFS
Breadth-First Search
Breadth-first search (BFS) is a BFS on a graph with n vertices and
general technique for traversing m edges takes O(n + m ) time
a graph
BFS can be further extended to
A BFS traversal of a graph G solve other graph problems
◦ Visits all the vertices and edges of G ◦ Find and report a path with the
◦ Determines whether G is connected minimum number of edges between
◦ Computes the connected two given vertices
components of G ◦ Find a simple cycle, if there is one
BREADTH-FIRST SEARCH 14
Single-source shortest path
15
BFS Algorithm
The algorithm uses a mechanism for Algorithm BFS(G, s)
setting and getting “labels” of vertices and L0 ← new empty sequence
edges L0.insertLast(s)
setLabel(s, VISITED)
i←0
while ¬Li.isEmpty()
Li +1 ← new empty sequence
for all v ∈ Li.elements()
for all e ∈ G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w ← opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
setLabel(w, VISITED)
Li +1.insertLast(w)
else
setLabel(e, CROSS)
i ← i +1
Breadth First Search
2 4 8
s 5 7
3 6 9
16
Breadth First Search
Shortest path
1
from s
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: s
Top of queue
Finished
17
Breadth First Search
1
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: s 2
Top of queue
Finished
18
Breadth First Search
1
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: s 2 3
Top of queue
Finished
19
Breadth First Search
1
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5
Top of queue
Finished
20
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5
Top of queue
Finished
21
Breadth First Search
1 2
2 4 8
5 already discovered:
0 s 5 7
don't enqueue
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5 4
Top of queue
Finished
22
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5 4
Top of queue
Finished
23
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 3 5 4
Top of queue
Finished
24
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 3 5 4
Top of queue
Finished
25
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 3 5 4 6
Top of queue
Finished
26
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 5 4 6
Top of queue
Finished
27
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 5 4 6
Top of queue
Finished
28
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 4 6
Top of queue
Finished
29
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 4 6
Top of queue
Finished
30
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 4 6 8
Top of queue
Finished
31
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2
Undiscovered
Discovered Queue: 6 8
Top of queue
Finished
32
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 6 8 7
Top of queue
Finished
33
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 6 8 7 9
Top of queue
Finished
34
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 8 7 9
Top of queue
Finished
35
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
36
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
37
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
38
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
39
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 9
Top of queue
Finished
40
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 9
Top of queue
Finished
41
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 9
Top of queue
Finished
42
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue:
Top of queue
Finished
43
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Level Graph
44
Single-source shortest path
45
Depth-First Search
Can be used to attempt to visit all nodes of a graph in a systematic manner
Works with directed and undirected graphs
Works with weighted and unweighted graphs
depth-first-search
mark vertex as visited
for each adjacent vertex
if unvisited
do a depth-first search on adjacent vertex
Walk-Through Visited Array Stack
F C A
A B
B C
D
H D
E
G E
F
G
H
Depth-First Search
A
B C
D E F G
Single-source shortest path
72
Depth-First Search
v
A
B C
D E F G
A
Single-source shortest path
73
Depth-First Search
v
A
B C
D E F G
A
Single-source shortest path
74
Depth-First Search
v
A
v
B C
D E F G
A B
Single-source shortest path
75
Depth-First Search
v
A
v
B C
D E F G
A B
Single-source shortest path
76
Depth-First Search
v
A
v
B C
D E F G
A B
Single-source shortest path
77
Depth-First Search
v
A
v
B C
v
D E F G
A B D
Single-source shortest path
78
Depth-First Search
v
A
v
B C
v
D E F G
A B D
Single-source shortest path
79
Depth-First Search
v
A
v
B C
v
D E F G
A B D
Single-source shortest path
80
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Single-source shortest path
81
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Single-source shortest path
82
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Single-source shortest path
83
Depth-First Search
v
A
v
B C
v
D E F G
A B D E
Single-source shortest path
84
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Single-source shortest path
85
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Single-source shortest path
86
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Single-source shortest path
87
Depth-First Search
v
A
v
B C
v
D E v F G
v
A B D E F
Single-source shortest path
88
Depth-First Search
v
A
v
B C
v
D E v F G
v
A B D E F
Single-source shortest path
89
Depth-First Search
v
A
v
B C
v
D E v F G
v
A B D E F
Single-source shortest path
90
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Single-source shortest path
91
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Single-source shortest path
92
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Single-source shortest path
93
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Single-source shortest path
94
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
95
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
96
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
97
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
98
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
99
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
10
0
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
10
1
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Single-source shortest path
10
2
Depth-First Search
A
B C
D E F G
A B D E F C G
Single-source shortest path
10
Space Complexity
◦ Stack to handle nodes as they are explored
◦ Worst case: all nodes put on stack (if graph is linear)
◦O(n)
The “Flood Fill” Algorithm
A flood-fill algorithm
void fill( int x, int y, int interiorcolor, int newcolor )
{ //Check boundary crossing
if ( get_pixel( x, y ) == interiorcolor )
{
put_pixel( x, y, newcolor );
fill( x -1, y, interiorcolor, newcolor );
fill( x+1, y, interiorcolor, newcolor );
fill( x, y -1, interiorcolor, newcolor );
fill( x, y+1, interiorcolor, newcolor );
}
}
Single-source shortest path
10
6
A flood-fill algorithm
Single-source shortest path
10
7
Negative Weights
If some of the edges have negative weight, so long as there are no cycles with negative
weight, the Bellman-Ford algorithm will find the minimum distance
π: nil
π: nil
d: 0
2 d: ∞
A B
-1
π: nil
2 d: ∞ 1
-3
4
C
5 1
-1
D 4 E
π: nil π: nil
d: ∞ d: ∞
Single-source shortest path
10
9
π: nil
π: nil A
d: 0
2 d: ∞ -1
A B
-1
π: nil A
2 d: ∞ 2 1
-3
4
C
5 1
-1
D 4 E
π: nil A π: nil
d: ∞ 4 d: ∞
Single-source shortest path
11
0
π: nil
π: nil A
d: 0
2 d: ∞ -1
A B
-1
π: nil A B
2 d: ∞ 2 0 1
-3
4
C
5 1
-1
D 4 E
π: nil A π: nil C
d: ∞ 4 d: ∞ 3
Single-source shortest path
11
1
π: nil
π: nil A
d: 0
2 d: ∞ -1
A B
-1
π: nil A B
2 d: ∞ 2 0 1
-3
4
C
5 1
-1
D 4 E
π: nil A π: nil C C
d: ∞ 4 d: ∞ 3 1
Single-source shortest path
11
2
π: nil
π: nil A E
d: 0
2 d: ∞ -1 -2
A B
-1
π: nil A B B
2 d: ∞ 2 0 -1 1
-3
4
C
5 1
-1
D 4 E
π: nil A π: nil C C
d: ∞ 4 d: ∞ 3 1
The Bellman-Ford Algorithm
Bellman-Ford(G, w, s)
1. Initialize-Single-Source(G, s)
2. for i := 1 to |V| - 1 do
3. for each edge (u, v) ∈ E do
4. Relax(u, v, w)
5. for each vertex v ∈ u.adj do
6. if d[v] > d[u] + w(u, v)
7. then return False // there is a negative cycle
8. return True
Relax(u, v, w)
if d[v] > d[u] + w(u, v)
then d[v] := d[u] + w(u, v)
parent[v] := u
113
Time Complexity
Bellman-Ford(G, w, s)
1. Initialize-Single-Source(G, s)
2. for i := 1 to |V| - 1 do O(|V|)
3. for each edge (u, v) ∈ E do
4. Relax(u, v, w) O(|V||E|)
5. for each vertex v ∈ u.adj do O(|E|)
6. if d[v] > d[u] + w(u, v)
7. then return False // there is a negative cycle
8. return True