πŸ“DFS 기초 λ¬Έμ œν’€κΈ°

10_2pangΒ·2023λ…„ 6μ›” 5일
0

βš½οΈνŠΈλŸ¬λΈ”μŠˆνŒ…

λͺ©λ‘ 보기
50/94
post-thumbnail

πŸ‘¨β€πŸ’»Β μ‚¬κ±΄


μ½”λ”©ν…ŒμŠ€νŠΈ μ€€λΉ„ν•˜κΈ° μ•žμ„œ, DFS 에 λŒ€ν•œ κ°œλ…μ„ 작고자 ν…ŒμŠ€νŠΈ ν•΄λ³΄μ•˜λ‹€.

βœ…Β ν•΄κ²°


  • μ½”λ“œ
const graph = {
  A: ["B", "C"],
  B: ["A", "D"],
  C: ["A", "G", "H", "I"],
  D: ["B", "E", "F"],
  E: ["D"],
  F: ["D"],
  G: ["C"],
  H: ["C"],
  I: ["C", "J"],
  J: ["I"],
};

// (graph, μ‹œμž‘ 정점)
const dfs = (graph, startNode) => {
  let needVisitStack = []; // 탐색을 ν•΄μ•Ό ν•  λ…Έλ“œλ“€
  let visitedQueue = []; // 탐색을 마친 λ…Έλ“œλ“€

  needVisitStack.push(startNode);

  // 탐색을 ν•΄μ•Ό ν•  λ…Έλ“œκ°€ 남아 μžˆλ‹€λ©΄
  while (needVisitStack.length !== 0) {
    const node = needVisitStack.pop();
    console.log("λ…Έλ“œ:",node)
    if (!visitedQueue.includes(node)) {
      visitedQueue.push(node);
        console.log('λ°©λ¬Έν•œ 큐',visitedQueue)
      needVisitStack = [...needVisitStack, ...graph[node]];
        console.log("λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ",needVisitStack)
        console.log("-----------------");
    }
      console.log("-----------------");
  }

  return visitedQueue;
};

console.log(dfs(graph, "A"));
  • κ²°κ³Ό
탐색할 λ…Έλ“œ: A
λ°©λ¬Έν•œ 큐 ['A']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (2) ['B', 'C']
-----------------
-----------------
탐색할 λ…Έλ“œ: C
λ°©λ¬Έν•œ 큐 (2) ['A', 'C']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (5) ['B', 'A', 'G', 'H', 'I']
-----------------
-----------------
탐색할 λ…Έλ“œ: I
λ°©λ¬Έν•œ 큐 (3) ['A', 'C', 'I']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (6) ['B', 'A', 'G', 'H', 'C', 'J']
-----------------
-----------------
탐색할 λ…Έλ“œ: J
λ°©λ¬Έν•œ 큐 (4) ['A', 'C', 'I', 'J']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (6) ['B', 'A', 'G', 'H', 'C', 'I']
-----------------
-----------------
탐색할 λ…Έλ“œ: I // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: C // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: H
λ°©λ¬Έν•œ 큐 (5) ['A', 'C', 'I', 'J', 'H']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (4) ['B', 'A', 'G', 'C']
-----------------
-----------------
탐색할 λ…Έλ“œ: C // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: G
λ°©λ¬Έν•œ 큐 (6) ['A', 'C', 'I', 'J', 'H', 'G']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (3) ['B', 'A', 'C']
-----------------
-----------------
탐색할 λ…Έλ“œ: C // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: A // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: B
λ°©λ¬Έν•œ 큐 (7) ['A', 'C', 'I', 'J', 'H', 'G', 'B']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (2) ['A', 'D']
-----------------
-----------------
탐색할 λ…Έλ“œ: D
λ°©λ¬Έν•œ 큐 (8) ['A', 'C', 'I', 'J', 'H', 'G', 'B', 'D']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (4) ['A', 'B', 'E', 'F']
-----------------
-----------------
탐색할 λ…Έλ“œ: F
λ°©λ¬Έν•œ 큐 (9) ['A', 'C', 'I', 'J', 'H', 'G', 'B', 'D', 'F']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (4) ['A', 'B', 'E', 'D']
-----------------
-----------------
탐색할 λ…Έλ“œ: D // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: E
λ°©λ¬Έν•œ 큐 (10) ['A', 'C', 'I', 'J', 'H', 'G', 'B', 'D', 'F', 'E']
λ°©λ¬Έν•΄μ•Όν•  μŠ€νƒ (3) ['A', 'B', 'D']
-----------------
-----------------
탐색할 λ…Έλ“œ: D // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: B // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
탐색할 λ…Έλ“œ: A // λ°©λ¬Έν•œ 큐에 쑴재
-----------------
(10) ['A', 'C', 'I', 'J', 'H', 'G', 'B', 'D', 'F', 'E']
profile
μ£Όλ‹ˆμ–΄ ν”„λ‘ νŠΈμ—”λ“œ 개발자 이광렬 μž…λ‹ˆλ‹€ 🌸

0개의 λŒ“κΈ€