跳到主要内容
Z Graph & Pathfinding

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

Prefer a focused tool?

常见问题

递归分割算法是如何生成迷宫的?

它从一个空白区域开始,然后递归地用一堵带有随机缺口的直墙分割每个空间,如此反复,直到空间小到无法继续分割。

什么是「完美迷宫」?

指任意两个格子之间有且仅有一条路径的迷宫——没有环路,也没有孤立区域。递归分割算法生成的始终是完美迷宫。

我可以求解这个迷宫吗?

可以——把这个布局复制到 BFS、Dijkstra 或 A* 可视化工具中(它们使用相同的方格网格),即可查看寻路算法如何穿过它。

还有哪些其他的迷宫生成算法?

递归回溯(随机化 DFS)、Prim 算法、Kruskal 算法、Wilson 算法和 Eller 算法——每种算法生成的迷宫在视觉结构上都各具特色。