[TIL] Day77 #Tree #Graph #BST

Beanxxยท2022๋…„ 8์›” 21์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
77/120
post-thumbnail

2022.08.16(Tues)

[TIL] Day77
[SEB FE] Day76

โ˜‘๏ธย Tree

๊ทธ๋ž˜ํ”„์˜ ์—ฌ๋Ÿฌ ๊ตฌ์กฐ ์ค‘ ๋‹จ๋ฑกํ–ฅ ๊ทธ๋ž˜ํ”„ ๊ตฌ์กฐ
๐Ÿ‘‰ย Cycle์ด ์—†๋Š” ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„ (Connected Graph)

  • ๋ฐ์ดํ„ฐ์— 1๊ฐœ ๊ฒฝ๋กœ์™€ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์—ฐ๊ฒฐ๋œ ๊ณ„์ธต์  ์ž๋ฃŒ๊ตฌ์กฐ
  • ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ ์•„๋ž˜์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌ ๊ฐ€๋Šฅํ•œ ๋น„์„ ํ˜• ๊ตฌ์กฐ
    (์ˆœ์ฐจ์  ๋‚˜์—ด ์„ ํ˜• ๊ตฌ์กฐ โŒ)
  • ํŠธ๋ฆฌ ๊ตฌ์กฐ๋Š” ๊ณ„์ธต์ ์œผ๋กœ ํ‘œํ˜„, ์•„๋ž˜๋กœ๋งŒ ๋ป—์–ด๋‚˜๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— Cycle ์—†์Œ

โž•ย Cycle: ํ•œ ์ •์ ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋‹ค์‹œ ์ถœ๋ฐœํ–ˆ๋˜ ์ •์ ์œผ๋กœ ๋Œ์•„๊ฐˆ ๋•Œ, ๋‹ค๋ฅธ ์ •์ ๊ณผ ๊ฐ„์„ ์„ ๊ฑฐ์ณ์„œ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ
โž•ย ์ž๊ธฐ ๋ฃจํ”„(self loof): ๋‹ค๋ฅธ ์ •์ ์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ์ •์ ์—์„œ ์ง„์ถœํ•˜๋Š” ๊ฐ„์„ ์ด ๋‹ค์‹œ ์ž์‹ ์—๊ฒŒ ์ง„์ž…ํ•˜๋Š” ๊ฒฝ์šฐ

