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 |