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)可视化工具
在方格网格上交互式演示深度优先搜索——绘制墙壁、拖动起点/终点、生成迷宫、逐步查看深入探索的过程。直接在浏览器中运行。
打开工具Dijkstra 算法可视化工具
在网格上可视化 Dijkstra 寻路算法——绘制墙壁、拖动起点/终点、生成迷宫,并逐步查看搜索过程。直接在浏览器中运行。
打开工具A* 寻路算法可视化工具
在方格网格上交互式演示 A* 寻路算法,使用曼哈顿距离启发式函数——绘制墙壁障碍、拖动起点/终点、生成迷宫、逐步执行搜索过程。完全在浏览器中运行。
打开工具迷宫生成器
使用递归分割(recursive division)算法逐步生成迷宫的动画工具——实时查看墙壁的构建过程、调整生成速度、随时生成新迷宫。与寻路算法可视化工具搭配使用效果极佳。完全在浏览器中运行。
打开工具Prefer a focused tool?
- Graph & Pathfinding → Watch graphs get traversed and shortest paths get found
常见问题
递归分割算法是如何生成迷宫的?
它从一个空白区域开始,然后递归地用一堵带有随机缺口的直墙分割每个空间,如此反复,直到空间小到无法继续分割。
什么是「完美迷宫」?
指任意两个格子之间有且仅有一条路径的迷宫——没有环路,也没有孤立区域。递归分割算法生成的始终是完美迷宫。
我可以求解这个迷宫吗?
可以——把这个布局复制到 BFS、Dijkstra 或 A* 可视化工具中(它们使用相同的方格网格),即可查看寻路算法如何穿过它。
还有哪些其他的迷宫生成算法?
递归回溯(随机化 DFS)、Prim 算法、Kruskal 算法、Wilson 算法和 Eller 算法——每种算法生成的迷宫在视觉结构上都各具特色。