網(wǎng)站首頁 編程語言 正文
常見的存圖方式有 鄰接矩陣,鄰接表,鏈式前向星 等。
一. 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
相關推薦
- 2024-01-07 Plugin ‘org.springframework.boot:spring-boot-maven
- 2022-09-04 Golang?實現(xiàn)?RTP音視頻傳輸示例詳解_Golang
- 2022-11-20 Python檢測PE所啟用保護方式詳解_python
- 2022-04-23 .NET?Core使用APB?vNext框架入門教程_實用技巧
- 2022-05-12 Centos python3 與 python2 共存
- 2023-04-24 python中argparse模塊及action='store_true'詳解_python
- 2022-05-19 gorm整合進go-zero的實現(xiàn)方法_Golang
- 2022-05-05 利用Python編寫一個記憶翻牌游戲_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支