{"id":148,"date":"2026-11-13T12:32:46","date_gmt":"2026-11-13T04:32:46","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-brandes-accumulates-betweenness\/"},"modified":"2026-11-13T21:51:19","modified_gmt":"2026-11-13T13:51:19","slug":"how-brandes-accumulates-betweenness","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-brandes-accumulates-betweenness\/","title":{"rendered":"How Brandes accumulates betweenness"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Brandes&#8217; algorithm<\/strong> computes <strong>betweenness centrality<\/strong>. For every source it builds a shortest-path DAG, counts the number of shortest paths \u03c3 through each vertex, and walks that DAG backward to add dependencies. One source costs a BFS, or a Dijkstra run when weights are nonnegative. All sources cost <strong>O(VE)<\/strong> on an unweighted graph and <strong>O(VE + V\u00b2 log V)<\/strong> with a binary heap on nonnegative weights. The centrality of v is the sum, over pairs, of the fraction of shortest paths between the pair that pass through v.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The definition sums, over all ordered pairs s, t different from v, the fraction \u03c3_st(v) \/ \u03c3_st. Computing each pair separately is cubic or worse and recomputes the same shortest-path DAGs. Brandes&#8217; observation is that the fractions at a source can be accumulated from the leaves of the shortest-path DAG inward, in linear time once the DAG and the path counts exist.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The dependency \u03b4_s(v) is the sum, over all targets, of the fraction of shortest s-paths that go through v. It satisfies a one-line recurrence along the DAG: a vertex sends its own dependency, plus one, down each incoming DAG edge in proportion to the path counts.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">After every source has been processed, the accumulated dependencies equal the betweenness scores, up to the convention of counting unordered pairs once, which is a factor of two on undirected graphs and must be stated. Unweighted time is O(VE) because each of V breadth-first searches is O(V+E) and the accumulation is linear in the DAG. Nonnegative weights replace each BFS with Dijkstra. Extra memory is one distance array, one path-count array, and a stack of vertices in order of discovery, reused per source.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Negative weights are outside the algorithm, because the inner search would no longer be Dijkstra or BFS. Brandes does not approximate. Sampling random sources is a later estimator and is not this procedure. Endpoints are usually excluded from a path&#8217;s credit. Including them is a different score.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Set centrality[v] = 0 for every vertex.<\/li>\n<li>For each source s, run BFS or Dijkstra. Let \u03c3[s] = 1 and \u03c3[v] = 0 elsewhere. Along a relaxation that finds an equal shortest distance, add the parent&#8217;s \u03c3 into the child&#8217;s \u03c3. Along a strictly better distance, reset \u03c3[child] to \u03c3[parent].<\/li>\n<li>Push vertices onto a stack in the order they are closed, so the stack pops from the farthest vertices back toward s.<\/li>\n<li>Set \u03b4[v] = 0. Pop v. For each predecessor u on the shortest-path DAG, add \u03b4[u] += (\u03c3[u]\/\u03c3[v]) \u00b7 (1+\u03b4[v]). Then, if v is not s, add \u03b4[v] to centrality[v].<\/li>\n<li>On an undirected graph, halve the final scores if the definition counts unordered pairs.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The accumulation assumes the inner search enumerated every shortest path&#8217;s predecessor edges. A single parent pointer, as in a shortest-path tree, drops alternate shortest paths and undercounts \u03c3. The algorithm is exact betweenness, not closeness centrality and not PageRank.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when only one shortest path is counted<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Keep a single parent pointer<\/td>\n<td>A vertex with two shortest paths from the source contributes a fraction on each. One parent treats the fraction as 1 or 0.<\/td>\n<\/tr>\n<tr>\n<td>Accumulate in arbitrary order<\/td>\n<td>The recurrence needs the child&#8217;s \u03b4 to be finished before the parent is updated. The stack of closing order provides that.<\/td>\n<\/tr>\n<tr>\n<td>Run it with negative weights under Dijkstra<\/td>\n<td>The shortest-path DAG is wrong as soon as Dijkstra is wrong. There is no betweenness number to accumulate.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(VE) for a weighted heap implementation<\/td>\n<td>Each source pays a Dijkstra bound. The unweighted O(VE) figure is the BFS version.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A single-source shortest-path tree is Dijkstra or BFS. PageRank is a different centrality, based on a damped walk rather than on fractions of shortest paths. Sampling sources estimates Brandes and does not replace the exact sum.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment runs one search per source, keeps every predecessor of equal distance, and accumulates dependency from the far end of a stack. On the undirected path 0\u20141\u20142 the halved score of the middle vertex is <code>1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">from collections import deque\nadj = {0: [1], 1: [0, 2], 2: [1]}\ncentrality = {v: 0.0 for v in adj}\nfor source in adj:\n    stack, pred = [], {v: [] for v in adj}\n    sigma = {v: 0.0 for v in adj}\n    dist = {v: -1 for v in adj}\n    sigma[source] = 1\n    dist[source] = 0\n    queue = deque([source])\n    while queue:\n        u = queue.popleft()\n        stack.append(u)\n        for v in adj[u]:\n            if dist[v] &lt; 0:\n                dist[v] = dist[u] + 1\n                queue.append(v)\n            if dist[v] == dist[u] + 1:\n                sigma[v] += sigma[u]\n                pred[v].append(u)\n    delta = {v: 0.0 for v in adj}\n    while stack:\n        v = stack.pop()\n        for u in pred[v]:\n            delta[u] += (sigma[u] \/ sigma[v]) * (1 + delta[v])\n        if v != source:\n            centrality[v] += delta[v]\nfor v in centrality:\n    centrality[v] \/= 2\nassert centrality[1] == 1 and centrality[0] == 0\nprint(int(centrality[1]))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>NetworkX<\/strong> and transport-planning tools compute betweenness with Brandes&#8217; accumulation: one search from each vertex, then a dependency pass. The score says which junctions lie on many shortest paths. PageRank scores a different question, the stationary visit rate.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop exact Brandes when V is large enough that V shortest-path searches will not finish. A sampled-source estimator is then a different algorithm and should be labeled as one. Keep Brandes when the graph is moderate and the score required is exact betweenness.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Brandes&#8217; algorithm returns the backward dependency (\u03c3[u]\/\u03c3[v]) \u00b7 (1+\u03b4[v]) and the stack that makes the walk legal. <strong>Brandes&#8217; algorithm computes betweenness by summing, over every source, a backward pass over the shortest-path DAG, in O(VE) time on an unweighted graph.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Brandes&#8217; algorithm computes betweenness centrality. For every source it builds a shortest-path DAG, counts the number of shortest paths \u03c3 through each vertex, and walks that DAG backward to add dependencies. One source costs a BFS, or a Dijkstra run when weights are nonnegative. All sources cost&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,32],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-graphs"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}