From c74654e354f273d70b833be2f159f954afc85b46 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 14:14:15 +0000 Subject: [PATCH 01/13] Add typing for bfs --- graphs/bfs.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index 9d9b1ac037d9..69ec9a885e95 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -16,6 +16,8 @@ """ +from typing import Set, Dict + G = { "A": ["B", "C"], "B": ["A", "D", "E"], @@ -26,13 +28,12 @@ } -def bfs(graph, start): +def bfs(graph: Dict, start: str) -> Set[int]: """ >>> ''.join(sorted(bfs(G, 'A'))) 'ABCDEF' """ - explored, queue = set(), [start] # collections.deque([start]) - explored.add(start) + explored, queue = set(start), [start] # collections.deque([start]) while queue: v = queue.pop(0) # queue.popleft() for w in graph[v]: From 7594d812f6b2c65f7ff7778b2af6b964165fcb81 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 14:23:54 +0000 Subject: [PATCH 02/13] Add url for BFS --- graphs/bfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index 69ec9a885e95..fda54d0ddf68 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -1,4 +1,4 @@ -""" +"""https://en.wikipedia.org/wiki/Breadth-first_search BFS. pseudo-code: From 2cac29442a5027519398d6b6fc876a4233fa8701 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 22:43:49 +0800 Subject: [PATCH 03/13] rename the function Co-authored-by: Christian Clauss --- graphs/bfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index fda54d0ddf68..bc7d5dfbaee7 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -28,7 +28,7 @@ } -def bfs(graph: Dict, start: str) -> Set[int]: +def breadth_first_search(graph: Dict, start: str) -> Set[int]: """ >>> ''.join(sorted(bfs(G, 'A'))) 'ABCDEF' From 76e680ff2080b0e29735cce118dd226a8bf2eee0 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 22:45:55 +0800 Subject: [PATCH 04/13] Update graphs/bfs.py Co-authored-by: Christian Clauss --- graphs/bfs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index bc7d5dfbaee7..8a07d792e24d 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -33,7 +33,8 @@ def breadth_first_search(graph: Dict, start: str) -> Set[int]: >>> ''.join(sorted(bfs(G, 'A'))) 'ABCDEF' """ - explored, queue = set(start), [start] # collections.deque([start]) + explored = {start} + queue = [start] while queue: v = queue.pop(0) # queue.popleft() for w in graph[v]: From 3796d0fdb73906d7be483a5157346fe8b35fe696 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 14:49:20 +0000 Subject: [PATCH 05/13] Change the return value type of bfs --- graphs/bfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index 8a07d792e24d..cf0f96a44d80 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -28,7 +28,7 @@ } -def breadth_first_search(graph: Dict, start: str) -> Set[int]: +def breadth_first_search(graph: Dict, start: str) -> Set[str]: """ >>> ''.join(sorted(bfs(G, 'A'))) 'ABCDEF' From ff831b150002a131d9ac0d4848c31f6d12b95f0d Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 14:56:41 +0000 Subject: [PATCH 06/13] change the function name. change all instances of bfs() to breadth_first_search(). --- graphs/bfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index cf0f96a44d80..ff4dd25a072a 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -45,4 +45,4 @@ def breadth_first_search(graph: Dict, start: str) -> Set[str]: if __name__ == "__main__": - print(bfs(G, "A")) + print(breadth_first_search(G, "A")) From 14d3412f6865b7b20a3ad83ccade9ca7c866f436 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 14:59:24 +0000 Subject: [PATCH 07/13] change the function name in annotate --- graphs/bfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index ff4dd25a072a..8b6dbe008b6c 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -30,7 +30,7 @@ def breadth_first_search(graph: Dict, start: str) -> Set[str]: """ - >>> ''.join(sorted(bfs(G, 'A'))) + >>> ''.join(sorted(breadth_first_search(G, 'A'))) 'ABCDEF' """ explored = {start} From 3131f52449329b7f86e2a430a07d7ea4d9c141ac Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 15:08:47 +0000 Subject: [PATCH 08/13] Add one more blank line. --- graphs/bfs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/graphs/bfs.py b/graphs/bfs.py index 8b6dbe008b6c..6a21c6741f3b 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -46,3 +46,4 @@ def breadth_first_search(graph: Dict, start: str) -> Set[str]: if __name__ == "__main__": print(breadth_first_search(G, "A")) + From ae0a5485b1016fe8c4c72c586b644391791bc6fa Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 15:17:00 +0000 Subject: [PATCH 09/13] Delete one blank line. --- graphs/bfs.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index 6a21c6741f3b..11795ef17504 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -1,8 +1,6 @@ """https://en.wikipedia.org/wiki/Breadth-first_search BFS. - pseudo-code: - BFS(graph G, start vertex s): // all nodes initially unexplored mark s as explored @@ -13,7 +11,6 @@ if w unexplored: mark w as explored add w to Q (at the end) - """ from typing import Set, Dict @@ -46,4 +43,3 @@ def breadth_first_search(graph: Dict, start: str) -> Set[str]: if __name__ == "__main__": print(breadth_first_search(G, "A")) - From b516227cea1ca19e1ae9171080d9e46508c58fb5 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 15:29:51 +0000 Subject: [PATCH 10/13] Delete one blank line. I've read the https://www.flake8rules.com/rules/W391.html, and still don't know how to do it. I've tried using 0 ,1,2 blank lines... From 39be6ba317d0e88a532667fa88993c421ec06428 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 23:33:29 +0800 Subject: [PATCH 11/13] Update graphs/bfs.py Co-authored-by: Christian Clauss --- graphs/bfs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index 11795ef17504..86b49b542096 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -1,5 +1,5 @@ -"""https://en.wikipedia.org/wiki/Breadth-first_search -BFS. +""" +https://en.wikipedia.org/wiki/Breadth-first_search pseudo-code: BFS(graph G, start vertex s): // all nodes initially unexplored From e523be611b6e52eb02c40dd04f00e8967f54363a Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 25 Jun 2020 23:33:41 +0800 Subject: [PATCH 12/13] Update graphs/bfs.py Co-authored-by: Christian Clauss --- graphs/bfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bfs.py b/graphs/bfs.py index 86b49b542096..0c87b5d8bf3d 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -1,7 +1,7 @@ """ https://en.wikipedia.org/wiki/Breadth-first_search pseudo-code: -BFS(graph G, start vertex s): +breadth_first_search(graph G, start vertex s): // all nodes initially unexplored mark s as explored let Q = queue data structure, initialized with s From b2b6492a35b7d53d86191fc07d0714f6e412f5ed Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 25 Jun 2020 17:54:09 +0200 Subject: [PATCH 13/13] Rename bfs.py to breadth_first_search_2.py --- graphs/{bfs.py => breadth_first_search_2.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename graphs/{bfs.py => breadth_first_search_2.py} (100%) diff --git a/graphs/bfs.py b/graphs/breadth_first_search_2.py similarity index 100% rename from graphs/bfs.py rename to graphs/breadth_first_search_2.py