๐Ÿ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • Depth First Search
  • ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(Depth-First Search)
  • ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์˜ ํŠน์ง•

Was this helpful?

  1. algorithm

DepthFirstSearch

PreviousGreedyNextbasic

Last updated 3 years ago

Was this helpful?

Depth First Search

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(Depth-First Search)

: ๋ฃจํŠธ ๋…ธ๋“œ(ํ˜น์€ ๋‹ค๋ฅธ ์ž„์˜์˜ ๋…ธ๋“œ)์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ(branch)๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์˜ ํŠน์ง•

  • ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ์ˆœํ™˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ˜•ํƒœ ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • ์ „์œ„ ์ˆœํšŒ(Pre-Order Traversals)๋ฅผ ํฌํ•จํ•œ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ํŠธ๋ฆฌ ์ˆœํšŒ๋Š” ๋ชจ๋‘ DFS์˜ ํ•œ ์ข…๋ฅ˜์ด๋‹ค.

  • ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•  ๋•Œ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€, ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์˜ ๊ฒฝ์šฐ ์–ด๋–ค ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ฒ€์‚ฌ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

  • ์ด๋ฅผ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์งˆ ์œ„ํ—˜์ด ์žˆ๋‹ค.

public class DFSGraph {
    private int V; // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
    private LinkedList<Integer> adj[]; // ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ

    /* ์ƒ์„ฑ์ž */
    public DFSGraph(int v) {
        V = v;
        adj = new LinkedList[v];
        for (int i = 0; i < v; ++i) // ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์ดˆ๊ธฐํ™”
            adj[i] = new LinkedList();
    }

    /* ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐ */
    void addEdge(int v, int w) {
        adj[v].add(w);
    }

    /* DFS์— ์˜ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜ */
    void DFSUtil(int v, boolean visited[]) {
        // ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•œ ๊ฑฐ์Šน๋กœ ํ‘œ์‹œํ•˜๊ณ  ๊ฐ’์„ ์ถœ๋ ฅ 
        visited[v] = true;
        System.out.print(v + " ");

        // ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์™€์ธ์ ‘ํ•œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค 
        Iterator<Integer> i = adj[v].listIterator();
        while (i.hasNext()) {
            int n = i.next();
            // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋Š” ๋…ธ๋“œ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ๋‹ค์‹œ DFSUtil ํ˜ธ์ถœ
            if (!visited[n]) 
                DFSUtil(n, visited);// ์ˆœํ™˜ ํ˜ธ์ถœ
        }
    }

    void DFS(int v) {
        // ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ํŒ๋‹จ 
        boolean visited[] = new boolean[V];

        // ๋น„์—ฐ๊ฒฐํ˜• ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ, ๋ชจ๋“  ์ •์ ์„ ํ•˜๋‚˜์”ฉ ๋ฐฉ๋ฌธ
        for (int i = 0; i < V ; ++i) {
            if (visited[i] == false)
                DFSUtil(i, visited);
        }
    }
    public static void main(String args[]) {
        DFSGraph g = new DFSGraph(4);

        g.addEdge(0, 1);
        g.addEdge(0, 2);
        g.addEdge(1, 2);
        g.addEdge(2, 0);
        g.addEdge(2, 3);
        g.addEdge(3, 3);

        System.out.println("vertax 2์—์„œ ์‹œ์ž‘ํ•˜๋Š” DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)");
        g.DFS(2); /* ์ฃผ์–ด์ง„ ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ DFS ํƒ์ƒ‰ */
        // 0 1 2 3

        // g.DFS(); /* ๋น„์—ฐ๊ฒฐํ˜• ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ */
    }
}
DFS