๐Ÿ”ทย Tree ๊ตฌ์กฐ & ํŠน์ง•

  • Root๋ผ๋Š” ํ•˜๋‚˜์˜ ๊ผญ์ง“์  ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ์ž‘์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ„์„ (edge)์œผ๋กœ ์—ฐ๊ฒฐ
  • 2๊ฐœ์˜ Node๊ฐ€ ์ƒํ•˜ ๊ณ„์ธต์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋ฉด ๋ถ€๋ชจ/์ž์‹ ๊ด€๊ณ„
  • Leaf Node: ์ž์‹์ด ์—†๋Š” ๋…ธ๋“œ

  • ๊นŠ์ด(depth): ๋ฃจํŠธ๋กœ๋ถ€ํ„ฐ ํ•˜์œ„ ๊ณ„์ธต์˜ ํŠน์ • ๋…ธ๋“œ๊นŒ์ง€์˜ ๊นŠ์ด(depth) ํ‘œํ˜„ โฌ‡๏ธ
    • ๋ฃจํŠธ ๋…ธ๋“œ ๊นŠ์ด๋Š” 0
  • ๋ ˆ๋ฒจ(Level): ํŠธ๋ฆฌ ๊ตฌ์กฐ์—์„œ ๊ฐ™์€ ๊นŠ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋…ธ๋“œ๋“ค
    • ํ˜•์ œ ๋…ธ๋“œ(Sibling Node): ๊ฐ™์€ ๋ ˆ๋ฒจ์— ๋‚˜๋ž€ํžˆ ์žˆ๋Š” ๋…ธ๋“œ
  • ๋†’์ด(Height): ๋ฆฌํ”„ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฃจํŠธ๊นŒ์ง€์˜ ๋†’์ด(Height) ํ‘œํ˜„ โฌ†๏ธ
    • ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ์˜ ๊ฐ€์žฅ ๋†’์€ height ๊ฐ’์— +1ํ•œ ๊ฐ’์„ ๋†’์ด๋กœ!
    • ๊ฐ ๋ฆฌํ”„ ๋…ธ๋“œ ๋†’์ด๋ฅผ 0์œผ๋กœ!
  • Sub tree: root์—์„œ ๋ป—์–ด ๋‚˜์˜ค๋Š” ํฐ ํŠธ๋ฆฌ ๋‚ด๋ถ€์˜ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ–์ถ˜ ์ž‘์€ ํŠธ๋ฆฌ

  • Node: ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ด๋ฃจ๋Š” ๋ชจ๋“  ๊ฐœ๋ณ„ ๋ฐ์ดํ„ฐ
  • Root: ํŠธ๋ฆฌ ๊ตฌ์กฐ ์‹œ์ž‘์ ์ด ๋˜๋Š” ๋…ธ๋“œ
  • Parent node: ๋‘ ๋…ธ๋“œ๊ฐ€ ์ƒํ•˜๊ด€๊ณ„๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ๋•Œ ์ƒ๋Œ€์ ์œผ๋กœ ๋ฃจํŠธ์—์„œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ
  • Child node: ๋‘ ๋…ธ๋“œ๊ฐ€ ์ƒํ•˜๊ด€๊ณ„๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ๋•Œ ์ƒ๋Œ€์ ์œผ๋กœ ๋ฃจํŠธ์—์„œ ๋จผ ๋…ธ๋“œ
  • Leaf: ํŠธ๋ฆฌ ๊ตฌ์กฐ์˜ ๋ ์ง€์ ์ด๊ณ , ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ๋…ธ๋“œ

// Class๋กœ Tree ๊ตฌํ˜„ ์˜ˆ์ œ

class Tree {
  // constructor๋กœ ๋งŒ๋“  ๊ฐ์ฒด = ํŠธ๋ฆฌ node!
  constructor(value) {
    this.value = value;
    this.children = []; // ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋“ค์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋„๋ก
  }

  // [ํŠธ๋ฆฌ ์‚ฝ์ž… ๋ฉ”์„œ๋“œ ์ƒ์„ฑ]
  insertNode(value) {
    const childNode = new Tree(value); // tree์˜ ์ž์‹ ๋…ธ๋“œ ์ƒ์„ฑ
    this.children.push(childNode);
  }

  // [tree์—์„œ value๊ฐ’ ํƒ์ƒ‰]
  // ํŠธ๋ฆฌ ์•ˆ์— ํ•ด๋‹น ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ
  contains(value) {
    // ํ˜„์žฌ ๋…ธ๋“œ์˜ value ๊ฐ’๊ณผ ์ฐพ๋Š” ๊ฐ’์ด ์ผ์น˜ํ•œ๋‹ค๋ฉด (๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด)
    if (this.value === value) {
      return true;
    }

    // ๊ฐ’์„ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์ž์‹ ๋…ธ๋“œ ์ˆœํšŒ => ๋…ธ๋“œ์˜ children ๋ฐฐ์—ด ํƒ์ƒ‰
    for (let i = 0; i < this.children.length; i += 1) {
      const childNode = this.children[i];
      if (childNode.contains(value)) {
        return true;
      }
    }

    // ์ „๋ถ€ ํƒ์ƒ‰ํ–ˆ์Œ์—๋„ ๊ฐ’์„ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด false ๋ฐ˜ํ™˜
    return false;
  }
}

// ---------------------------------------------------

// Tree ์‚ฌ์šฉ ์˜ˆ์ œ
const rootNode = new Tree(null);

for (let i = 0; i <= 4; i++) {
  if (rootNode.children[i]) {
    rootNode.children[i].insertNode(i);
  }
  rootNode.insertNode(i);
}
rootNode; // {value: null, children: Array(5)}
rootNode.contains(5); // false
rootNode.contains(1); // true


