Paths & Cycles

Eulerian paths, Hamiltonian cycles, and shortest paths

Paths & Cycles

A path is a sequence of vertices connected by edges, while a cycle is a path that returns to its starting vertex. These concepts are fundamental to graph theory and lead to fascinating questions about traversing graphs efficiently.

In this section, you'll learn the difference between Eulerian and Hamiltonian paths, and discover Dijkstra's famous algorithm for finding shortest paths in weighted graphs!

What You'll Learn

🔄
Eulerian Paths
Visit every edge exactly once
🚶
Hamiltonian Paths
Visit every vertex exactly once
📍
Shortest Paths
Dijkstra's algorithm for weighted graphs

Eulerian Path Finder

An Eulerian path visits every edge exactly once. Click vertices to add/remove edges and see when a path exists. Red vertices have odd degree!

Click two vertices to toggle an edge

4
Edges
0
Odd Degree
Yes
Connected
Eulerian Circuit Exists!
All vertices have even degree — can return to start

Eulerian Path Conditions

  • Circuit: All vertices must have even degree (returns to start)
  • Path: Exactly 0 or 2 vertices with odd degree
  • The graph must be connected

Hamiltonian Path Explorer

A Hamiltonian path visits every vertex exactly once. Click vertices to try building one yourself, or watch the backtracking algorithm search!

Click vertices to build a path visiting each exactly once

0 / 5
Vertices Visited
0
Backtracks
Yes
Has Path?

Hamiltonian vs Eulerian

Eulerian: Visit every edge once
Easy to check (count odd degrees)
Hamiltonian: Visit every vertex once
NP-complete (no efficient algorithm!)

Dijkstra's Shortest Path

Dijkstra's algorithm finds the shortest path between two vertices in a weighted graph. Watch it explore outward from the source, always choosing the closest unvisited vertex!

Source
Target
Current
Visited

Dijkstra's Algorithm

  1. Set source distance to 0, all others to ∞
  2. Pick unvisited vertex with smallest distance
  3. Update distances to its neighbors: d[v] = min(d[v], d[u] + weight)
  4. Mark current vertex as visited
  5. Repeat until target is visited or all reachable vertices done

Time complexity: O(V² ) or O((V + E) log V) with priority queue

What We've Learned

1. Königsberg Bridges: Euler proved in 1736 that crossing all 7 bridges exactly once is impossible. This problem founded graph theory and introduced the concept of vertex degree.

2. Eulerian Paths: A path that visits every edge exactly once. Exists only when the graph has 0 or 2 vertices of odd degree. An Eulerian circuit (returns to start) requires all vertices to have even degree.

3. Hamiltonian Paths: A path that visits every vertex exactly once. Unlike Eulerian paths, there's no simple criterion to check—the problem is NP-complete! We must use backtracking or other search algorithms.

4. Dijkstra's Algorithm: A greedy algorithm that finds shortest paths in weighted graphs. It maintains distances to all vertices and always processes the closest unvisited vertex next.

Next Up: In the next section, you'll learn about graph coloring — including chromatic numbers, map coloring, and the famous four-color theorem!