-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Initialize set with source in DFS #1872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3a7b2d2
Update dfs.py
SandersLin de64f33
Add type hints, rearrange doc-strings and comments
poyea e185f4f
fixup! Format Python code with psf/black push
da7c95e
dfs -> depth_first_search
poyea 07c21ee
dfs -> depth_first_search
poyea 398d00b
Add doctest for DFS
poyea 5b56b19
fixup! Format Python code with psf/black push
e6d68ab
Rename dfs.py to depth_first_search_dictionary.py
poyea 377482b
updating DIRECTORY.md
9d7e9b7
Rename depth_first_search_dictionary.py to depth_first_search_dfs.py
poyea fb5b3ee
updating DIRECTORY.md
14034a3
Rename depth_first_search.py to depth_first_search_2.py
poyea 51e06cf
updating DIRECTORY.md
4b8d1c6
Rename depth_first_search_dfs.py to depth_first_search.py
poyea a329f4e
updating DIRECTORY.md
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,52 @@ | ||
"""pseudo-code""" | ||
|
||
""" | ||
DFS(graph G, start vertex s): | ||
// all nodes initially unexplored | ||
mark s as explored | ||
for every edge (s, v): | ||
if v unexplored: | ||
DFS(G, v) | ||
"""The DFS function simply calls itself recursively for every unvisited child of | ||
its argument. We can emulate that behaviour precisely using a stack of iterators. | ||
Instead of recursively calling with a node, we'll push an iterator to the node's | ||
children onto the iterator stack. When the iterator at the top of the stack | ||
terminates, we'll pop it off the stack. | ||
|
||
Pseudocode: | ||
all nodes initially unexplored | ||
mark s as explored | ||
for every edge (s, v): | ||
if v unexplored: | ||
DFS(G, v) | ||
""" | ||
|
||
|
||
def dfs(graph, start): | ||
"""The DFS function simply calls itself recursively for every unvisited child of its argument. We can emulate that | ||
behaviour precisely using a stack of iterators. Instead of recursively calling with a node, we'll push an iterator | ||
to the node's children onto the iterator stack. When the iterator at the top of the stack terminates, we'll pop | ||
it off the stack.""" | ||
explored, stack = set(), [start] | ||
from typing import Set, Dict | ||
|
||
|
||
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 | ||
""" | ||
explored, stack = set(start), [start] | ||
while stack: | ||
v = ( | ||
stack.pop() | ||
) # one difference from BFS is to pop last element here instead of first one | ||
|
||
if v in explored: | ||
continue | ||
|
||
explored.add(v) | ||
|
||
v = stack.pop() | ||
# one difference from BFS is to pop last element here instead of first one | ||
for w in graph[v]: | ||
if w not in explored: | ||
explored.add(w) | ||
stack.append(w) | ||
return explored | ||
|
||
|
||
G = { | ||
"A": ["B", "C"], | ||
"B": ["A", "D", "E"], | ||
"C": ["A", "F"], | ||
"D": ["B"], | ||
"E": ["B", "F"], | ||
"F": ["C", "E"], | ||
} | ||
G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], | ||
"C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], | ||
"F": ["C", "E", "G"], "G": ["F"] } | ||
|
||
print(dfs(G, "A")) | ||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() | ||
print(depth_first_search(G, "A")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.