โ˜‘๏ธย Binary Search Tree (BST)

โžฐย ์ด์ง„ ํŠธ๋ฆฌ(Binary tree): ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ตœ๋Œ€ 2๊ฐœ์ธ ๋…ธ๋“œ๋“ค๋กœ ๊ตฌ์„ฑ๋œ ํŠธ๋ฆฌ
โžฐย BST: ์ด์ง„ ํƒ์ƒ‰(binary search) & ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(linked list)๋ฅผ ๊ฒฐํ•ฉํ•œ ์ด์ง„ํŠธ๋ฆฌ

  • ์ •์ด์ง„ ํŠธ๋ฆฌ(Full binary tree): ๊ฐ ๋…ธ๋“œ๊ฐ€ 0๊ฐœ / 2๊ฐœ ์ž์‹ ๋…ธ๋“œ ๊ฐ–์Œ
  • ํฌํ™” ์ด์ง„ ํŠธ๋ฆฌ(Perfect binary tree): ์ • ์ด์ง„ ํŠธ๋ฆฌ & ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ์ธ ๊ฒฝ์šฐ
    • ๋ชจ๋“  ๋ฆฌํ”„ ๋…ธ๋“œ ๋ ˆ๋ฒจ ๋™์ผํ•˜๊ณ , ๋ชจ๋“  ๋ ˆ๋ฒจ์ด ๊ฐ€๋“ ์ฑ„์›Œ์ ธ ์žˆ๋Š” ํŠธ๋ฆฌ
  • ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ(Complete binary tree): ๋งˆ์ง€๋ง‰ ๋ ˆ๋ฒจ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋“ ์ฐจ ์žˆ์–ด์•ผ ํ•˜๊ณ , ๋งˆ์ง€๋ง‰ ๋ ˆ๋ฒจ ๋…ธ๋“œ๋Š” ์ „๋ถ€ ์ฐจ ์žˆ์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ ์™ผ์ชฝ์ด ์ฑ„์›Œ์ ธ์•ผ ํ•จ

๐Ÿ”ทย BST ํŠน์ง•

  • ๊ฐ ๋…ธ๋“œ์— ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” Key ์กด์žฌ
  • ๋ฃจํŠธ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ๋Š” ํ•ด๋‹น ๋…ธ๋“œ ํ‚ค๋ณด๋‹ค ์ž‘์€ ํ‚ค๋ฅผ ๊ฐ–๋Š” ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ
  • ๋ฃจ๋“œ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ๋Š” ํ•ด๋‹น ๋…ธ๋“œ ํ‚ค๋ณด๋‹ค ํฐ ํ‚ค๋ฅผ ๊ฐ–๋Š” ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ
  • ์ขŒ์šฐ ์„œ๋ธŒํŠธ๋ฆฌ๋„ ๋ชจ๋‘ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์—ฌ์•ผ ํ•จ
    ๐Ÿ‘‰ย ๋ชจ๋“  ์™ผ์ชฝ ์ž์‹ ๊ฐ’์ด ๋ฃจํŠธ๋‚˜ ๋ถ€๋ชจ๋ณด๋‹ค ์ž‘๊ณ , ๋ชจ๋“  ์˜ค๋ฅธ์ชฝ ์ž์‹ ๊ฐ’์ด ๋ฃจํŠธ๋‚˜ ๋ถ€๋ชจ๋ณด๋‹ค ํฐ ๊ฐ’์„ ๊ฐ€์ง
  • ๊ธฐ์กด ์ด์ง„ ํŠธ๋ฆฌ๋ณด๋‹ค ํƒ์ƒ‰ ๋น ๋ฆ„
    • ํŠธ๋ฆฌ ๋†’์ด h โ†’ O(h) ๋ณต์žก๋„
  1. ๋ฃจํŠธ ๋…ธ๋“œ ํ‚ค์™€ ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’ ๋น„๊ต (๋งŒ์•ฝ ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด๋ฉด ํƒ์ƒ‰ ์ข…๋ฃŒ)
  2. ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ๋ฃจํŠธ ๋…ธ๋“œ ํ‚ค๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์™ผ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ๋กœ ํƒ์ƒ‰ ์ง„ํ–‰
  3. ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ๋ฃจํŠธ ๋…ธ๋“œ ํ‚ค๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ๋กœ ํƒ์ƒ‰ ์ง„ํ–‰

