Skip to main content
ProductsDesktop toolsTASTECADDrawing reviewTASTEFILESFile managementTASTEFTPFile transferTASTESSHRemote workspaceTASTEZIPCompression and extractionTASTEWINMFile managementDeveloper toolsTASTECODERust integrated development environmentTASTESTUDIOIntegrated development and test environmentTASTEETLData integration (ETL)ServersTASTEREMOTERemote desktop serverTASTEGEOGeospatial serverTASTEMAILMail workspaceInvestingTASTESTOCKStock auto-trading desktop appGet startedDownloadsDocumentationBuyAll products
Marketplace
News
TechNewsToday's dev newsDeveloper docsProgramming languagesRustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngularServer frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · ExpressApp & desktop frameworksTauriFlutterReact NativeElectronAlgorithmsAlgorithm basicsCore algorithmsAdvanced algorithmsPractical algorithms
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcaseCommunity guide
Work with usTech transferWebsite buildDesktop app buildRelease and payment pipelineSales partnerContact
ServicesRemote desktopCustom installer packages
TASTEDEV
Products

Desktop tools

TASTECADComplex drawings, opened with easeTASTEFILESJust the files you need, as fast as possibleTASTEFTPThe tidiest path for your filesTASTESSHA stronger link between you and your serversTASTEZIPPack faster, unpack saferTASTEWINMA keyboard-first file manager with two panes

Developer tools

TASTECODEEdit, build, debug and test Rust apps in one windowTASTESTUDIOFrom editing to remote tests and approved AI fixesTASTEETLMove data while you watch the values

Servers

TASTEREMOTEA remote desktop you run on your own serverTASTEGEORebuilding a GeoServer-compatible geospatial server in RustTASTEMAILTake back control of your email

Investing

TASTESTOCKStock auto-trading that follows the rules you set: when, and what

Get started

DownloadsLatest builds of every productDocsHow to use each productBuySubscriptions and perpetual licencesAll productsEvery product at a glance
All products
MarketplaceNews
Tech

News

Today's dev newsOfficial announcements on languages, frameworks, GitHub and AIDeveloper docsGuides to languages, frameworks and algorithms

Programming languages

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

Frameworks

Web front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngular
Server frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
App & desktop frameworksTauriFlutterReact NativeElectron

Algorithms

Algorithm basicsComplexity analysis · Sorting · Searching · Basic data structures · Trees and heapsCore algorithmsGraph traversal · Shortest paths & minimum spanning trees · Dynamic programming · Greedy algorithms · Divide and conquer · BacktrackingAdvanced algorithmsString algorithms · Math and number theory · Bit manipulation · Segment trees · Union-findPractical algorithmsCoding interview strategy · LRU cache · Rate limiting · Consistent hashing
All development docs
Community
ProjectsDiscover builders and their workDevelopment logsDecisions and lessons learnedPromptsReusable instructionsAI tool issuesProblems and workarounds for Claude, Codex, GeminiShowcaseFinished resultsCommunity guideHow we build in the open
Work with us
Tech transferNative apps in Rust and eguiWebsite build10 languages, accounts, docs and downloadsDesktop app buildRust and egui, no WebViewRelease and payment pipelineSigning, MSI, deb, rpm, AppImage, hashesSales partnerRecommend us and earn 30%ContactBuilds, partnerships and investment
Services
Remote desktopReach your computer from a browserCustom installer packagesOnly the features you need, settings preloaded
Buy
EN
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
Log in
Log in
TASTEDEV

From small tools to better development.

support@tastedev.netSupport TASTEDEV
ProductsProduct lineupBuyDocsDownloadsMarketplaceSupport
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcase
PoliciesAbout usPrivacy policyTerms of serviceSubscription termsRefund policyCommunity guidelines
AccountLog inSign up
Visitors
Today
Yesterday
Total views
Company
GXSOFT
Representative
CHOI YOUNG GU
Business registration number
853-44-00368
Address
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
Phone
+82-10-4404-1523
Email
whitecalvin@tastedev.net
Privacy officer
CHOI YOUNG GU

© 2026 TASTEDEVProduct information is based on verified development sources.Released · improving

Shortest paths & minimum spanning trees docs
Shortest paths & minimum spanning trees
ConceptsHow it worksImplementationComplexityPractice problemsReal-world use
Discussions

Shortest paths & minimum spanning trees guide · 2/6

How it works

This chapter traces each algorithm on a small graph so you can see the order of relaxations. The Dijkstra example uses directed edges A→B 4, A→C 1, C→B 2, B→D 1, C→D 5, D→E 3 with source A.

Dijkstra step by step

