Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 913c589

Browse files
refactor: update largest_component_v2.py
1 parent 76fd2ad commit 913c589

File tree

1 file changed

+83
-36
lines changed

1 file changed

+83
-36
lines changed
Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,83 @@
1-
def largest_component(graph):
2-
max = 0
3-
visited = set()
4-
for node in graph:
5-
size = explore_size(graph, node, visited)
6-
if size > max:
7-
max = size
8-
9-
return max
10-
11-
def explore_size(graph, node, visited):
12-
stack = [node]
13-
size = 0
14-
15-
while stack:
16-
current = stack.pop()
17-
if current in visited:
18-
continue
19-
20-
size += 1
21-
visited.add(current)
22-
23-
for neighbor in graph[current]:
24-
stack.append(neighbor)
25-
26-
return size
27-
28-
"""
29-
Depth first iterative approach
30-
31-
n = number of nodes
32-
e = number edges
33-
Time: O(e)
34-
Space: O(n)
35-
36-
"""
1+
def largest_component(graph: dict[int, list[int]]) -> int:
2+
"""
3+
Finds the size of the largest connected component in an undirected graph.
4+
5+
This function takes an undirected graph represented as a dictionary `graph` as input.
6+
The graph is a dictionary where keys are nodes (integers) and values are lists of neighbors
7+
(other nodes connected to the key node). The function uses an iterative depth-first search (DFS)
8+
approach to explore connected components and returns the size of the largest component
9+
(the largest group of nodes reachable from each other).
10+
11+
Args:
12+
graph (dict[int, list[int]]): A dictionary representing the undirected graph. Keys are nodes,
13+
values are lists of neighboring nodes.
14+
15+
Returns:
16+
int: The size of the largest connected component in the graph.
17+
"""
18+
19+
visited = set() # Set to keep track of visited nodes during DFS exploration
20+
max_size = 0 # Variable to store the size of the largest component found so far
21+
22+
# Iterate through each node in the graph (potential starting point for a DFS exploration)
23+
for node in graph:
24+
if node not in visited: # Only explore unvisited nodes (avoid revisiting)
25+
size = explore_size(graph, node, visited) # Explore the connected component starting from this node
26+
max_size = max(max_size, size) # Update max size if the current component is larger
27+
28+
return max_size
29+
30+
31+
def explore_size(graph: dict[int, list[int]], node: int, visited: set[int]) -> int:
32+
"""
33+
Explores a connected component in the graph using iterative DFS and returns its size.
34+
35+
This helper function performs an iterative DFS traversal starting from a given node (`node`)
36+
in the graph represented by the dictionary `graph`. It keeps track of visited nodes in the `visited` set.
37+
The function uses a stack to keep track of nodes to explore. It repeatedly pops a node from the stack,
38+
marks it as visited, and adds its unvisited neighbors to the stack. This process continues until the
39+
stack is empty, signifying the exploration of the entire connected component. The function returns
40+
the total number of nodes visited (size of the connected component).
41+
42+
Args:
43+
graph (dict[int, list[int]]): A dictionary representing the undirected graph. Keys are nodes,
44+
values are lists of neighboring nodes.
45+
node (int): The starting node for the DFS exploration.
46+
visited (set[int]): A set to keep track of visited nodes during DFS exploration.
47+
48+
Returns:
49+
int: The size (number of nodes) of the connected component explored from the starting node.
50+
"""
51+
52+
stack = [node] # Stack to store nodes to be explored
53+
size = 0 # Initialize the size of the connected component
54+
55+
while stack:
56+
current = stack.pop() # Get the next node to explore from the stack
57+
if current in visited: # Skip already visited nodes (avoid cycles)
58+
continue
59+
60+
visited.add(current) # Mark the current node as visited
61+
size += 1 # Increment the size of the connected component
62+
63+
# Add unvisited neighbors of the current node to the stack for further exploration
64+
for neighbor in graph[current]:
65+
if neighbor not in visited:
66+
stack.append(neighbor)
67+
68+
return size
69+
70+
# Time Complexity: O(E)
71+
# The time complexity of this algorithm is O(E), where E is the number of edges in the graph. This is because
72+
# the `explore_size` function performs an iterative DFS traversal. In the worst case, it might visit every edge
73+
# in the graph once to explore all connected components. The outer loop in `largest_component` iterates through
74+
# all nodes (V), but the dominant factor is the exploration within connected components.
75+
76+
# Space Complexity: O(n)
77+
# The space complexity of this algorithm is O(V), where V is the number of nodes in the graph. This is because
78+
# the `visited` set stores information about visited nodes, and in the worst case, it might need to store all
79+
# nodes in the graph if they are part of a large connected component. Additionally, the stack used in the
80+
# iterative DFS can grow up to V nodes deep in the worst case, depending on the structure of the graph.
81+
82+
# Note:
83+
# This algorithm utilizes an iterative depth-first search (DFS) approach to explore connected components

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /