Notice
Recent Posts
Recent Comments
Link
ยซ   2024/09   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 28
29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[๋ฐฑ์ค€ 1260๋ฒˆ] DFS์™€ BFS - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 1260๋ฒˆ] DFS์™€ BFS - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 3. 18. 18:21
728x90
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/1260

 

1260๋ฒˆ: DFS์™€ BFS

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ

www.acmicpc.net

const { off } = require('process');
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

class Graph {
  constructor() {
    this.node = {};
  }

  addVertex(vertex) {
    if (!this.node[vertex]) {
      this.node[vertex] = [];
    }
  }

  addEdge(v1, v2) {
    this.node[v1].push(v2);
    this.node[v2].push(v1);
  }

  depthFirstSearch(start) {
    const result = [];
    const visited = {};
    const node = this.node;

    function dfs(vertex) {
      if (!vertex) return;
      result.push(vertex);
      visited[vertex] = true;
      node[vertex]
        .sort((a, b) => a - b)
        .forEach((v) => {
          if (!visited[v]) {
            visited[v] = true;

            return dfs(v);
          }
        });
    }

    dfs(start);
    return result;
  }

  breadthFirstSearch(start) {
    const queue = [start];
    const result = [];
    const visited = {};

    visited[start] = true;

    while (queue.length) {
      const value = queue.shift();
      result.push(value);
      this.node[value]
        .sort((a, b) => a - b)
        .forEach((v) => {
          if (!visited[v]) {
            visited[v] = true;
            queue.push(v);
          }
        });
    }
    return result;
  }
}

let input = [];

rl.on('line', function (line) {
  input.push(line);
  if (input.length === +input[0].split(' ')[1] + 1) {
    rl.close();
  }
}).on('close', function () {
  const [num, ver, start] = input.shift().split(' ');
  const vertex = input.map((v) => v.split(' ').map(Number));

  const graph = new Graph();

  for (let i = 1; i <= +num; i++) {
    graph.addVertex(i);
  }

  vertex.sort((a, b) => a[0] - b[0]);

  vertex.forEach((v) => {
    graph.addEdge(v[0], v[1]);
  });
  console.log(graph.depthFirstSearch(start).join(' '));
  console.log(graph.breadthFirstSearch(start).join(' '));
});

์ค‘์š”ํ•œ ๊ฑด ์ œ์‹œ๋œ ๊ฐ„์„ ๋“ค์„ ์ •๋ ฌํ•˜๊ณ , ๊ฐ„์„ ์„ ๋„ฃ์€ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ๋„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ์ƒํƒœ์—ฌ์•ผ ๋œ๋‹ค!

(์•„๋‹ˆ๋ฉด ์˜ˆ์ œ๊ฐ€ ๋งž๊ณ , ์งˆ๋ฌธํ•˜๊ธฐ์— ์žˆ๋Š” ๋ฐ˜๋ก€๊ฐ€ ๋งž๋“  ๊ณ„์† ํ‹€๋ฆฝ๋‹ˆ๋‹ค.)

 

์šฐ์„  ๊ฐ„์„ ์„ ์ˆซ์ž๋ณ„๋กœ ์ •๋ ฌํ•ด์„œ ๊ฐ’์— ๋„ฃ์–ด์ค€๋‹ค.

 

Graph {
  node: {
    '1': [ 2, 3 ],
    '2': [ 1, 5 ],
    '3': [ 4, 1 ],
    '4': [ 3, 5 ],
    '5': [ 4, 2 ]
  }
}

 

๊ทธ๋ฆฌ๊ณ  bfs, dfs๋ฅผ ์ˆœํšŒํ•  ๋•Œ ํ•ด๋‹น ๊ฐ„์„ ๋“ค์˜ ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ sortํ•ด์ฃผ๋ฉด์„œ ํ•˜๋ฉด ๋œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments