-
Notifications
You must be signed in to change notification settings - Fork 0
Create dfs #7
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
Create dfs #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from collections import defaultdict | ||
|
|
||
| # This class represents a directed graph using | ||
| # adjacency list representation | ||
| class Graph: | ||
|
|
||
| # Constructor | ||
| def __init__(self): | ||
|
|
||
| # Default dictionary to store graph | ||
| self.graph = defaultdict(list) | ||
|
|
||
|
|
||
| # Function to add an edge to graph | ||
| def addEdge(self, u, v): | ||
| self.graph[u].append(v) | ||
|
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| # A function used by DFS | ||
| def DFSUtil(self, v, visited): | ||
|
|
||
| # Mark the current node as visited | ||
| # and print it | ||
| visited.add(v) | ||
| print(v, end=' ') | ||
|
|
||
| # Recur for all the vertices | ||
| # adjacent to this vertex | ||
| for neighbour in self.graph[v]: | ||
| if neighbour not in visited: | ||
| self.DFSUtil(neighbour, visited) | ||
|
Comment on lines
+20
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be significantly improved in terms of reusability and Python conventions:
def _dfs_util(self, v, visited):
"""A recursive generator for DFS traversal from a given vertex."""
visited.add(v)
yield v
# Recur for all the vertices adjacent to this vertex
for neighbour in self.graph[v]:
if neighbour not in visited:
yield from self._dfs_util(neighbour, visited) |
||
|
|
||
|
|
||
| # The function to do DFS traversal. It uses | ||
| # recursive DFSUtil() | ||
| def DFS(self, v): | ||
|
|
||
| # Create a set to store visited vertices | ||
| visited = set() | ||
|
|
||
| # Call the recursive helper function | ||
| # to print DFS traversal | ||
| self.DFSUtil(v, visited) | ||
|
Comment on lines
+36
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
def dfs_from_vertex(self, v):
"""Performs DFS traversal starting from a given vertex.
Returns:
A generator that yields nodes in DFS order.
"""
# Create a set to store visited vertices for this traversal
visited = set()
# Return the generator from the helper function
return self._dfs_util(v, visited) |
||
|
|
||
|
|
||
| # Driver's code | ||
| if __name__ == "__main__": | ||
| g = Graph() | ||
| g.addEdge(0, 1) | ||
| g.addEdge(0, 2) | ||
| g.addEdge(1, 2) | ||
| g.addEdge(2, 0) | ||
| g.addEdge(2, 3) | ||
| g.addEdge(3, 3) | ||
|
|
||
| print("Following is Depth First Traversal (starting from vertex 2)") | ||
|
|
||
| # Function call | ||
| g.DFS(2) | ||
|
Comment on lines
+47
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This driver code needs to be updated to reflect the suggested changes to method names ( if __name__ == "__main__":
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
print("Following is Depth First Traversal (starting from vertex 2)")
# The dfs_from_vertex method returns a generator.
# We can unpack it with * to print all items.
traversal_generator = g.dfs_from_vertex(2)
print(*traversal_generator) |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Python's style guide PEP 257, it's standard practice to use docstrings (
"""...""") for documenting classes, functions, and modules. This is more idiomatic than using a comment block above the class definition and allows tools to automatically generate documentation and users to get help via thehelp()function."""Represents a directed graph using an adjacency list."""