Skip to content

Commit

Permalink
programmers level1 네트워크 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
AEJIJEON committed May 24, 2021
1 parent 0048370 commit bec3662
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions AejiJeon/programmers/210516sprint/네트워크.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# graph is represented in adjacent matrix -> find the number of connected components
from collections import deque

# explores all nodes of a connected component
def bfs(graph, visited, start, n):
q = deque()
q.append(start)
visited[start] = True
while q:
now = q.popleft()
for i in range(n):
if graph[now][i] == 1 and not visited[i]:
q.append(i)
visited[i] = True


def solution(n, computers):
answer = 0
visited = [False] * n
for i in range(n):
# find connected component
if not visited[i]:
# explores all nodes of the component
bfs(computers, visited, i, n)
answer += 1
return answer


print(solution(3, [[1, 1, 0], [1, 1, 0], [0, 0, 1]]))
print(solution(3, [[1, 1, 0], [1, 1, 1], [0, 1, 1]]))

0 comments on commit bec3662

Please sign in to comment.