ย ย ย ย โœ‹ย ํŠธ๋ฆฌ ์•ˆ์— ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ์—†๋”๋ผ๋„ ์ตœ๋Œ€ h๋ฒˆ ์—ฐ์‚ฐ/ํƒ์ƒ‰ ์ง„ํ–‰


// Class๋กœ ์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ(BST) ๊ตฌํ˜„ ์˜ˆ์ œ

class BinarySearchTree {
  // constructor๋กœ ๋งŒ๋“  ๊ฐ์ฒด๋Š” ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์˜ Node๊ฐ€ ๋จ
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }

  // [์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ์‚ฝ์ž… ๋ฉ”์„œ๋“œ]
  // tree์— value ์ถ”๊ฐ€
  insert(value) {
    // ์ž…๋ ฅ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ์™ผ์ชฝ ๋…ธ๋“œ์—์„œ ์ง„ํ–‰
    if (value < this.value) {
      // ์™ผ์ชฝ์ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ’ ์‚ฝ์ž… (์ฆ‰, this.left์— ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ๊ฒฝ์šฐ, ์ƒˆ๋กœ์šด ์ž์‹ ๋…ธ๋“œ ์ถ”๊ฐ€)
      if (this.left === null) {
        this.left = new BinarySearchTree(value);
      }
      // this.left์˜ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ์ž์‹ ๋…ธ๋“œ์—์„œ insert ์žฌ๊ท€ ์‚ฌ์šฉ
      else {
        this.left.insert(value);
      }
    }

    // ์ž…๋ ฅ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ์—์„œ ์ง„ํ–‰
    else if (value > this.value) {
      // this.right์— ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ๊ฒฝ์šฐ, ์ƒˆ๋กœ์šด ์ž์‹ ๋…ธ๋“œ ์ถ”๊ฐ€
      if (this.right === null) {
        this.right = new BinarySearchTree(value);
      }
      // this.left์˜ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ์ž์‹ ๋…ธ๋“œ์—์„œ insert ์žฌ๊ท€ ์‚ฌ์šฉ
      else {
        this.right.insert(value);
      }
    } else {
      // ์ด๋ฏธ value๊ฐ’์„ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์Œ
    }
  }

  // [์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ์•ˆ์— ํ•ด๋‹น ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ ๋ฉ”์„œ๋“œ]
  // tree์˜ value๊ฐ’ ํƒ์ƒ‰
  contains(value) {
    // ์ฐพ๋Š” value๊ฐ’์ด ๋…ธ๋“œ์˜ value์™€ ์ผ์น˜ํ•œ๋‹ค๋ฉด, true ๋ฆฌํ„ด
    if (value === this.value) {
      return true;
    }
    // ์ฐพ๋Š” value ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ์˜ value๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ์™ผ์ชฝ์—์„œ contains ์žฌ๊ท€ ์ง„ํ–‰
    if (value < this.value) {
      return !!(this.left && this.left.contains(value));
    }
    // ์ฐพ๋Š” value ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ์˜ value๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ์˜ค๋ฅธ์ชฝ์—์„œ contains ์žฌ๊ท€ ์ง„ํ–‰
    if (value > this.value) {
      return !!(this.right && this.right.contains(value));
    }
  }

  // --------------------------------------------

  // [Tree ์ „์œ„ ์ˆœํšŒ]
  preorder(callback) {
    callback(this.value);

    if (this.left) {
      this.left.preorder(callback);
    }
    if (this.right) {
      this.right.preorder(callback);
    }
  }

  // [Tree ์ค‘์œ„ ์ˆœํšŒ]
  inorder(callback) {
    if (this.left) {
      this.left.inorder(callback);
    }
    callback(this.value);
    if (this.right) {
      this.right.inorder(callback);
    }
  }

  // [Tree ํ›„์œ„ ์ˆœํšŒ]
  postorder(callback) {
    if (this.left) {
      this.left.postorder(callback);
    }
    if (this.right) {
      this.right.postorder(callback);
    }
    callback(this.value);
  }
}


