Paths & Cycles

Eulerian paths, Hamiltonian cycles, and shortest paths

Paths and 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 deep questions about traversing graphs efficiently.

Eulerian paths traverse every edge exactly once, and their existence can be checked with a simple degree condition. Hamiltonian paths visit every vertex exactly once, but deciding their existence is NP-complete. Dijkstra's algorithm finds shortest paths in weighted graphs using a greedy expansion strategy.

Eulerian Path Finder

An Eulerian path visits every edge exactly once. Toggle edges by clicking pairs of vertices and observe how the odd-degree count determines whether a path or circuit exists. Red vertices indicate 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

Key insight: An Eulerian circuit exists when all vertices have even degree. An Eulerian path exists when exactly two vertices have odd degree -- those two vertices become the start and end of the path.

Hamiltonian Path Explorer

A Hamiltonian path visits every vertex exactly once. Try building one manually by clicking vertices, or watch the backtracking algorithm systematically explore all possibilities. Unlike Eulerian paths, no simple existence criterion is known.

Click vertices to build a path visiting each exactly once

0 / 5
Vertices Visited
0
Backtracks
Yes
Has Path?

Key insight: Deciding whether a Hamiltonian path exists is NP-complete. The backtracking algorithm must potentially explore an exponential number of orderings, making the contrast with the efficiently-solvable Eulerian case especially striking.

Dijkstra's Shortest Path

Dijkstra's algorithm finds the shortest path between two vertices in a weighted graph. It greedily expands from the source, always processing the closest unvisited vertex. Select source and target vertices, then run the algorithm to watch it explore.

Source
Target
Current
Visited

Key insight: Dijkstra's algorithm runs in O(V²) time with a simple array, or O((V + E) log V) with a priority queue. It requires all edge weights to be non-negative.

Key Takeaways

  • Eulerian paths -- visit every edge exactly once; existence depends on the count of odd-degree vertices (0 for circuit, 2 for path)
  • Hamiltonian paths -- visit every vertex exactly once; existence is NP-complete with no known efficient criterion
  • Dijkstra's algorithm -- finds shortest paths in weighted graphs by greedily expanding from the source vertex
  • Complexity contrast -- Eulerian path existence is O(V) to check, while Hamiltonian path existence requires exponential search in the worst case