メインコンテンツへスキップ
Z

Graph Algorithms

BFS, DFS, Dijkstra & A* — pathfinding and traversal, visualized

A graph is just a set of nodes connected by edges — it models road networks, social connections, dependency chains and game maps alike. Graph algorithms answer two recurring questions: can I reach this node? and what is the cheapest way to get there?

Traversal: BFS vs DFS

Breadth-first search (BFS) explores a graph layer by layer using a queue, visiting all neighbours at distance 1 before distance 2. On an unweighted graph this finds the shortest path in terms of edge count. Depth-first search (DFS) instead follows one branch as far as it can using a stack (or recursion) before backtracking — ideal for cycle detection, topological sorting and maze generation. Both run in O(V + E) time.

Shortest paths: Dijkstra & A*

When edges carry weights (distances, costs, times), Dijkstra’s algorithm greedily settles the nearest unvisited node, guaranteeing the shortest path from a source to every other node in O((V + E) log V) with a priority queue. A* speeds this up for point-to-point queries by adding a heuristic that biases the search toward the goal — the same core relaxation step, just better-informed.

Where graphs show up

GPS routing, network packet forwarding, social-graph friend suggestions, build-system dependency ordering and game-AI pathfinding are all graph problems underneath. Master the four algorithms below and you have the toolkit for most of them. Drive each one yourself and watch the frontier expand in real time.

Try it interactively

幅優先探索(BFS)ビジュアライザー

グリッド上でインタラクティブに幅優先探索を体験できるツール。壁を描き、スタート地点とゴール地点を自由に動かし、迷路を自動生成しながら、探索が層ごとに広がっていく様子をステップ単位で観察できます。ブラウザだけですぐに動きます。

ツールを開く

深さ優先探索(DFS)ビジュアライザー

グリッド上でインタラクティブに深さ優先探索を体験。壁を描いたり、スタート/ゴール地点を動かしたり、迷路を生成したり、探索の様子をステップごとに確認できます。ブラウザ上ですぐに動作します。

ツールを開く

ダイクストラ法ビジュアライザー

グリッド上でダイクストラ法による経路探索を可視化。壁を描き、始点・終点を動かし、迷路を生成し、探索の進行をステップごとに確認できます。すべてブラウザ上で動作します。

ツールを開く

A*経路探索ビジュアライザー

マンハッタン距離ヒューリスティックを使ったA*経路探索をグリッド上でインタラクティブに可視化。壁を描いてスタート/ゴールを動かし、迷路を自動生成し、探索をステップごとに実行できます。すべてブラウザ内で完結します。

ツールを開く

迷路ジェネレーター

再帰分割法(recursive division)によるアニメーション付き迷路ジェネレーター — 壁が組み上がっていく過程をステップごとに確認し、速度を調整して、新しい迷路を何度でも生成できます。経路探索アルゴリズムのビジュアライザーと組み合わせて使うのに最適です。すべてブラウザ上で完結します。

ツールを開く

Prefer a focused tool?

よくある質問

再帰分割アルゴリズムはどのように迷路を作るのですか?

まず何もない空間から始め、各区画をランダムな位置に隙間を残した1本の直線の壁で再帰的に仕切っていきます。これを区画がそれ以上分割できなくなるまで繰り返します。

「完全な迷路」とは何ですか?

任意の2マス間にちょうどひとつの経路しか存在しない迷路のことです——ループも孤立した領域もありません。再帰分割アルゴリズムは常に完全な迷路を生成します。

この迷路を解くことはできますか?

はい——同じ方眼グリッドを使うBFS、Dijkstra、A*のビジュアライザーにレイアウトをコピーすれば、経路探索アルゴリズムがどのように迷路を進んでいくか確認できます。

他にどんな迷路生成アルゴリズムがありますか?

Recursive backtracker(ランダム化DFS)、Prim法、Kruskal法、Wilson法、Eller法などがあり、それぞれ見た目の異なる構造の迷路が生成されます。