์ž๋ฃŒ ๊ตฌ์กฐ

DFS ๊ตฌํ˜„

youngdeok 2023. 2. 28. 19:41

DFS ๊ฐœ๋…: https://yongdeok.tistory.com/entry/%EA%B7%B8%EB%9E%98%ED%94%84

๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ์ง€ ์•Š์œผ๋ฉด ์ดํ•ดํ•˜๊ธฐ ํž˜๋“ค๊ฒƒ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™๋‹ค.

compile with

Stack.c→https://yongdeok.tistory.com/6

LinkedList.c → https://yongdeok.tistory.com/3

Graph.h

#ifndef __AL_GRAPH_H__
#define __AL_GRAPH_H__

#include  "DLinkedList.h"

enum {A, B, C, D, E, F, G, H, I, J};

typedef struct _ual
{
    int numV; // ๋…ธ๋“œ ๊ฐฏ์ˆ˜
    int numE; // ๊ฐ„์„  ๊ฐฏ์ˆ˜
    List *adjList;
    int *visitInfo; // ๋ฐฉ๋ฌธ ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ณ€์ˆ˜
} ALGraph;

void GraphInit(ALGraph *pg, int nv);
void GraphDestroy(ALGraph *pg);
void AddEdge(ALGraph *pg, int fromV, int toV);
void ShowGraphEdgeInfo(ALGraph *pg);

void DFShowGraphVertex(ALGraph *pg, int startV); // DFS๊ธฐ๋ฐ˜ ํƒ์ƒ‰

#endif

Graph.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ALGraph.h"
#include "ArrayBaseStack.h"
#include "DLinkedList.h"

int WhoIsPrecede(int data1, int data2);

void GraphInit(ALGraph *pg, int nv)
{
    int i;

    pg->numE = 0;
    pg->numV = nv;
    pg->adjList = (List *)malloc(sizeof(List) * nv);
    for (int i = 0; i < nv; i++)
    {
        ListInit(&(pg->adjList[i]));
        SetSortRule(&(pg->adjList[i]), WhoIsPrecede);
    }
    pg->visitInfo = (int *)malloc(sizeof(int) * pg->numV); // ๋ฐฉ๋ฌธ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ 
    memset(pg->visitInfo, 0, sizeof(int) * pg->numV); // ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ธฐํ™”
}

void GraphDestroy(ALGraph *pg)
{
    if (pg->adjList != NULL)
        free(pg->adjList);
    if (pg->visitInfo != NULL)
        free(pg->visitInfo);
}

void AddEdge(ALGraph *pg, int fromV, int toV)
{
    ListInsert(&(pg->adjList[fromV]), toV); // ๋ฐฉํ–ฅ์„ฑ์ด  ์—†๋Š” ๊ทธ๋ž˜ํ”„์ด๊ธฐ ๋•Œ๋ฌธ์— ์–‘์ชฝ ๋‹ค ์—ฐ๊ฒฐ
    ListInsert(&(pg->adjList[toV]), fromV);
    pg->numE += 1;
}

void ShowGraphEdgeInfo(ALGraph *pg)
{
    int nv;
    for (int i = 0; i < pg->numV; i++)
    {
			//enum {A, B, C, D, E, F, G, H, I, J};
        printf("%c์™€ ์—ฐ๊ฒฐ๋œ ์ •์  : ",i + 65); // ๋ฌธ์ž๋ฅผ ๋ฝ‘๊ธฐ ์œ„ํ•ด ์•„์Šคํ‚ค ๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค

        if (LFirst(&(pg->adjList[i]), &nv) != NULL)
        {
            printf("%c ", nv + 65);

            while (LNext(&(pg->adjList[i]), &nv) != NULL)
            {
                printf("%c ", nv + 65);
            }
        }
        printf("\\n");
    }
}

int WhoIsPrecede(int data1, int data2)
{
	if(data1 < data2)
		return 0;
	else
		return 1;
}

int VisitVertex(ALGraph *pg, int visitV)
{
    if (pg->visitInfo[visitV] == 0) // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด?
    {
        pg->visitInfo[visitV] = 1;
        printf("%c ", visitV + 65);
        return TRUE;
    }
    return FALSE;
}

