[TIL]21.07.31

박주홍·2021λ…„ 7μ›” 31일
0

Today I Learned

λͺ©λ‘ 보기
45/104

πŸ‘¨β€πŸ’» 였늘 κ³΅λΆ€ν•œ 것

μ˜€λŠ˜μ€ μΈμ ‘λ¦¬μŠ€νŠΈλ₯Ό μΈμ ‘ν–‰λ ¬λ‘œ λ°”κΎΈλŠ” λ‘œμ§μ„ μž‘μ„±ν–ˆλ‹€.

μΈμ ‘λ¦¬μŠ€νŠΈλ₯Ό μΈμ ‘ν–‰λ ¬λ‘œ λ°”κΎΈλŠ” κ³Όμ •μ—μ„œ

function addVertex(){
    for(let i = 0; i < matrix.length; i++){
      matrix.push(0);
    }
    matrix.push(new Array(matrix.length + 1).fill(0));
  }

μ²˜μŒμ— λ‘œμ§μ„ μ΄λ ‡κ²Œ κ΅¬ν˜„ν•΄μ„œ μ—λŸ¬κ°€ λ‚¬μ—ˆλ‹€.

forλ¬Έμ•ˆμ— matrixλ‘œν•΄μ„œ pushλ₯Ό ν•  경우 λ‹€μŒ matrixκ°€

0: 0
1: 0
2: 0
3: [0, 0, 0]

μ΄λ ‡κ²Œ λœλ‹€. 각 property λ§ˆλ‹€ 즉 vertex λ§ˆλ‹€ [0, 0, 0] 와 같이 인접행렬을 κ°€μ Έμ•Όν•˜κΈ° λ•Œλ¬Έμ— [i]λ₯Ό λΆ™μ—¬μ€˜μ•Όν•œλ‹€.

function μΈμ ‘λ¦¬μŠ€νŠΈλ₯Ό μΈμ ‘ν–‰λ ¬λ‘œ λ°”κΎΈλŠ” ν•¨μˆ˜(insertEdges, removeEdges) {
  // 1. insertEdegsλŒ€λ‘œ μ΅œλŒ€λ²„ν…μŠ€μ˜ 수λ₯Ό κ΅¬ν•˜κ³  μ΅œλŒ€ λ²„ν…μŠ€ 수만큼 adjacency Listλ₯Ό κ΅¬ν•˜κ³ 
  // insertEdegseλŒ€λ‘œ adjacency Listλ₯Ό κ΅¬μ„±ν•œλ‹€.
  // 2. removeEdgesλŒ€λ‘œ μœ„μ™€ λ§ˆμ°¬κ°€μ§€λ‘œ adjacency Listλ₯Ό κ΅¬μ„±ν•œλ‹€.
  // 3. insertEdegs둜 λ§Œλ“  adjacency List와 removeEdges둜 λ§Œλ“  adjacency Listλ₯Ό 비ꡐ
  // ν•˜μ—¬ λ§Œμ•½ 쀑볡이 μ•ˆλ˜λŠ” 경우 μƒˆλ‘œμš΄ adjacency List을 λ§Œλ“€μ–΄ κ΅¬μ„±ν•œλ‹€.
  // 4. μ€‘λ³΅λ˜λŠ” vertexλŠ” μ œμ™Έν•œ insertAdListλ₯Ό λ§Œλ“ λ‹€.
  let maxVertex = insertEdges.reduce((a, c) => {
    const bigger = Math.max(...c);
    if (bigger > a) return bigger;
    return a;
  }, 0);
  let insertAdList = {};
  let removeAdList = {};

  for (let i = 0; i <= maxVertex; i++) {
    insertAdList[i] = [];
    removeAdList[i] = [];
  }

  for (let i = 0; i < insertEdges.length; i++) {
    insertAdList[insertEdges[i][0]].push(insertEdges[i][1]);
    insertAdList[insertEdges[i][1]].push(insertEdges[i][0]);
  }

  for (let i = 0; i < removeEdges.length; i++) {
    removeAdList[removeEdges[i][0]].push(removeEdges[i][1]);
    removeAdList[removeEdges[i][1]].push(removeEdges[i][0]);
  }

  for (let i = 0; i < Object.keys(insertAdList).length; i++) {
    for (let vertex = 0; vertex < removeAdList[i].length; vertex++) {
      if (insertAdList[i].includes(removeAdList[i][vertex])) {
        insertAdList[i] = insertAdList[i].filter((el) => el !== removeAdList[i][vertex]);
      }
    }
  }


// 5. adjacency matrixλ₯Ό λ§Œλ“€ κ²ƒμž„.
let matrix = [];
function addVertex(){
    for(let i = 0; i < matrix.length; i++){
      matrix[i].push(0);
    }
    matrix.push(new Array(matrix.length + 1).fill(0));
  }
for(let i = 0; i <= maxVertex; i++){
    addVertex();
}


for(let vertex = 0; vertex <= maxVertex; vertex++){
    for(let i = 0; i < insertAdList[vertex].length; i++){
        matrix[vertex][insertAdList[vertex][i]] = 1;
    }
}

    return matrix;
}

const insertEdges = [
  [0, 2],
  [2, 4],
  [1, 3],
  [2, 1],
];
const removeEdges = [
  [0, 3],
  [2, 1],
  [1, 0],
  [4, 2]
];

let output2 = μΈμ ‘λ¦¬μŠ€νŠΈλ₯Ό μΈμ ‘ν–‰λ ¬λ‘œ λ°”κΎΈλŠ” ν•¨μˆ˜(insertEdges2, removeEdges2);

console.log(output2);
/**
 * [
 *  [0, 0, 1, 0, 0],
 *  [0, 0, 0, 1, 0],
 *  [1, 0, 0, 0, 0],
 *  [0, 1, 0, 0, 0],
 *  [0, 0, 0, 0, 0],
 * ]
 */
profile
κ³ ν†΅μ—†λŠ” μ„±μž₯은 μ—†λ‹€κ³  ν•  수 μžˆκ² λ‹€....

0개의 λŒ“κΈ€