图C语言实现

实现图的创建邻接矩阵方法和遍历DFS和BFS,DFS核心就是递归,递归的回退蛮有意思.BFS用的是队列

是用一个结构储存图,用二维数组储存节点连接情况

DFS是访问和当且节点A相连且未被访问的节点B,然后以B为当前的节点深入下去(进入递归).而BFS是访问当且节点(被删除的)相连的所有节点,然后放入队列

 

明天体测,本来不想去,但我想知道跟喜欢的女生一起跑会发生什么,下个周马原实践,形策登台演讲,蛮有意思,生活好像是在一个又有一个的期待中度过的.我是屌丝吗....

我觉得青春的美好之处就是它的无限可能

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct GNode* PtrToGNode;
typedef struct ENode* PtrToENode;

typedef int Vertex;
typedef int WeightType;
typedef char DataType;
struct GNode {
	int Nv;
	int Ne;
	WeightType** G;//二维矩阵,元素为边的权重,若为无向图则权重为1
	DataType* Data;
};
struct ENode {				//每一条边都对应一个结构
	Vertex V1, V2;
	WeightType weight;

};
typedef PtrToENode Edge;
typedef PtrToGNode MyGraph;
MyGraph CreateGraph(int VertexNum);
MyGraph Build_Graph();
_Bool Visited[100];
void DFS(MyGraph Grapth, Vertex V);
void Visit(Vertex v);

#include<stdio.h>
#include<stdlib.h> 
typedef int Position;
typedef struct QNode* PtrToQNode;
struct QNode {
	int* Data;
	Position Front, Rear;//头部和尾部;数组中头部对应的部分不是队列的元素.>=0&&<=MaxSize-1
	int MaxSize;//数组长度,但为了区分空和满,只存放MaxSize-1个元素
};
typedef PtrToQNode Queue;
Queue CreateQueue(int MaxSize);
_Bool AddQueue(Queue Q, int Element);
_Bool IsFull(Queue Q);
_Bool IsEmpty(Queue Q);
int DeleteQ(Queue Q);
void BFS(MyGraph Graph, Vertex S);
int main()
{
	MyGraph a = Build_Graph();
	for (int i = 0; i < a->Nv; i++)//将所有节点设置成FALSE,以访问的设置成TRUE
	{
		Visited[i] = FALSE;
	}
	BFS(a, 4);
}
MyGraph CreateGraph(int VertexNum)//创建一个图并将邻接矩阵所有节点都初始化为0
{
	MyGraph a = (MyGraph)malloc(sizeof(struct GNode));
	a->G = (WeightType**)malloc(sizeof(WeightType*) * VertexNum);//G是一个指向指针的指针
	for (int i = 0; i < VertexNum; i++)
	{
		a->G[i] = (WeightType*)malloc(sizeof(WeightType) * VertexNum);//a->G[i]是一个指向int的指针
	}
	for (int i = 0; i < VertexNum; i++)
	{
		for (int j = 0; j < VertexNum; j++)
		{
			a->G[i][j] = 0;
		}
	}
	a->Nv = VertexNum;
	a->Ne = 0;
	return a;
}
void InsertEdge(MyGraph Graph, Edge E)//创建无向图,所以G[V2][V1]也得改
{
	Graph->G[E->V1][E->V2] = E->weight;
	Graph->G[E->V2][E->V1] = E->weight;
}
MyGraph Build_Graph()
{
	int VertexNum;
	Edge E = (Edge)malloc(sizeof(struct ENode));
	printf("please Enter the number of the vertexs\n");
	scanf("%d", &VertexNum);
	MyGraph Graph = CreateGraph(VertexNum);
	printf("please Enter the number of the Edges\n");
	scanf("%d", &Graph->Ne);
	if (Graph->Ne)
	{
		for (int i = 0; i < Graph->Ne; i++)
		{
			scanf("%d %d %d", &E->V1,&E->V2,&E->weight);
			InsertEdge(Graph, E);
		}
	}
	return Graph;

}
void Visit(Vertex v)
{
	printf("当前访问的节点是%d\n", v);
}
void DFS(MyGraph Graph, Vertex V)
{
	printf("正在访问的是%d号节点\n", V);
	Visited[V] = TRUE;
	for (int i = 0; i < Graph->Nv; i++)
	{
		if (Graph->G[V][i] == 1 && Visited[i] == FALSE)
			DFS(Graph, i);
	}
}

Queue CreateQueue(int MaxSize)
{
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (int*)malloc(sizeof(int) * MaxSize);
	Q->Front = 0;
	Q->Rear = 0;
	Q->MaxSize = MaxSize;
	return Q;

}
_Bool IsFull(Queue Q)
{
	return(Q->Front == ((Q->Rear + 1) % Q->MaxSize));
}
_Bool AddQueue(Queue Q, int Element)
{
	if (IsFull(Q))
		return 0;
	else
	{
		Q->Rear = (Q->Rear + 1) % Q->MaxSize;
		Q->Data[Q->Rear] = Element;

		return 1;
	}
}
_Bool IsEmpty(Queue Q)
{
	return(Q->Front == Q->Rear);
}
int DeleteQ(Queue Q)
{
	if (IsEmpty(Q))
		return 0;
	else {
		Q->Front = (Q->Front + 1) % Q->MaxSize;
		return Q->Data[Q->Front];
	}
}
void BFS(MyGraph Graph,Vertex S)
{
	int Del_Node;//被删除的节点
	Queue Q = CreateQueue(Graph->Nv);
	Visit(S);//第五个节点开始
	Visited[S] = TRUE;
	AddQueue(Q, S);
	while (!IsEmpty(Q))
	{
		Del_Node = DeleteQ(Q);
		for (int i = 0; i < Graph->Nv; i++)
		{
			if (Graph->G[Del_Node][i]!=0&&Visited[i]==FALSE)
			{
				Visit(i);
				Visited[i] = TRUE;
				AddQueue(Q, i);
			}
		}


	}
}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值