Optimized recursive_bubble_sort (#2410)

* optimized recursive_bubble_sort

* Fixed doctest error due whitespace

* reduce loop times for optimization

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Du Yuanchao
2020-09-10 16:31:26 +08:00
committed by GitHub
parent 25946e4570
commit 4d0a8f2355
60 changed files with 900 additions and 859 deletions

View File

@@ -20,18 +20,18 @@ graph = {
def bfs_shortest_path(graph: dict, start, goal) -> str:
"""Find shortest path between `start` and `goal` nodes.
Args:
graph (dict): node/list of neighboring nodes key/value pairs.
start: start node.
goal: target node.
Args:
graph (dict): node/list of neighboring nodes key/value pairs.
start: start node.
goal: target node.
Returns:
Shortest path between `start` and `goal` nodes as a string of nodes.
'Not found' string if no path found.
Returns:
Shortest path between `start` and `goal` nodes as a string of nodes.
'Not found' string if no path found.
Example:
>>> bfs_shortest_path(graph, "G", "D")
['G', 'C', 'A', 'B', 'D']
Example:
>>> bfs_shortest_path(graph, "G", "D")
['G', 'C', 'A', 'B', 'D']
"""
# keep track of explored nodes
explored = []
@@ -70,22 +70,22 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:
def bfs_shortest_path_distance(graph: dict, start, target) -> int:
"""Find shortest path distance between `start` and `target` nodes.
Args:
graph: node/list of neighboring nodes key/value pairs.
start: node to start search from.
target: node to search for.
Args:
graph: node/list of neighboring nodes key/value pairs.
start: node to start search from.
target: node to search for.
Returns:
Number of edges in shortest path between `start` and `target` nodes.
-1 if no path exists.
Returns:
Number of edges in shortest path between `start` and `target` nodes.
-1 if no path exists.
Example:
>>> bfs_shortest_path_distance(graph, "G", "D")
4
>>> bfs_shortest_path_distance(graph, "A", "A")
0
>>> bfs_shortest_path_distance(graph, "A", "H")
-1
Example:
>>> bfs_shortest_path_distance(graph, "G", "D")
4
>>> bfs_shortest_path_distance(graph, "A", "A")
0
>>> bfs_shortest_path_distance(graph, "A", "H")
-1
"""
if not graph or start not in graph or target not in graph:
return -1

View File

@@ -17,18 +17,18 @@ from typing import Dict, Set
def depth_first_search(graph: Dict, start: str) -> Set[int]:
"""Depth First Search on Graph
:param graph: directed graph in dictionary format
:param vertex: starting vectex as a string
:returns: the trace of the search
>>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"],
... "F": ["C", "E", "G"], "G": ["F"] }
>>> start = "A"
>>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'})
>>> all(x in output_G for x in list(depth_first_search(G, "A")))
True
>>> all(x in output_G for x in list(depth_first_search(G, "G")))
True
:param graph: directed graph in dictionary format
:param vertex: starting vectex as a string
:returns: the trace of the search
>>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"],
... "F": ["C", "E", "G"], "G": ["F"] }
>>> start = "A"
>>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'})
>>> all(x in output_G for x in list(depth_first_search(G, "A")))
True
>>> all(x in output_G for x in list(depth_first_search(G, "G")))
True
"""
explored, stack = set(start), [start]
while stack:

View File

@@ -56,14 +56,14 @@ def connect(graph, a, b, edge):
def prim(graph: list, root: Vertex) -> list:
"""Prim's Algorithm.
Runtime:
O(mn) with `m` edges and `n` vertices
Runtime:
O(mn) with `m` edges and `n` vertices
Return:
List with the edges of a Minimum Spanning Tree
Return:
List with the edges of a Minimum Spanning Tree
Usage:
prim(graph, graph[0])
Usage:
prim(graph, graph[0])
"""
a = []
for u in graph:
@@ -86,14 +86,14 @@ def prim(graph: list, root: Vertex) -> list:
def prim_heap(graph: list, root: Vertex) -> Iterator[tuple]:
"""Prim's Algorithm with min heap.
Runtime:
O((m + n)log n) with `m` edges and `n` vertices
Runtime:
O((m + n)log n) with `m` edges and `n` vertices
Yield:
Edges of a Minimum Spanning Tree
Yield:
Edges of a Minimum Spanning Tree
Usage:
prim(graph, graph[0])
Usage:
prim(graph, graph[0])
"""
for u in graph:
u.key = math.inf