[Algorithm] 네트워크
공부

[Algorithm] 네트워크

작성일: 2023년 10월 10일0

문제 설명

네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다.

컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크의 개수를 return 하도록 solution 함수를 작성하시오.

제한사항

컴퓨터의 개수 n은 1 이상 200 이하인 자연수입니다.

각 컴퓨터는 0부터 n-1인 정수로 표현합니다.

i번 컴퓨터와 j번 컴퓨터가 연결되어 있으면 computers[i][j]를 1로 표현합니다.

computer[i][i]는 항상 1입니다.

입출력 예

3	| [[1, 1, 0], [1, 1, 0], [0, 0, 1]]	| 2
3	| [[1, 1, 0], [1, 1, 1], [0, 1, 1]]	| 1

입출력 예 설명

{
  "object": "block",
  "id": "53cb5087-894d-4bcd-8c69-71a1a5947311",
  "parent": {
    "type": "page_id",
    "page_id": "390765fe-8b67-815d-afa0-c7b94278c826"
  },
  "created_time": "2026-07-01T14:53:00.000Z",
  "last_edited_time": "2026-07-01T14:53:00.000Z",
  "created_by": {
    "object": "user",
    "id": "2ecd872b-594c-81f7-92e3-000210a8d473"
  },
  "last_edited_by": {
    "object": "user",
    "id": "2ecd872b-594c-81f7-92e3-000210a8d473"
  },
  "has_children": false,
  "in_trash": false,
  "type": "heading_4",
  "heading_4": {
    "rich_text": [
      {
        "type": "text",
        "text": {
          "content": "입출력 예 #1",
          "link": null
        },
        "annotations": {
          "bold": false,
          "italic": false,
          "strikethrough": false,
          "underline": false,
          "code": false,
          "color": "default"
        },
        "plain_text": "입출력 예 #1",
        "href": null
      }
    ],
    "is_toggleable": false,
    "color": "default"
  },
  "archived": false
}

아래와 같이 2개의 네트워크가 있습니다.

Notion Image
{
  "object": "block",
  "id": "cf389c0c-b64f-4f75-9ced-7f4cc2d3bc6c",
  "parent": {
    "type": "page_id",
    "page_id": "390765fe-8b67-815d-afa0-c7b94278c826"
  },
  "created_time": "2026-07-01T14:53:00.000Z",
  "last_edited_time": "2026-07-01T14:53:00.000Z",
  "created_by": {
    "object": "user",
    "id": "2ecd872b-594c-81f7-92e3-000210a8d473"
  },
  "last_edited_by": {
    "object": "user",
    "id": "2ecd872b-594c-81f7-92e3-000210a8d473"
  },
  "has_children": false,
  "in_trash": false,
  "type": "heading_4",
  "heading_4": {
    "rich_text": [
      {
        "type": "text",
        "text": {
          "content": "입출력 예 #2",
          "link": null
        },
        "annotations": {
          "bold": false,
          "italic": false,
          "strikethrough": false,
          "underline": false,
          "code": false,
          "color": "default"
        },
        "plain_text": "입출력 예 #2",
        "href": null
      }
    ],
    "is_toggleable": false,
    "color": "default"
  },
  "archived": false
}

아래와 같이 1개의 네트워크가 있습니다.

Notion Image

문제 해결

주어진 컴퓨터들의 연결이 담긴 배열을 재귀적으로 순회하면서 네트워크 개수를 찾는다.

function solution(n, computers) {
  // n개의 방문하지 않은 컴퓨터
  const visited = new Array(n).fill(false);
  let network = 0;
  
  function dfs(com) {
    // 컴퓨터를 방문했다
  visited[com] = true;
    for (let i = 0; i < n; i++) {
		if (computers[com][i] === 1 && !visited[i]) {
         dfs(i)
        }
    }
  }
  
  for (let i = 0; i < n; i++) {
  	if (!visited[i]) {
      dfs(i);
      network++;
    }
  }
  return network;
}