From e1271617b796e6574005fe12435d19d31cc7025b Mon Sep 17 00:00:00 2001 From: Jordan Sinclair Date: Tue, 8 Oct 2024 00:24:57 -0500 Subject: [PATCH 1/4] Added doc_tests to eulerian_path_and_circuit_for_undirected_graph.py --- ...n_path_and_circuit_for_undirected_graph.py | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/graphs/eulerian_path_and_circuit_for_undirected_graph.py b/graphs/eulerian_path_and_circuit_for_undirected_graph.py index 5b146eaa845b..48ba92ca5a4e 100644 --- a/graphs/eulerian_path_and_circuit_for_undirected_graph.py +++ b/graphs/eulerian_path_and_circuit_for_undirected_graph.py @@ -5,8 +5,31 @@ # space complexity is O(VE) -# using dfs for finding eulerian path traversal def dfs(u, graph, visited_edge, path=None): + """ + Using dfs for finding eulerian path traversal + Args: + u: The start_node + graph: The graph to check + visited_edge: Specify if a node has been visited or not + path: Optional path parameter + + Returns: + Path + + Example: + >>> visited_edge = [[False] * 11 for _ in range(11)] + >>> dfs(1, {1: [2, 3], 2: [1, 3], 3: [1, 2]}, visited_edge) + [1, 2, 3, 1] + >>> dfs(5, {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, visited_edge) + [5, 4, 1] + >>> dfs(1, {1: [], 2: [], 3: [1, 2]}, visited_edge) + [1] + >>> dfs(1, {1: [], 2: []}, visited_edge) + [1] + >>> dfs(1, {1: [], 2: []}, visited_edge, [1, 3]) + [1, 3, 1] + """ path = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: @@ -15,8 +38,29 @@ def dfs(u, graph, visited_edge, path=None): return path -# for checking in graph has euler path or circuit def check_circuit_or_path(graph, max_node): + """ + For checking in graph has euler path or circuit + + Args: + graph: The graph to check + max_node: The maximum node to check + + Returns: + Type of graph, and its circuit or path + + Example: + >>> check_circuit_or_path({1: [2, 3], 2: [1, 3], 3: [1, 2]}, 10) + (1, -1) + >>> check_circuit_or_path({1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, 10) + (2, 5) + >>> check_circuit_or_path({1: [2, 3, 1], 2: [2, 3, 4], 3: [1, 3], 4: [1], 5: []}, 10) + (3, 4) + >>> check_circuit_or_path({1: [], 2: [], 3: [1, 2]}, 10) + (1, -1) + >>> check_circuit_or_path({1: [], 2: []}, 10) + (1, -1) + """ odd_degree_nodes = 0 odd_node = -1 for i in range(max_node): @@ -33,6 +77,28 @@ def check_circuit_or_path(graph, max_node): def check_euler(graph, max_node): + """ + Args: + graph: The graph to check + max_node: The maximum node to check + + Example: + >>> check_euler({1: [2, 3], 2: [1, 3], 3: [1, 2]}, 10) + graph has a Euler cycle + [1, 2, 3, 1] + >>> check_euler({1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, 10) + graph has a Euler path + [5, 4, 1, 2, 3, 1] + >>> check_euler({1: [2, 3, 1], 2: [2, 3, 4], 3: [1, 3], 4: [1], 5: []}, 10) + graph is not Eulerian + no path + >>> check_euler({1: [], 2: [], 3: [1, 2]}, 10) + graph has a Euler cycle + [1] + >>> check_euler({1: [], 2: []}, 10) + graph has a Euler cycle + [1] + """ visited_edge = [[False for _ in range(max_node + 1)] for _ in range(max_node + 1)] check, odd_node = check_circuit_or_path(graph, max_node) if check == 3: @@ -69,3 +135,7 @@ def main(): if __name__ == "__main__": main() + + import doctest + + doctest.testmod() From e6a78476a8773c0fa7cb3de5165488afbd302db8 Mon Sep 17 00:00:00 2001 From: Jordan Sinclair Date: Tue, 8 Oct 2024 00:42:15 -0500 Subject: [PATCH 2/4] Minor update to doctests, to meet ruff requirements --- graphs/eulerian_path_and_circuit_for_undirected_graph.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/graphs/eulerian_path_and_circuit_for_undirected_graph.py b/graphs/eulerian_path_and_circuit_for_undirected_graph.py index 48ba92ca5a4e..01ab2aebbdd8 100644 --- a/graphs/eulerian_path_and_circuit_for_undirected_graph.py +++ b/graphs/eulerian_path_and_circuit_for_undirected_graph.py @@ -21,7 +21,7 @@ def dfs(u, graph, visited_edge, path=None): >>> visited_edge = [[False] * 11 for _ in range(11)] >>> dfs(1, {1: [2, 3], 2: [1, 3], 3: [1, 2]}, visited_edge) [1, 2, 3, 1] - >>> dfs(5, {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, visited_edge) + >>> dfs(5, {1: [2, 3, 4], 2: [1, 3], 3: [1], 4: [1, 5], 5: [4]}, visited_edge) [5, 4, 1] >>> dfs(1, {1: [], 2: [], 3: [1, 2]}, visited_edge) [1] @@ -52,9 +52,9 @@ def check_circuit_or_path(graph, max_node): Example: >>> check_circuit_or_path({1: [2, 3], 2: [1, 3], 3: [1, 2]}, 10) (1, -1) - >>> check_circuit_or_path({1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}, 10) + >>> check_circuit_or_path({1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [], 5: [4]}, 10) (2, 5) - >>> check_circuit_or_path({1: [2, 3, 1], 2: [2, 3, 4], 3: [1, 3], 4: [1], 5: []}, 10) + >>> check_circuit_or_path({1: [2, 3, 1], 2: [2], 3: [1, 3], 4: [1], 5: []}, 10) (3, 4) >>> check_circuit_or_path({1: [], 2: [], 3: [1, 2]}, 10) (1, -1) From 8c96ae3680a484659c02ce2ff2037225936d4b2d Mon Sep 17 00:00:00 2001 From: Jordan Sinclair Date: Tue, 8 Oct 2024 00:45:53 -0500 Subject: [PATCH 3/4] Minor update to doctests, to meet ruff requirements --- graphs/eulerian_path_and_circuit_for_undirected_graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/eulerian_path_and_circuit_for_undirected_graph.py b/graphs/eulerian_path_and_circuit_for_undirected_graph.py index 01ab2aebbdd8..ade5754d74ae 100644 --- a/graphs/eulerian_path_and_circuit_for_undirected_graph.py +++ b/graphs/eulerian_path_and_circuit_for_undirected_graph.py @@ -52,7 +52,7 @@ def check_circuit_or_path(graph, max_node): Example: >>> check_circuit_or_path({1: [2, 3], 2: [1, 3], 3: [1, 2]}, 10) (1, -1) - >>> check_circuit_or_path({1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [], 5: [4]}, 10) + >>> check_circuit_or_path({1: [2, 3, 4], 2: [], 3: [1, 2], 4: [], 5: [4]}, 10) (2, 5) >>> check_circuit_or_path({1: [2, 3, 1], 2: [2], 3: [1, 3], 4: [1], 5: []}, 10) (3, 4) From 1c821832a6ffd374b2f1f31eb1f1b4615b2e1d25 Mon Sep 17 00:00:00 2001 From: Sinclo Date: Wed, 15 Jan 2025 03:14:55 +0000 Subject: [PATCH 4/4] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index aad6c72aa8ee..941e30dfe721 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -377,6 +377,7 @@ * [Longest Common Subsequence](dynamic_programming/longest_common_subsequence.py) * [Longest Common Substring](dynamic_programming/longest_common_substring.py) * [Longest Increasing Subsequence](dynamic_programming/longest_increasing_subsequence.py) + * [Longest Increasing Subsequence Iterative](dynamic_programming/longest_increasing_subsequence_iterative.py) * [Longest Increasing Subsequence O Nlogn](dynamic_programming/longest_increasing_subsequence_o_nlogn.py) * [Longest Palindromic Subsequence](dynamic_programming/longest_palindromic_subsequence.py) * [Matrix Chain Multiplication](dynamic_programming/matrix_chain_multiplication.py) @@ -462,6 +463,7 @@ ## Graphics * [Bezier Curve](graphics/bezier_curve.py) + * [Butterfly Pattern](graphics/butterfly_pattern.py) * [Digital Differential Analyzer Line](graphics/digital_differential_analyzer_line.py) * [Vector3 For 2D Rendering](graphics/vector3_for_2d_rendering.py)