SSSP

Work in Progress

Summary

SSSP algorithms

Time ComplexityConstraintType of Graph ADT
BFSgenerally -
trees -
unweighted/uniformed weight

tree
AL
DFStreeAL
bellman-ford algorithmno -ve weight cycleAL + EL(optional)
dijkstra’s algorithmno -ve weightAL
modified dijkstrano -ve weight cycleAL
dynamic programmingDAGsAL

SSSP related problems

  • single-destination shortest path(multiple sources) -> SSSP on transposed AL
  • single-source single-destination shortest path -> early termination, dijkstra’s invariant
  • kth shortest path -> dijkstra with PQ of distances, revisit

Concept

Single-source shortest path(SSSP)

  • shortest path(smallest weight) from any source node to the other node visitable from the source
  • SSSP is not unique, is the graph is not a tree, there may be multiple paths to same node
  • 6 vertex cyclic graph: SSSP from 0 to 5
0123451522123122

Shortest path spanning tree

  • has the source-node as the root
  • may not span all nodes, since not all nodes are necessarily visitable by all other nodes
  • 6 vertex cyclic graph: SSSP spanning tree from 0
0011233342541522123122

Negative weight cycles

00112211-2001¡12¡111-2

negative weights are not in the cs2040s syllabus

Edge relaxation

  • check if an incoming edge results in a shorter path
java
if (dist[v] > dist[u] + w) { // if using this edge can give v a shorter distance
	dist[v] = dist[u] + w; // "relax" the edge
	p[v] = u; // update parent array	
} 
001123131001122131

Infinity

  • to employ relaxation, all the distances must be initialised to infinity(a very big number)
  • can use Integer.MAX_VALUE, but we risk overflowing if we add
java
int INF = 1e9;

List<Integer> dist = new ArrayList<>(Collections.nCopies(V, INF));
dist.set(s, 0); // distance to source from source = 0

Simple path

  • a shortest path(with no negative edges) is a simple path
  • there are no cycles in a shortest path

BFS method

  • only on uniform +ve weights/unweighted graph
  • counting the least number of jumps

DFS method

  • DFS finds the first path to any visitable node
  • correctness is only garunteed when there is only 1 path to any node

Dynamic Programming

  • given a DAG
  • topological sort + one pass Bellman-Ford

Application

Leetcode: Cheapest Flights Within K Stops

  • per-level BFS
  • care about jumps not weights
java
List<List<int[]>> AL = new ArrayList<>();
for (int i = 0; i < n; i++)
	AL.add(new ArrayList<>());

for (int[] e : flights) // directed edges
	AL.get(e[0]).add(new int[] { e[1], e[2] });

int INF = 1000000000;
int[] dist = new int[n];
Arrays.fill(dist, INF);
dist[src] = 0;

// BFS by level
Queue<int[]> q = new LinkedList<>();
q.add(new int[] { src, 0 }); // [u, d]

while (!q.isEmpty() && k-- >= 0) { // if k = 0, one jump to dst
	// each iteration level/jump from src
	int sz = q.size(); // important, we only want to go through the number of nodes in this level
	for (int i = 0; i < sz; i++) {
		int[] u_d = q.poll();

		for (int[] v_w : AL.get(u_d[0])) {
			int v = v_w[0];
			int d = u_d[1] + v_w[1];
			if (d < dist[v]) {
				dist[v] = d;
				q.add(new int[] { v, d });
			}
		}
	}
}

return dist[dst] == INF ? -1 : dist[dst];