2. خان سنور Algorithm Analysis
Dijkstra Algorithm
Single Source Shortest Path
Floyd Warshall Algorithm
Bellman Ford Algorithm
Contents
3. خان سنور Algorithm Analysis
Shortest Path in Graph - The Problems
● Given a directed graph G with edge weights, find
■ The shortest path from a given vertex s to all other vertices (Single Source
Shortest Paths)
■ The shortest paths between all pairs of vertices (All Pairs Shortest Paths)
● where the length of a path is the sum of its edge weights.
4. خان سنور Algorithm Analysis
Shortest Paths: Applications
• Robot Applications
• Typesetting in TEX
• Urban Traffic Planning
• Tramp Steamer Problem
• Optimal pipelining of VLSI Chip
• Telemarketer Operator Scheduling
• Subroutine in higher level algorithms
• Approximating Piecewise linear functions
• Open Shortest Path First(OSPF) routing protocol for IP
6. خان سنور Algorithm Analysis
Single Source Shortest Path
• Suppose G be a weighted directed graph where a minimum labeled w(u, v)
associated with each edge (u, v) in E, called weight of edge (u, v). These
weights represent the cost to traverse the edge. A path from vertex u to
vertex v is a sequence of one or more edges.
<(v1,v2), (v2,v3), . . . , (vn-1, vn)> in E[G] where u = v1 and v = vn
• The cost (or length or weight) of the path P is the sum of the weights of
edges in the sequence.
• The shortest-path weight from a vertex u ∈ V to a vertex v ∈ V in the
weighted graph is the minimum cost of all paths from u to v. If there exists
no such path from vertex u to vertex v then the weight of the shortest-path
is ∞.
7. خان سنور Algorithm Analysis
Variant of Single Source Shortest Path
• Given a source vertex, in the weighted diagraph, find the shortest path
weights to all other vertices in the digraph.
• Given a destination vertex, t, in the weighted digraph, find the shortest
path weights from all other vertices in the digraph.
• Given any two vertices in the weighted digraph, find the shortest path
(from u to vor v to u)
8. خان سنور Algorithm Analysis
Negative-Weight Edges
• The negative weight cycle is a cycle whose total is negative. No
path from starting vertex S to a vertex on the cycle can be a
shortest path. Since a path can run around the cycle many, many
times and get any negative cost desired. in other words, a
negative cycle invalidates the noton of distance based on edge
weights.
•
9. خان سنور Algorithm Analysis
Relaxation Technique
• This technique consists of testing whether we can improve the shortest
path found so far if so update the shortest path. A relaxation step may or
may not decrease the value of the shortest-path estimate.
• The following code performs a relaxation step on edge(u,v)
•Relax (u, v, w)
If d[u] + w(u, v) < d[v]
then d[v] d[u] + w[u, v]
10. خان سنور Algorithm Analysis
A Fact About Shortest Paths
● Theorem: If p is a shortest path from u to v, then any subpath of p is
also a shortest path.
● Proof: Consider a subpath of p from x to y. If there were a shorter
path from x to y, then there would be a shorter path from u to v.
u x y v
shorter?
11. خان سنور Algorithm Analysis
The Problem-SSS
•Given a weighted graph G, find a shortest path from given
vertex to each other vertex in G.
•Note that we can solve this problem quite easily with BFS
traversal algorithm in the special case when all weights
are 1.
•The greedy approach to this problem is repeatedly
selecting the best choice from those available at that time.
12. خان سنور Algorithm Analysis
Dijkstra’s Algorithm
• Dijkstra's algorithm solves the single-source shortest-path problem when
all edges have non-negative weights.
• It is a greedy algorithm and similar to Prim's algorithm. Algorithm starts
at the source vertex, s, it grows a tree, T, that ultimately spans all vertices
reachable from S.
• Vertices are added to T in order of distance i.e., first S, then the vertex
closest to S, then the next closest, and so on.
• Following implementation assumes that graph G is represented by
adjacency lists.
13. خان سنور Algorithm Analysis
Implementation
1. INITIALIZE SINGLE-SOURCE (G, s)
2. S ← { } // S will ultimately contains vertices of final shortest-path weights from s
3. Initialize priority queue Q i.e., Q ← V[G]
4. while priority queue Q is not empty do
5. u ← EXTRACT_MIN(Q) // Pull out new vertex
6. S ← S ∪ {u} // Perform relaxation for each vertex v adjacent to u
7. for each vertex v in Adj[u] do
8. Relax (u, v, w)
DIJKSTRA (G, w, s)
14. خان سنور Algorithm Analysis
Analysis
• Like Prim's algorithm, Dijkstra's algorithm runs in
O(|E|lg|V|) time.
15. خان سنور Algorithm Analysis
Step By Step Operations . . .
• Step1. Given initial graph G=(V, E). All nodes nodes have infinite cost
except the source node, s, which has 0 cost.
16. خان سنور Algorithm Analysis
• Step 2. First we choose the node, which is closest to the source node, s. We
initialize d[s] to 0. Add it to S. Relax all nodes adjacent to source, s. Update
predecessor (see red arrow in diagram below) for all nodes updated.
17. خان سنور Algorithm Analysis
• Step 3. Choose the closest node, x. Relax all nodes adjacent to node x.
Update predecessors for nodes u, v and y (again notice red arrows in
diagram below).
18. خان سنور Algorithm Analysis
• Step 4. Now, node y is the closest node, so add it to S. Relax node v and
adjust its predecessor (red arrows remember!).
19. خان سنور Algorithm Analysis
• Step 5. Now we have node u that is closest. Choose this node and adjust its
neighbor node v.
20. خان سنور Algorithm Analysis
• Step 6. Finally, add node v. The predecessor list now defines the shortest
path from each node to the source node, s.
21. خان سنور Algorithm Analysis
Q as a linear Array
• EXTRACT_MIN takes O(V) time and there are |V| such operations.
Therefore, a total time for EXTRACT_MIN in while-loop is O(V2).
• Since the total number of edges in all the adjacency list is |E|.
• Therefore for-loop iterates |E| times with each iteration taking O(1) time.
• Hence, the running time of the algorithm with array implementation
is O(V2 + E) = O(V2).
22. خان سنور Algorithm Analysis
Q as a Binary Heap
• In this case, EXTRACT_MIN operations takes O(lg V) time and there
are |V| such operations.
• The binary heap can be build in O(V) time.
• Operation DECREASE (in the RELAX) takes O(lg V) time and there are at
most such operations.
• Hence, the running time of the algorithm with binary heap provided given
graph is sparse is O((V + E) lg V).
• Note:- that this time becomes O(E lg V) if all vertices in the graph is
reachable from the source vertices
23. خان سنور Algorithm Analysis
Bellman-Ford Algorithm
• Bellman-Ford algorithm solves the single-source shortest-path problem in
the general case in which edges of a given digraph can have negative
weight as long as G contains no negative cycles.
• This algorithm, like Dijkstra's algorithm uses the notion of edge relaxation
but does not use with greedy method. Again, it uses d[u] as an upper
bound on the distance d[u, v] from u to v.
• The algorithm progressively decreases an estimate d[v] on the weight of
the shortest path from the source vertex s to each vertex v in V until it
achieve the actual shortest-path. The algorithm returns Boolean TRUE if
the given digraph contains no negative cycles that are reachable from
source vertex s otherwise it returns Boolean FALSE.
24. خان سنور Algorithm Analysis
The Bellman-Ford Algorithm
● Handles negative edge weights
● Detects negative cycles
● Is slower than Dijkstra
4
-10
5
a negative cycle
25. خان سنور Algorithm Analysis
Bellman-Ford: Idea
● Repeatedly update d for all pairs of vertices connected by an
edge.
● Theorem: If u and v are two vertices with an edge from u to v,
and s u v is a shortest path, and d[u] = (s,u),
● then d[u]+w(u,v) is the length of a shortest path to v.
● Proof: Since s u v is a shortest path, its length is (s,u) +
w(u,v) = d[u] + w(u,v).
26. خان سنور Algorithm Analysis
Why Bellman-Ford Works
• On the first pass, we find (s,u) for all vertices whose shortest paths have one edge.
• On the second pass, the d[u] values computed for the one- edge-away vertices are correct (=
(s,u)), so they are used to compute the correct d values for vertices whose shortest paths
have two edges.
• Since no shortest path can have more than |V[G]|-1 edges, after that many passes all d
values are correct.
• Note: all vertices not reachable from s will have their original values of infinity.(Same,
by the way, for Dijkstra).
27. خان سنور Algorithm Analysis
Bellman-Ford: Algorithm
● BELLMAN-FORD(G, w, s)
1 foreach vertex v V[G] do //INIT_SINGLE_SOURCE
2 d[v]
3 [v] NIL
4 d[s] 0
5 for i 1 to |V[G]|-1 do > each iteration is a “pass”
6 for each edge (u,v) in E[G] do
7 RELAX(u, v, w)
8 > check for negative cycles
9 for each edge (u,v) in E[G] do
10 if d[v] > d[u] + w(u,v) then
11 return FALSE
12 return TRUE
Running time VE)
28. خان سنور Algorithm Analysis
Negative Cycle Detection
● What if there is a negative-weight
cycle reachable from s?
d[u] d[x]+4
d[v] d[u]+5
d[x] d[v]-10
● Assume:
●
●
● Adding:
● d[u]+d[v]+d[x] d[x]+d[u]+d[v]-1
● Because it’s a cycle, vertices on left are same as those on right.
Thus we get 0 -1; a contradiction.
So for at least one edge (u,v),
● d[v] > d[u] + w(u,v)
● This is exactly what Bellman-Ford checks for.
u
x
v
4
5
-10
35. خان سنور Algorithm Analysis
Running Time Analysis
• The initialization in line 1 takes 𝜃 𝑣 𝑡𝑖𝑚𝑒.
• For loop of lines 2-4 takes O(E) time and For-loop of line 5-7
takes O(E) time.
• Thus, the Bellman-Ford algorithm runs in O(E) time.