邻接表与邻接矩阵的深度优先算法和广度优先算法

邻接矩阵的深度优先算法:

#include<iostream>
using namespace std;
#define MAX 20
// 注:邻接矩阵是图的顺序存储方式
typedef struct node
{
	int no; //顶点编号
			//	char data;	//顶点其它信息,不常用
}VertexType;
typedef struct  //图的定义
{
	int edges[MAX][MAX]; //邻接矩阵定义
	int n, e;	//顶点数和边数
	VertexType vex[MAX]; //存放节点信息
}MGraph;
MGraph graph;
bool visited[MAX];//访问过的标志为true   未访问的设置为false
void Create()
{
	cout << "输入顶点数和边数: ";
	cin >> graph.n >> graph.e;
	int N1 = graph.n, N2 = graph.e;
	for (int i = 0; i < N1; i++)
	{
		graph.vex[i].no = i;
	}

	for (int i = 0; i < N1; i++)//初始化edges数组
		for (int j = 0; j < N1; j++)
			graph.edges[i][j] = 0;
	//给邻接矩阵赋值
	for (int i = 0; i < N2; i++)
	{
		int v1, v2;
		cout << "v1,v2 = ";
		cin >> v1 >> v2;
		graph.edges[v1][v2] = 1;
	}
}
// 邻接矩阵的深度遍历操作
void DFS(int i)
{
	if (visited[i] == false)
	{
		visited[i] = true;
		cout << graph.vex[i].no << ' ';
		for (int j = 0; j < graph.n; j++)
		{
			if (graph.edges[i][j] == 1 && visited[j] == false) //如果 ij两点可达,且j未被访问过,就递归遍历j
				DFS(j);
		}
	}
}
void DFSTraverse()
{
	for (int i = 0; i < graph.n; i++)//初始化将visited所有点设置为false
		visited[i] = false;
	for (int j = 0; j < graph.n; j++)
		DFS(j);
}
int main(void)
{
	Create();
	DFSTraverse();

	system("pause");
	return 0;
}

邻接矩阵广度优先算法:

#include<iostream>
using namespace std;
#define MAX 20
// 注:邻接矩阵是图的顺序存储方式
typedef struct node
{
	int no; //顶点编号
			//	char data;	//顶点其它信息,不常用
}VertexType;
typedef struct  //图的定义
{
	int edges[MAX][MAX]; //邻接矩阵定义
	int n, e;	//顶点数和边数
	VertexType vex[MAX]; //存放节点信息
}MGraph;

MGraph graph;
bool visited[MAX];//访问过的标志为true   未访问的设置为false
void Create()
{
	cout << "输入顶点数和边数: ";
	cin >> graph.n >> graph.e;
	int N1 = graph.n, N2 = graph.e;
	for (int i = 0; i < N1; i++)
	{
		graph.vex[i].no = i;
	}

	for (int i = 0; i < N1; i++)//初始化edges数组
		for (int j = 0; j < N1; j++)
			graph.edges[i][j] = 0;
	//给邻接矩阵赋值
	for (int i = 0; i < N2; i++)
	{
		int v1, v2;
		cout << "v1,v2 = ";
		cin >> v1 >> v2;
		graph.edges[v1][v2] = 1;
	}
}
// 邻接矩阵的广度遍历操作
//类似于二叉树的按层遍历
void BFS()
{
	for (int i = 0; i < graph.n; i++)//初始化visited数组为false
		visited[i] = false;

	int queue[MAX];
	memset(queue, 0, sizeof(int)*MAX);
	int front = -1, rear = 0;

	queue[0] = graph.vex[0].no;//设置从0开始遍历
	visited[0] = true;

	while (front != rear)
	{
		front++;
		cout << queue[front];
		for (int j = 0; j < graph.n; j++)
		{
			//如果二者相连且该点没有被访问过,将此点入队
			if (graph.edges[queue[front]][j] == 1 && visited[j] == false)
			{
				queue[++rear] = j;
				visited[j] = true;
			}
		}
	}
}
int main(void)
{
	Create();
	BFS();

	system("pause");
	return 0;
}


邻接表建立图解:



邻接表的深度优先算法:

#include<iostream>
using namespace std;