void DFShowGraphVertex(ALGraph *pg, int startV)
{
    Stack stack;
    int visitV = startV;
    int nextV;

    StackInit(&stack); // ์ฒ˜์Œ ์‹œ์ž‘์ ์„ pushํ•ด์ค€๋‹ค
    VisitVertex(pg, visitV); // ๋ฐฉ๋ฌธ
    SPush(&stack, visitV); // push

    while (LFirst(&(pg->adjList[visitV]), &nextV) == TRUE) // ๋ฐฉ๋ฌธ ์„ฑ๊ณต(์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ˆ?)
    {
        int visitFlag = FALSE; // ๋’ค๋กœ ๋Œ์•„๊ฐ€์•ผ ํ• ๋•Œ ์‚ฌ์šฉํ•˜๋Š” flag

        if (VisitVertex(pg, nextV) == TRUE) // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด?
        {
            SPush(&stack, visitV); // push!
            visitV = nextV; // ์—ฐ๊ฒฐ ๋œ ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด
            visitFlag = TRUE; // ๋’ค๋กœ๋Œ์•„๊ฐ€์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
        }
        else
        {
            while (LNext(&(pg->adjList[visitV]), &nextV) == TRUE)
            {
                if (VisitVertex(pg, nextV) == TRUE)
                {
                    SPush(&stack, visitV);
                    visitV = nextV;
                    visitFlag = TRUE;
                    break;
                }
            }
        }
        if (visitFlag == FALSE) // ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๊ฐ€ ์—†์„ ๋•Œ
        {
            if (SIsEmpty(&stack) == TRUE) // ๋‹ค ๋Œ์•˜๊ฑฐ๋‚˜!
                break;
            else // ์•„๋‹ˆ๊ฑฐ๋‚˜
                visitV = SPop(&stack);
        }
    }
    memset(pg->visitInfo, 0, sizeof(int) * pg->numV); // ๋‹ค์‹œ ์‚ฌ์šฉ ํ•˜๊ธฐ ์œ„ํ•ด memset
}

์ปดํŒŒ์ผ ํ•ด์•ผํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋งŽ์•„์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ makefile์„ ๋งŒ๋“ค์—ˆ๋‹ค.

compile with Stack, LinkedList

MakeFile

NAME = DFS
CC = GCC
FLAGS = -Wall -Wextra -Werror
SRCS  = ALGraph.c \\
		ArrayBaseStack.c\\
		DFSMain.c\\
		DLinkedList.c\\

OBJ = $(SRCS:.c=.o)

all: $(LIB) $(NAME)

$(NAME) : $(OBJ)
	$(CC) $(FLAGS) $^ -o $@ 

clean:
	rm -f $(OBJ)

fclean: clean
	rm -f $(NAME)
re: fclean all

c++ code

compile with

Stack.cpp→https://yongdeok.tistory.com/6

LinkedList.cpp →  https://yongdeok.tistory.com/3

#include <iostream>
#include "ALGraph.h"
#include "DLinkedList.h"
#include "Stack.h"

void ALGraph::GraphInit(int nv)
{
    numV = nv;
    numE = 0;
    adjList = new LinkedListCpp<int>[nv];
    for (int i = 0; i < numV; i++)
    {
        adjList[i].ListInit();
    }
    visitInfo = new int[nv];
    memset(visitInfo, 0, sizeof(int) * nv);
}
void ALGraph::GraphDestroy()
{
    delete[] visitInfo;
    delete[] adjList;
}
void ALGraph::AddEdge(int fromV, int toV)
{
    adjList[fromV].LInsert(toV);
    adjList[toV].LInsert(fromV);
}
void ALGraph::ShowGraphEdgeInfo()
{
    int i;
    int nv;
    for (int i = 0; i < numV; i++)
    {
        std::cout << (char)(i + 65) << "์™€ ์—ฐ๊ฒฐ๋œ ์ •์  : ";
        if ((adjList[i]).LFirst(&nv))
        {
            std::cout << (char)(nv + 65);

            while (adjList[i].LNext(&nv) != NULL)
            {
                std::cout << " " << (char)(nv + 65);
            }
        }
        std::cout << "\\n";
    }
}

int WhoIsPrecede(int data1, int data2)
{
	if(data1 < data2)
		return 0;
	else
		return 1;
}
int ALGraph::VisitVertex(int visitV)
{
    if (visitInfo[visitV] == 0)
    {
        visitInfo[visitV] = 1;
        std::cout << (char)(visitV + 65)  <<  " ";
        return true;
    }
    return false;
}
void ALGraph::DFShowGraphVertex(int startV)
{
    ArrayStack<int> stack;
    int visitV = startV;
    int nextV;

    stack.StackInit();
    VisitVertex(visitV);
    stack.SPush(visitV);

    while (adjList[visitV].LFirst(&nextV) == true)
    {
        int visitFlag = false;

        if (VisitVertex(nextV) == true)
        {
            stack.SPush(visitV);
            visitV = nextV;
            visitFlag = true;
        }
        else
        {
            while (adjList[visitV].LNext(&nextV) == true)
            {
                if (VisitVertex(nextV) == true)
                {
                    stack.SPush(visitV);
                    visitV = nextV;
                    visitFlag = true;
                    break;
                }
            }
        }
        if (visitFlag == false)
        {
            if (stack.SIsEmpty() == true)
                break;
            else
                visitV = stack.SPop();
        }
    }
    memset(visitInfo, 0, sizeof(int) * numV);
}

'์ž๋ฃŒ ๊ตฌ์กฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ  (0) 2023.02.28
BFS ๊ตฌํ˜„  (0) 2023.02.28
ํ…Œ์ด๋ธ”๊ณผ ํ•ด์‰ฌ ๊ฐœ๋…  (0) 2023.02.28
ํž™ ๊ฐœ๋…  (0) 2023.02.28
ํŠธ๋ฆฌ ๊ฐœ๋… ๋ฐ ๊ตฌํ˜„  (0) 2023.02.28