โ˜‘๏ธย Tree Traversal

  • ํŠธ๋ฆฌ ์ˆœํšŒ: ํŠน์ • ๋ชฉ์ ์„ ์œ„ํ•ด ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ 1๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒƒ

โœ‹ย ํŠธ๋ฆฌ ๊ตฌ์กฐ์—์„œ ๋…ธ๋“œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์กฐํšŒํ•  ๋•Œ์˜ ์ˆœ์„œ๋Š” ํ•ญ์ƒ ์™ผ์ชฝ โ†’ ์˜ค๋ฅธ์ชฝ

๐Ÿ”นย ์ „์œ„ ์ˆœํšŒ (preorder traverse)

  • ๋ฃจํŠธ์—์„œ ์‹œ์ž‘ํ•ด ์™ผ์ชฝ ๋…ธ๋“œ๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋‘˜๋Ÿฌ๋ณธ ๋’ค, ์™ผ์ชฝ ๋…ธ๋“œ ํƒ์ƒ‰ โ†’ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ ํƒ์ƒ‰
    ๐Ÿ‘‰ย ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ œ์ผ ๋จผ์ € ๋ฐฉ๋ฌธ๋˜๋Š” ์ˆœํšŒ ๋ฐฉ์‹
  • ์ฃผ๋กœ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๋จผ์ € ์ƒ์„ฑ๋˜์–ด์•ผ ํ•˜๋Š” ํŠธ๋ฆฌ๋ฅผ ๋ณต์‚ฌํ•  ๋•Œ ์‚ฌ์šฉ

๐Ÿ”นย ์ค‘์œ„ ์ˆœํšŒ (inorder traverse)

  • ๋ฃจํŠธ๋ฅผ ๊ฐ€์šด๋ฐ์— ๋‘๊ณ  ์ˆœํšŒํ•จ.
  • ๋ฃจํŠธ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ๋…ธ๋“œ ์ˆœํšŒ๊ฐ€ ๋๋‚˜๋ฉด ๋ฃจํŠธ๋ฅผ ๊ฑฐ์ณ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜์—ฌ ๋งˆ์ € ํƒ์ƒ‰
    ๐Ÿ‘‰ย ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์„œ๋ธŒ ํŠธ๋ฆฌ์˜ ๋ฐฉ๋ฌธ ์ค‘๊ฐ„์— ๋ฐฉ๋ฌธ๋˜๋Š” ์ˆœํšŒ ๋ฐฉ์‹
  • ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ

๐Ÿ”นย ํ›„์œ„ ์ˆœํšŒ (postorder traverse)

  • ๋ฃจํŠธ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์ˆœํšŒ
  • ์ œ์ผ ์™ผ์ชฝ ๋ ๋…ธ๋“œ๋ถ€ํ„ฐ ๋ฃจํŠธ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ์˜ค๋ฅธ์ชฝ์„ ์ˆœํšŒํ•œ ๋’ค, ์ œ์ผ ๋งˆ์ง€๋ง‰์— ๋ฃจํŠธ ๋ฐฉ๋ฌธ
  • ํŠธ๋ฆฌ ์‚ญ์ œ์‹œ ์‚ฌ์šฉ (why? ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋จผ์ € ์‚ญ์ œ๋˜์–ด์•ผ ์ƒ์œ„ ๋…ธ๋“œ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ)


โ˜‘๏ธย Graph

์—ฌ๋Ÿฌ๊ฐœ์˜ ์ ๋“ค์ด ์„œ๋กœ ๋ณต์žกํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ

  • ์ง์ ‘์ ์ธ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋‘ ์  ์‚ฌ์ด๋ฅผ ์ด์–ด์ฃผ๋Š” ์„  ์กด์žฌ
  • ๊ฐ„์ ‘์ ์ธ ๊ด€๊ณ„๋ผ๋ฉด ๋ช‡ ๊ฐœ์˜ ์ ๊ณผ ์„ ์— ๊ฑธ์ณ ์ด์–ด์ง
  • ์ •์ (vertex): ํ•˜๋‚˜์˜ ์ 
  • ๊ฐ„์„ (edge): ํ•˜๋‚˜์˜ ์„ 

