mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-03-15 13:49:59 +00:00
Update Shortest_path_dp.md
This commit is contained in:
parent
8ba77e289e
commit
014e2d984f
@ -20,7 +20,7 @@ We will use the above property together with memoization to solve the problem. H
|
||||
|
||||
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.
|
||||
|
||||
@ -46,7 +46,7 @@ One thing to notice is that, once the shortest distance for a vertex is found, w
|
||||
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 incoming edges to the vertex $v$.
|
||||

|
||||

|
||||
|
||||
In the image above $x$,$y$,$z$ are these vertices, we can reached by moving backward over the incoming edges to the vertex $v$.
|
||||
|
||||
@ -56,33 +56,33 @@ $distance[v] = min(distance[v], ShortestDistance(u_i) + EdgeWeight(u_i,v) )$ $\
|
||||
|
||||
**Note:** Stop the recursion at the source vertex, which is a base case.
|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||
```c++
|
||||
#include <bits/stdc++.h>
|
||||
@ -193,29 +193,29 @@ Let's see the algorithm.
|
||||
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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user