mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-03-15 21:59:56 +00:00
Update Shortest_path_dp.md
This commit is contained in:
parent
9f17bd604c
commit
2caddf97c2
@ -2,7 +2,7 @@
|
||||
|
||||
We can solve the single source shortest path problem using dynamic programming. How?
|
||||
|
||||
First of all, we will see how to solve this problem using recursion with memoization.
|
||||
First of all, we will see how to solve this problem using recursion with memoization(top-down approach) followed by bottom-up dynamic programming approach.
|
||||
|
||||
## Quiz Time
|
||||
|
||||
@ -14,33 +14,73 @@ $SD$ stands for Shortest Distance.
|
||||
|
||||
-- --
|
||||
|
||||
The above answer can be proved using contradiction.
|
||||
The above answer can be proved using contradiction. Suppose that shortest path between $A$ and $B$ passes through $C$ and the path from $A$ to $C$ is not shortest, then we can replace it with some other vertex $D$ such that the shortest path becomes $A \to D \to B$ which is better than $A \to C \to B$, but it is a contradiction becuase we have asssumed that $A \to C \to B$ is the shortest path between $A$ and $B$.
|
||||
|
||||
We will use the above property together with memoization to solve the problem.
|
||||
We will use the above property together with memoization to solve the problem. How?
|
||||
|
||||
Suppose that we are searching a shortest path between $u$(source) and $v$. Then we know that the shortest path between $u$ and $v$ must be passing through one of the vertices which are putting a directed edge on $v$ (incoming edges for $v$).
|
||||
|
||||

|
||||
|
||||
So, first of all we will find a shortest path between source and $x$, $y$, $z$ one by one. Then we can see that the shortest path between $u$ to v will be either $\text{SP}(x) \to v$ or $\text{SP}(y) \to v$ or $\text{SP}(z) \to v$ depending on which one is minimum from $\text{SD}(x)+w1$, $\text{SD}(y)+w2$, $\text{SD}(z)+w3$, respectively.
|
||||
|
||||
**Note:** $\text{SP}$ stands for shortest path upto the given vertex and $\text{SD}$ stands for shortest distance.
|
||||
|
||||
Done? Let's see proper algorithm.
|
||||
|
||||
## Recursive Memoization Approach
|
||||
|
||||
Now, if we want to find out the shortest path from source $u$ to vertex $v$, then start the recursion from the vertex $v$.
|
||||
|
||||
Move in the reverse direction of the directed edges connected with $v$ and recurse on each (reverse) adjacent vertex until you reach at the source u.
|
||||
Move in the reverse direction of the directed edges connected with $v$ and recurse on each of the (reverse)adjacent vertex(i.e. $x,y,z$) until you reach at the source u.
|
||||
|
||||
Meanwhile update the shortest distances accordingly.
|
||||
|
||||
One thing to notice is that, once the shortest distance for a vertex is found, we can do memoization and use it again to block unnecessary recursive calls.
|
||||
**Note:** **Memoization** is just the memorization of the obtained results, which can be used again and again to obtain new results.
|
||||
|
||||
**Memoization** is just the memorization of the obtained results which are used again and again to obtain new results.
|
||||
One thing to notice is that, once the shortest distance for a vertex is found, we can do memoization and use it again to block unnecessary recursive calls.
|
||||
|
||||
### Algorithm
|
||||
1. Assign all the distances to $\infty$.
|
||||
2. Now, assign source to source distance to $0$ and start the algorithm.
|
||||
3. Loop through all the vertices, if the distance to a vertex is not found yet then start the recursion over that vertex.
|
||||
4. In the recursive function, suppose you are starting from a vertex $v$, then move backward over the edges coming to the vertex $v$.
|
||||
|
||||
4. In the recursive function, suppose you are starting from a vertex $v$, then move backward over the incoming edges to the vertex $v$.
|
||||

|
||||
|
||||
Say $u_1, u_2, \ldots , u_n$ are the vertices we reached by moving backward over the edges.
|
||||
5. Now, we will do recursive call over all these vertices and find out the shortest distance to all of them first and update $distance[v]$ as below:
|
||||
$distance[v] = min(distance[v], ShortestDistance(u_i) + EdgeWeight(u_i,v) )$ $\forall i<=n$.
|
||||
|
||||
**Note:** Stop the recursion at the source vertex, which is a base case.
|
||||
**Note:** Stop the recursion at the source vertex, which is a base case.
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
```c++
|
||||
#include <bits/stdc++.h>
|
||||
@ -135,19 +175,45 @@ int main()
|
||||
|
||||
## Bottom-Up Dynamic Programming Approach
|
||||
|
||||
How can we do botton-up dynamic programming to solve the SSSP problem?
|
||||
How can we do bottom-up dynamic programming to solve the SSSP problem?
|
||||
|
||||
The key here is that, we will order the vertices using topological sort first and then process them one by one in the order.
|
||||
Where to start the bottom-up dp? Can we start it at any random vertex? No, We cannot. Right? Now the thing is that, we have to order the vertices in some order, such that before reaching to a particular vertex(say $v$), we must have found the shortest distances to all the vertices which are putting an incoming edge over $v$.
|
||||
|
||||
Are you familiar with this kind of ordering of vertices? It is **"Topological ordering"**. Topological ordering ensures that we will process vertices in the required manner.
|
||||
|
||||
The key here is that, we will order the vertices using topological sort first and then process them one by one in the order. Let's see the algorithm.
|
||||
|
||||
### Algorithm
|
||||
|
||||
1. First of all, find the topological sort of the vertices.
|
||||
2. Assign all the distances to $\infty$.
|
||||
3. Start the algorithm by assigning the distance from source to source to zero.
|
||||
3. Start the algorithm by assigning the distance from source to source as zero.
|
||||
4. Loop over the vertices in the order generated by the topological sort and update the shortest distances to all the adjacent vertices of all of them, one by one in the order.
|
||||
|
||||
**Visualization**
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
```c++
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
@ -206,7 +272,7 @@ list<int> Topological_Sort(vector<vector<edge> > &graph, int no_vertices)
|
||||
|
||||
}
|
||||
|
||||
void shortest_path(vector<vector<edge> > &graph, vector<int> &distances, int source)
|
||||
void shortest_path_dp(vector<vector<edge> > &graph, vector<int> &distances, int source)
|
||||
{
|
||||
distances[source] = 0;
|
||||
|
||||
@ -237,7 +303,7 @@ int main()
|
||||
|
||||
int source = 1;
|
||||
|
||||
shortest_path(graph, distances, source);
|
||||
shortest_path_dp(graph, distances, source);
|
||||
|
||||
for(int i = 1; i <= no_vertices; i++)
|
||||
cout << distances[i] << " ";
|
||||
|
Loading…
x
Reference in New Issue
Block a user