StudyHub

Algorithms · Hub

Graphs

Eleven pages on graphs: how to store them, how to walk them (BFS, DFS), how to find shortest paths (Dijkstra, Bellman-Ford, Floyd-Warshall), how to connect everything cheaply (Kruskal, Prim), and how to order and check them (topological sort, cycle detection, Union-Find).

Why it matters: roads, networks, dependencies and social links are all graphs. Knowing which algorithm fits which question, and its precondition, is most of the work.

5 random questions mixed from all eleven graph pages.

Quick comparison

AlgorithmSolvesTimePrecondition
BFSTraversal, shortest path in edges$O(V + E)$Unweighted for shortest paths
DFSTraversal, components, order$O(V + E)$None
DijkstraSingle-source shortest paths$O((V + E) \log V)$Non-negative weights
Bellman-FordSingle-source shortest paths$O(VE)$No reachable negative cycle
Floyd-WarshallAll-pairs shortest paths$O(V^3)$No negative cycle
KruskalMinimum spanning tree$O(E \log E)$Undirected, connected
PrimMinimum spanning tree$O(V^2)$ or $O(E \log V)$Undirected, connected
Topological sortDependency order$O(V + E)$Directed acyclic graph
Cycle detectionIs there a cycle?$O(V + E)$None
Union-FindSame group? Merge groups$O(\alpha(n))$ per operationMerges only

The eleven graph pages

Graph representations

Adjacency matrix versus adjacency lists.

O(V²) vs O(V + E) memory · O(1) vs O(deg) edge check

Study

Breadth-first search

Explore in layers with a queue.

O(V + E) · shortest paths in edges

Study

Depth-first search

Dive deep, backtrack at dead ends.

O(V + E) · stack / recursion

Study

Dijkstra

Settle the closest vertex, relax its edges.

O((V + E) log V) · weights ≥ 0

Study

Bellman-Ford

Relax every edge V − 1 times.

O(VE) · negative edges OK · finds negative cycles

Study

Floyd-Warshall

Allow each vertex as a stop-over.

O(V³) · all pairs

Study

Kruskal

Cheapest edges first, skip cycles.

O(E log E) · minimum spanning tree

Study

Prim

Grow one tree by its cheapest edge.

O(V²) or O(E log V) · minimum spanning tree

Study

Topological sort

Order tasks so every arrow points forward.

O(V + E) · DAG only

Study

Cycle detection

An edge to a grey vertex closes a loop.

O(V + E) · directed or undirected

Study

Union-Find

Merge groups, ask "same group?".

O(α(n)) per operation · nearly constant

Study

Which one to pick?

flowchart TD
  A{"What do you need?"} -->|"visit everything"| T{"Need shortest paths in edges?"}
  T -->|yes| BFS["BFS"]
  T -->|no| DFS["DFS"]
  A -->|"shortest weighted paths"| S{"From one source or all pairs?"}
  S -->|"all pairs"| FW["Floyd-Warshall"]
  S -->|"one source"| N{"Any negative edge?"}
  N -->|no| DJ["Dijkstra"]
  N -->|yes| BF["Bellman-Ford"]
  A -->|"cheapest network connecting all"| M{"Dense graph?"}
  M -->|yes| PR["Prim"]
  M -->|no| KR["Kruskal"]
  A -->|"order tasks with dependencies"| TS["Topological sort"]
  A -->|"is there a loop?"| CD["Cycle detection"]
  A -->|"which items are connected?"| UF["Union-Find"]
      

Key points

External resources

Videos

Review schedule

Come back after 3, 7, 14, then 30 days. Spacing beats cramming.

Feynman prompt

Explain this topic in your own words, as if teaching a beginner.

Explain it in your own words first, then record a 2–3 minute video on your phone.

25:00

Ready to record? Open your phone camera now and record a 2–3 minute explanation.

My notes

Saved only in this browser (localStorage). Nothing is uploaded.