View

[Java] BFS(Breadth First Search)

์ฑ…์ฝ๋Š” ๊ฐ์ž 2022. 8. 30. 21:33
๋ฐ˜์‘ํ˜•

๐Ÿ’กBFS(Breadth First Search)

: ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ BFS๋Š” ์‰ฝ๊ฒŒ๋งํ•ด ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

BFS๊ตฌํ˜„์—๋Š” ์„ ์ž…์„ ์ถœ ๋ฐฉ์‹์ธ ํ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.


๐Ÿ’กBFS ๋™์ž‘ ๋ฐฉ์‹

  1. ํƒ์ƒ‰ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
  2. ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ธ ๋’ค ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ์ ‘ ๋…ธ๋“œ ์ค‘์—์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
  3. 2๋ฒˆ์˜ ๊ณผ์ •์„ ๋” ์ด์ƒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

1๏ธโƒฃ ์‹œ์ž‘๋…ธ๋“œ์ธ 1์„ ํ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•จ

2๏ธโƒฃ ํ์—์„œ 1์„ ๊บผ๋‚ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ 2, 3, 8์„ ๋ชจ๋‘ ํ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•จ

3๏ธโƒฃ ํ์—์„œ ๋…ธ๋“œ 2๋ฅผ ๊บผ๋‚ธ ๋’ค, ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘๋…ธ๋“œ 7์„ ํ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•จ

4๏ธโƒฃ ํ์—์„œ ๋…ธ๋“œ 3์„ ๊บผ๋‚ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘๋…ธ๋“œ์ธ 4,5๋ฅผ ๋ชจ๋‘ ํ์— ์‚ฝ์ž… ํ›„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

5๏ธโƒฃ ํ์—์„œ ๋…ธ๋“œ 8์„ ๊บผ๋‚ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฌด์‹œํ•œ๋‹ค

6๏ธโƒฃ ํ์—์„œ ๋…ธ๋“œ7์„ ๊บผ๋‚ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ 6์„ ์‚ฝ์ž…ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•จ

7๏ธโƒฃ ๋‚จ์•„์žˆ๋Š” ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ์ ‘ ๋…ธ๋“œ๊ฐ€ ์—†์Œ
โ†’ ๋”ฐ๋ผ์„œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๊บผ๋‚ด๋ฉด ์ตœ์ข…์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Œ


๐Ÿ’กBFS์ฝ”๋“œ (Java)

import java.util.ArrayDeque;
import java.util.Queue;

public class BASIC_BFS_DFS {

static int[][] map = { // 0 index๋Š” dummy
        {0,  0,  0,  0,  0,  0,  0},
        {0, 11, 12, 13, 14, 15, 16},
        {0, 21, 22, 23, 24, 25, 26},
        {0, 31, 32, 33, 34, 35, 36},
        {0, 41, 42, 43, 44, 45, 46},
        {0, 51, 52, 53, 54, 55, 56},
        {0, 61, 62, 63, 64, 65, 66},
};

// ์ž๋ฃŒ ๊ตฌ์กฐ 
static Queue<Node> queue = new ArrayDeque<>();
static boolean[][] visit;

// ์‚ฌ๋ฐฉํƒ์ƒ‰ delta ์ƒ - ํ•˜ - ์ขŒ - ์šฐ
static int[] dy = { -1, 1, 0, 0 };
static int[] dx = {  0, 0,-1, 1 };

static int COUNT;

public static void main(String[] args) {
    // bfs
    COUNT = 0;
    visit  = new boolean[7][7];
    bfs( 1, 1 );
}

// ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์„ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž ๊น ๋‹ด์•„๋†“๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ํ•„์š” Queue
// ๋ณ„๋„์˜ ํด๋ž˜์Šค๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ ๋งŒ๋“ ๋‹ค. ( ํด๋ž˜์Šค ๋˜๋Š” ๋ฐฐ์—ด )
// visit
// ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœ
static void bfs( int y, int x ) {

    // ์ตœ์ดˆ y, x ๋ฅผ ์ด์šฉํ•ด์„œ Node ํ•œ ๊ฐœ๋ฅผ ๋‹ด๋Š”๋‹ค.
    queue.offer(new Node(y, x));
    visit[y][x] = true;

    while( ! queue.isEmpty() ) {

        Node node = queue.poll();
        COUNT++;

        //System.out.println(node);

        for (int d = 0; d < 4; d++) {
            int ny = node.y + dy[d];
            int nx = node.x + dx[d];

            if( ny < 1 || nx < 1 || ny >=7 || nx >= 7 || visit[ny][nx]) continue;

            queue.offer(new Node(ny, nx));
            visit[ny][nx] = true;
        }

    }

    System.out.println("BFS : COUNT : " + COUNT);
}

static class Node{
    int y, x;

    public Node(int y, int x) {
        this.y = y;
        this.x = x;
    }

    @Override
    public String toString() {
        return "Node [y=" + y + ", x=" + x + "]";
    }
}
}

์ฐธ๊ณ : Copy of ์ด๊ฒƒ์ด ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30