当前位置:网站首页>图的存储和遍历
图的存储和遍历
2022-07-24 15:07:00 【柘木木】
图的存储
图的存储方式有两种:分别是邻接矩阵和邻接表;
邻接矩阵:
开一个二维数组G[i][j],值为1则说明有边,反之则0说明没有边,其值也可以存放边权。
只适用于顶点数不太大(一般不超过1000)的题目
邻接表:
用vector实现邻接表,在一些顶点数目较大(一般顶点个数在1000以上)的情况下,一般是使用邻接表而不是邻接矩阵才存储图。
图的遍历
图的遍历是指按一定顺序遍历所有顶点, 一般遍历方法有两种,分别是深度优先搜索和广度优先搜索。
深度优先搜索遍历图:
以"深度"为关键词,沿着一条路直到无法继续前进,才退回到路径上离当前顶点最近的且有未遍历分支的岔路口,并前往访问那些未遍历的分支,直到遍历完整个图。
如果是用邻接矩阵存储图,就用以下方法遍历图:
//可以相互到达的结点的集合是连同块,无向图又叫做连同分量,有向图又叫做强连通分量;
const int maxn = 10000;//最大顶点数;
const int INF = 1e6;
//邻接矩阵实现图存储;
int n, G[maxn][maxn];
bool vis[maxn] = {false};//判断该结点是否有访问;
//遍历顶点的分支;
void DFS(int u, int depth) {//顶点编号;
vis[u] = true;//当前顶点已被访问;
//此处可以对u进行一些操作;
for(int v = 0; v < n; v++) {//对u的分支顶点进行枚举;
if(vis[v] == false && G[u][v] != INF){
DFS(v, depth+1);
}
}
}
//遍历顶点;
void DFSTrave( ) {
for(int u = 0; u < n; u++) {//遍历图的所有顶点;
if(vis[u] == false) {
DFS(u, 1);//u == 0的顶点是第一层;
}
}
} 邻接表存储图:
//邻接表存储图;
const int maxn = 1010;
const int INF = 1e6 + 10;
bool vis[maxn] = {false};//判断该结点是否有被访问;
vector<int > Adj[maxn];//邻接表,每个vector代表每个顶点,push_back是每个顶点能直到达的顶点:
void DFS(int u, int depth) {//要遍历的顶点;
vis[u] = true;
for(int v = 0; v < Adj[u].size( ); v++) {//遍历u顶点下能直接到达的顶点;
if(vis[v] == false) {
DFS(v, depth + 1);
}
}
}
void DFSTrave( ) {
for(int u = 0; u < n; u++) {//遍历图内所有顶点;
if(vis[u] == false) {
DFS(u, 1);
}
}
} 边栏推荐
- JS data transformation -- Transformation of tree structure and tile structure
- Data analysis and mining 2
- Fraud detection cases and Titanic rescued cases
- 华为无线设备配置WPA2-802.1X-AES安全策略
- Performance test - Test Execution
- Date class and time class definitions (operator overload application)
- Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
- “00后”来了!数睿数据迎来新生代「无代码」生力军
- Which securities company is the best and safest to open an account? How to open an account and speculate in stocks
- 【OpenCV 例程300篇】238. OpenCV 中的 Harris 角点检测
猜你喜欢

Kotlin类与继承

Rest style

The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic

【MATLAB】MATLAB画图系列二 1.元胞与数组转化 2.属性元胞 3.删除nan值 4.合并多fig为同一fig 5.合并多fig至同一axes

Overall testing framework for performance testing
![Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022](/img/fd/c7bff1ce199e8b600761d77828c674.png)
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022

Spark Learning Notes (III) -- basic knowledge of spark core

Discussion and legitimacy of the order of entering and leaving the stack

Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection

Data analysis and mining 1
随机推荐
华为无线设备配置WPA2-802.1X-AES安全策略
LeetCode高频题56. 合并区间,将重叠的区间合并为一个区间,包含所有区间
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
The spiral matrix of the force buckle rotates together (you can understand it)
Is it safe for Huatai Securities to open an account? Can it be handled on the mobile phone?
How do novices buy stocks for the first time? Which securities company is the best and safest to open an account
Outlook tutorial, how to create tasks and to DOS in outlook?
DDD based on ABP -- Entity creation and update
Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals
Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
Leetcode · daily question · 1184. distance between bus stops · simulation
Learning rate adjustment strategy in deep learning (1)
Data analysis and mining 1
Fraud detection cases and Titanic rescued cases
Self join usage of SQL
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
佣金哪家券商最低,我要开户,手机上开户安不安全
pip换源
Error when using Fiddler hook: 502 Fiddler - connection failed
“00后”来了!数睿数据迎来新生代「无代码」生力军