Skip to content

Latest commit

Β 

History

History
140 lines (108 loc) Β· 4.51 KB

BreadthFirstSearch.md

File metadata and controls

140 lines (108 loc) Β· 4.51 KB

λ„ˆλΉ„ μš°μ„  탐색(Breadth First Search : BFS)

written by sohyeon, hyemin πŸ’‘


1. λ„ˆλΉ„ μš°μ„  탐색(BFS)μ΄λž€?

λ„ˆλΉ„ μš°μ„  탐색(Breadth First Search)은 μ‹œμž‘ λ…Έλ“œλ‘œλΆ€ν„° κ°€κΉŒμš΄ λ…Έλ“œλ₯Ό λ¨Όμ € λ°©λ¬Έν•˜κ³  멀리 λ–¨μ–΄μ Έ μžˆλŠ” λ…Έλ“œλ₯Ό λ‚˜μ€‘μ— λ°©λ¬Έν•˜λŠ” 순회 방법이닀.

예λ₯Ό λ“€μ–΄, a λ…Έλ“œμ—μ„œ μ‹œμž‘ν•œλ‹€κ³  ν–ˆμ„ λ•Œ, BFSλŠ” a λ…Έλ“œμ˜ 이웃 λ…Έλ“œλ₯Ό λͺ¨λ‘ λ°©λ¬Έν•œ λ‹€μŒμ— μ΄μ›ƒμ˜ 이웃듀을 λ°©λ¬Έν•œλ‹€.


2. λ„ˆλΉ„ μš°μ„  탐색(BFS)의 νŠΉμ§•

  • BFSλŠ” DFS와 달리 μž¬κ·€μ μœΌλ‘œ λ™μž‘ν•˜μ§€ μ•ŠλŠ”λ‹€.

  • BFSλŠ” λ°©λ¬Έν•œ λ…Έλ“œλ₯Ό μ°¨λ‘€λ‘œ μ €μž₯ν•œ ν›„ κΊΌλ‚Ό 수 μžˆλŠ” 큐(queue)λ₯Ό μ‚¬μš©ν•œλ‹€.


3. λ„ˆλΉ„ μš°μ„  탐색(BFS)의 κ³Όμ •

1. λ¨Όμ € μ‹œμž‘ λ…Έλ“œμΈ 0을 λ°©λ¬Έν•œ λ‹€μŒ, 큐에 μ‚½μž…ν•œλ‹€.
2. λ…Έλ“œ 0의 인접 λ…Έλ“œμΈ {1,2,4}λ₯Ό μ°¨λ‘€λŒ€λ‘œ λ°©λ¬Έν•œ λ‹€μŒ, 큐에 μ‚½μž…ν•œλ‹€.
3. 큐에 μ‚½μž…λœ {1,2,4} 순으둜 νμ—μ„œ μ‚­μ œν•˜λ©΄μ„œ μΈμ ‘ν•œ λ…Έλ“œμ— λ°©λ¬Έ κ°€λŠ₯ν•œμ§€ ν™•μΈν•œλ‹€. 
4. {1}의 μΈμ ‘ν•œ λ…Έλ“œ {0,2}λŠ” 이미 λ°©λ¬Έν–ˆμœΌλ―€λ‘œ νμ—μ„œ μ‚­μ œν•œλ‹€.
5. {2}의 μΈμ ‘ν•œ λ…Έλ“œ {1,0,3,4}μ—μ„œ λ°©λ¬Έν•˜μ§€ μ•ŠλŠ” {3}을 λ°©λ¬Έν•œ ν›„ νμ—μ„œ μ‚­μ œν•œλ‹€.
6. μ•žμ˜ μˆœμ„œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ λͺ¨λ“  λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜κ³ , 탐색을 μ’…λ£Œν•œλ‹€.


4. λ„ˆλΉ„ μš°μ„  탐색(BFS)의 κ΅¬ν˜„

κ΅¬ν˜„ λ°©λ²•μœΌλ‘œλŠ” 자료 ꡬ쑰 큐(queue)λ₯Ό μ΄μš©ν•˜λŠ” 것이닀.

