SlideShare a Scribd company logo
3
Most read
4
Most read
11
Most read
FLEURY’S
ALGORITHM
FLEURYS’S ALGORITHM
Purpose
Find an Eulerian Path or Circuit in a graph.
Graph Requirements
• Eulerian Path: Exactly two vertices with odd degrees.
• Eulerian Circuit: All vertices have even degrees.
Steps
• Start at an odd-degree vertex (for a path) or any vertex (for a circuit).
• Traverse edges one at a time, selecting a valid edge.
• Avoid bridges unless they are the only remaining edges at the vertex.
• Remove each edge after traversing it.
FLEURYS’S ALGORITHM
Output
The sequence of edges representing the Eulerian Path or Circuit.
ALGORITHM STEPS
Step 1: Verify the Graph's Suitability: Check vertex degrees to ensure the graph has either two odd-
degree vertices (Eulerian Path) or all even-degree vertices (Eulerian Circuit).
Step 2: Choose a Starting Vertex: Start at one odd-degree vertex (Eulerian Path) or any vertex (Eulerian
Circuit).
Step 3: Select a Valid Edge: At the current vertex, choose an edge that is not a bridge unless it is the
only edge left.
Step 4: Traverse the Edge: Print the edge, remove it from the graph, and move to the next vertex.
Step 5: Repeat: Continue until all edges in the graph are traversed exactly once.
Step 6: Output the Path/Circuit: Print the Eulerian Path or Circuit based on the traversal order.
IMPLEMENTATION
Pyt def __init__(self, vertices):
self.graph = defaultdict(list) #
adjacency list
self.V = vertices # number of vertices
def add_edge(self, u, v):
"""Add an edge to the graph (undirected)."""
self.graph[u].append(v)
self.graph[v].append(u)
def remove_edge(self, u, v):
"""Remove an edge from the graph."""
if v in self.graph[u]:
self.graph[u].remove(v)
if u in self.graph[v]:
self.graph[v].remove(u)
def dfs_count(self, v, visited):
"""Utility function to perform DFS and count reachable
vertices."""
visited[v] = True
count = 1
for neighbor in self.graph[v]:
if not visited[neighbor]:
count += self.dfs_count(neighbor, visited)
return count
IMPLEMENTATION
def is_valid_next_edge(self, u, v):
"""Check if edge u-v is a valid edge to use."""
# Case 1: If u-v is the only edge, it's valid
if len(self.graph[u]) == 1:
return True
# Case 2: If removing u-v leaves the graph connected, it's valid
# Count reachable vertices before removing the edge
visited = [False] * self.V
count_before_removal = self.dfs_count(u, visited)
# Remove the edge and count reachable vertices
self.remove_edge(u, v)
visited = [False] * self.V
count_after_removal = self.dfs_count(u, visited)
# Add the edge back to restore original graph
self.add_edge(u, v)
# If the counts are the same, the edge is not a bridge
return count_before_removal == count_after_removal
def print_euler_util(self, u):
"""Recursive function to print Eulerian path or circuit."""
for v in list(self.graph[u]): # Use list to avoid modification during
iteration
if self.is_valid_next_edge(u, v):
print(f"{u} -> {v}")
self.remove_edge(u, v) # Safely remove the edge
self.print_euler_util(v)
def print_euler(self):
"""Print the Eulerian path or circuit."""
# Find a vertex with an odd degree to start (if exists)
start_vertex = 0
for i in range(self.V):
if len(self.graph[i]) % 2 != 0:
start_vertex = i
break
# Print Eulerian path/circuit starting from start_vertex
print(f"Eulerian path/circuit starting from vertex {start_vertex}:")
self.print_euler_util(start_vertex)
Key Concepts
1.Eulerian Path: A path that visits every edge of the
graph exactly once. The graph must have exactly 0 or
2 vertices with an odd degree (number of edges
connected to a vertex).
2.Eulerian Circuit: A circuit (closed path) that visits
every edge of the graph exactly once and returns to
the starting vertex. All vertices in the
What the Code Does
We use Fleury's Algorithm to find an Eulerian Path or
Circuit. This algorithm works by finding a valid edge to
traverse and removing it after it's used.
Code Explanation
1. Graph Class
The Graph class is used to represent the graph.
It contains methods to add edges, check if an edge is valid to use, and
perform depth-first search (DFS) to check connectivity.
2. add_edge(u, v)
This method adds an edge between vertices u and v. The graph is
undirected, so it adds v to u's adjacency list and u to v's adjacency list.
3. is_valid_next_edge(u, v)
This method checks if the edge between u and v can be safely removed
without disconnecting the graph.
If a vertex has only one adjacent edge, the edge is always valid (because
there’s no other choice).
For other edges, it temporarily removes the edge and checks if the graph is
still connected using DFS. If it is, the edge can be safely used.
4. dfs(v, visited):
This is a helper method that performs a Depth First Search (DFS) to check if
the graph is still connected after removing an edge.
It marks visited vertices and recursively visits all connected vertices.
5. fleury(start)
• This method starts from the given start vertex and finds an Eulerian
path/circuit.
• It looks at all the edges connected to the current vertex and chooses a
valid edge to traverse using the is_valid_next_edge() method.
• Once an edge is traversed, it is removed, and the algorithm continues
until all edges are visited.
6. is_eulerian()
• This method checks if the graph has an Eulerian Path or Circuit by
counting how many vertices have an odd degree (an odd number of
edges).
• If there are 0 or 2 vertices with odd degrees, the graph has an Eulerian
Path or Circuit.
⚬ 0 odd-degree vertices Eulerian Circuit.
→
⚬ 2 odd-degree vertices Eulerian Path.
→
Test Case 1 (Eulerian Circuit)
• Graph
• Vertices: 0, 1, 2, 3
• Edges: (0-1), (1-2), (2-3), (3-0)
• All vertices have even degrees (each vertex has 2 edges), so the graph
has an Eulerian Circuit.
• The fleury() function starts from vertex 0 and follows the edges:
⚬ 0 -> 1
⚬ 1 -> 2
⚬ 2 -> 3
⚬ 3 -> 0
⚬ This completes a loop and returns to the starting point, forming an
Eulerian Circuit.
Test Case 2 (Eulerian Path)
• Graph
• Vertices: 0, 1, 2, 3, 4
• Edges: (0-1), (1-2), (2-3), (3-4)
• Vertices 0 and 4 have odd degrees (each has only 1 edge), so the graph
has an Eulerian Path.
• The fleury() function starts from vertex 0 and follows the edges:
⚬ 0 -> 1
⚬ 1 -> 2
⚬ 2 -> 3
⚬ 3 -> 4
⚬ This path starts at 0 and ends at 4, forming an Eulerian Path.
Why Use Fleury's Algorithm?
1.Simple
⚬ Fleury's Algorithm provides an easy-to-follow step-by-step approach
to solve Eulerian problems without requiring complex calculations.
2.Guaranteed Results (If Applicable)
⚬ If the graph meets the conditions for an Eulerian path or circuit,
Fleury's algorithm ensures a correct solution.
3.Visualization
⚬ It allows you to trace the path step by step, which is helpful in
understanding graph traversal.
4.Specific to Eulerian Problems
⚬ The algorithm specifically addresses problems where you need to
traverse every edge of a graph exactly once.
When to Use Fleury's Algorithm?
You use Fleury's Algorithm when
1.Finding an Eulerian Circuit
⚬ The graph is Eulerian, meaning all vertices have an even degree, and
the graph is connected.
2.Finding an Eulerian Path
⚬ The graph is semi-Eulerian, meaning it has exactly two vertices with
an odd degree, and the graph is connected.
Advantages of Fleury's Algorithm:
• Simple and Intuitive: Easy to understand and implement.
• Guaranteed Correctness: Always finds a solution if the graph is
Eulerian or semi-Eulerian.
• Educational Tool: Helps visualize and understand graph traversal
processes.
• Specific Use Case: Designed specifically for Eulerian paths and circuits.
• Efficient for Small Graphs: Works well with small to moderately sized
graphs.
Practical Applications of Fleury's Algorithm
• Route Planning: Finding optimal paths for deliveries or postal routes.
• Networking: Traversing all connections in a network without
redundancy.
• Puzzle Solving: Solving Eulerian path problems like the famous "Seven
Bridges of Königsberg."
THANK YOU
ANY QUESTIONS