๐Ÿ”นย Graph ํ‘œํ˜„ ๋ฐฉ์‹

๐Ÿ”ธ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ

: ๊ฐ ์ •์ ์ด ์–ด๋–ค ์ •์ ๊ณผ ์ธ์ ‘ํ•˜๋Š”์ง€ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ํ‘œํ˜„

  • ๊ฐ ์ •์ ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ด ๋ฆฌ์ŠคํŠธ๋Š” ์ž์‹ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์„ ๋‹ด๊ณ  ์žˆ์Œ
  • ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
    • โ†”๏ธย ์ธ์ ‘ ํ–‰๋ ฌ์€ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ๋งŽ์ด ์ฐจ์ง€

๐Ÿ”ธย ์ธ์ ‘ ํ–‰๋ ฌ

: ์„œ๋กœ ๋‹ค๋ฅธ ์ •์ ๋“ค์ด ์ธ์ ‘ํ•œ ์ƒํƒœ์ธ์ง€๋ฅผ ํ‘œ์‹œํ•œ ํ–‰๋ ฌ๋กœ, 2์ฐจ์› ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋ƒ„

  • ๋‘ ์ •์ ์€ ์ธ์ ‘ํ•˜๋‹ค: ๋‘ ์ •์ ์„ ๋ฐ”๋กœ ์ด์–ด์ฃผ๋Š” ๊ฐ„์„  ์กด์žฌ
  • ๋‘ ์ •์ ์ด ์ด์–ด์ ธ ์žˆ๋‹ค๋ฉด 1(true), ์ด์–ด์ ธ ์žˆ์ง€ ์•Š๋‹ค๋ฉด 0(false)์œผ๋กœ ํ‘œ์‹œ
  • ๋‘ ์ •์  ์‚ฌ์ด์˜ ๊ด€๊ณ„ ์œ ๋ฌด ํ™•์ธ์‹œ ์šฉ์ด
  • ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ณ ์ž ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ

// Class๋กœ ์ธ์ ‘ํ–‰๋ ฌ ๊ตฌํ˜„ ์˜ˆ์ œ

// Directed graph(๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„)
// unweighted(๋น„๊ฐ€์ค‘์น˜)
// adjacency matrix(์ธ์ ‘ ํ–‰๋ ฌ)
// ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ •์ ์œผ๋กœ ์‚ฌ์šฉ(ex. 0, 1, 2, ... --> ์ •์ )

class GraphWithAdjacencyMatrix {
  // constructor ๊ตฌํ˜„
  constructor() {
    this.matrix = [];
  }

  // [vertex(์ •์ ) ์ถ”๊ฐ€]
  addVertex() {
    const currentLength = this.matrix.length;
    for (let i = 0; i < currentLength; i++) {
      this.matrix[i].push(0);
    }
    this.matrix.push(new Array(currentLength + 1).fill(0));
  }

  // [vertex(์ •์ ) ํƒ์ƒ‰]
  contains(vertex) {
    // this.matrix์— vertex ์กด์žฌ -> true, ์•„๋‹ˆ๋ฉด -> false ๋ฆฌํ„ด
    return !!this.matrix[vertex];
  }

