Skip to content

Commit e127161

Browse files
committed
Added doc_tests to eulerian_path_and_circuit_for_undirected_graph.py
1 parent 2d671df commit e127161

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

graphs/eulerian_path_and_circuit_for_undirected_graph.py

+72-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,31 @@
55
# space complexity is O(VE)
66

77

8-
# using dfs for finding eulerian path traversal
98
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+
"""
1033
path = (path or []) + [u]
1134
for v in graph[u]:
1235
if visited_edge[u][v] is False:
@@ -15,8 +38,29 @@ def dfs(u, graph, visited_edge, path=None):
1538
return path
1639

1740

18-
# for checking in graph has euler path or circuit
1941
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+
"""
2064
odd_degree_nodes = 0
2165
odd_node = -1
2266
for i in range(max_node):
@@ -33,6 +77,28 @@ def check_circuit_or_path(graph, max_node):
3377

3478

3579
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+
"""
36102
visited_edge = [[False for _ in range(max_node + 1)] for _ in range(max_node + 1)]
37103
check, odd_node = check_circuit_or_path(graph, max_node)
38104
if check == 3:
@@ -69,3 +135,7 @@ def main():
69135

70136
if __name__ == "__main__":
71137
main()
138+
139+
import doctest
140+
141+
doctest.testmod()

0 commit comments

Comments
 (0)