广度优先算法(BFS)的要点:
(1)从一个图节点编号开始加入队列(创建一个队列,FIFO)
(2)只要队列不为空,则取出队列中一个图节点编号,将该节点编号所有还未遍历的节点之间的边。(创建一个已经遍历节点的数组)
深度优先遍历的要点:
从第一个节点开始,遍历一个相邻的,然后把该节点作为起始点遍历未曾遍历的节点。
如何遍历所有边?
-
#define MAX 100
-
typedef enum
-
{
-
NoVisit=0,
-
HasVisit=1
-
}VisitStat;
-
-
typedef enum
-
{
-
NoExisit = 0,
-
IsExisit = 1
-
}ExistStat;
-
-
typedef enum
-
{
-
NoEmpty = 0,
-
IsEmpty = 1
-
}EmptyStat;
-
-
typedef struct ListNode
-
{
-
int pointNo;
-
int wight;
-
struct ListNode* next;
-
}ListNode;
-
-
typedef struct GNODE
-
{
-
int pointNo;
-
ListNode* listHead;
-
}GNODE,GPoint[MAX];
-
-
typedef struct Graph
-
{
-
GPoint GListNode;
-
int points;
-
int edges;
-
}GRAPH;
-
-
void CreateG(GRAPH* grap)
-
{
-
scanf("%d %d",&grap->points,&grap->edges);
-
-
int i;
-
ListNode* tmpHead;
-
-
for (i=0; i<grap->points;i++)
-
{
-
grap->GListNode[i].pointNo = i;
-
tmpHead = (ListNode*)malloc(sizeof(ListNode));
-
if (NULL != tmpHead)
-
{
-
tmpHead->next = NULL;
-
grap->GListNode[i].listHead = tmpHead;
-
}
-
}
-
-
int start=0,end=0,weight=0;
-
ListNode* InsertNode;
-
ListNode* Head;
-
ListNode* Next;
-
for (i=0;i<grap->edges;i++)
-
{
-
scanf("%d %d %d",&start,&end,&weight);
-
-
InsertNode = (ListNode*)malloc(sizeof(ListNode));
-
if (NULL == InsertNode)
-
return ;
-
InsertNode->pointNo = end;
-
InsertNode->wight = weight;
-
Head = grap->GListNode[start].listHead;
-
Next = Head->next;
-
while (Next != NULL)
-
{
-
Head = Next;
-
Next = Head->next;
-
}
-
InsertNode->next = Next;
-
Head->next = InsertNode;
-
-
-
InsertNode = (ListNode*)malloc(sizeof(ListNode));
-
if (NULL == InsertNode)
-
return ;
-
InsertNode->pointNo = start;
-
InsertNode->wight = weight;
-
Head = grap->GListNode[end].listHead;
-
Next = Head->next;
-
while (Next != NULL)
-
{
-
Head = Next;
-
Next = Head->next;
-
}
-
InsertNode->next = Next;
-
Head->next = InsertNode;
-
}
-
-
}
-
typedef struct QueueNode
-
{
-
int PonintNo;
-
struct QueueNode* next;
-
}QueueNode;
-
-
QueueNode* CreateQueue()
-
{
-
QueueNode* head = (QueueNode*)malloc(sizeof(QueueNode));
-
if (head == NULL)
-
{
-
return NULL;
-
}
-
head->next = NULL;
-
}
-
-
EmptyStat EmptyQueue(QueueNode* queueHead)
-
{
-
if (NULL == queueHead)
-
return IsEmpty;
-
QueueNode* Head = queueHead;
-
QueueNode* Next = Head->next;
-
if (NULL == Next)
-
return IsEmpty;
-
else
-
return NoEmpty;
-
}
-
-
void EnterQueue(QueueNode** queue,int pointNo)
-
{
-
if (queue == NULL)
-
return;
-
-
QueueNode* Head = *queue;
-
QueueNode* Next = Head->next;
-
while(Next != NULL)
-
{
-
if (Next->PonintNo == pointNo)
-
{
-
return;
-
}
-
Head = Next;
-
Next = Head->next;
-
}
-
QueueNode* InsertNode = (QueueNode*)malloc(sizeof(QueueNode));
-
if (NULL == InsertNode)
-
{
-
return;
-
}
-
InsertNode->PonintNo = pointNo;
-
InsertNode->next = Next;
-
Head->next = InsertNode;
-
return;
-
}
-
QueueNode* OutQueue(QueueNode** queue)
-
{
-
if (NULL == queue)
-
return NULL;
-
-
QueueNode* Head = *queue;
-
QueueNode* Next = Head->next;
-
QueueNode* tmp;
-
if (NULL == Next)
-
return NULL;
-
tmp = Next;
-
Head->next = Next->next;
-
return tmp;
-
}
-
-
void BFS(GRAPH* grap)
-
{
-
int VisitLog[MAX];
-
int i;
-
for (i=0; i<grap->points; i++)
-
{
-
VisitLog[i] = NoVisit;
-
}
-
-
QueueNode* OutNode;
-
QueueNode* QueueHead = CreateQueue();
-
EnterQueue(&QueueHead,grap->GListNode[0].pointNo);
-
while(EmptyQueue(QueueHead) == NoEmpty)
-
{
-
OutNode = OutQueue(&QueueHead);
-
if (OutNode == NULL)
-
{
-
return;
-
}
-
-
VisitLog[OutNode->PonintNo] = HasVisit;
-
ListNode* ListHead = grap->GListNode[OutNode->PonintNo].listHead;
-
ListNode* ListNext = ListHead->next;
-
while (ListNext != NULL)
-
{
-
if (VisitLog[ListNext->pointNo] == NoVisit)
-
{
-
printf("(%d,%d) = %d\n",OutNode->PonintNo,ListNext->pointNo,ListNext->wight);
-
EnterQueue(&QueueHead,ListNext->pointNo);
-
}
-
-
ListHead = ListNext;
-
ListNext = ListHead->next;
-
}
-
free(OutNode);
-
OutNode = NULL;
-
}
-
-
}
-
-
void DFS_Trav(GRAPH* grap,int i,int* Visit)
-
{
-
Visit[i] = HasVisit;
-
ListNode* ListNext = grap->GListNode[i].listHead->next;
-
while (NULL != ListNext)
-
{
-
if (Visit[ListNext->pointNo] == NoVisit)
-
{
-
printf("(%d,%d)=%d\n",i,ListNext->pointNo,ListNext->pointNo);
-
DFS_Trav(grap,ListNext->pointNo,Visit);
-
}
-
ListNext = ListNext->next;
-
}
-
}
-
void DFS(GRAPH* grap)
-
{
-
int VisitLog[MAX];
-
int i;
-
for (i=0; i<grap->points; i++)
-
{
-
VisitLog[i] = NoVisit;
-
}
-
-
for (i=0; i<grap->points; i++)
-
{
-
if (VisitLog[i] == NoVisit)
-
{
-
DFS_Trav(grap,i,VisitLog);
-
}
-
}
-
}
阅读(1338) | 评论(0) | 转发(0) |