graph-traversal-patterns

Selects and implements BFS, DFS, topological sort, and union-find for graph-shaped problems.

1|Updated Jul 3, 2026
One-click install
npx skills add https://github.com/Nandansai08/skillz --skill graph-traversal-patterns-nandansai08
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: graph-traversal-patterns
Source: https://github.com/Nandansai08/skillz/tree/main/skills/dsa-algorithms/graph-traversal-patterns
Command: npx skills add https://github.com/Nandansai08/skillz --skill graph-traversal-patterns-nandansai08

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Many coding problems are secretly graph problems — grids, dependency lists, state spaces — and choosing the wrong traversal (like DFS for shortest paths) produces solutions that pass small tests but fail in production. This Skill provides the modeling step and the correct algorithm selection for reachability, shortest paths, dependency ordering, and connected components. ## Core Features & Use Cases - Algorithm selection by question type: Maps the problem's question to the right algorithm — BFS for unweighted shortest paths, DFS for reachability and backtracking, Kahn's topological sort for dependency ordering, union-find for dynamic grouping. - Bug-proof templates: Provides BFS with enqueue-time marking, three-color directed cycle detection, Kahn's with free cycle detection via leftover nodes, and union-find with path compression plus union by size. - Use Case: Given 40,000 microservice dependency edges, produce a deploy order and, if impossible, name the offending services — Kahn's algorithm orders the acyclic portion and exposes the exact services forming the cycle. ## Quick Start Use the graph-traversal-patterns skill to find the minimum number of moves between two states in this puzzle and detect any cycles in my dependency list.

Frequently Asked Questions about graph-traversal-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose between BFS and DFS for shortest path problems?▼

Use BFS for unweighted shortest paths with no exceptions, since it explores nodes in order of distance from the source. DFS finds the first path it encounters, which passes small tests but is not guaranteed to be the shortest.

How to detect a cycle in a directed graph in Python?▼

Use three-color DFS (white/gray/black): a gray-to-gray edge indicates a cycle. A two-state visited set gives false positives on diamond shapes where two paths converge on the same node without forming a cycle.

When should I use union-find instead of BFS for connectivity?▼

Use union-find when connectivity queries interleave with incremental edge additions, since path compression and union by size make each operation effectively O(1). Re-running BFS per query degrades to accidental O(VE) on streaming merges.

Why does my BFS run exponentially slow on dense graphs?▼

The cause is marking nodes as visited when popped instead of when enqueued. Mark-on-pop lets the same node enter the queue once per incoming edge, causing exponential queue growth; mark nodes at enqueue time.

Can BFS solve weighted shortest path problems?▼

No, BFS only handles unweighted graphs. For weighted edges, escalate to Dijkstra for positive weights, 0-1 BFS for 0/1 weights, or Bellman-Ford when negative weights are present.

How do I find which nodes form a cycle with topological sort?▼

Run Kahn's algorithm and check the leftover nodes with indegree greater than zero after processing — they contain the cycle. This also gives a loud failure instead of the plausible wrong order that DFS post-order toposort produces on cyclic input.