Deep_Dev
article thumbnail

πŸ’‘BFS

BFSλŠ” λ„ˆλΉ„ μš°μ„  νƒμƒ‰μœΌλ‘œ, 루트 λ…Έλ“œμ—μ„œ 탐색을 μ‹œμž‘ν•˜μ—¬ 같은 λ ˆλ²¨μ— μžˆλŠ” λ…Έλ“œλ₯Ό λͺ¨λ‘ νƒμƒ‰ν•œ λ‹€μŒ

ν•˜μœ„ 레벨둜 λ‚΄λ €κ°€ 또 λͺ¨λ‘ 탐색을 μ§„ν–‰ν•˜λ‹€κ°€, 더 이상 탐색할 λ…Έλ“œκ°€ 없을 λ•Œ 탐색을 λ©ˆμΆ”λŠ” λ°©μ‹μœΌλ‘œ λ™μž‘ν•œλ‹€.

 

큐λ₯Ό μ΄μš©ν•˜μ—¬ κ΅¬ν˜„ν•œλ‹€.

 

πŸ’‘ BFS μ½”λ“œκ΅¬ν˜„

import java.util.*;

class Node{
    int data;
    Node lt,rt;
    public Node(int val){
        data=val;
        lt=rt=null;
    }
}
public class Solution {
    Node root;
    public void BFS(Node root) {
        Queue<Node> Q=new LinkedList<>();
        Q.offer(root);
        int L=0;
        while(!Q.isEmpty()){
            int len=Q.size();
            System.out.print(L+" : ");
            for(int i=0; i<len; i++){
                Node cur=Q.poll();
                System.out.print(cur.data+" ");
                if(cur.lt!=null) Q.offer(cur.lt);
                if(cur.rt!=null) Q.offer(cur.rt);
            }
            L++;
            System.out.println();
        }
    }
    public static void main(String[] args) {
        Solution tree=new Solution();
        tree.root=new Node(1);
        tree.root.lt=new Node(2);
        tree.root.rt=new Node(3);
        tree.root.lt.lt=new Node(4);
        tree.root.lt.rt=new Node(5);
        tree.root.rt.lt=new Node(6);
        tree.root.rt.rt=new Node(7);
    }
}

 

 

 

 

 

πŸ’‘ BFS 솑아지 문제

 

 

import java.util.*;

public class Solution {
    int[] dis={1,-1,5};
    int[] ch; // 이미 μžˆλŠ” 값인지 check ν•  λ°°μ—΄
    Queue<Integer> Q=new LinkedList<>(); // μ „μ—­ Q μ„ μ–Έ

    public int BFS(int s, int e){ // s = 좜발점 / e = 도착지점
        ch=new int[10001];
        ch[s]=1;
        Q.offer(s);
        int L=0; // root node level = 0
        while(!Q.isEmpty()){
            int len=Q.size(); // 레벨의 길이
            for(int i=0; i<len; i++){
                int x=Q.poll();
                if(x==e) return L;
                for(int j=0; j<3; j++){
                    int nx=x+dis[j]; // nx : μžμ‹ node
                    if(nx>0 && nx<=10000 && ch[nx]==0){ // ch[nx]==0이면 방문 X
                        ch[nx]=1; // λ°©λ¬Έν–ˆλ‹€κ³  check
                        Q.offer(nx);
                    }
                }
            }
            L++; // level 증가
        }
        return 0;
    }
    public static void main(String[] args) {
        Solution T = new Solution();
        Scanner kb=new Scanner(System.in);
        int s=kb.nextInt();
        int e=kb.nextInt();
        System.out.println(T.BFS(s,e));
    }
}

 

 

 

πŸ’‘DFS, BFSλ₯Ό ν™œμš©ν•˜λ©΄ 쒋은 상황

1. κ·Έλž˜ν”„μ˜ λͺ¨λ“  정점을 λ°©λ¬Έν•˜λŠ” 것이 μ£Όμš”ν•œ 문제 : DFS, BFS λͺ¨λ‘ 무방

 

2. 경둜의 νŠΉμ§•μ„ μ €μž₯해둬야 ν•˜λŠ” 문제 : 각 정점에 μˆ«μžκ°€ 있고 a λΆ€ν„° b κΉŒμ§€ κ°€λŠ” 경둜λ₯Ό κ΅¬ν•˜λŠ”λ° 같은 μˆ«μžκ°€ 있으면 μ•ˆλœλ‹€λŠ” 문제 λ“±, 각각의 κ²½λ‘œλ§ˆλ‹€ νŠΉμ§•μ„ μ €μž₯해둬야 ν•˜λŠ” κ²½μš°λŠ” DFSλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€. 

BFSλŠ” 경둜의 νŠΉμ§•μ„ μ €μž₯ν•˜μ§€ λͺ»ν•œλ‹€.

 

3. μ΅œλ‹¨κ±°λ¦¬λ₯Ό κ΅¬ν•˜λŠ” 문제 : BFSκ°€ μœ λ¦¬ν•˜λ‹€. DFS의 경우 처음으둜 λ°œκ²¬λ˜λŠ” 해닡이 μ΅œλ‹¨κ±°λ¦¬κ°€ 아닐 수 μžˆμ§€λ§Œ, BFS의 경우 λ¨Όμ € μ°Ύμ•„μ§€λŠ” 해닡이 곧 μ΅œλ‹¨κ±°λ¦¬μ΄κΈ° λ•Œλ¬Έμ΄λ‹€.