Skip to content

Commit

Permalink
GITBOOK-48: Describe Bellman and Dijkstra algo
Browse files Browse the repository at this point in the history
  • Loading branch information
VallariAg authored and gitbook-bot committed Jul 28, 2024
1 parent 7be360c commit c3ff7fb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
53 changes: 41 additions & 12 deletions data-structures/graphs/shortest-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,53 @@

Shorted Path Algorithms:

Negative cycles -> where the sum of the edges in a cycle is negative. It's not possible to find shortest path with negative cycles. Question would usually ask to "detect" it only. Only Bellman Ford can detect with negative cycles.

Let `u` be source node, `v` be destination node, `w` be weight of the edge between the `u` and `v`.

### [Bellman Ford](https://www.programiz.com/dsa/bellman-ford-algorithm)

**Time**: `O(VE)` **Space**: `O(N)`

Bellman Ford algorithm helps us find **the shortest path from a vertex to all other vertices** of a weighted graph.

```python
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
dist = [float("inf") for _ in range(N)]
* Uses edges as inputs `[[u, v, w]]`
* Slower than Dijkstra
* Able to use negative edges.
* Used to detected negative cycles. (If Nth iteration of loop determines negative-cycles)

<pre class="language-python"><code class="lang-python"><strong>def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
</strong> dist = [float("inf") for _ in range(N)]
dist[K-1] = 0
for _ in range(N-1):
for u, v, w in times:
if dist[u-1] + w < dist[v-1]:
if dist[u-1] + w &#x3C; dist[v-1]:
dist[v-1] = dist[u-1] + w
# nth loop determines negative cycles
for u, v, w in times:
if dist[u-1] + w &#x3C; dist[v-1]:
return None # negative cycles detected!
return dist
```
</code></pre>

### [Dijkstra](https://www.programiz.com/dsa/dijkstra-algorithm)

**Time:** `O(E+VlogV)` **Space:** `O(V+E)`

Dijkstra's algorithm allows us to find **the** **shortest path between any two vertices of a graph**.

Implementation can be done in naive way and easy way (with heaps - given below) \[[read here](https://pythonalgos.com/dijkstras-algorithm-in-5-steps-with-python/)]
* Input has to be changed to `{u:{v: w}}`
* Only for non-negative edges.
* Don't work with negative cycles.
* Differs from the minimum spanning tree because the shortest distance between two vertices might not include all the vertices of the graph.

```python
def dijkstra(self, times: List[List[int]], root, destination) -> int:
pyth weight = collections.defaultdict(dict)
for u, v, w in times:
[https://www.youtube.com/watch?v=EaphyqKU4PQ](https://www.youtube.com/watch?v=EaphyqKU4PQ)

Implementation can be done in naive way and easy way (with min-heaps/pq - given below) \[[read here](https://pythonalgos.com/dijkstras-algorithm-in-5-steps-with-python/)]

<pre class="language-python"><code class="lang-python"> def dijkstra(self, times: List[List[int]], root, destination) -> int:
<strong> weight = collections.defaultdict(dict)
</strong> for u, v, w in times:
weight[u][v] = w
heap = [(0, root)]
dist = {}
Expand All @@ -41,13 +59,16 @@ Implementation can be done in naive way and easy way (with heaps - given below)
for v in weight[u]:
heapq.heappush(heap, (dist[u] + weight[u][v], v))
return dist[destination]
```
</code></pre>

### [Floyd Warshall](https://www.programiz.com/dsa/floyd-warshall-algorithm)

**Time**: `O(V^3)` **Space**: `O(V^2)`

Floyd-Warshall Algorithm is an algorithm for finding **the shortest path between all the pairs of vertices** in a weighted graph. This algorithm works for both the directed and undirected weighted graphs. But, it does not work for the graphs with negative cycles (where the sum of the edges in a cycle is negative).
Floyd-Warshall Algorithm is an algorithm for finding **the shortest path between all the pairs of vertices** in a weighted graph.&#x20;

* Works for both the directed and undirected weighted graphs.&#x20;
* Does not work for the graphs with negative cycles.

```python
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
Expand Down Expand Up @@ -89,6 +110,14 @@ Floyd-Warshall Algorithm is an algorithm for finding **the shortest path between



***

### Comparing&#x20;

* It is similar to Dijkstra's algorithm but it can work with graphs in which edges can have negative weights. Dijkstra's algorithm has better time complexity than BF.



### Resources

* Understand algorithms from Programiz (click on algo heading above)
Expand Down
4 changes: 2 additions & 2 deletions data-structures/heaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Basic Functions:

* `heapq.heapify(list)`
* `heapq.heappush`(_heap_, _item_)
* `heapq.heappop`(_heap_)
* `heapq.heappop`(_heap_) - returns smallest&#x20;
* `heapq.nlargest`(_n_, _iterable_, _key=None_)
* `heapq.nsmallest`(_n_, _iterable_, _key=None_)

Expand All @@ -47,7 +47,7 @@ class MaxHeap:
def push(self, item):
heapq.heappush(self.heap, -item)

def pop(self):
def pop(self): # returns largest
popped = heapq.heappop(self.heap)
return -popped

Expand Down

0 comments on commit c3ff7fb

Please sign in to comment.