Graphs

Shortest Path for Weighted Graph

Dijkstra’s

O((V + E) log V) with a binary heap.

(Only works with pos weights)

Video

Basic idea:

  • Determine shortest path from src to dest

  • To do this, determine shortest path to EVERY node along the way

Approach:

  • Mark every node as distance=inf except src (0)

Main steps after:

  • In map, update distances to neighbors (if shorter), mark as visited

  • Next, travel to next shortest node away (use prio queue). Mark visited.

  • Repeat Use a visited set and skip in pq if already visited

EG:

      [A]
     /   \
   1/     \10
   /       \
 [B] ----1-- [C]
   \       /
   6\     /4
     \   /
      [D]

Bellman-Ford Algorithm

Use if we have negative path weights.

DP strategy.


Intuition: Bellman–Ford finds the shortest path from a source node by:

Trying all edges again and again (up to |V|−1 times), and updating a node’s distance whenever we find a cheaper way to get there through another node.

It's like saying:

“If I already know the shortest way to get to A, and there’s an edge from A → B, then maybe I now also know a better way to get to B.”

Because the longest possible simple path in a graph with V nodes has V−1 edges — so if we relax all edges V−1 times, we’re guaranteed to propagate all minimal costs forward.


Bellman Ford

O(V * E)

Theorem 1: In a "graph with no negative-weight cycles? with N vertices, the shortest path between any two vertices has at most N-1 edges.

A negative weight cycle is one that results in a negative path after the cycle is completed.

For example, a positive weight cycle could be

Neg weights

The cost from A to B is +2, going back to A we subtract one resulting in a weight greater than zero (2).

The reason the shortest path has at most N-1 edges is that because there are no negative weight cycles, starting over from the original cycle we'll just increase the cost or make it the same.

So, basic idea - "relax" edges N-1 times (N is num vertices).

Relaxation refers to trying a connection and seeing if it reduces the cost to get to that vertex. If it does we reduce it accordingly and that is relaxation

Initially mark the cost to get to a vertex as Infinity for all vertex Loop over all edges in any order.

For an edge between vertex u and v.

Full example:

Note:

We can limit the maximum number of edges traversed to see the shortest path with an additional restriction of how many edges we can traverse. For example if we wanted to traverse a maximum of K edges we would use that instead of N-1

If we do this we must use a temp variable! This is because otherwise we update additional times in the cycle:

Topological Sort

Video (Kahn's)

Leetcode Explore Card Kahn's Algorithm

Lets us model dependencies with a graph.

O(V+E)

Gives us an order we can traverse to path through graph from most dependant to least.

There can be multiple orderings.

Kahn's Algorithm

In-degree for node represents how many must come before

If a node’s in-degree is 0, it means no prerequisites

Repeatedly remove nodes without dependencies from the graph and add them to the topological ordering.

As we remove them from the graph, we removed their outgoing edges, and new nodes without dependencies become free.

We repeat the process until we have looked at every node or we have found a cycle

Use a queue to maintain zero dependencies nodes.

Example

Or, a more conventional example:

Last updated