-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.py
27 lines (26 loc) · 827 Bytes
/
dfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# create an adjacency list from an adjacency matrix
# run dfs on the adjacency list
# 1. create a dict of ajd list
# 2. calling dfs on multiple unvisited sources
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
adj = defaultdict(list)
count = 0
visit = set()
def dfs(x):
if x in visit:
return
visit.add(x)
for neigh in adj[x]:
dfs(neigh)
n = len(isConnected)
for i in range(n):
for j in range(n):
if isConnected[i][j]:
adj[i].append(j)
adj[j].append(i)
for key in adj:
if key not in visit:
dfs(key)
count += 1
return count