Unit Iv
Unit Iv
Syllabus: Basic strategy, multistage graphs, all pairs shortest path-Floyd Warshall algorithm, single source shortest
paths-Bellman Ford algorithm, optimal binary search trees, traveling salesman problem, longest common
subsequence problem, chained matrix multiplication.
Basic strategy:
Dynamic programming is a technique that breaks the problems into sub-problems, and saves the result for
future purposes so that we do not need to compute the result again. The subproblems are optimized to
optimize the overall solution is known as optimal substructure property. The main use of dynamic
programming is to solve optimization problems. Here, optimization problems mean that when we are
trying to find out the minimum or the maximum solution of a problem. The dynamic programming
guarantees to find the optimal solution of a problem if the solution exists.
The definition of dynamic programming says that it is a technique for solving a complex problem by first
breaking into a collection of simpler subproblems, solving each subproblem just once, and then storing
their solutions to avoid repetitive computations.SSSS
Time Complexity:
• O(E) where E is the number of edges in the graph.
• If E is not given, assume O(n²) for dense graphs.
v. After all iterations, the matrix D will contain the shortest course distances between all pairs of
vertices.
Bellman-Ford is a single source shortest path algorithm that determines the shortest path
between a given source vertex and every other vertex in a graph. This algorithm can be used
on both weighted and unweighted graphs.
A Bellman-Ford algorithm is also guaranteed to find the shortest path in a graph, similar to Dijkstra’s
algorithm. Although Bellman-Ford is slower than Dijkstra’s algorithm, it is capable of handling graphs
with negative edge weights, which makes it more versatile. The shortest path cannot be found if there
exists a negative cycle in the graph. If we continue to go around the negative cycle an infinite number of
times, then the cost of the path will continue to decrease (even though the length of the path is increasing).
As a result, Bellman-Ford is also capable of detecting negative cycles, which is an important feature.
Since the graph has six vertices so it will have five iterations.
First iteration
Consider the edge (A, B). Denote vertex 'A' as 'u' and vertex 'B' as 'v'. Now use the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 6
Since (0 + 6) is less than ∞, so update
1. d(v) = d(u) + c(u , v)
d(v) = 0 + 6 = 6
Therefore, the distance of vertex B is 6.
Second iteration:
In the second iteration, we again check all the edges. The first edge is (A, B). Since (0 + 6) is greater than
1 so there would be no updation in the vertex B.
The next edge is (A, C). Since (0 + 4) is greater than 3 so there would be no updation in the vertex C.
The next edge is (A, D). Since (0 + 5) equals to 5 so there would be no updation in the vertex D.
The next edge is (B, E). Since (1 - 1) equals to 0 which is less than 5 so update:
d(v) = d(u) + c(u, v)
d(E) = d(B) +c(B , E)
=1-1=0
The next edge is (C, E). Since (3 + 3) equals to 6 which is greater than 5 so there would be no updation in
the vertex E.
The next edge is (D, C). Since (5 - 2) equals to 3 so there would be no updation in the vertex C.
The next edge is (D, F). Since (5 - 1) equals to 4 so there would be no updation in the vertex F.
The next edge is (E, F). Since (5 + 3) equals to 8 which is greater than 4 so there would be no updation in
the vertex F.
The next edge is (C, B). Since (3 - 2) equals to 1` so there would be no updation in the vertex B.
Third iteration
We will perform the same steps as we did in the previous iterations. We will observe that there will be no
updation in the distance of vertices.
1. The following are the distances of vertices:
2. A: 0
3. B: 1
4. C: 3
5. D: 5
6. E: 0
7. F: 3
Complexity Analysis of Bellman-Ford Algorithm:
• Time Complexity when graph is connected:
• Best Case: O(E), when distance array after 1st and 2nd relaxation are same , we can simply
stop further processing
• Average Case: O(V*E)
• Worst Case: O(V*E)
• Time Complexity when graph is disconnected:
• All the cases: O(E*(V^2))
• Auxiliary Space: O(V), where V is the number of vertices in the graph.
Bellman Ford’s Algorithm Applications:
• Network Routing: Bellman-Ford is used in computer networking to find the shortest paths in
routing tables, helping data packets navigate efficiently across networks.
• GPS Navigation: GPS devices use Bellman-Ford to calculate the shortest or fastest routes between
locations, aiding navigation apps and devices.
• Transportation and Logistics: Bellman-Ford’s algorithm can be applied to determine the optimal
paths for vehicles in transportation and logistics, minimizing fuel consumption and travel time.
• Game Development: Bellman-Ford can be used to model movement and navigation within virtual
worlds in game development, where different paths may have varying costs or obstacles.
• Robotics and Autonomous Vehicles: The algorithm aids in path planning for robots or autonomous
vehicles, considering obstacles, terrain, and energy consumption
Drawback of Bellman Ford’s Algorithm:
• Bellman-Ford algorithm will fail if the graph contains any negative edge cycle.
For example, consider the graph shown in the figure on the right side. A TSP tour in the graph is 1-2-4-3-
1. The cost of the tour is 10+25+30+15 which is 80. The problem is a famous NP-hard problem. There is
no polynomial-time know solution for this problem. The following are different solutions for the traveling
salesman problem.
Naive Solution:
1) Consider city 1 as the starting and ending point.
2) Generate all (n-1)! Permutations of cities.
3) Calculate the cost of every permutation and keep track of the minimum cost permutation.
4) Return the permutation with minimum cost.