5
5
# space complexity is O(VE)
6
6
7
7
8
- # using dfs for finding eulerian path traversal
9
8
def dfs (u , graph , visited_edge , path = None ):
9
+ """
10
+ Using dfs for finding eulerian path traversal
11
+ Args:
12
+ u: The start_node
13
+ graph: The graph to check
14
+ visited_edge: Specify if a node has been visited or not
15
+ path: Optional path parameter
16
+
17
+ Returns:
18
+ Path
19
+
20
+ Example:
21
+ >>> visited_edge = [[False] * 11 for _ in range(11)]
22
+ >>> dfs(1, {1: [2, 3], 2: [1, 3], 3: [1, 2]}, visited_edge)
23
+ [1, 2, 3, 1]
24
+ >>> dfs(5, {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, visited_edge)
25
+ [5, 4, 1]
26
+ >>> dfs(1, {1: [], 2: [], 3: [1, 2]}, visited_edge)
27
+ [1]
28
+ >>> dfs(1, {1: [], 2: []}, visited_edge)
29
+ [1]
30
+ >>> dfs(1, {1: [], 2: []}, visited_edge, [1, 3])
31
+ [1, 3, 1]
32
+ """
10
33
path = (path or []) + [u ]
11
34
for v in graph [u ]:
12
35
if visited_edge [u ][v ] is False :
@@ -15,8 +38,29 @@ def dfs(u, graph, visited_edge, path=None):
15
38
return path
16
39
17
40
18
- # for checking in graph has euler path or circuit
19
41
def check_circuit_or_path (graph , max_node ):
42
+ """
43
+ For checking in graph has euler path or circuit
44
+
45
+ Args:
46
+ graph: The graph to check
47
+ max_node: The maximum node to check
48
+
49
+ Returns:
50
+ Type of graph, and its circuit or path
51
+
52
+ Example:
53
+ >>> check_circuit_or_path({1: [2, 3], 2: [1, 3], 3: [1, 2]}, 10)
54
+ (1, -1)
55
+ >>> check_circuit_or_path({1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, 10)
56
+ (2, 5)
57
+ >>> check_circuit_or_path({1: [2, 3, 1], 2: [2, 3, 4], 3: [1, 3], 4: [1], 5: []}, 10)
58
+ (3, 4)
59
+ >>> check_circuit_or_path({1: [], 2: [], 3: [1, 2]}, 10)
60
+ (1, -1)
61
+ >>> check_circuit_or_path({1: [], 2: []}, 10)
62
+ (1, -1)
63
+ """
20
64
odd_degree_nodes = 0
21
65
odd_node = - 1
22
66
for i in range (max_node ):
@@ -33,6 +77,28 @@ def check_circuit_or_path(graph, max_node):
33
77
34
78
35
79
def check_euler (graph , max_node ):
80
+ """
81
+ Args:
82
+ graph: The graph to check
83
+ max_node: The maximum node to check
84
+
85
+ Example:
86
+ >>> check_euler({1: [2, 3], 2: [1, 3], 3: [1, 2]}, 10)
87
+ graph has a Euler cycle
88
+ [1, 2, 3, 1]
89
+ >>> check_euler({1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, 10)
90
+ graph has a Euler path
91
+ [5, 4, 1, 2, 3, 1]
92
+ >>> check_euler({1: [2, 3, 1], 2: [2, 3, 4], 3: [1, 3], 4: [1], 5: []}, 10)
93
+ graph is not Eulerian
94
+ no path
95
+ >>> check_euler({1: [], 2: [], 3: [1, 2]}, 10)
96
+ graph has a Euler cycle
97
+ [1]
98
+ >>> check_euler({1: [], 2: []}, 10)
99
+ graph has a Euler cycle
100
+ [1]
101
+ """
36
102
visited_edge = [[False for _ in range (max_node + 1 )] for _ in range (max_node + 1 )]
37
103
check , odd_node = check_circuit_or_path (graph , max_node )
38
104
if check == 3 :
@@ -69,3 +135,7 @@ def main():
69
135
70
136
if __name__ == "__main__" :
71
137
main ()
138
+
139
+ import doctest
140
+
141
+ doctest .testmod ()
0 commit comments