不同存图方式下的DFS和BFS实现

常见的存图方式有 邻接矩阵,邻接表,链式前向星 等。

一. DFS:

  1.邻接矩阵:

(一定记住初始化或清空)

数据结构实验之图论二:图的深度遍历

#include <bits/stdc++.h>
using namespace std;

int mp[110][110];
int vis[110];
int n, m;
vector<int> ans;
void dfs(int x)
{
	vis[x] = 1;
	ans.push_back(x);
	for (int i = 0; i < n; i++)
	{
		if (mp[x][i] && !vis[i]) //保证进入递归的都是没被遍历过的
		{
			dfs(i);
		}
	}
}
/* void dfs(int x)
{
	if (vis[x] == 1)//vis也可以在新的递归中判断
		return;
	vis[x] = 1;
	ans.push_back(x);
	for (int i = 0; i < n; i++)
	{
		if (mp[x][i])
			dfs(i);
	}
} */
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		memset(vis, 0, sizeof vis);
		memset(mp, 0, sizeof mp);
		ans.clear(); //一定要记得清空,尤其是多组输入的时候
		cin >> n >> m;
		for (int i = 1; i <= m; i++)
		{
			int u, v;
			cin >> u >> v;
			mp[u][v] = mp[v][u] = 1;
		}
		dfs(0);
		for (int res : ans)
			cout << res << " ";
		cout << endl;
	}
	return 0;
}

2.邻接表:

对于某些题目来说,题目要求要在遍历时从小到大,故这时就要将vector存图改为优先队列存图,同时还要将排序方式改为升序排列。

而对于邻接矩阵来说就没有这样的问题,因为在遍历出边时是从按照编号从头开始,故可以保证编号从小到大。

数据结构实验之图论四:迷宫探索

#include <bits/stdc++.h>
using namespace std;

priority_queue<int,vector<int>,greater<int> > mp[1100];
int vis[1100];
int n, m, k;
vector<int> ans;

inline void clear(priority_queue<int,vector<int>,greater<int> > &q) {
    priority_queue<int,vector<int>,greater<int> > empty;
    swap(empty,q);
}//自定义队列清空函数

void dfs(int x)
{
	vis[x] = 1;
	ans.push_back(x);
	while(!mp[x].empty())
	{
		int g=mp[x].top();
		mp[x].pop();
		if (!vis[g]) //保证进入递归的都是没被遍历过的
		{
			dfs(g);
			ans.push_back(x);
		}
	}
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		memset(vis, 0, sizeof vis);
		for (int i = 0; i < 1100; i++)
		{
			 clear(mp[i]);
		}
		ans.clear(); //一定要记得清空,尤其是多组输入的时候
		cin >> n >> m >> k;
		for (int i = 1; i <= m; i++)
		{
			int u, v;
			cin >> u >> v;
			mp[u].push(v);
			mp[v].push(u);
		}
		dfs(k);
		for (int res : ans)
			cout << res << " ";
			 if (ans.size() != n * 2 - 1)
            cout << "0";
		cout << endl;
	}
	return 0;
}

3.链式前向星:

应当注意,由于链式前向星的存储原理是无序的,并且只能按照存好后的顺序进行遍历,故在某些要求遍历顺序的题目中尽量谨慎使用。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;

int vis[N];
int n, m, k;
vector<int> ans;
int e[N], ne[N], h[N], idx;

void add(int u, int v)
{
	e[idx] = v;		// e[i]代表第i条边的尾节点
	ne[idx] = h[u]; // ne[i]代表与第i条边同头节点的上一条边的编号
	h[u] = idx++;	// h[i]代表最新存入头节点为i的边的位置
	//链式前向星存图方式是将同一头节点的边的位置信息顺序存储下来,只记录最新的位置,遍历时从最新的位置就可逐步遍历
	// e:位置->节点
	// ne:位置->位置
	// h:节点->位置
}

void dfs(int x)
{
	vis[x] = 1;
	ans.push_back(x);
	for (int i = h[x]; i != -1; i = ne[i])
	{
		int t = e[i];
		if (!vis[t])
		{
			dfs(t);
		}
	}
}

