๐Ÿ“—
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
  • BreadthFirstSearch
  • ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS, Breadth-First Search)
  • ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์˜ ํŠน์ง•

Was this helpful?

  1. algorithm

BreadthFirstSearch

PrevioushashNexttensorflow

Last updated 3 years ago

Was this helpful?

BreadthFirstSearch

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS, Breadth-First Search)

: ๋ฃจํŠธ ๋…ธ๋“œ(ํ˜น์€ ๋‹ค๋ฅธ ์ž„์˜์˜ ๋…ธ๋“œ)์—์„œ ์‹œ์ž‘ํ•ด์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ• ( ์ฆ‰, ๊นŠ๊ฒŒ(deep) ํƒ์ƒ‰ํ•˜๊ธฐ ์ „์— ๋„“๊ฒŒ(wide) ํƒ์ƒ‰) ๋‘ ๋…ธ๋“œ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ํ˜น์€ ์ž„์˜์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ณ  ์‹ถ์„ ๋•Œ ์ด ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•œ๋‹ค.

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์˜ ํŠน์ง•

  • ์ง๊ด€์ ์ด์ง€ ์•Š์€ ๋ฉด์ด ์žˆ๋‹ค.(BFS๋Š” ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๊ฑฐ๋ฆฌ์— ๋”ฐ๋ผ ๋‹จ๊ณ„๋ณ„๋กœ ํƒ์ƒ‰ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.)

  • BFS๋Š” ์žฌ๊ท€์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • BFS๋Š” ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋“ค์„ ์ฐจ๋ก€๋กœ ์ €์žฅํ•œ ํ›„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ์ธ ํ(Queue)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.(์ฆ‰, ์„ ์ž…์„ ์ถœ(FIFO) ์›์น™์œผ๋กœ ํƒ์ƒ‰)

  • โ€˜Primโ€™, โ€˜Dijkstraโ€™ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์œ ์‚ฌํ•˜๋‹ค.

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

    /* ์ƒ์„ฑ์ž */
    public BFSGraph(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);
    }

    /* s๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ํ•œ BFS๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ํƒ์ƒ‰ํ•œ ๋…ธ๋“œ๋“ค์„ ์ถœ๋ ฅ */
    void BFS(int s) {
        // ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จ
        boolean visited[] = new boolean[V];
        // BFS ๊ตฌํ˜„์„ ์œ„ํ•œ ํ (Queue) ์ƒ์„ฑ
        LinkedList<Integer> queue = new LinkedList<Integer>();

        // ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์„ ํ์— ์‚ฝ์ž… (enqueue)
        visited[s] = true;
        queue.add(s);

        // ๋ชจ๋‘๊ฐ€ ์ถœ๋ ฅ ํ•  ๋•Œ๊นŒ์ง€ Queue๋ฅผ ๋ฐ˜๋ณต
        while (queue.size() != 0) {
            // ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ํ์—์„œ ์ถ”์ถœ(dequeue)ํ•˜๊ณ  ๊ฐ’์„ ์ถœ๋ ฅ
            s = queue.poll();
            System.out.print(s + " ");

            // ๋ฐฉ๋ฌธํ•œ ๋„์œผ์™€ ์ธ์—…ํ•œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
            Iterator<Integer> i = adj[s].listIterator();
            while (i.hasNext()) {
                int n = i.next();
                // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋Š” ๋…ธ๋“œ๋ฉด ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์œผ๋กœ ํ‘œ์‹œํ•˜๊ณ  ํ์— ์‚ฝ์ž…
                if (!visited[n]) {
                    visited[n] = true;
                    queue.add(n);
                }
            }
        }
    }

    public static void main(String[] args) {
        BFSGraph g = new BFSGraph(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์—์„œ ์‹œ์ž‘ํ•˜๋Š” BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰)");
        g.BFS(2); /* ์ฃผ์–ด์ง„ ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ BFS ํƒ์ƒ‰ */
        // 2 0 3 1 
    }
}
BFS