图的广度优先遍历类似于树的按层次遍历过程。图的深度优先遍历类似于树中的先根遍历。树中不存在回路,但图中可能有回路,为避免走过重复的点,应该在每个结点设立一个访问标志,在遍历中,应为每个结点设立一个访问标志,每扫描到一个结点,要检查它的访问标志,若标志为“未访问”,则按正常方式对其进行处理(如访问或转到它的邻接点等);若标志为“已访问”,则扫描下一个结点。
#include<stdio.h>
#include<stdlib.h>
int* visited; //标记数组
typedef struct{
int Graph[5][5];
}Graph,*GraphPtr;
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}
};
GraphPtr initialize(){
int i,j;
GraphPtr resultPtr=(GraphPtr)malloc(sizeof(Graph));
for(i=0;i<5;i++){
for(j=0;j<5;j++){
resultPtr->Graph[i][j]=myGraph[i][j];
}
}
return resultPtr;
}
void Deep(GraphPtr paraGraph,int paraNode){
int i;
printf("%d\t",paraNode);
visited[paraNode]=1;
for(i=0;i<5;i++){
if(!visited[i]){
if(paraGraph->Graph[paraNode][i]){
Deep(paraGraph,i);
}
}
}
}
void DeepTranverse(GraphPtr paraGraph,int paraStart){
int i;
visited=(int*)malloc(sizeof(int));
for(i=0;i<5;i++){
visited[i]=0;
}
Deep(paraGraph,paraStart);
}
void test(){
GraphPtr tempGraph=initialize();
DeepTranverse(tempGraph,0);
}
int main(){
test();
}
广度优先遍历中采用了队列保存遍历结果
#include<stdio.h>
#include<malloc.h>
#define numNode 5
typedef struct
{
int ele[5][5];
}Graph,*GraphPtr;
typedef struct BIQueue
{
GraphPtr* array;
int size;
int head;
int tail;
}*BQPtr;
int *visitedPtr;
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 } };
void BFSFirst(GraphPtr paraGraphPtr, int paraStart);
GraphPtr initGraph()
{
int i, j;
GraphPtr temPtr = (GraphPtr)malloc(sizeof(Graph));
for (i = 0; i < numNode; i++)
{
for (j = 0; j < numNode; j++)
{
temPtr->ele[i][j] = myGraph[i][j];
}
}
return temPtr;
}
BQPtr initqueue()
{
BQPtr resultPtr = (BQPtr)malloc(sizeof(struct BIQueue));
resultPtr->size =numNode;
resultPtr->array = (BQPtr)malloc(numNode*sizeof(struct BIQueue));
resultPtr->head = 0;
resultPtr->tail = 0;
return resultPtr;
}
void initArray(GraphPtr paraGraphPtr, int paraStart)
{
int i;
visitedPtr = (int *)malloc(numNode*sizeof(int));
for (i = 0; i<numNode; i++)
{
visitedPtr[i] = 0;
}
BFSFirst(paraGraphPtr, paraStart);
}
int enqueue(BQPtr paraQueue)
{
if (paraQueue->tail - paraQueue->head + 1>paraQueue->size)
{
printf("Queue full");
return -1;
}
paraQueue->tail++;
//paraQueue->array[paraQueue->tail%paraQueue->size] = paraelements;
return 0;
}
void dequeue(BQPtr paraQueue)
{
if (paraQueue->tail == paraQueue->head)
{
printf("Null queue");
return NULL;
}
paraQueue->head++;
//return paraQueue->array[paraQueue->head % paraQueue->size];
}
int isEmpty(BQPtr paraQueue)
{
if (paraQueue->head == paraQueue->tail)
{
return 1;
}
else
{
return 0;
}
}
void BFSFirst(GraphPtr paraGraphPtr, int paraNode)
{
int i, j;
BQPtr newPtr=initqueue();
enqueue(newPtr);
visitedPtr[paraNode] = 1;
printf("%d ", paraNode);
while (isEmpty(newPtr) == 0)
{
dequeue(newPtr);
for (j = 0; j<numNode; j++)
{
if (paraGraphPtr->ele[paraNode][j] != 0 && visitedPtr[j] == 0)
{
enqueue(newPtr);
visitedPtr[j] = 1;
printf("%d ", j);
}
}
}
for (i = 0; i < numNode; i++)
{
if (visitedPtr[i] == 0)
{
BFSFirst(paraGraphPtr,i);
}
}
}
int main()
{
GraphPtr lastPtr = initGraph();
initArray(lastPtr,3);
printf("\n");
printf("图的广度遍历\n");
return 0;
}