網站首頁 編程語言 正文
圖的定義
圖由頂點集V(G)和邊集E(G)組成,記為G=(V,E)。其中E(G)是邊的有限集合,邊是頂點的無序對(無向圖)或有序對(有向圖)。對于有向圖來說,E(G)是有向邊(也稱弧(Arc))的有限集合,弧是頂點的有序對,記為<v,w>,v、w是頂點,v為弧尾(箭頭根部),w為弧頭(箭頭處)。對于無向圖來說,E(G)是邊的有限集合,邊是頂點的無序對,記為(v, w)或者(w, v),并且(v, w)=(w,v)。
圖的相關術語
①頂點(Vertex):圖中的數據元素。
②頂點v的度:與v相關聯的邊的數目;
③頂點v的出度:以v為起點有向邊數;
④頂點v的入度:以v為終點有向邊數。
⑤邊:頂點之間的邏輯關系用邊來表示,邊集可以是空的。
⑥無向邊(Edge):若頂點V1到V2之間的邊沒有方向,則稱這條邊為無向邊。
⑦無向圖(Undirected graphs):圖中任意兩個頂點之間的邊都是無向邊。(A,D)=(D,A)
⑧有向邊:若從頂點V1到V2的邊有方向,則稱這條邊為有向邊,也稱弧(Arc)。用<V1,V2>表示,V1為狐尾(Tail),V2為弧頭(Head)。(V1,V2)≠(V2,V1)。
⑨有向圖(Directed graphs):圖中任意兩個頂點之間的邊都是有向邊。
注意:無向邊用“()”,而有向邊用“< >”表示。
⑩簡單圖:圖中不存在頂點到其自身的邊,且同一條邊不重復出現。
?無向完全圖:無向圖中,任意兩個頂點之間都存在邊。
?有向完全圖:有向圖中,任意兩個頂點之間都存在方向互為相反的兩條弧。
?稀疏圖:有很少條邊。
?稠密圖:有很多條邊。
?權(Weight):與圖的邊或弧相關的數。
?網(Network):帶權的圖。
?連通圖:圖中任意兩個頂點都是連通的。
?極大連通子圖:該子圖是G連通子圖,將G的任何不在該子圖的頂點加入,子圖將不再連通。
?極小連通子圖:該子圖是G的連通子圖,在該子圖中刪除任何一條邊,子圖都將不再連通。
圖的創建(鄰接矩陣)---結構體
typedef struct
{
//用來存放頂點
int vexs[MAX];
//二維數組:用來存放兩點之間的關系
int arcs[MAX][MAX];
//圖的頂點數和邊數
int vexsum, arcsnum;
}AMGraph,*StrAMGraph;
圖的創建(鄰接矩陣)---鄰接矩陣的創建
int locate(AMGraph&G, int n)
{
for (int i = 0; i < G.vexsum; i++)
{
if (G.vexs[i] == n)
{
return i;
}
}
}
//創建鄰接矩陣
void Creat(AMGraph&G)
{
int v1 = 0, v2 = 0, w = 0;
cin >> G.vexsum >> G.arcsnum;
for (int i = 0; i < G.vexsum; i++)
{
cin >> G.vexs[i];
}
for (int i = 0; i < G.vexsum; i++)
{
for (int j = 0; j < G.vexsum; j++)
{
G.arcs[i][j] = 0;
}
}
for (int k = 0; k < G.arcsnum; k++)
{
cin >> v1 >> v2 >> w;
int i = locate(G, v1);
int j = locate(G, v2);
G.arcs[i][j] = w;
}
}
圖的創建(鄰接表)---結構體
typedef struct ArcNode
{
int Adjust;
struct ArcNode *next;
}AcrNode,*StrAcrNode;
typedef struct
{
int data;
StrAcrNode next;
}HeadNode, *StrHeadNode;
typedef struct
{
HeadNode arr[MAX];
int acsrnum, vexsnum;
}ALGraph, *StrALGraph;
圖的創建(鄰接表)---鄰接表的創建
int locate1(ALGraph&G, int n)
{
for (int i = 0; i < G.vexsnum; i++)
{
if (G.arr[i].data == n)
{
return i;
}
}
}
void CreatALGraph(ALGraph&G)
{
int v1 = 0, v2 = 0, w = 0;
cin >> G.vexsnum >> G.acsrnum;
for (int i = 0; i < G.vexsnum; i++)
{
cin >> G.arr[i].data;
G.arr[i].next = NULL;
}
for (int k = 0; k < G.acsrnum; k++)
{
cin >> v1 >> v2;
int i = locate1(G, v1);
int j = locate1(G, v2);
StrAcrNode p1;
p1 = new AcrNode;
p1->next = G.arr[i].next;
}
}
對鄰接矩陣進行深度優先遍歷
//對鄰接矩陣進行深度優先遍歷
void DFS(AMGraph&G, int n)
{
cout << G.vexs[n] << " ";
visit[n] = 1;
for (int i = 0; i < G.vexsum; i++)
{
if (G.arcs[n][i] != 1 && visit[i] != 1)
{
DFS(G, G.arcs[n][i]);
}
}
}
對鄰接矩陣進行廣度優先遍歷
queue<int> qu;
//對鄰接矩陣進行廣度優先遍歷
void BFS(AMGraph&G, int n)
{
cout << G.vexs[n] << " ";
qu.push(n);
while (!qu.empty())
{
int m = qu.front();
qu.pop();
for (int i = 0; i < G.vexsum; i++)
{
if (visit[i] != 1 && G.arcs[m][i] != 1)
{
cout << G.vexs[i] << " ";
visit[i] = 1;
qu.push(i);
}
}
}
}
對鄰接表進行深度優先遍歷?
void DFS1(ALGraph&G, int n)
{
cout << G.arr[n].data << " ";
visit3[n] = 1;
StrAcrNode p1;
p1 = G.arr[n].next;
while (p1)
{
int w = p1->Adjust;
if (visit3[w] != 1)
{
DFS1(G, w);
}
p1 = p1->next;
}
}
queue<int> qu1;
對鄰接表進行廣度優先遍歷?
queue<int> qu1;
void BFS(ALGraph&G, int n)
{
cout << G.arr[n].data << " ";
visit4[n] = 1;
qu1.push(n);
StrAcrNode p1;
p1 = G.arr[n].next;
while (!qu1.empty())
{
qu1.pop();
int w = p1->Adjust;
while (p1)
{
if (visit4[w] != 1)
{
qu1.push(w);
visit4[w] = 1;
}
p1 = p1->next;
}
}
}
整體代碼
#include<iostream>
#include<queue>
using namespace std;
const int MAxInt = 10;
int visit[MAxInt];
typedef struct
{
int vexs[MAxInt];
int arcs[MAxInt][MAxInt];
int arcnum, vexsnum;
}AMGraph;
int locate(AMGraph&G, int n)
{
for (int i = 0; i < G.vexsnum; i++)
{
if (G.vexs[i] == n)
{
return i;
}
}
}
void Creat(AMGraph&G)
{
int v1 = 0, v2 = 0, w = 0;
cin >> G.vexsnum >> G.arcnum;
for (int i = 0; i < G.vexsnum; i++)
{
cin >> G.vexs[i];
}
for (int i = 0; i < G.vexsnum; i++)
{
for (int j = 0; j < G.vexsnum; j++)
{
G.arcs[i][j] = MAxInt;
}
}
for (int k = 0; k < G.arcnum; k++)
{
cin >> v1 >> v2 >> w;
int i = locate(G, v1);
int j = locate(G, v2);
G.arcs[i][j] = w;
G.arcs[j][i] = w;
}
}
queue<int> qu;
void BFS(AMGraph G, int v)
{
cout << G.vexs[v];
qu.push(v);
visit[v] = 1;
while (!qu.empty())
{
int w = qu.front();
qu.pop();
for (int i = 0; i < G.vexsnum; i++)
{
if (visit[i] != 1 && G.arcs[w][i] != MAxInt)
{
cout << G.vexs[i] << " ";
visit[i] = 1;
qu.push(i);
}
}
}
}
int main()
{
AMGraph G;
Creat(G);
cout << "對圖進行廣度優先遍歷的結果為" << endl;
BFS(G, 1);
return 0;
}
注意 :這里的代碼是創建一個鄰接矩陣來對圖進行廣度優先遍歷,對圖進行深度優先遍歷以及臨界表實現對圖進行廣度優先遍歷,對圖進行深度優先遍歷大家都可以通過上面的代碼塊進行自由組合實現,這里就不進行一一實現。
結果展示
原文鏈接:https://blog.csdn.net/weixin_64067830/article/details/125965460
相關推薦
- 2023-03-25 Redis實現UV統計的示例代碼_Redis
- 2022-10-31 Kotlin集合List?Set?Map使用介紹詳解_Android
- 2022-07-06 python數據分析之DateFrame數據排序和排名方式_python
- 2022-07-13 數據結構之冒泡排序
- 2023-01-19 React?classnames原理及測試用例_React
- 2023-06-17 Python結合Sprak實現計算曲線與X軸上方的面積_python
- 2022-05-23 Android實現類似iOS分欄控制器_Android
- 2023-07-31 elementui表單密碼強度自定義驗證
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支