{"id":165,"date":"2026-11-13T12:33:17","date_gmt":"2026-11-13T04:33:17","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-dials-algorithm-buckets-integer-distances\/"},"modified":"2026-11-13T21:51:39","modified_gmt":"2026-11-13T13:51:39","slug":"how-dials-algorithm-buckets-integer-distances","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-dials-algorithm-buckets-integer-distances\/","title":{"rendered":"How Dial&#8217;s algorithm buckets integer distances"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Dial&#8217;s algorithm<\/strong> is Dijkstra for nonnegative integer weights whose maximum is <strong>C<\/strong>. It keeps a bucket for each possible distance. Because an edge increases distance by at most C, a circular array of <strong>C+1<\/strong> buckets holds the whole frontier. Scanning buckets in order and relaxing edges is <strong>O(V\u00b7C + E)<\/strong> in the form that walks empty buckets up to the largest finite distance. The heap has disappeared, and C has taken its place in the bound.<\/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\">A binary heap is a general priority queue. Integer distances need only a list of vertices sitting at each exact distance. The next vertex to settle is the smallest nonempty bucket. If weights lie in 0..C, once distance d is being settled the only new distances are in d..d+C, so buckets can wrap modulo C+1.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The cost moves from the logarithm of the heap to the number of buckets the scan walks. A large C makes that walk more expensive than a heap. A tiny C makes it cheaper.<\/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\">Correctness is Dijkstra&#8217;s correctness restricted to nonnegative integer weights. Each vertex is settled once, when its bucket is processed. Time is O(E + V\u00b7C) for the classic implementation whose outer scan advances a distance cursor up to the maximum finite distance, which is at most (V\u22121)\u00b7C. The circular form uses O(C) buckets plus the vertex records, rather than a bucket for every possible distance in advance. Extra memory beyond the graph is \u0398(V+C).<\/p>\n\n\n<p class=\"wp-block-paragraph\">When C = 1 the structure is 0-1 BFS and the bound is linear. When C is larger than a small constant and E is moderate, a binary heap&#8217;s O((V+E) log V) is the better engineering choice. Dial does not accept a non-integer weight, because there is no bucket for it.<\/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 dist[source] = 0. Place the source in bucket 0. The circular bucket array has length C+1.<\/li>\n<li>Advance a cursor from distance 0 upward until every vertex is settled or the remaining vertices are unreachable. The cursor may skip empty buckets.<\/li>\n<li>Settle each vertex in the bucket of the current distance, and do not resettle a vertex whose distance was already final.<\/li>\n<li>An edge of weight w updates the head&#8217;s distance and moves that vertex to the bucket (dist mod (C+1)).<\/li>\n<li>Stop when a full cycle of C+1 empty steps proves that no larger finite distance is waiting. Unreached vertices stay at infinity.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The bound trades the heap logarithm for a factor C. It is attractive for very small integer C and unattractive when C is in the thousands and a heap would have touched far fewer entries. It is still Dijkstra, so a negative weight is still illegal.<\/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 C is not part of the cost<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Allocate a bucket per possible distance up to (V\u22121)\u00b7C with no circular reuse<\/td>\n<td>The result can be correct and the memory is huge. The C+1 circular array is the form that matches the proof&#8217;s memory.<\/td>\n<\/tr>\n<tr>\n<td>Use the algorithm for floating-point weights<\/td>\n<td>Buckets are integer distances. A float has no finite set of residues modulo C+1.<\/td>\n<\/tr>\n<tr>\n<td>Ignore empty-bucket scans in the complexity<\/td>\n<td>The cursor may walk C steps per vertex in the worst case. That term is why a large C loses to a heap.<\/td>\n<\/tr>\n<tr>\n<td>Apply it when weights exceed C and hope the modulo wraps correctly<\/td>\n<td>A wrap collision puts two different distances in one bucket. The settle order is then wrong.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Weights in {0, 1} belong to 0-1 BFS, which is this idea with a deque instead of a cursor. Arbitrary nonnegative weights belong to a binary-heap Dijkstra.<\/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 buckets integer distances in a circular array of width C+1. Edge weights are 0, 1, or 2. It prints the same distances a shortest-path computation would, <code>0 1 0 3<\/code>.<\/p>\n\n<pre><code class=\"language-python\">from collections import deque\nadj = {0: [(1, 1), (2, 0)], 1: [(3, 2)], 2: [(3, 3)], 3: []}\nC = 3\nn = 4\ndist = [10**9] * n\ndist[0] = 0\nbuckets = [deque() for _ in range(C + 1)]\nbuckets[0].append(0)\npending = 1\nwhile pending:\n    for _ in range(C + 1):\n        while buckets[0]:\n            u = buckets[0].popleft()\n            pending -= 1\n            for v, w in adj[u]:\n                nd = dist[u] + w\n                if nd &lt; dist[v]:\n                    dist[v] = nd\n                    buckets[w].append(v)\n                    pending += 1\n        buckets = buckets[1:] + buckets[:1]\nassert dist == [0, 1, 0, 3]\nprint(*dist)\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\">Dial&#8217;s algorithm is the bucket queue for shortest paths whose edge weights are small integers, such as a grid of hop costs. <strong>0-1 BFS<\/strong> is the two-bucket special case. Dijkstra&#8217;s binary heap is the algorithm once the weights are arbitrary nonnegative numbers.<\/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 Dial&#8217;s algorithm when C is not small. A binary heap is simpler and usually faster. Keep it for nonnegative integers bounded by a small C, where walking buckets beats heap traffic.<\/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 Dial&#8217;s algorithm returns the circular C+1 buckets and the V\u00b7C term in the running time. <strong>Dial&#8217;s algorithm implements Dijkstra for integer weights up to C with C+1 distance buckets, in O(V\u00b7C + E) time.<\/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: Dial&#8217;s algorithm is Dijkstra for nonnegative integer weights whose maximum is C. It keeps a bucket for each possible distance. Because an edge increases distance by at most C, a circular array of C+1 buckets holds the whole frontier. Scanning buckets in order and relaxing edges is&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-165","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\/165","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=165"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/165\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}