#define MAX 20
typedef struct arcnode
{
	int adjvex; //该边所指向的节点的位置
	struct arcnode* nextarc; //指向下一条边的指针
							 //	int info;	//该边的其它信息,如权值等。
}ArcNODE; //表结点类型
typedef struct  vnode
{
	char data;//顶点信息
	ArcNODE *firstarc; //指向第一条边的指针
}VNode;//头结点
typedef struct
{
	VNode adjust[MAX]; //邻接表
	int n, e;// n为顶点数,e为边数
}AGraph;
AGraph graph;
bool visited[MAX];
void Create()
{

	cout << "输入顶点数和边数";
	cin >> graph.n >> graph.e;
	for (int i = 0; i < graph.n; i++)//初始化邻接表
	{
		graph.adjust[i].firstarc = (ArcNODE*)malloc(sizeof(ArcNODE));
		graph.adjust[i].firstarc->nextarc = NULL; //将第一个节点的next指针赋值为NULL
	}
	//建立邻接表
	for (int i = 0; i < graph.n; i++)
	{
		int n;
		cout << "输入" << i << "的顶点值";
		cin >> graph.adjust[i].data;
		cout << "输入" << i << "的出度";
		cin >> n;
		ArcNODE *q = graph.adjust[i].firstarc;
		while (n)
		{
			int num;
			cin >> num;
			ArcNODE *p;
			p = (ArcNODE*)malloc(sizeof(ArcNODE));
			p->adjvex = num;
			q->nextarc = p;
			q = p;
			q->nextarc = NULL;
			n--;
		}
	}

}
void DFS(int i)
{
	if (visited[i] == 0)
	{
		visited[i] = 1;
		ArcNODE*p = graph.adjust[i].firstarc->nextarc;//p指向表头节点
		cout << graph.adjust[i].data << ' ';
		while (p != NULL)
		{
			if (visited[p->adjvex] == 0) //由于表头里不存放值,所以判断nextarc的值
				DFS(p->adjvex);
			p = p->nextarc;
		}
	}
}
void DFSTraverse()
{
	for (int v = 0; v<graph.n; ++v)
		visited[v] = 0;
	for (int v = 0; v<graph.n; ++v)
		if (visited[v] == 0)
			DFS(v);
}
int main(void)
{
	Create();
	for (int i = 0; i < graph.n; i++)
		DFS(i);
	system("pause");
	return 0;
}


邻接表的广度优先算法:

#include<iostream>
using namespace std;

#define MAX 20
typedef struct arcnode
{
	int adjvex; //该边所指向的节点的位置
	struct arcnode* nextarc; //指向下一条边的指针
							 //	int info;	//该边的其它信息,如权值等。
}ArcNODE; //表结点类型
typedef struct  vnode
{
	char data;//顶点信息
	ArcNODE *firstarc; //指向第一条边的指针
}VNode;//头结点
typedef struct
{
	VNode adjust[MAX]; //邻接表
	int n, e;// n为顶点数,e为边数
}AGraph;
AGraph graph;
bool visited[MAX];
void Create()
{

	cout << "输入顶点数和边数";
	cin >> graph.n >> graph.e;
	for (int i = 0; i < graph.n; i++)//初始化邻接表
	{
		graph.adjust[i].firstarc = (ArcNODE*)malloc(sizeof(ArcNODE));
		graph.adjust[i].firstarc->nextarc = NULL; //将第一个节点的next指针赋值为NULL
	}
	//建立邻接表
	for (int i = 0; i < graph.n; i++)
	{
		int n;
		cout << "输入" << i << "的顶点值";
		cin >> graph.adjust[i].data;
		cout << "输入" << i << "的出度";
		cin >> n;
		ArcNODE *q = graph.adjust[i].firstarc;
		while (n)
		{
			int num;
			cin >> num;
			ArcNODE *p;
			p = (ArcNODE*)malloc(sizeof(ArcNODE));
			p->adjvex = num;
			q->nextarc = p;
			q = p;
			q->nextarc = NULL;
			n--;
		}
	}

}
void BFS()
{
	for (int i = 0; i < graph.n; i++)//初始化visited数组为false
		visited[i] = false;

	int queue[MAX];
	int front = -1, rear = 0;
	queue[0] = 0;
	visited[0] = true;
	while (front != rear)
	{
		front++;
		cout << graph.adjust[queue[front]].data;
		ArcNODE *p = graph.adjust[queue[front]].firstarc->nextarc;//p永远指向队列头所代表的那个表头节点的next
		while (p != NULL)
		{
			if (visited[p->adjvex] == 0)
			{
				visited[p->adjvex] = 1;
				queue[++rear] = p->adjvex;
			}
			p = p->nextarc;
		}
	}

}
int main(void)
{
	Create();
	BFS();
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值