ex) BFSλ₯Ό κ΅¬ν˜„ν•œ μ˜μ‚¬μ½”λ“œ(pseudocode)

void search(Node root) {
    Queue queue = new Queue();
    root.marked = true; 
    queue.enqueue(root); // 루트 λ…Έλ“œλ₯Ό 큐의 끝에 μΆ”κ°€ν•œλ‹€.

    // 큐가 λΉ„μ–΄ μžˆμ„ λ•ŒκΉŒμ§€ κ³„μ†ν•œλ‹€.
    while (!queue.isEmpty()) {
        Node r = queue.dequeue(); // 큐의 μ•žμ—μ„œ μ‚­μ œν•œλ‹€.
        visit(r); // νμ—μ„œ μ‚­μ œν•œ λ…Έλ“œλ₯Ό λ°©λ¬Έν•œλ‹€. 
        
        // νμ—μ„œ μ‚­μ œν•œ λ…Έλ“œμ™€ μΈμ ‘ν•œ λ…Έλ“œλ“€μ„ λͺ¨λ‘ μ°¨λ‘€λ‘œ λ°©λ¬Έν•œλ‹€.
        foreach (Node n in r.adjacent) {
            if (n.marked == false) {
                n.marked = true; 
                queue.enqueue(n); // 큐의 끝에 μΆ”κ°€ν•œλ‹€.
            }
        }
    }
}

BFS κ΅¬ν˜„

import java.io.*; 
import java.util.*; 

// 인접 리슀트λ₯Ό μ΄μš©ν•œ λ°©ν–₯μ„± μžˆλŠ” κ·Έλž˜ν”„ 클래슀
class Graph { 
    private int V; // λ…Έλ“œμ˜ 개수
    private LinkedList<Integer> adj[]; // 인접 리슀트

    // Constructor(μƒμ„±μž)
    Graph(int v) { 
        V = v; 
        adj = new LinkedList[v]; 
        for (int i=0; i<v; ++i) 
            adj[i] = new LinkedList(); 
    } 

    // λ…Έλ“œλ₯Ό μ—°κ²°ν•œλ‹€. (v->w)
    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>(); 

        // ν˜„μž¬ λ…Έλ“œλ₯Ό λ°©λ¬Έν•œ κ²ƒμœΌλ‘œ ν‘œμ‹œν•˜κ³  큐에 μ‚½μž…(add)ν•œλ‹€.
        visited[s]=true; 
        queue.add(s); 

        // 큐가 λΉ„μ–΄ μžˆμ„ λ•ŒκΉŒμ§€ λ°˜λ³΅ν•œλ‹€.
        while (queue.size() != 0) { 
            // νμ—μ„œ λ°©λ¬Έν•œ λ…Έλ“œλ₯Ό μ‚­μ œ(poll)ν•˜κ³  값을 좜λ ₯ν•œλ‹€.
            s = queue.poll(); 
            System.out.print(s+" "); 

            // λ°©λ¬Έν•œ λ…Έλ“œμ™€ μΈμ ‘ν•œ λͺ¨λ“  λ…Έλ“œλ₯Ό κ°€μ Έμ˜¨λ‹€.
            Iterator<Integer> i = adj[s].listIterator(); 
            while (i.hasNext()) { 
                int n = i.next(); 
                
                // λ°©λ¬Έν•˜μ§€ μ•Šμ€ λ…Έλ“œκ°€ μžˆλ‹€λ©΄ λ°©λ¬Έν•œ κ²ƒμœΌλ‘œ ν‘œμ‹œν•˜κ³  큐에 μ‚½μž…(add)ν•œλ‹€.
                if (!visited[n]) { 
                    visited[n] = true; 
                    queue.add(n); 
                } 
            } 
        } 
    } 

    // 2λ₯Ό μ‹œμž‘ λ…Έλ“œλ‘œ ν•˜μ—¬ BFSλ₯Ό νƒμƒ‰ν•œλ‹€.
    public static void main(String args[]) { 
        Graph g = new Graph(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); 
    
        g.BFS(2); 
    } 
} 

Reference & Additional Resources