Dijkstra keeps a min-heap of (distance, vertex) pairs. It repeatedly pops the closest vertex; because all weights are non-negative, no later path can make that vertex any cheaper, so its distance is final. It then relaxes the vertex's outgoing edges. When a distance improves we simply push a new pair instead of updating the old one; outdated pairs are skipped when popped ("lazy deletion").

PopABCDENote
start0infinfinfinfheap: (0,A)
(0,A)041infinfpush (4,B), (1,C)
(1,C)0316infB improves 4 to 3
(3,B)0314infD improves 6 to 4
(4,B)0314infstale, skipped
(4,D)03147push (7,E)
(6,D)03147stale, skipped
(7,E)03147heap empty

Final distances: A 0, B 3, C 1, D 4, E 7. The shortest path to E is A→C→B→D→E.

Why negative edges break Dijkstra

Take A→B 2, A→C 3, C→B -2. Dijkstra settles B at distance 2 before it looks at C, but the true distance is 3 + (-2) = 1. The greedy "closest is final" argument assumes that extending a path never makes it shorter, which is false with negative weights. Use Bellman-Ford instead.

Bellman-Ford

Bellman-Ford relaxes every edge, V - 1 times. A shortest path without cycles has at most V - 1 edges, and after round k every path of k edges has been accounted for. One extra round then acts as a test: if any edge can still be relaxed, a negative cycle is reachable from the source.

from math import inf

def bellman_ford(n, edges, source):
    dist = [inf] * n
    dist[source] = 0
    for _ in range(n - 1):
        changed = False
        for u, v, w in edges:
            if dist[u] != inf and dist[u] + w < dist[v]:
                dist[v] = dist[u] + w
                changed = True
        if not changed:          # early exit: nothing moved this round
            break
    for u, v, w in edges:
        if dist[u] != inf and dist[u] + w < dist[v]:
            raise ValueError("negative cycle reachable from source")
    return dist

# S=0, A=1, B=2, C=3
print(bellman_ford(4, [(0, 1, 4), (0, 2, 5), (2, 1, -3), (1, 3, 2)], 0))  # [0, 2, 5, 4]

In round 1 the edges are scanned in the listed order: A becomes 4, B becomes 5, then B→A lowers A to 2, and A→C sets C to 4. Round 2 changes nothing, so the loop stops early.

Floyd-Warshall

Floyd-Warshall computes all pairs at once with a three-level loop. After processing intermediate vertex k, d[i][j] is the shortest path from i to j that uses only vertices 0..k in between. The order of the loops matters: k must be the outermost.

from math import inf

def floyd_warshall(d):
    n = len(d)
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    d[i][j] = d[i][k] + d[k][j]
    return d

d = [[0, 3, inf], [inf, 0, 1], [2, inf, 0]]
print(floyd_warshall(d))  # [[0, 3, 4], [3, 0, 1], [2, 5, 0]]

After the run, a negative value on the diagonal (d[i][i] < 0) means vertex i lies on a negative cycle.

0-1 BFS

When every weight is 0 or 1, a deque replaces the heap. A vertex reached by a 0-edge goes to the front, by a 1-edge to the back, so the deque always stays sorted by distance and each step is O(1).

from collections import deque
from math import inf

def zero_one_bfs(adj, source):
    dist = [inf] * len(adj)
    dist[source] = 0
    dq = deque([source])
    while dq:
        u = dq.popleft()
        for v, w in adj[u]:
            if dist[u] + w < dist[v]:
                dist[v] = dist[u] + w
                if w == 0:
                    dq.appendleft(v)
                else:
                    dq.append(v)
    return dist

print(zero_one_bfs([[(1, 1), (2, 0)], [(3, 1)], [(1, 0), (3, 1)], []], 0))  # [0, 0, 0, 1]

Kruskal and Prim on one graph

Undirected edges: A-B 1, B-C 2, A-C 3, C-D 4, B-D 5.

Kruskal (sorted edges)DecisionTree weight
A-B 1take, joins two components1
B-C 2take3
A-C 3skip, A and C already connected3
C-D 4take, now V - 1 = 3 edges7

Prim from A pops the cheapest edge that leaves the tree: A-B 1, then B-C 2 (cheaper than A-C 3), then C-D 4 (cheaper than B-D 5). Both reach the same tree of weight 7. They can pick different trees only when equal weights allow several MSTs, and the total is always the same.

Summary

  • Dijkstra settles the closest vertex each time and skips stale heap entries.
  • Bellman-Ford needs V - 1 rounds; a change in an extra round means a negative cycle.
  • Floyd-Warshall loops over intermediate vertices k, outermost.
  • 0-1 BFS keeps the deque ordered by pushing 0-edges to the front.
  • Kruskal works edge by edge, Prim grows a single tree; both are greedy and both are correct.
Questions about this chapter?
Previous chapterConceptsNext chapterImplementation

0 comments

Top commentsNewest first

Sign in · Sign in to leave a comment.