DFS - KimTaebin-ai/study_posts GitHub Wiki

DFS

: ๋‹ค์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ฐ ์นธ์„ ๋ฐฉ๋ฌธํ•  ๋•Œ ๊นŠ์ด๋ฅผ ์šฐ์„ ์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. ์‹œ์ž‘ํ•˜๋Š” ์นธ์„ ์Šคํƒ์— ๋„ฃ๊ณ  ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋Š” ํ‘œ์‹œ๋ฅผ ๋‚จ๊น€
  2. ์Šคํƒ์—์„œ ์›์†Œ๋ฅผ ๊บผ๋‚ด์–ด ๊ทธ ์นธ๊ณผ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ธ์ ‘ํ•œ ์นธ์— ๋Œ€ํ•ด 3๋ฒˆ์„ ์ง„ํ–‰
  3. ํ•ด๋‹น ์นธ์„ ์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด ์•„๋ฌด ๊ฒƒ๋„ ํ•˜์ง€ ์•Š๊ณ , ์ฒ˜์Œ์œผ๋กœ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋Š” ํ‘œ์‹œ๋ฅผ ๋‚จ๊ธฐ๊ณ  ํ•ด๋‹น ์นธ์„ ์Šคํƒ์— ์‚ฝ์ž…
  4. ์Šคํƒ์ด ๋นŒ ๋•Œ๊นŒ์ง€ 2๋ฒˆ์„ ๋ฐ˜๋ณต

๋ชจ๋“  ์นธ์ด ์Šคํƒ์— 1๋ฒˆ์”ฉ ๋“ค์–ด๊ฐ€๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์นธ์ด N๊ฐœ์ผ ๋•Œ O(N)

๊ฒฐ๊ตญ DFS์™€ BFS๋Š” ํฌ๊ฒŒ ์Šคํƒ์— ์ขŒํ‘œ๋ฅผ ๋„ฃ๋ƒ, ํ์— ์ขŒํ‘œ๋ฅผ ๋„ฃ๋ƒ๋กœ ๊ตฌ๋ถ„ ์ง€์„ ์ˆ˜ ์žˆ๋‹ค.

#include <stdio.h>

#define MAX 502

typedef struct
{
    int x;
    int y;
} LocationPoint;

typedef struct
{
    LocationPoint data[MAX * MAX];
    int top;
} Stack;

int board[MAX][MAX] = {
    {1, 1, 1, 0, 1, 0, 0, 0, 0, 0},
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {1, 1, 1, 0, 1, 0, 0, 0, 0, 0},
    {1, 1, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

int n = 7, m = 10;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; // ์ƒํ•˜์ขŒ์šฐ

int visit[MAX][MAX] = {0}; // ํ•ด๋‹น ์นธ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ €์žฅ

void initStack(Stack *s) // ์Šคํƒ์˜ top ๊ฐ’์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
{
    s->top = 0;
}

void push(Stack *s, LocationPoint p)
{
    s->data[s->top++] = p;
}

int empty(Stack *s)
{
    return s->top == 0;
}

LocationPoint pop(Stack *s)
{
    return s->data[--s->top];
}

int main()
{
    Stack S;
    initStack(&S);

    visit[0][0] = 1;                 // 1. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹œ์ž‘ ์‹œ (0, 0) ๋ฐฉ๋ฌธ ํ‘œ์‹œ
    push(&S, (LocationPoint){0, 0}); // 2. ํ•ด๋‹น ์นธ์„ ์Šคํƒ์— ๋„ฃ๋Š”๋‹ค.

    while (!empty(&S))
    {
        LocationPoint current = pop(&S); // 3. ์ดˆ๊ธฐ ์„ธํŒ… ํ›„ ์Šคํƒ์ด ๋นŒ ๋•Œ๊นŒ์ง€ ์Šคํƒ์˜ top์„ ๋นผ์คŒ
        printf("(%d, %d) -> ", current.x, current.y);

        // 4. ํ•ด๋‹น ์ขŒํ‘œ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์‚ดํŽด๋ณด๋ฉฐ ์Šคํƒ์— ๋„ฃ๋Š”๋‹ค

        for (int dir = 0; dir < 4; dir++) // ์ƒํ•˜์ขŒ์šฐ ์‚ดํŽด๋ณด๊ธฐ
        {
            int nX = current.x + dx[dir];
            int nY = current.y + dy[dir]; // nx, ny์— dir์—์„œ ์ •ํ•œ ๋ฐฉํ–ฅ์˜ ์ธ์ ‘ํ•œ ์นธ์˜ ์ขŒํ‘œ๊ฐ€ ๋“ค์–ด๊ฐ

            if (nX < 0 || nX >= n || nY < 0 || nY >= m) // ๋ฒ”์œ„ ๋ฐ–์ผ ๊ฒฝ์šฐ ๋„˜์–ด๊ฐ
                continue;
            if (visit[nX][nY] || board[nX][nY] != 1) // ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์นธ์ด๊ฑฐ๋‚˜ 0์ธ ๊ฒฝ์šฐ
                continue;
            visit[nX][nY] = 1;                 // ์กฐ๊ฑด์— ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅ, ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ์ €์žฅ
            push(&S, (LocationPoint){nX, nY}); // ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ์Šคํƒ์— ๋„ฃ์–ด์คŒ
        }
    }

    return 0;
}

bfs dfs ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ 1260๋ฒˆ

#include <stdio.h>
#include <stdlib.h>

#define MAX_N 1001
#define MAX_M 10001

int N, M, V;
int graph[MAX_N][MAX_N];
int visited[MAX_N];

// ํ ๊ตฌํ˜„
typedef struct
{
    int items[MAX_M];
    int front;
    int rear;
} Queue;

void initQueue(Queue *q)
{
    q->front = -1;
    q->rear = -1;
}

int isEmpty(Queue *q)
{
    return q->front == -1;
}

void enqueue(Queue *q, int value)
{
    if (q->rear == MAX_M - 1)
        return;
    if (isEmpty(q))
        q->front = 0;
    q->rear++;
    q->items[q->rear] = value;
}

int dequeue(Queue *q)
{
    if (isEmpty(q))
        return -1;
    int item = q->items[q->front];
    q->front++;
    if (q->front > q->rear)
        initQueue(q);
    return item;
}

// DFS ๊ตฌํ˜„
void dfs(int v)
{
    visited[v] = 1;
    printf("%d ", v);

    for (int i = 1; i <= N; i++)
    {
        if (graph[v][i] && !visited[i])
        {
            dfs(i);
        }
    }
}

// BFS ๊ตฌํ˜„
void bfs(int start)
{
    Queue q;
    initQueue(&q);

    visited[start] = 1;
    enqueue(&q, start);

    while (!isEmpty(&q))
    {
        int v = dequeue(&q);
        printf("%d ", v);

        for (int i = 1; i <= N; i++)
        {
            if (graph[v][i] && !visited[i])
            {
                visited[i] = 1;
                enqueue(&q, i);
            }
        }
    }
}

int main()
{
    scanf("%d %d %d", &N, &M, &V);

    for (int i = 0; i < M; i++)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        graph[a][b] = graph[b][a] = 1;
    }

    // DFS
    printf("DFS: ");
    dfs(V);
    printf("\n");

    // ๋ฐฉ๋ฌธ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
    for (int i = 1; i <= N; i++)
        visited[i] = 0;

    // BFS
    printf("BFS: ");
    bfs(V);
    printf("\n");

    return 0;
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