数据结构图的创建和遍历(dfs,bfs,附代码)

145 篇文章 1 订阅
122 篇文章 0 订阅

1.图的存储结构:第一种为邻接矩阵,矩阵为n*n(n为图的顶点数),其中行代表图的顶点,列代表图的关系,[a][b]代表从a顶点到b顶点的边,其中没右边用0表示,有边用1表示,主对角线恒为0

,如果路径带权则原本1的位置替换为权重

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
	int n;
	cout << "输入矩阵顶点数" << endl;
	cin >> n;
	vector<vector<int>>sove(n,vector<int>(n, 0)) ;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << sove[i][j] << ' ';
		}
		cout << endl;
	}
	printf("请输入矩阵边数\n");
	int m;
	cin >> m;
	printf("请输入矩阵边的关系\n");
	for (int x = 0; x < m; x++)
	{
		int row;
		int col;
		cin >> row >> col;
		sove[row][col] = 1;
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << sove[i][j] << ' ';
		}
		cout << endl;
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (sove[i][j] == 1)
			{
				sove[i][j] = 0;
			}
			else
			{
				sove[i][j] = 99999;
			}
		}
		
	}
	printf("现在建立带权矩阵,请重新输入顶点及权重\n");
	for (int x = 0; x < m; x++)
	{
		int row;
		int col;
		int value;
		cin >> row >> col >> value;
		sove[row][col] = value;
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << sove[i][j] << ' ';
		}
		cout << endl;
	}
	return 0;

}

第二种存储结构为邻接表,首先它的主体是一个包含着各顶点编号的数组,每个对应位置的节点都包含着一个结构体指针,这个指针指向了起点为该顶点的所有边,其中包含着结束位置,权重等信息

typedef struct cnode
{
	int node;
	int value;
    cnode* next1;
}cnode;
typedef struct  fnode
{

	cnode* head;
	
}fnode;
vector<fnode>father;
printf("请输入矩阵的顶点数\n");
int n;
cin >> n;

for (int x = 0; x < n; x++)
{
	fnode newnode;
	newnode.head = nullptr;
	father.push_back(newnode);

}
printf("请输入矩阵的边数\n");
int m;
cin >> m;
printf("请输入矩阵的边和权重\n");
for (int x = 0; x < m; x++)
{
	int begin;
	int end;
	int v;
	cin >> begin >> end >> v;
	cnode* newnode=new cnode;
	newnode->next1 = nullptr;
	newnode->node = end;
	newnode->value = v;
	cnode* point = father[begin].head;
	if (point== nullptr)
	{
		father[begin].head = newnode;
	 }
	else
	{
		while (point->next1!=nullptr)
		{
			point = point->next1;
		}
		point->next1 = newnode;
	}

}

再来讲解一下bfs的递归形式,这个函数和平常的bfs完全没有区别,首先由于每个顶点只需访问一次,所以使用check数组(visit),且递归出口为某顶点已经被访问过,这里的问题更像子集形式,所以选择内嵌循环,不断向后遍历边

void dfs(vector<bool>& visit, vector<fnode>& father,int pos)
{
	if (visit[pos] == true)
	{
		return;
	}
	visit[pos] = true;
	cout << pos << "->";
	cnode* next = father[pos].head;
	while (next!=nullptr)
	{
		int pos = next->node;
		if (visit[pos] == false)
		{
			dfs(visit, father, pos);
		}
		next = next->next1;
	}

}

最后将它改为非递归形式,首先将第一个顶点入栈,然后进行出栈操作,对顶点进行打印,然后再对边进行进栈操作,等待下一层循环进行依次打印

void dfs1(vector<fnode>& father, vector<bool>& visit, int pos)
{
	stack<int>s;
	visit[pos] = true;
	s.push(pos);
	while (!s.empty())
	{
		int data = s.top();
		s.pop();
		cout << data<< "->";
		cnode* next = father[data].head;
		while (next != nullptr)
		{
			int cur = next->node;
			if (visit[cur] == false)
			{
				s.push(cur);
				visit[cur] = true;
			}
			next = next->next1;
		}
	}
}

bfs,只需把上面非递归的代码一切有关栈的操作改成队列即可

void bfs(vector<fnode>& father, vector<bool>& visit, int pos)
{
	queue<int>s;
	visit[pos] = true;
	s.push(pos);
	while (!s.empty())
	{
		int data = s.front();
		s.pop();
		cout << data << "->";
		cnode* next = father[data].head;
		while (next != nullptr)
		{
			int cur = next->node;
			if (visit[cur] == false)
			{
				s.push(cur);
				visit[cur] = true;
			}
			next = next->next1;
		}
	}
}

  • 13
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值