Jieunny์˜ ๋ธ”๋กœ๊ทธ

S4) Unit 1. [์‹ค์Šต] Tree & Graph ๋ฌธ์ œ ๋ณธ๋ฌธ

CodeStates/Training

S4) Unit 1. [์‹ค์Šต] Tree & Graph ๋ฌธ์ œ

Jieunny 2023. 3. 15. 17:00

๐Ÿ“ฃ  ์ธ์ ‘ ํ–‰๋ ฌ ๊ธธ์ฐพ๊ธฐ

Q. ์ฃผ์–ด์ง„ ์ธ์ ‘ํ–‰๋ ฌ์—์„œ ํ•œ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ์ •์ ์œผ๋กœ ์ด์–ด์ง€๋Š” ๊ธธ์ด ์กด์žฌํ•˜๋Š”์ง€ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

function getDirections(matrix, from, to) {
  // TODO: ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  if(matrix[from][to] === 1) return true;

  const queue = [];
  queue.push(from);
  const isVisited = new Array(matrix.length).fill(false);
  isVisited[from] = true;
  //console.log(matrix[0].length);
  console.log(isVisited);

  while(queue.length > 0){
    let pop = queue.shift();
    if(pop === to) return true;
    // ๋‚ด๊ฐ€ ํ์—์„œ ๋บ€ ์ •์ ์ด ๋„์ฐฉ์ง€์ ์ด๋ผ๋ฉด true ๋ฐ˜ํ™˜
    for(let i=0; i<matrix[pop].length; i++){
      // ํ์—์„œ ๋บ€ ์ •์ ์˜ ๊ฐ„์„  ํ™•์ธ
      if(matrix[pop][i] && isVisited[i] === false){
        // ๊ฐ„์„ ์ด ์žˆ๊ณ , ํ•ด๋‹น ์ •์  ์ง€๋‚˜๊ฐ„ ์  ์—†์œผ๋ฉด
        queue.unshift(i);
        // ๋‹ค์Œ์— ๊ฐ€๊ธฐ ์œ„ํ•ด์„œ ํ์— ์ €์žฅ
        isVisited[i] = true;
      }
    }
  }
  return false;
  // ๋‹ค ๋Œ์•˜๋Š”๋ฐ๋„ ์ฐพ๋Š” ์ •์ ์— ๋„๋‹ฌํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด false ๋ฐ˜ํ™˜
}

 


๐Ÿ“ฃ  ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค

 

Q. ๋ฐฉํ–ฅ์ด ์—†๋Š” ๊ฐ„์„ ๋“ค์˜ ๋ชฉ๋ก์ด ์ฃผ์–ด์งˆ ๋•Œ, ์—ฐ๊ฒฐ๋œ ์ •์ ์˜ ์ปดํฌ๋„ŒํŠธ(๊ทธ๋ฃน๋“ค)๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

function connectedVertices(edges) {
  // TODO: ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  const max = Math.max(...edges.flat());
  // flat() => ๋ชจ๋“  ๋ฐฐ์—ด์„ ์ด์–ด๋ถ™์ธ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
  const matrix = new Array(max + 1).fill(0).map(item => new Array(max + 1).fill(0));
  // ์ธ์ ‘ ํ–‰๋ ฌ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
  let isvisited = new Array(matrix.length).fill(false);
  // ๋ฐฉ๋ฌธํ•œ ์  ์žˆ๋Š”์ง€ ์ €์žฅํ•  ๋ฐฐ์—ด

  for(let i=0; i<edges.length; i++){
  // ๋งŒ๋“ค์–ด ๋‘” ์ธ์ ‘ํ–‰๋ ฌ์— ์ธ์ž๋กœ ๋ฐ›์€ ๋ฐฐ์—ด ๋Œ๋ฉด์„œ ์ธ์ ‘ํ–‰๋ ฌ ์™„์„ฑํ•ด์ฃผ๊ธฐ
    matrix[edges[i][0]][edges[i][1]] = 1;
    matrix[edges[i][1]][edges[i][0]] = 1;
  }

  let count = 0;
  //i๋ฒˆ์งธ ์ •์ ์„ ์ง€๋‚˜๊ฐ„ ์ ์ด ์—†๋‹ค๋ฉด bfs๋ฅผ ์‹คํ–‰
    for (let i = 0; i < matrix.length; i++) {
      if (!isvisited[i]) {
      // ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด
        bfs(matrix, i, isvisited);
        // bfs๋กœ ์‹คํ–‰ํ•ด์ค€๋‹ค.
        count++;
      }
    }
    return count;
}

function bfs(matrix, from, isvisited) {
// ๋ชจ๋“  ์ •์  ๋Œ๋ฉด์„œ ํ™•์ธ
  let queue = [from];
  // ์‹œ์ž‘ํ•  ์ •์  queue์— ๋„ฃ์–ด์ฃผ๊ณ 
  isvisited[from] = true;
  // queue์— ๋„ฃ์–ด์ค€ ์ •์ ์€ ๋ฐฉ๋ฌธ ํ•œ๊ฑฐ๋‹ˆ๊นŒ ๋ฐฉ๋ฌธ ๋ฐฐ์—ด true๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
  while (queue.length > 0) {
    let pop = queue.pop();
    // ํ์—์„œ ํ•˜๋‚˜์”ฉ ๋นผ๋ฉด์„œ
    for (let i = 0; i < matrix[pop].length; i++) {
    // ์ง€๊ธˆ ์žˆ๋Š” ์ •์ ์—์„œ ๋ชจ๋“  ์ •์ ์„ ๋Œ๋ฉด์„œ
      if (matrix[pop][i] === 1 && !isvisited[i]) {
      // ์ง€๊ธˆ ์žˆ๋Š” ์ •์ ์—์„œ ์ธ์ ‘ํ•œ ์ •์  ์ฐพ๊ณ , ๊ทธ ์ •์ ์„ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด
        queue.unshift(i);
        // ํ์˜ ๋งจ ์•ž์— ๊ทธ ์ •์ ์„ ๋„ฃ์–ด์ฃผ๊ณ 
        isvisited[i] = true;
      	// ๊ทธ ์ •์  ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ‘œ์‹œํ•ด์ฃผ๊ธฐ
      }
    }
  }
}