Skip to content

Adds Doc test in depth_first_search_2.py #10094

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 5 commits into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 71 additions & 9 deletions graphs/depth_first_search_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,44 @@ def __init__(self):

# for printing the Graph vertices
def print_graph(self) -> None:
"""
Print the graph vertices.

Example:
>>> 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)
>>> g.print_graph()
{0: [1, 2], 1: [2], 2: [0, 3], 3: [3]}
0 -> 1 -> 2
1 -> 2
2 -> 0 -> 3
3 -> 3
"""
print(self.vertex)
for i in self.vertex:
print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))

# for adding the edge between two vertices
def add_edge(self, from_vertex: int, to_vertex: int) -> None:
"""
Add an edge between two vertices.

:param from_vertex: The source vertex.
:param to_vertex: The destination vertex.

Example:
>>> g = Graph()
>>> g.add_edge(0, 1)
>>> g.add_edge(0, 2)
>>> g.print_graph()
{0: [1, 2]}
0 -> 1 -> 2
"""
# check if vertex is already present,
if from_vertex in self.vertex:
self.vertex[from_vertex].append(to_vertex)
Expand All @@ -23,6 +55,21 @@ def add_edge(self, from_vertex: int, to_vertex: int) -> None:
self.vertex[from_vertex] = [to_vertex]

def dfs(self) -> None:
"""
Perform depth-first search (DFS) traversal on the graph
and print the visited vertices.

Example:
>>> 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)
>>> g.dfs()
0 1 2 3
"""
# visited array for storing already visited nodes
visited = [False] * len(self.vertex)

Expand All @@ -32,18 +79,41 @@ def dfs(self) -> None:
self.dfs_recursive(i, visited)

def dfs_recursive(self, start_vertex: int, visited: list) -> None:
"""
Perform a recursive depth-first search (DFS) traversal on the graph.

:param start_vertex: The starting vertex for the traversal.
:param visited: A list to track visited vertices.

Example:
>>> 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)
>>> visited = [False] * len(g.vertex)
>>> g.dfs_recursive(0, visited)
0 1 2 3
"""
# mark start vertex as visited
visited[start_vertex] = True

print(start_vertex, end=" ")
print(start_vertex, end="")

# Recur for all the vertices that are adjacent to this node
for i in self.vertex:
if not visited[i]:
print(" ", end="")
self.dfs_recursive(i, visited)


if __name__ == "__main__":
import doctest

doctest.testmod()

g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
Expand All @@ -55,11 +125,3 @@ def dfs_recursive(self, start_vertex: int, visited: list) -> None:
g.print_graph()
print("DFS:")
g.dfs()

# OUTPUT:
# 0 -> 1 -> 2
# 1 -> 2
# 2 -> 0 -> 3
# 3 -> 3
# DFS:
# 0 1 2 3