codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

52 / 65 topics
49Graph Coloring50Topological Sorting51Strongly Connected Components52Eulerian Cycles and Paths53Hamiltonian Cycles
Tutorials/Data Structures & Algorithms/Eulerian Cycles and Paths
🧮Data Structures & Algorithms

Eulerian Cycles and Paths

Updated 2026-04-20
3 min read

Eulerian Cycles and Paths

Introduction

In graph theory, an Eulerian Path is a path in a graph that visits every edge exactly once. An Eulerian Cycle, also known as an Euler circuit or Euler tour, is an Eulerian path that starts and ends at the same vertex. These concepts are named after Leonhard Euler, who first discussed them in his 1736 paper on the Seven Bridges of Königsberg problem.

Understanding Eulerian cycles and paths is crucial for various applications, including network design, routing problems, and even puzzles like mazes. In this tutorial, we will explore the definitions, properties, algorithms to detect them, and real-world applications of Eulerian cycles and paths.

Definitions

  • Eulerian Path: A path in a graph that visits every edge exactly once.
  • Eulerian Cycle: An Eulerian path that starts and ends at the same vertex.

Key Properties

  1. Connected Graph: Both Eulerian paths and cycles require the graph to be connected, meaning there is a path between any two vertices.
  2. Degree of Vertices:
    • For an Eulerian Path: Exactly 0 or 2 vertices have odd degrees (the start and end points).
    • For an Eulerian Cycle: All vertices must have even degrees.

Algorithms

Fleury's Algorithm

Fleury's algorithm is a method to find an Eulerian path in a graph. It was one of the first algorithms proposed for this problem and is based on the following steps:

  1. Choose Start Vertex: If there are exactly two vertices with odd degree, start at one of them. Otherwise, start at any vertex.
  2. Traverse Edges:
    • Choose an edge to traverse such that it does not disconnect the graph unless there is no other choice.
    • Remove the chosen edge from the graph.
  3. Repeat: Continue until all edges are traversed.

Implementation

Here's a Python implementation of Fleury's algorithm:

def fleurys_algorithm(graph):
    def dfs(u, path):
        for v in range(len(graph)):
            if graph[u][v] == 1:
                graph[u][v] = 0
                graph[v][u] = 0
                dfs(v, path)
        path.append(u)

    # Find a vertex with an odd degree to start the Eulerian Path
    start_vertex = None
    for i in range(len(graph)):
        if sum(graph[i]) % 2 != 0:
            start_vertex = i
            break

    if start_vertex is None:
        start_vertex = 0

    path = []
    dfs(start_vertex, path)
    return path[::-1]

# Example usage
graph = [
    [0, 1, 0, 0],
    [1, 0, 1, 1],
    [0, 1, 0, 1],
    [0, 1, 1, 0]
]

eulerian_path = fleurys_algorithm(graph)
print("Eulerian Path:", eulerian_path)

Hierholzer's Algorithm

Hierholzer's algorithm is a more efficient method to find an Eulerian cycle. It uses depth-first search (DFS) and runs in O(E + V) time, where E is the number of edges and V is the number of vertices.

Implementation

Here's a Python implementation of Hierholzer's algorithm:

def hierholzers_algorithm(graph):
    def dfs(u, path):
        while graph[u]:
            v = graph[u].pop()
            dfs(v, path)
        path.append(u)

    # Find an Eulerian Cycle starting from any vertex
    start_vertex = 0
    path = []
    dfs(start_vertex, path)
    return path[::-1]

# Example usage
graph = {
    0: [1],
    1: [2, 3],
    2: [1, 3],
    3: [1, 2]
}

eulerian_cycle = hierholzers_algorithm(graph)
print("Eulerian Cycle:", eulerian_cycle)

Best Practices

  • Graph Representation: Use an adjacency list for efficient edge traversal in Hierholzer's algorithm.
  • Edge Removal: In Fleury's algorithm, ensure that removing an edge does not disconnect the graph unless necessary.
  • Complexity Considerations: Hierholzer's algorithm is preferred for its linear time complexity compared to the more complex logic of Fleury's algorithm.

Real-World Applications

  1. Network Design: Eulerian cycles are used in designing efficient network routes, such as postal delivery routes or garbage collection.
  2. Routing Problems: In computer networks, finding an Eulerian path can help optimize data routing paths.
  3. Puzzles and Games: Puzzles like mazes often involve finding Eulerian paths to solve them.

Conclusion

Eulerian cycles and paths are fundamental concepts in graph theory with wide-ranging applications. Understanding how to detect and utilize these structures can lead to more efficient solutions for various problems in computer science and beyond. By mastering the algorithms discussed here, you'll be well-equipped to tackle complex network design and routing challenges.


PreviousStrongly Connected ComponentsNext Hamiltonian Cycles

Recommended Gear

Strongly Connected ComponentsHamiltonian Cycles