  // [1. vertex(์ •์ )๊ณผ edge(๊ฐ„์„ ) ์ถ”๊ฐ€]
  addEdge(from, to) {
    const currentLength = this.matrix.length - 1;

    if (from === undefined || to === undefined) {
      console.log("2๊ฐœ์˜ ์ธ์ž๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
      return;
    }

    // ๊ฐ„์„ ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์—์„  ์ถ”๊ฐ€ X
    // from vertex์™€ to vertex๊ฐ€ ์ „์ฒด ๊ทธ๋ž˜ํ”„์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด return
    if (from > currentLength || to > currentLength || from < 0 || to < 0) {
      console.log("๋ฒ”์œ„๊ฐ€ ๋งคํŠธ๋ฆญ์Šค ๋ฐ–์— ์žˆ์Šต๋‹ˆ๋‹ค.");
      return;
    }

    // this.matrix[from][to]์˜ ๊ฐ’์„ 1๋กœ ๋ฐ”๊ฟ”์คŒ -> edge์œผ๋กœ ์—ฐ๊ฒฐ์ด ๋˜์–ด ์žˆ์Œ ํ‘œ์‹œ
    this.matrix[from][to] = 1;
  }

  // 2. from vertex์™€ to vertex ์‚ฌ์ด์— edge๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํŒ๋‹จ
  hasEdge(from, to) {
    return !!this.matrix[from][to];
  }

  // [3. edge(๊ฐ„์„ ) ์ œ๊ฑฐ]
  // from vertex์™€ to vertex ์‚ฌ์ด์— ๊ด€๋ จ์ด ์—†๋‹ค๋ฉด, edge ์ œ๊ฑฐ
  removeEdge(from, to) {
    const currentLength = this.matrix.length - 1;

    if (from === undefined || to === undefined) {
      console.log("2๊ฐœ์˜ ์ธ์ž๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
      return;
    }

    // from vertex์™€ to vertex๊ฐ€ ์ „์ฒด ๊ทธ๋ž˜ํ”„์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด return
    if (from > currentLength || to > currentLength || from < 0 || to < 0) {
      console.log("๋ฒ”์œ„๊ฐ€ ๋งคํŠธ๋ฆญ์Šค ๋ฐ–์— ์žˆ์Šต๋‹ˆ๋‹ค.");
      return;
    }

    // ๊ฐ„์„  ์‚ญ์ œ
    // this.matrix[from][to]์˜ ๊ฐ’์„ 0์œผ๋กœ ๋ฐ”๊ฟ”์คŒ -> ๊ด€๋ จ ์—†์Œ์„ ํ‘œ์‹œ
    this.matrix[from][to] = 0;
  }
}

  • ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„: ์ •์ ๋“ค์ด ๊ฐ„์„ ์œผ๋กœ ์ „๋ถ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ทธ๋ž˜ํ”„
  • ๋น„์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„: ์ •์ ์ด ํ•˜๋‚˜๋ผ๋„ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ทธ๋ž˜ํ”„
  • ๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„: ์ถ”๊ฐ€์ ์ธ ์ •๋ณด๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์—†๋Š” ๊ทธ๋ž˜ํ”„
  • ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„: ๊ฐ„์„ ์— ์—ฐ๊ฒฐ ๊ฐ•๋„๋ฅผ ํ‘œํ˜„ํ•œ ๊ทธ๋ž˜ํ”„


โ˜‘๏ธย BFS & DFS

: ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์œผ๋กœ, ์ฃผ๋กœ ๋‘ ์ •์  ์‚ฌ์ด ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉ
โœ‹ย ์ถœ๋ฐœํ•˜๋Š” ์ •์ ๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฏ€๋กœ ๊ฒฝ๋กœ ํƒ์ƒ‰์‹œ ๊ฐ€์žฅ ๋จผ์ € ์ฐพ์•„์ง€๋Š” ํ•ด๋‹ต์ด ๊ณง ์ตœ๋‹จ๊ฑฐ๋ฆฌ!

: ๊นŠ์ด๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ํ•œ ์ •์ ์—์„œ ๋‹ค์Œ ๊ฒฝ๋กœ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ์™„๋ฒฝํžˆ ํƒ์ƒ‰ํ•  ๋•Œ ์‚ฌ์šฉ
โœ‹ย BFS๋ณด๋‹จ ํƒ์ƒ‰ ์‹œ๊ฐ„์ด ๋” ๊ฑธ๋ฆด์ง€๋ผ๋„ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์™„์ „ํžˆ ํƒ์ƒ‰ ๊ฐ€๋Šฅ

profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€