์๋ฃ ๊ตฌ์กฐ
BFS ๊ตฌํ
youngdeok
2023. 2. 28. 19:46
BFS ๊ฐ๋ : https://yongdeok.tistory.com/entry/%EA%B7%B8%EB%9E%98%ED%94%84
DFS๋ฅผ ์์ ํ ์ดํดํ๋ฉด ๋ ์ฝ๊ฒ ์ดํดํ ์ ์๋ค.
compile with
CircularQueue → https://yongdeok.tistory.com/7
LinkedList → https://yongdeok.tistory.com/3
Bfs.c
void BFShowGraphVertex(ALGraph *pg, int startV)
{
Queue queue;
int visitV = startV;
int nextV;
QueueInit(&queue);
VisitVertex(pg, visitV);
while (LFirst(&(pg->adjList[visitV]), &nextV) == TRUE)
{
if (VisitVertex(pg, nextV) == TRUE)
Enqueue(&queue, nextV);
while (LNext(&(pg->adjList[visitV]), &nextV) == TRUE)
{
if (VisitVertex(pg, nextV) == TRUE)
Enqueue(&queue, nextV);
}
if (QIsEmpty(&queue) == TRUE)
break;
else
visitV = Dequeue(&queue);
}
memset(pg->visitInfo, 0, sizeof(int) * pg->numV);
}
DFS์์ ์ ์ผํ๊ฒ ๋ค๋ฅธ ์ฝ๋์ด๋ค. ์ด ์ญ์ ์ฝ๋๋ฅผ ๊ทธ๋ ค๊ฐ๋ฉด์ ์ ๋ฆฌํ๋ค๋ณด๋ฉด ๊ธ๋ฐฉ ์ดํด๊ฐ ๋ ๊ฒ์ด๋ค. ๋ฌผ๋ก Stack๋์ Queue๋ฅผ includeํด์ผ ํ๋ค!.
Bfs.cpp
void ALGraph::BFShowGraphVertex(int startV)
{
Queue queue;
int visitV = startV;
int nextV;
queue.QueueInit();
VisitVertex(visitV);
while (adjList[visitV].LFirst(&nextV) == true)
{
if (VisitVertex(nextV) == true)
queue.EnQueue(nextV);
while (adjList[visitV].LNext(&nextV) == true)
{
if (VisitVertex(nextV) == true)
queue.EnQueue(nextV);
}
if (queue.QIsEmpty() == true)
break;
else
visitV = queue.DeQueue();
}
memset(visitInfo, 0, sizeof(int) * numV);
}