ALGO3
ALGO3
DAY 3 ALGORITHMS
SHORTEST PATH PROBLEMS
DJIKSTRA BELLMAN FORD FLOYD WARSHALL
BASIC TO ADVANCED CONCEPTS
(150 PRACTICE & PREVIOUS YEAR
QUESTIONS)
GATE NOTEBOOK
Shortest path problem = finding a path between Dijkstra's algorithm solves the single-source shortest
two vertices in a graph such that the sum of the
path problem with non-negative edge weight.
weights of its constituent edges is minimized.
Bellman–Ford algorithm solves the single-source
problem if edge weights may be negative.
• The single-source shortest path problem, in which we
A* search algorithm solves for single pair shortest
have to find shortest paths from a source vertex v to all
path using heuristics to try to speed up the search.
other vertices in the graph.
Floyd–Warshall algorithm solves all pairs shortest
• The single-destination shortest path problem, in which
paths.
we have to find shortest paths from all vertices in the
Johnson's algorithm solves all pairs shortest paths,
directed graph to a single destination vertex v.
and may be faster than Floyd–Warshall on sparse
• This can be reduced to the single-source shortest path
graphs.
problem by reversing the arcs in the directed graph.
Viterbi algorithm solves the shortest stochastic path
• The all-pairs shortest path problem, in which we have
problem with an additional probabilistic weight on
to find shortest paths between every pair of vertices v,
each node.
v' in the graph
Dijkstra’s Algorithm Works For : Time Complexity Analysis of Dijkstra’s Algorithm-
4 12 19
8 7
1 2 3
4 2 9
0
11 8 14 4 14 4
0
7 21
6
10
8 7 6 5
1
8 9 2 11
3. DJIKSTRA
ALGORITHM
4. DJIKSTRA
ALGORITHM
1. BELLMAN FORD
ALGORITHM
2. BELLMAN FORD ALGORITHM
The Floyd Warshall Algorithm is for solving the All
Pairs Shortest Path problem.
The problem is to find shortest distances between
every pair of vertices in a given edge weighted
directed Graph. DJIKSTRA O(V^2) = Matrix
(GREEDY Representation
Time Complexity- ALGORITHM)
O(ELogV) adjacency list
Floyd Warshall Algorithm consists of three loops over representation (Fibonacci
all nodes. Heap)
The inner most loop consists of only operations of a
constant complexity. Bellman Ford O(VE)
Hence, the asymptotic complexity of Floyd-Warshall (Dynamic
algorithm is O(n3), where n is the number of nodes in Programming)
the given graph. Floyd Warshall O(V^3)
(Dynamic There are three nested
When Floyd- Warshall Algorithm is used?
Programming) loops that run through the
vertices of the graph.
Floyd-Warshall Algorithm is best suited for dense
graphs since its complexity depends only on the
number of vertices in the graph.
For sparse graphs, Johnson’s Algorithm is more
suitable.
1. Using Floyd-Warshall Algorithm, find the shortest path distance between every pair of vertices.
Distance Between 3 to 2 ?
GATE CS 2006
To implement Dijkstra’s shortest path algorithm on
unweighted graphs so that it runs in linear time, the
data structure to be used is:
(A) Queue
(B) Stack
(C) Heap
(D) B-Tree
ANSWER (A)
The shortest path in an un-weighted graph means the smallest number of
edges that must be traversed in order to reach the destination in the graph.
This is the same problem as solving the weighted version where all the
weights happen to be 1.
If we use Queue (FIFO) instead of Priority Queue (Min Heap), we get the
shortest path in linear time O(|V| + |E|).
Basically we do BFS traversal of the graph to get the shortest paths.
GATE CS 2017
Let G = (V, E) be any connected undirected edge-weighted graph. The weights
of the edges in E are positive any distinct. Consider the following statements:
I. Minimum Spanning Tree of G is always unique.
II. Shortest path between any two vertices of G is always unique.
Which of the above statements is/are necessarily true?
(A) I only
(B) II only
(C) both I and II
(D) neither I and II
ANSWER (A)
I. Minimum Spanning Tree of G is always unique – MST will
always be distinct if the edges are unique so Correct
II. Shortest path between any two vertices of G is always unique
– Shortest path between any two vertices can be same so
incorrect
Therefore, option A is correct
Background Explanation
We know that minimum spanning
tree of a graph is always unique if all
the weight are distinct, so statement 3
1 is correct. a b
Now statement 2 , this might not be
true in all cases.
1
2
There are two shortest paths from a c
to b (one is direct and other via node
c) So statement 2 is false
Hence option a is correct.
GATE CS 2016
Let G be a weighted connected undirected graph with distinct positive
edge weights. If every edge weight is increased by the same value,
then which of the following statements is/are TRUE?
P: Minimum spanning tree of G does not change Q: Shortest path
between any pair of vertices does not change
(A) P only
(B) Q only
(C) Neither P nor Q
(D) Both P and Q
ANSWER (A)
• The shortest path may change. The reason is, there may be different
number of edges in different paths from s to t.
• For example, let shortest path be of weight 15 and has 5 edges.
• Let there be another path with 2 edges and total weight 25.
• The weight of the shortest path is increased by 5*10 and becomes 15 + 50.
• Weight of the other path is increased by 2*10 and becomes 25 + 20.
• So the shortest path changes to the other path with weight as 45.
• The Minimum Spanning Tree doesn’t change.
• Remember the Kruskal’s algorithm where we sort the edges first.
• IF we increase all weights, then order of edges won’t change.
GATE CS 2016
• Consider the weighted undirected graph with 4 vertices, where the weight
of edge {i, j} is given by the entry WijWij in the matrix W.
• The largest possible integer value of x, for which at least one shortest path
between some pair of vertices will contain the edge with weight x is
________ .
0 2 8 5
2 0 5 8
8 5 0 x
5 8 x 0
• We're asked to find value 'x' so that
shortest path between any two vertices
contains the edge with cost 'x'. 3
• Let us verify for the vertices 3 and 4.
8
• To travel from vertex 3 to vertex 4: we 5
have 3 possible paths. x
• 1. Path 3-1-4 with cost=8+5=13
• 2. Path 3-2-4 with cost=5+8=13 1 2
2
• 3. Edge 3-4 with cost=x
• So, for the edge 3-4 to be the shortest
path between vertices 3 and 4, x should 5 8
be less than 13. 4
• Since we're asked to find the maximum 0 2 8 5
value for x, the answer is 12.
2 0 5 8
8 5 0 x
5 8 x 0
∞ B
5
9 6
∞
6 11 ∞
2 ∞ 4
3
14 9 15
10
0 ∞
7
A 1
2
∞ B
5
9 6
14
6 11 ∞
2 4
9
3
14 9 15
10
0 7
A 1 7
2
∞ B
5
9 6
14
6 11 ∞
2 4
9
3
14 9 15
10
0 7
A 1 7
2
∞ B
5
9 6
14
6 11 ∞
2 4
9
3
14 9 15
10
0 7
A 1 7
2
FULL COURSE ON
DAY 3 ALGORITHMS
SRP 3
GATE NOTEBOOK
Shortest path problem = finding a path between
Dijkstra's algorithm solves the single-source shortest
two vertices in a graph such that the sum of the path problem with non-negative edge weight.
weights of its constituent edges is minimized. Bellman–Ford algorithm solves the single-source
problem if edge weights may be negative.
• The single-source shortest path problem, in which we Floyd–Warshall algorithm solves all pairs shortest
have to find shortest paths from a source vertex v to all paths.
other vertices in the graph.
• The single-destination shortest path problem, in which DJIKSTRA O(V^2) = Matrix
we have to find shortest paths from all vertices in the (GREEDY Representation
directed graph to a single destination vertex v. ALGORITHM)
• The all-pairs shortest path problem, in which we have O(ELogV) adjacency list
to find shortest paths between every pair of vertices v, representation (Fibonacci
v' in the graph Heap)
• Dijkstra doesn’t work for Graphs with negative weight Bellman Ford O(VE)
edges, Bellman-Ford works for such graphs. (Dynamic
• Bellman-Ford is also simpler than Dijkstra and suites Programming)
well for distributed systems.
Floyd Warshall O(V^3)
Floyd-Warshall Algorithm is best suited for dense graphs (Dynamic There are three nested
since its complexity depends only on the number of vertices Programming) loops that run through the
in the graph. vertices of the graph.
For sparse graphs, Johnson’s Algorithm is more suitable.
THANK YOU
KEEP LEARNING !