Proofs: Brandes' algorithm
These are the human proofs of Brandes' algorithm, at full length.
Brandes' algorithm computes betweenness centrality full
This proof shows: correctness, termination.
Let be a directed graph; an undirected graph is the directed graph with both arcs and for each edge. Each arc has a length , and in the form without weights. Fix a source . Let , with if is not reachable, and let be the number of shortest paths from to , with and for unreachable . The predecessor set is .
Termination. Line L2 runs times. In one run, each vertex enters the queue at most once (line L8 enqueues only when and makes it finite), so lines L5 to L9 run at most times and scan finite lists. Line L11 pops exactly the vertices pushed at line L6. So the algorithm stops. The Dijkstra form settles each vertex once and stops in the same way.
Lemma 1 (Bellman criterion). A vertex lies on a shortest path from to if and only if . Every shortest path to ends with an arc with , and its part up to is a shortest path to . Hence for .
Proof. A subpath of a shortest path is a shortest path, since a shorter subpath could replace it. So the first statement holds, and a shortest path has , so . Conversely, for , each shortest path to followed by has length . This is a bijection between the shortest paths to and the pairs (a predecessor , a shortest path to ), which gives the sum.
Lemma 2 (the search). After lines L4 to L9 (or their Dijkstra form) for the source : , and for every , and holds the reachable vertices with non-decreasing from the bottom to the top.
Proof. Without weights, lines L4 to L8 are the breadth-first search, which sets and dequeues the vertices in non-decreasing order of ; line L6 pushes them in that order. Line L9 runs once for each arc out of a reachable , after is final (either was discovered before, or line L8 just set it), and it appends to exactly when , that is when . So . When is dequeued, all its predecessors have smaller and were dequeued before, so all their contributions to were made, and by Lemma 1 and induction on , when passes it on. The final is the sum over all , which is by Lemma 1.
With positive lengths, Dijkstra's algorithm settles the vertices in non-decreasing order of and with at settling. Every has , since , so it is settled before , and when it scans the test either makes the first predecessor found with the current best value or adds it to a list of equal value. Values found later that are smaller reset the list and the count; at the time is settled, , and the list holds exactly the settled with , which are all of , and by induction .
Lemma 3 (the dependency recursion). For each ,
where , with the sum over reachable .
Proof. Fix a reachable with . Each shortest path from to through leaves along exactly one arc , and by Lemma 1, and is on the path. So , where counts the shortest - paths that use the arc .
Count . If , it is : a shortest path to followed by the arc. If , a path that uses is a shortest path to , the arc, and a shortest path from to , so . The number of shortest - paths through is , so . Hence
Sum over and over , and exchange the two sums. For a fixed , the term gives , and the terms give . Pairs with contribute 0 on both sides, since then or is not reachable. This gives the formula.
Lemma 4 (the accumulation). After lines L10 to L14 for the source , for every reachable , and the values added to are for .
Proof. If , then , so is below in by Lemma 2 and is popped after . When is popped, every with is further from and was popped before, and each such pop added to at line L13. Line L13 adds to only at these pops. By induction on the order of pops, each used was already final and equal to , so by Lemma 3, when is popped. It is not changed later, since later pops only add to their predecessors, which are closer to . Line L14 then adds it for . Unreachable vertices are never pushed and get no addition, which is right since for them.
Theorem. BRANDES returns for every in the directed case, and half of it in the undirected case.
Proof. By Lemma 4, after the loop of line L2, , which is the sum over ordered pairs. For an undirected graph, the pairs and have the same shortest paths reversed and give equal terms, so line L15 turns the sum into one over unordered pairs.