More Related Content

PPTX
Fleurys abas abbasli_
PPTX
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
PDF
Unit-10 Graphs .pdf
DOCX
Graphs and eularian circuit & path with c++ program
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
PDF
Graphhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pdf
Fleurys abas abbasli_
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
Unit-10 Graphs .pdf
Graphs and eularian circuit & path with c++ program
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
Graphhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pdf

Similar to FLEURY’S algorithm graph theory presentation.pptx (20)

PPTX
Depth first traversal(data structure algorithms)
PPTX
Improvement of shortest path algorithms using subgraphs heuristics
PPTX
UNIT III.pptx
PPTX
ppt 1.pptx
PPTX
Graphs_Euler_Circuit_Final_Presentation.pptx
PDF
Analysis of Pathfinding Algorithms
PPTX
UNIT II - Graph Algorithms techniques.pptx
PPT
Graphs
PPTX
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
PPTX
Euler paths and circuits
PDF
shortestpathalgorithm-180109112405 (1).pdf
PPTX
Shortest path algorithm
PPTX
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm
PPTX
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
PPTX
PPTX
CONTOUR-INTEGRATION-VIVIAN-A.-LAGING-.pptx
PPTX
Maths Presentation.pptx
PPTX
Maths Presentation.pptx
PDF
Daa chpater 12
PDF
Algorithms of graph
Depth first traversal(data structure algorithms)
Improvement of shortest path algorithms using subgraphs heuristics
UNIT III.pptx
ppt 1.pptx
Graphs_Euler_Circuit_Final_Presentation.pptx
Analysis of Pathfinding Algorithms
UNIT II - Graph Algorithms techniques.pptx
Graphs
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Euler paths and circuits
shortestpathalgorithm-180109112405 (1).pdf
Shortest path algorithm
Raster Scan Graphics, Line Drawing Algorithm and Circle Drawing Algorithm
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
CONTOUR-INTEGRATION-VIVIAN-A.-LAGING-.pptx
Maths Presentation.pptx
Maths Presentation.pptx
Daa chpater 12
Algorithms of graph
Ad

