本文へスキップ
製品デスクトップツールTASTECAD図面の確認TASTEFILESファイル管理TASTEFTPファイル転送TASTESSHリモートワークスペースTASTEZIP圧縮と展開TASTEWINMファイル管理開発ツールTASTECODERust 統合開発環境TASTESTUDIO統合開発・テスト環境TASTEETLデータ統合(ETL)サーバーTASTEREMOTEリモートデスクトップサーバーTASTEGEO空間情報サーバーTASTEMAILメールワークスペース投資TASTESTOCK株式自動売買デスクトップアプリはじめるダウンロードドキュメント購入すべての製品
マーケットプレイス
ニュース
技術ニュース今日の開発ニュース開発ドキュメントプログラミング言語RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWebフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngularサーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Expressアプリ・デスクトップ フレームワークTauriFlutterReact NativeElectronアルゴリズムアルゴリズムの基礎主要アルゴリズム発展アルゴリズム実務のアルゴリズム
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケースコミュニティガイド
協業技術移転ウェブサイト構築デスクトップアプリ構築リリース・決済パイプライン販売パートナーお問い合わせ
サービスリモートデスクトップインストールパッケージのカスタマイズ
TASTEDEV
製品

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらくTASTEWINM2つの画面で整理する、キーボード中心のファイルマネージャー

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

投資

TASTESTOCKいつ・何を。自分で決めたルールどおりに動く株式自動売買

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケース
ポリシー会社概要プライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
昨日
累計閲覧
商号
GXSOFT
代表者
CHOI YOUNG GU
事業者登録番号
853-44-00368
所在地
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
メール
whitecalvin@tastedev.net
個人情報保護責任者
CHOI YOUNG GU

© 2026 TASTEDEV製品情報は検証済みの開発ソースに基づいています。リリース・改善中

最短経路と最小全域木 ドキュメント
最短経路と最小全域木
ConceptsHow it worksImplementationComplexityPractice problemsReal-world use
ディスカッション

最短経路と最小全域木 ガイド · 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.

この章について質問がありますか?
前の章Concepts次の章Implementation
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.

コメント 0件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。