#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
#define QUEUE_SIZE 10
int* visitedPtr;
typedef struct GraphNodeQueue{
int* nodes;
int front;
int rear;
}GraphNodeQueue,*QueuePtr;
QueuePtr iniQueue()
{
QueuePtr resultQueuePtr=(QueuePtr)malloc(sizeof(struct GraphNodeQueue));
resultQueuePtr->nodes=(int*)malloc(QUEUE_SIZE*sizeof(int));
resultQueuePtr->front=0;
resultQueuePtr->rear=1;
return resultQueuePtr;
}
bool isQueueEmpty(QueuePtr paraQueuePtr)
{
if((paraQueuePtr->front+1)%QUEUE_SIZE==paraQueuePtr->rear)
{
return true;
}
return false;
}
void enqueue(QueuePtr paraQueuePtr,int paraNode)
{
if((paraQueuePtr->rear+1)%QUEUE_SIZE==paraQueuePtr->front)
{
printf("Error,trying to enqueue %d.queue full.\r\n",paraNode);
return;
}
paraQueuePtr->nodes[paraQueuePtr->rear]=paraNode;
paraQueuePtr->rear=(paraQueuePtr->rear+1)%QUEUE_SIZE;
}
int dequeue(QueuePtr paraQueuePtr)
{
if(isQueueEmpty(paraQueuePtr))
{
printf("error,empty queue\r\n");
return -1;
}
paraQueuePtr->front=(paraQueuePtr->front+1)%QUEUE_SIZE;
return paraQueuePtr->nodes[paraQueuePtr->front];
}
typedef struct Graph{
int** connections;
int numNodes;
}*GraphPtr;
GraphPtr intiGraph(int paraSize,int** paraData)
{
int i,j;
GraphPtr resultPtr=(GraphPtr)malloc(sizeof(struct Graph));
resultPtr->numNodes=paraSize;
resultPtr->connections=(int**)malloc(paraSize*sizeof(int*));
for(i=0;i<paraSize;i++)
{
resultPtr->connections[i]=(int*)malloc(paraSize*sizeof(int));
for(j=0;j<paraSize;j++)
{
resultPtr->connections[i][j]=paraData[i][j];
}
}
return resultPtr;
}
void initTranverse(GraphPtr paraGraphPtr)
{
int i;
visitedPtr=(int*)malloc(paraGraphPtr->numNodes* sizeof(int));
for(i=0;i<paraGraphPtr->numNodes;i++)
{
visitedPtr[i]=0;
//初始化visitedPtr数组中的每个元素为0,表示所有节点都没有被访问过。
}
}
void depthFirstTranverse(GraphPtr paraGraphPtr,int paraNode)
{
int i;
visitedPtr[paraNode]=1;
//函数将节点paraNode标记为已访问
printf("%d\t",paraNode);
for(i=0;i<paraGraphPtr->numNodes;i++)
{
if(!visitedPtr[i])
{
if(paraGraphPtr->connections[paraNode][i])
//图的连接表示为paraGraphPtr -> connections[paraNode][i]
{
depthFirstTranverse(paraGraphPtr,i);
}
}
}
}
//用于宽度优先搜索(BFS)图
//paraStart:起始节点的标号
void widthFirstTranverse(GraphPtr paraGraphPtr,int paraStart)
{
int i,j,tempNode;
i=0;
QueuePtr tempQueuePtr=iniQueue();
printf("%d\t",paraStart);
visitedPtr[paraStart]=1;//将起始节点加入队列,并标记为已访问。
enqueue(tempQueuePtr,paraStart);
while(!isQueueEmpty(tempQueuePtr))
{
tempNode=dequeue(tempQueuePtr);
//从队列中取出一个节点,并将其标记为已访问
visitedPtr[tempNode]=1;
i++;
for(j=0;j<paraGraphPtr->numNodes;j++)
{
if(visitedPtr[j])
{
continue;
}
if(paraGraphPtr->connections[tempNode][j]==0)
{
continue;
}
printf("%d\t",j);
visitedPtr[j]=1;
enqueue(tempQueuePtr,j);
}
}
}
void testGraphTranverse()
{
int i,j;
int myGraph[5][5]={
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 1},
{1, 0, 1, 0, 0},
{0, 1, 1, 0, 0}};
int** tempPtr;
printf("Prepared graph:\r\n");
tempPtr=(int**)malloc(5*sizeof(int*));
for(i=0;i<5;i++)
{
tempPtr[i]=(int*)malloc(sizeof(int)*5);
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
tempPtr[i][j]=myGraph[i][j];
}
}
printf("Dara ready\r\n");
GraphPtr tempGraphPtr=intiGraph(5,tempPtr);
printf("num nodes =%d\r\n",tempGraphPtr->numNodes);
printf("Graph initialized\r\n");
printf("Depth first visit:\r\n");//深度优先访问
initTranverse(tempGraphPtr);
depthFirstTranverse(tempGraphPtr, 4);
printf("\r\nWidth first visit:\r\n");//宽度优先访问
initTranverse(tempGraphPtr);
widthFirstTranverse(tempGraphPtr, 4);
}
int main(){
testGraphTranverse();
return 1;
}//Of main