More from mehnazakhtar980 (7)

PDF
Writing user manuals and guides Business writing (1).pdf
PPTX
HIERHOLZER'S ALGORITHM GRAPH THEORY.pptx
PPTX
Search engine optimization presentation .pptx
PPTX
Final year project idea baby guard app t.pptx
PPTX
Breath first search Traversal algorithm DSA .pptx
PDF
Leadership challenges issues in workplace.pdf
PPTX
Depth first Search DFS DSA presentiation
Writing user manuals and guides Business writing (1).pdf
HIERHOLZER'S ALGORITHM GRAPH THEORY.pptx
Search engine optimization presentation .pptx
Final year project idea baby guard app t.pptx
Breath first search Traversal algorithm DSA .pptx
Leadership challenges issues in workplace.pdf
Depth first Search DFS DSA presentiation
Ad

Recently uploaded (20)

PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Well-logging-methods_new................
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
web development for engineering and engineering
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
composite construction of structures.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Well-logging-methods_new................
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
web development for engineering and engineering
OOP with Java - Java Introduction (Basics)
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Safety Seminar civil to be ensured for safe working.
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
composite construction of structures.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Operating System & Kernel Study Guide-1 - converted.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...

FLEURY’S algorithm graph theory presentation.pptx

  • 2. FLEURYS’S ALGORITHM Purpose Find an Eulerian Path or Circuit in a graph. Graph Requirements • Eulerian Path: Exactly two vertices with odd degrees. • Eulerian Circuit: All vertices have even degrees. Steps • Start at an odd-degree vertex (for a path) or any vertex (for a circuit). • Traverse edges one at a time, selecting a valid edge. • Avoid bridges unless they are the only remaining edges at the vertex. • Remove each edge after traversing it.
  • 3. FLEURYS’S ALGORITHM Output The sequence of edges representing the Eulerian Path or Circuit.
  • 4. ALGORITHM STEPS Step 1: Verify the Graph's Suitability: Check vertex degrees to ensure the graph has either two odd- degree vertices (Eulerian Path) or all even-degree vertices (Eulerian Circuit). Step 2: Choose a Starting Vertex: Start at one odd-degree vertex (Eulerian Path) or any vertex (Eulerian Circuit). Step 3: Select a Valid Edge: At the current vertex, choose an edge that is not a bridge unless it is the only edge left. Step 4: Traverse the Edge: Print the edge, remove it from the graph, and move to the next vertex. Step 5: Repeat: Continue until all edges in the graph are traversed exactly once. Step 6: Output the Path/Circuit: Print the Eulerian Path or Circuit based on the traversal order.
  • 5. IMPLEMENTATION Pyt def __init__(self, vertices): self.graph = defaultdict(list) # adjacency list self.V = vertices # number of vertices def add_edge(self, u, v): """Add an edge to the graph (undirected).""" self.graph[u].append(v) self.graph[v].append(u) def remove_edge(self, u, v): """Remove an edge from the graph.""" if v in self.graph[u]: self.graph[u].remove(v) if u in self.graph[v]: self.graph[v].remove(u) def dfs_count(self, v, visited): """Utility function to perform DFS and count reachable vertices.""" visited[v] = True count = 1 for neighbor in self.graph[v]: if not visited[neighbor]: count += self.dfs_count(neighbor, visited) return count
  • 6. IMPLEMENTATION def is_valid_next_edge(self, u, v): """Check if edge u-v is a valid edge to use.""" # Case 1: If u-v is the only edge, it's valid if len(self.graph[u]) == 1: return True # Case 2: If removing u-v leaves the graph connected, it's valid # Count reachable vertices before removing the edge visited = [False] * self.V count_before_removal = self.dfs_count(u, visited) # Remove the edge and count reachable vertices self.remove_edge(u, v) visited = [False] * self.V count_after_removal = self.dfs_count(u, visited) # Add the edge back to restore original graph self.add_edge(u, v) # If the counts are the same, the edge is not a bridge return count_before_removal == count_after_removal def print_euler_util(self, u): """Recursive function to print Eulerian path or circuit.""" for v in list(self.graph[u]): # Use list to avoid modification during iteration if self.is_valid_next_edge(u, v): print(f"{u} -> {v}") self.remove_edge(u, v) # Safely remove the edge self.print_euler_util(v) def print_euler(self): """Print the Eulerian path or circuit.""" # Find a vertex with an odd degree to start (if exists) start_vertex = 0 for i in range(self.V): if len(self.graph[i]) % 2 != 0: start_vertex = i break # Print Eulerian path/circuit starting from start_vertex print(f"Eulerian path/circuit starting from vertex {start_vertex}:") self.print_euler_util(start_vertex)
  • 7. Key Concepts 1.Eulerian Path: A path that visits every edge of the graph exactly once. The graph must have exactly 0 or 2 vertices with an odd degree (number of edges connected to a vertex). 2.Eulerian Circuit: A circuit (closed path) that visits every edge of the graph exactly once and returns to the starting vertex. All vertices in the
  • 8. What the Code Does We use Fleury's Algorithm to find an Eulerian Path or Circuit. This algorithm works by finding a valid edge to traverse and removing it after it's used. Code Explanation 1. Graph Class The Graph class is used to represent the graph. It contains methods to add edges, check if an edge is valid to use, and perform depth-first search (DFS) to check connectivity. 2. add_edge(u, v) This method adds an edge between vertices u and v. The graph is undirected, so it adds v to u's adjacency list and u to v's adjacency list.
  • 9. 3. is_valid_next_edge(u, v) This method checks if the edge between u and v can be safely removed without disconnecting the graph. If a vertex has only one adjacent edge, the edge is always valid (because there’s no other choice). For other edges, it temporarily removes the edge and checks if the graph is still connected using DFS. If it is, the edge can be safely used. 4. dfs(v, visited): This is a helper method that performs a Depth First Search (DFS) to check if the graph is still connected after removing an edge. It marks visited vertices and recursively visits all connected vertices.
  • 10. 5. fleury(start) • This method starts from the given start vertex and finds an Eulerian path/circuit. • It looks at all the edges connected to the current vertex and chooses a valid edge to traverse using the is_valid_next_edge() method. • Once an edge is traversed, it is removed, and the algorithm continues until all edges are visited. 6. is_eulerian() • This method checks if the graph has an Eulerian Path or Circuit by counting how many vertices have an odd degree (an odd number of edges). • If there are 0 or 2 vertices with odd degrees, the graph has an Eulerian Path or Circuit. ⚬ 0 odd-degree vertices Eulerian Circuit. → ⚬ 2 odd-degree vertices Eulerian Path. →
  • 11. Test Case 1 (Eulerian Circuit) • Graph • Vertices: 0, 1, 2, 3 • Edges: (0-1), (1-2), (2-3), (3-0) • All vertices have even degrees (each vertex has 2 edges), so the graph has an Eulerian Circuit. • The fleury() function starts from vertex 0 and follows the edges: ⚬ 0 -> 1 ⚬ 1 -> 2 ⚬ 2 -> 3 ⚬ 3 -> 0 ⚬ This completes a loop and returns to the starting point, forming an Eulerian Circuit.
  • 12. Test Case 2 (Eulerian Path) • Graph • Vertices: 0, 1, 2, 3, 4 • Edges: (0-1), (1-2), (2-3), (3-4) • Vertices 0 and 4 have odd degrees (each has only 1 edge), so the graph has an Eulerian Path. • The fleury() function starts from vertex 0 and follows the edges: ⚬ 0 -> 1 ⚬ 1 -> 2 ⚬ 2 -> 3 ⚬ 3 -> 4 ⚬ This path starts at 0 and ends at 4, forming an Eulerian Path.
  • 13. Why Use Fleury's Algorithm? 1.Simple ⚬ Fleury's Algorithm provides an easy-to-follow step-by-step approach to solve Eulerian problems without requiring complex calculations. 2.Guaranteed Results (If Applicable) ⚬ If the graph meets the conditions for an Eulerian path or circuit, Fleury's algorithm ensures a correct solution. 3.Visualization ⚬ It allows you to trace the path step by step, which is helpful in understanding graph traversal. 4.Specific to Eulerian Problems ⚬ The algorithm specifically addresses problems where you need to traverse every edge of a graph exactly once.
  • 14. When to Use Fleury's Algorithm? You use Fleury's Algorithm when 1.Finding an Eulerian Circuit ⚬ The graph is Eulerian, meaning all vertices have an even degree, and the graph is connected. 2.Finding an Eulerian Path ⚬ The graph is semi-Eulerian, meaning it has exactly two vertices with an odd degree, and the graph is connected.
  • 15. Advantages of Fleury's Algorithm: • Simple and Intuitive: Easy to understand and implement. • Guaranteed Correctness: Always finds a solution if the graph is Eulerian or semi-Eulerian. • Educational Tool: Helps visualize and understand graph traversal processes. • Specific Use Case: Designed specifically for Eulerian paths and circuits. • Efficient for Small Graphs: Works well with small to moderately sized graphs.
  • 16. Practical Applications of Fleury's Algorithm • Route Planning: Finding optimal paths for deliveries or postal routes. • Networking: Traversing all connections in a network without redundancy. • Puzzle Solving: Solving Eulerian path problems like the famous "Seven Bridges of Königsberg."