图的深度搜索DFS和广度搜索BFS

本文详细介绍了图的深度优先搜索(DFS)和广度优先搜索(BFS)算法的实现方式,包括递归、标记数组和队列的应用,并提供了完整的C语言代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图的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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值