日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

不同存圖方式下的DFS和BFS實現(xiàn)

作者:一般六 更新時間: 2022-07-16 編程語言

常見的存圖方式有 鄰接矩陣,鄰接表,鏈式前向星 等。

一. DFS:

??1.鄰接矩陣:

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

數(shù)據(jù)結構實驗之圖論二:圖的深度遍歷

#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存圖改為優(yōu)先隊列存圖,同時還要將排序方式改為升序排列。

而對于鄰接矩陣來說就沒有這樣的問題,因為在遍歷出邊時是從按照編號從頭開始,故可以保證編號從小到大。

數(shù)據(jù)結構實驗之圖論四:迷宮探索

#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);
}//自定義隊列清空函數(shù)

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條邊的尾節(jié)點
	ne[idx] = h[u]; // ne[i]代表與第i條邊同頭節(jié)點的上一條邊的編號
	h[u] = idx++;	// h[i]代表最新存入頭節(jié)點為i的邊的位置
	//鏈式前向星存圖方式是將同一頭節(jié)點的邊的位置信息順序存儲下來,只記錄最新的位置,遍歷時從最新的位置就可逐步遍歷
	// e:位置->節(jié)點
	// ne:位置->位置
	// h:節(jié)點->位置
}

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.鄰接矩陣:

鄰接矩陣適合存頂點數(shù)量級不大的稠密圖。

數(shù)據(jù)結構實驗之圖論一:基于鄰接矩陣的廣度優(yōu)先搜索遍歷

#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. 鄰接表

(個人感覺鄰接表是最好用的,空間夠大,還可以利用堆優(yōu)化。)

數(shù)據(jù)結構實驗之圖論二:基于鄰接表的廣度優(yōu)先搜索遍歷

#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;
}

原文鏈接:https://blog.csdn.net/qq_64637635/article/details/125797254

欄目分類
最近更新