//同样的两种不同的时候判断是否遍历过
/* void dfs(int x)
{
	if(vis[x])
	return;
	vis[x]=1;
	ans.push_back(x);
	for(int i=h[x];i!=-1;i=ne[i])
	{
		int t=e[i];
		dfs(t);
	}
} */
void init()
{
	memset(h, -1, sizeof(h));
	idx = 0;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		init();
		memset(vis, 0, sizeof vis);
		ans.clear(); //一定要记得清空,尤其是多组输入的时候
		cin >> n >> m /* >> k */;
		for (int i = 1; i <= m; i++)
		{
			int u, v;
			cin >> u >> v;
			add(u, v);
			add(v, u);
		}
		dfs(1);
		for (int res : ans)
			cout << res << " ";
		cout << endl;
	}
	return 0;
}

二. BFS:

1.邻接矩阵:

邻接矩阵适合存顶点数量级不大的稠密图。

数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;

int vis[N];
int n,m,k;
int mp[1010][1010];//邻接矩阵存不了太大的图
vector<int> ans;
void init()
{
	ans.clear();
	memset(vis,0,sizeof vis);
	memset(mp,0,sizeof mp);
}
void bfs(int x)
{
	queue<int> q;
	q.push(x);
	vis[x]=1;
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		ans.push_back(t);
		for(int i=0;i<n;i++)
		{
			if(mp[t][i]==1&&vis[i]==0)
			{
				vis[i]=1;
				q.push(i);
			}
		}
	}
}
int main()
{	
	int t;
	cin>>t;
	while(t--)
	{
		init();
		cin>>n>>m>>k;
		for(int i=1;i<=m;i++)
		{
			int u,v;
			cin>>u>>v;
			mp[u][v]=1;
			mp[v][u]=1;
		}
		bfs(k);
		for(int res:ans)
		cout<<res<<" ";
		cout<<endl;
	}
	return 0;
}

2. 邻接表

(个人感觉邻接表是最好用的,空间够大,还可以利用堆优化。)

数据结构实验之图论二:基于邻接表的广度优先搜索遍历

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;

int vis[N];
int n,m,k;
vector<int> mp[N];
vector<int> ans;
void init()
{
	ans.clear();
	memset(vis,0,sizeof vis);
	memset(mp,0,sizeof mp);
}
void bfs(int x)
{
	queue<int> q;
	q.push(x);
	vis[x]=1;
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		ans.push_back(t);
		for(int r:mp[t])
		{
			if(vis[r]==0)
			{
				vis[r]=1;
				q.push(r);
			}
		}
	}
}
int main()
{	
	int t;
	cin>>t;
	while(t--)
	{
		init();
		cin>>n>>m>>k;
		for(int i=1;i<=m;i++)
		{
			int u,v;
			cin>>u>>v;
			mp[u].push_back(v);
			mp[v].push_back(u);
		}
		bfs(k);
		for(int res:ans)
		cout<<res<<" ";
		cout<<endl;
	}
	return 0;
}

3.链式前向星

(链式前向星还是要多练)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int vis[N];
int n, m, k;
struct edge
{
    int to;
    int ne;
} e[N];
int h[N];
int idx;
void add(int u, int v)
{
    e[idx].to = v;
    e[idx].ne = h[u];
    h[u] = idx++;
}
vector<int> ans;
void init()
{
    memset(vis, 0, sizeof(vis));
    ans.clear();
    memset(h, -1, sizeof(h));
    idx =0;
}

void bfs(int x)
{
    queue<int> q;
    q.push(x);
    vis[x] = 1;
    while (!q.empty())
    {
        int t = q.front();
        q.pop();
        ans.push_back(t);
        for (int i = h[t]; i != -1; i = e[i].ne)
        {
            int r = e[i].to;
            if (!vis[r])
            {
                vis[r] = 1;
                q.push(r);
            }
        }
    }
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        init();
        cin >> n >> m >> k;
        for (int i = 1; i <= m; i++)
        {
            int u, v;
            cin >> u >> v;
            add(u, v);
            add(v, u);
        }
        bfs(k);
        for (int res : ans)
            cout << res << " ";
        cout << endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值