一键导入
algo-graph-traversal
Pick the right traversal — BFS for unweighted shortest path, Dijkstra for weighted, A* for goal-directed, 0/1-BFS for binary weights.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Pick the right traversal — BFS for unweighted shortest path, Dijkstra for weighted, A* for goal-directed, 0/1-BFS for binary weights.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | algo-graph-traversal |
| description | Pick the right traversal — BFS for unweighted shortest path, Dijkstra for weighted, A* for goal-directed, 0/1-BFS for binary weights. |
| when-to-use | Shortest path, reachability, components, MST, A* on grids and weighted graphs. |
Choosing the wrong traversal is the single biggest source of wrong answers on graph problems. The decision tree below is short on purpose.
| input shape | algorithm | time |
|---|---|---|
| unweighted | BFS | O(V + E) |
edge weights {0, 1} | 0/1-BFS (deque) | O(V + E) |
| non-negative weights | Dijkstra (heap) | O((V + E) log V) |
| any real weights, no neg cycles | Bellman-Ford | O(V E) |
| all-pairs | Floyd-Warshall | O(V^3) |
| heuristic + goal node | A* | depends on h |
| MST | Kruskal / Prim | O(E log V) |
if d > dist[u]: continue).V - 1 relaxation rounds, all distances
are final unless a negative cycle exists.O(V + E) time, O(V) extra space.O((V + E) log V) time, O(V) extra space.O(V · E) time, O(V) space — slower but handles
negative weights and detects negative cycles.O(V³) time, O(V²) space — only viable when
V ≤ ~400.O(|path|) with a tight heuristic, worst-case the
same as Dijkstra. Memory dominates on huge state spaces.import heapq
def dijkstra(adj: dict[int, list[tuple[int, int]]], src: int) -> dict[int, int]:
"""Shortest distances from src on a non-negatively weighted graph."""
dist: dict[int, int] = {src: 0}
pq: list[tuple[int, int]] = [(0, src)]
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]:
continue # stale entry
for v, w in adj.get(u, ()):
nd = d + w
if nd < dist.get(v, float("inf")):
dist[v] = nd
heapq.heappush(pq, (nd, v))
return dist
// Edges have weight 0 or 1. Push 0-edges to the front, 1-edges to the back.
function zeroOneBFS(adj, src) {
const dist = new Map([[src, 0]]);
const dq = [src];
while (dq.length) {
const u = dq.shift();
for (const [v, w] of adj.get(u) || []) {
const nd = dist.get(u) + w;
if (nd < (dist.get(v) ?? Infinity)) {
dist.set(v, nd);
if (w === 0) dq.unshift(v); else dq.push(v);
}
}
}
return dist;
}
if d > dist[u]: continue, the heap can grow to O(E) and runtime degrades.inf distance.E = V^2) — Dijkstra heap version vs O(V^2) array
version: the array version is faster on dense graphs.When in doubt, draw the graph by hand for V <= 6, run the chosen
algorithm on paper, and compare with the brute-force answer. The
algorithm is wrong nine times out of ten on small examples before
it's wrong on large ones.
Breadth-first search — shortest path on unweighted graphs, level-order traversal, and the `visited` discipline that prevents O(2^n) blowups.
Binary search invariants, half-open intervals, and the `lo<hi` template that beats off-by-one bugs.
Depth-first search — recursive vs iterative, recursion-depth gotchas, three-color cycle detection, topological sort.
Dynamic programming — state design, memoization vs tabulation, dimension-reduction, and when DP is the wrong tool.
Greedy algorithms — exchange-argument proofs, when greedy beats DP, classic patterns (interval scheduling, Huffman, scheduling).
Hash maps and sets — when O(1) lookup pays off, hash collisions, frozen keys, and hashing custom types.