图的DFS和BFS实现方式:
- DFS:递归和标记数组
- BFS:队列和标记数组
代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct graph
{
int nVertex;
int nEdge;
int* pMatrix;
}Graph;
Graph *CreateGraph()
{
Graph *pGraph = NULL;
pGraph = (Graph*)malloc(sizeof(Graph));
int nV;
int nE;
printf("input nV and nE:\n");
scanf("%d%d", &nV, &nE);
pGraph->nVertex = nV;
pGraph->nEdge = nE;
pGraph->pMatrix = (int *)malloc(sizeof(int)*nV*nV);
memset(pGraph->pMatrix, 0, sizeof(int)*nV*nV);
int i;
int v1,v2;
for(i=0;i<nE;i++)
{
printf("input Edge <v1,v2>:");
scanf("%d%d", &v1, &v2);
if(v1 >=1 && v1<=nV && v2>=1 && v2<=nV && v1!=v2 && pGraph->pMatrix[(v1-1)*nV+(v2-1)]==0)
{
pGraph->pMatrix[(v1-1)*nV+(v2-1)] = 1;
pGraph->pMatrix[(v2-1)*nV+(v1-1)] = 1;
}
else
{
i--;
}
}
return pGraph;
}
void MyDFS(Graph *pGraph, int nBegin, int *pMark)
{
printf("%d ", nBegin);
pMark[nBegin-1] = 1;
int i;
for(i=0;i<pGraph->nVertex;i++)
{
if(pGraph->pMatrix[(nBegin-1)*pGraph->nVertex+i]==1 && pMark[i]==0)
{
MyDFS(pGraph, i+1, pMark);
}
}
}
void DFS(Graph *pGraph, int nBegin)
{
if(pGraph == NULL) return;
int *pMark = NULL;
pMark = (int*)malloc(sizeof(int)*pGraph->nVertex);
memset(pMark, 0, sizeof(int)*pGraph->nVertex);
MyDFS(pGraph, nBegin, pMark);
free(pMark);
pMark = NULL;
}
int main()
{
Graph *pGraph = NULL;
pGraph = CreateGraph();
DFS(pGraph, 1);
printf("\n");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct graph
{
int nVertex;
int nEdge;
int* pMatrix;
}Graph;
typedef struct node
{
int nValue;
struct node *pNext;
}Node;
typedef struct queue
{
int nCount;
Node *pHead;
Node *pTail;
}Queue;
void Init(Queue **pQueue)
{
*pQueue = (Queue*)malloc(sizeof(Queue));
(*pQueue)->pHead = NULL;
(*pQueue)->pTail = NULL;
(*pQueue)->nCount = 0;
}
void Push(Queue *pQueue, int nValue)
{
if(pQueue == NULL) exit(1);
Node *pTemp = NULL;
pTemp = (Node*)malloc(sizeof(Node));
pTemp->nValue = nValue;
pTemp->pNext = NULL;
if(pQueue->pHead == NULL)
{
pQueue->pHead = pTemp;
}
else
{
pQueue->pTail->pNext = pTemp;
}
pQueue->pTail = pTemp;
pQueue->nCount++;
}
int Pop(Queue *pQueue)
{
if(pQueue == NULL) exit(1);
if(pQueue->nCount == 0) return -1;
Node *pDel = NULL;
int nNum;
pDel = pQueue->pHead;
nNum = pQueue->pHead->nValue;
pQueue->pHead = pQueue->pHead->pNext;
free(pDel);
pDel = NULL;
pQueue->nCount--;
if(pQueue->nCount == 0)
{
pQueue->pTail = NULL;
}
return nNum;
}
int IsEmpty(Queue *pQueue)
{
if(pQueue == NULL) exit(1);
return pQueue->nCount==0? 1:0;
}
Graph *CreateGraph()
{
Graph *pGraph = NULL;
pGraph = (Graph*)malloc(sizeof(Graph));
int nV;
int nE;
printf("input nV and nE:\n");
scanf("%d%d", &nV, &nE);
pGraph->nVertex = nV;
pGraph->nEdge = nE;
pGraph->pMatrix = (int *)malloc(sizeof(int)*nV*nV);
memset(pGraph->pMatrix, 0, sizeof(int)*nV*nV);
int i;
int v1,v2;
for(i=0;i<nE;i++)
{
printf("input Edge <v1,v2>:");
scanf("%d%d", &v1, &v2);
if(v1 >=1 && v1<=nV && v2>=1 && v2<=nV && v1!=v2 && pGraph->pMatrix[(v1-1)*nV+(v2-1)]==0)
{
pGraph->pMatrix[(v1-1)*nV+(v2-1)] = 1;
pGraph->pMatrix[(v2-1)*nV+(v1-1)] = 1;
}
else
{
i--;
}
}
return pGraph;
}
void BFS(Graph *pGraph, int nBegin)
{
if(pGraph == NULL) return;
int *pMark = NULL;
pMark = (int*)malloc(sizeof(int)*pGraph->nVertex);
memset(pMark, 0, sizeof(int)*pGraph->nVertex);
Queue *pQueue = NULL;
Init(&pQueue);
int i;
Push(pQueue, nBegin);
pMark[nBegin-1] = 1;
while(!IsEmpty(pQueue))
{
nBegin = Pop(pQueue);
printf("%d ", nDel);
for(i=0;i<pGraph->nVertex;i++)
{
if(pGraph->pMatrix[(nBegin-1)*pGraph->nVertex+i] == 1 && pMark[i] == 0)
{
Push(pQueue, i+1);
pMark[i] = 1;
}
}
}
printf("\n");
}
int main()
{
Graph *pGraph = NULL;
pGraph = CreateGraph();
BFS(pGraph, 1);
return 0;
}