Amazing-Python-Scripts
126 строк · 4.9 Кб
1'''
2------------------------------------- Fleury's Algorithm -------------------------------------
3
4Approach:-
51. Start with any vertex in the graph.
6
72. While there are unused edges in the graph, do the following steps:
8a. Choose any unused edge connected to the current vertex. It doesn't matter which one you choose.
9b. If removing the chosen edge doesn't disconnect the graph, go to the vertex at the other end of the chosen edge.
10c. If removing the chosen edge disconnects the graph, backtrack to the previous vertex that still has unused edges and choose a different edge.
11d. Repeat steps (a) to (c) until you can no longer choose any unused edges from the current vertex.
12
133. The algorithm terminates when you have traversed all the edges of the graph.
14
154. If all the vertices in the graph have even degrees, you will end up with an Eulerian circuit, which is a closed path that visits each edge exactly once.
16
175. If exactly two vertices in the graph have odd degrees, you will end up with an Eulerian path, which is a path that starts and ends at different vertices and visits each edge exactly once.
18'''
19
20# Program Starts
21from collections import defaultdict
22
23
24class Graph:
25
26def __init__(self, vertices):
27self.V = vertices # No. of vertices
28self.graph = defaultdict(list)
29self.Time = 0
30
31# function to add an edge to graph
32def addEdge(self, source, destination):
33self.graph[source].append(destination)
34self.graph[destination].append(source)
35
36# This function removes edge source-destination from graph
37def removeEdge(self, source, destination):
38for index, key in enumerate(self.graph[source]):
39if key == destination:
40self.graph[source].pop(index)
41for index, key in enumerate(self.graph[destination]):
42if key == source:
43self.graph[destination].pop(index)
44
45# A DFS based function to count reachable vertices from destination
46def DFSCount(self, destination, visited):
47count = 1
48visited[destination] = True
49for i in self.graph[destination]:
50if visited[i] == False:
51count = count + self.DFSCount(i, visited)
52return count
53
54# The function to check if edge source-destination can be considered as next edge in Euler Trail
55def isValidNextEdge(self, source, destination):
56# The edge source-destination is valid in one of the following two cases:
57
58# 1) If destination is the only adjacent vertex of source
59if len(self.graph[source]) == 1:
60return True
61else:
62'''
632) If there are multiple adjacents, then source-destination is not a bridge
64Do following steps to check if source-destination is a bridge
65
662.a) count of vertices reachable from source'''
67visited = [False]*(self.V)
68count1 = self.DFSCount(source, visited)
69
70'''2.b) Remove edge (source, destination) and after removing the edge, count
71vertices reachable from source'''
72self.removeEdge(source, destination)
73visited = [False]*(self.V)
74count2 = self.DFSCount(source, visited)
75
76# 2.c) Add the edge back to the graph
77self.addEdge(source, destination)
78
79# 2.d) If count1 is greater, then edge (source, destination) is a bridge
80return False if count1 > count2 else True
81
82# Print Euler Trail starting from vertex source
83
84def printEulerUtil(self, source):
85# Recur for all the vertices adjacent to this vertex
86for destination in self.graph[source]:
87# If edge source-destination is not removed and it's a a valid next edge
88if self.isValidNextEdge(source, destination):
89print("%d-%d " % (source, destination)),
90self.removeEdge(source, destination)
91self.printEulerUtil(destination)
92
93'''The main function that print Eulerian Trail. It first finds an odd
94degree vertex (if there is any) and then calls printEulerUtil()
95to print the path '''
96
97def printEulerTrail(self):
98# Find a vertex with odd degree
99source = 0
100for i in range(self.V):
101if len(self.graph[i]) % 2 != 0:
102source = i
103break
104# Print Trail starting from odd vertex
105print("\n")
106self.printEulerUtil(source)
107
108
109# Driver program
110V = int(input("\nEnter the number of vertices in the graph: "))
111
112g = Graph(V)
113
114E = int(input("\nEnter the number of edges in the graph: "))
115
116# Taking input from the user
117print("\nEnter the edges in the format (source destination)")
118for i in range(E):
119source = int(input(f"Source {i+1}: "))
120destination = int(input(f"Destination {i+1}: "))
121g.addEdge(source, destination)
122
123# Printing the final result after analysing
124print("\nResult of Fleury Algorithm: ", end="")
125g.printEulerTrail()
126print()
127