前言
上篇文章介紹了圖的相關基本概念,本篇我們將繼續探索圖的應用。
基本概念
連通圖的生成樹:所謂一個連通圖的生成樹是一個極小的連通子圖,它含有圖中全部的n個頂點,但只足以構成一棵樹的n-1條邊。
條件如下
:
- 圖是連通圖。
- 圖中包含了n個頂點。
- 圖中邊的數量等于n-1條邊。
上圖中圖1中有8個頂點和9條邊,圖2中有8個頂點7條邊,均滿足連通的條件,但是圖1不滿足n-1條邊的條件,則不是連通圖的生成樹。圖2則全滿足生成樹的所有條件。
面試題
上面連通圖的生成樹問題中,只需滿足上方三個條件即可,但如果給每條邊添加上權值,找出權值和最小的生成樹那就相對更復雜了。
下面是阿里的一道數據結構與算法題,假設目前N個頂點,每個頂點連接的路徑不一樣,請你設計一個算法,快速找出能覆蓋所有頂點的最小成本
的路徑。(類似于一個村莊的網絡布局,在成本最小的情況下全村通網)
那么就引出了最小生成樹的概念!
最小生成樹:把構成連通網的最小代價的生成樹稱為最小生成樹。
普里姆[Prim]算法
思路:
- 定義兩個數組:adjvex數組用來保存相關頂點的下標,lowcost保存頂點之間的權值。
- 初始化2個數組,從v0開始尋找最小生成樹,默認v0時最小生成樹上第一個頂點。即lowcost數組默認為鄰接矩陣第一行數據。
- 循環lowcost數組,根據權值大小,找到最小的權值對應的下標k,即頂點k。
- 更新lowcost數組。
-
比較頂點k相連的頂點權值與lowcost數組中相應位置權值大小,取較小值。已經存入最小生成樹的則不用管。
鄰接矩陣
執行過程:
-
比較lowcost數組中權重值,由于默認V0已經加入最小生成樹,則lowcost[0] = 0;10、11 分別為V0與V1、V5鏈接的權值,10更小,則此時k = 1。
第一次執行 -
lowcost[1] = 0,表示V1加入最小樹,將與V1相連的頂點對應的權值放進lowcost數組中,接著比較數組中權值大小,此時為11,則k = 5,將與V5連接的頂點權值與lowcost數組相應位置比較,小了的就替換,大了就不變,發現有V4、V6與V5相連,V5-V6權值17大于lowcost[6] = 16,則保持16不變,V5-V4權值26小于lowcost[4]最大值,則替換lowcost[4] = 26.
第二次執行 -
接著找出lowcost數組中最小權值12,此時k = 8,lowcost[8] = 0,表示V8添加到最小樹中,與V8相連的頂點有V1、V2、V3,V1已經添加進最小樹,則只需關注V2、V3,比較相應的權值。
第三次執行 -
依照如上方式依次執行得到如下最終結果:
最終結果
最終所有頂點都加入到最小樹中。最小路徑則為下圖中所有黑色粗線連線。
最小生成樹
代碼實現
#define MAXEDGE 20
#define MAXVEX 20
#define INFINITYC 65535
typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */
typedef struct
{
int arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
/*9.1 創建鄰接矩陣*/
void CreateMGraph(MGraph *G)/* 構件圖 */
{
int i, j;
/* printf("請輸入邊數和頂點數:"); */
G->numEdges=15;
G->numVertexes=9;
for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */
{
for ( j = 0; j < G->numVertexes; j++)
{
if (i==j)
G->arc[i][j]=0;
else
G->arc[i][j] = G->arc[j][i] = INFINITYC;
}
}
G->arc[0][1]=10;
G->arc[0][5]=11;
G->arc[1][2]=18;
G->arc[1][8]=12;
G->arc[1][6]=16;
G->arc[2][8]=8;
G->arc[2][3]=22;
G->arc[3][8]=21;
G->arc[3][6]=24;
G->arc[3][7]=16;
G->arc[3][4]=20;
G->arc[4][7]=7;
G->arc[4][5]=26;
G->arc[5][6]=17;
G->arc[6][7]=19;
for(i = 0; i < G->numVertexes; i++)
{
for(j = i; j < G->numVertexes; j++)
{
G->arc[j][i] =G->arc[i][j];
}
}
}
/* Prim算法生成最小生成樹 */
void MiniSpanTree_Prim(MGraph G)
{
int min, i, j, k;
int sum = 0;
/* 保存相關頂點下標 */
int adjvex[MAXVEX];
/* 保存相關頂點間邊的權值 */
int lowcost[MAXVEX];
/* 初始化第一個權值為0,即v0加入生成樹 */
/* lowcost的值為0,在這里就是此下標的頂點已經加入生成樹 */
lowcost[0] = 0;
/* 初始化第一個頂點下標為0 */
adjvex[0] = 0;
//1. 初始化
for(i = 1; i < G.numVertexes; i++) /* 循環除下標為0外的全部頂點 */
{
lowcost[i] = G.arc[0][i]; /* 將v0頂點與之有邊的權值存入數組 */
adjvex[i] = 0; /* 初始化都為v0的下標 */
}
//2. 循環除了下標為0以外的全部頂點, 找到lowcost數組中最小的頂點k
for(i = 1; i < G.numVertexes; i++)
{
/* 初始化最小權值為∞, */
/* 通常設置為不可能的大數字如32767、65535等 */
min = INFINITYC;
j = 1;k = 0;
while(j < G.numVertexes) /* 循環全部頂點 */
{
/* 如果權值不為0且權值小于min */
if(lowcost[j]!=0 && lowcost[j] < min)
{
/* 則讓當前權值成為最小值,更新min */
min = lowcost[j];
/* 將當前最小值的下標存入k */
k = j;
}
j++;
}
/* 打印當前頂點邊中權值最小的邊 */
printf("(V%d, V%d)=%d\n", adjvex[k], k ,G.arc[adjvex[k]][k]);
sum+=G.arc[adjvex[k]][k];
/* 3.將當前頂點的權值設置為0,表示此頂點已經完成任務 */
lowcost[k] = 0;
/* 循環所有頂點,找到與頂點k 相連接的頂點
1. 與頂點k 之間連接;
2. 該結點沒有被加入到生成樹;
3. 頂點k 與 頂點j 之間的權值 < 頂點j 與其他頂點的權值,則更新lowcost 數組;
*/
for(j = 1; j < G.numVertexes; j++)
{
/* 如果下標為k頂點各邊權值小于此前這些頂點未被加入生成樹權值 */
if(lowcost[j]!=0 && G.arc[k][j] < lowcost[j])
{
/* 將較小的權值存入lowcost相應位置 */
lowcost[j] = G.arc[k][j];
/* 將下標為k的頂點存入adjvex */
adjvex[j] = k;
}
}
}
printf("sum = %d\n",sum);
}
int main(void)
{
printf("Hello,最小生成樹_Prim算法\n");
MGraph G;
CreateMGraph(&G);
MiniSpanTree_Prim(G);
return 0;
}
克魯斯卡爾[Kruskal]算法
思路:
- 將鄰接矩陣轉化為邊表數組。
- 對邊表數組根據權值從小到大順序排序。
- 遍歷所有的邊,通過parent數組找到邊的連接信息,避免閉環問題。
- 如果不存在閉環問題,則加入到最小生成樹中,并修改parent數組。
執行過程:
-
begin = 4, end = 7, weight = 7 ,parent[4] = 0, n = 4, parent[7] = 0, m = 7, n != m ,所以此時parent[4] = 7。
-
begin = 2, end = 8, weight = 8,n = 2, m = 8, 2 != 8,所以parent[2] = 8。
-
begin = 0, end = 1, weight = 10, n = 0, m = 1, 0 != 1 , 所以parent[0] = 1.
-
begin = 0, end = 5, weight = 11, n = 1, m = 5, 因為parent[0] = 1, parent[1] = 0, 所以n = 1, parent[5] = 0, 所以返回5,即m = 5, n != m, parent[1] = 5。
-
begin = 1, end = 8, weight = 12, n = 5, m = 8, 因為parent[1] = 5,parent[5] = 0, 返回5,parent[8] = 0, 直接返回8,n != m,parent[5] = 8。
........
........
-
begin = 5, end = 6, weight = 17, n = 6, m = 6, 因為parent[5] = 8, parent[8] = 6, 所以n = 6, parent[6] = 0, m = 6,n = m, 所以不用修改parent數組,因為V5、V6會形成環路,所以不能將它們加入最小樹。
.....
.....
.....
最終parent數組為下面數組:
此時,
parent[0] = 1,V0,V1加入到最小樹。
parent[1] = 5,V1,V5加入到最小樹。
parent[2] = 8,V2,V8加入到最小樹。
parent[3] = 7,V3,V7加入到最小樹。
parent[4] = 7,V4,V7加入到最小樹。
parent[5] = 8,V5,V8加入到最小樹。
parent[6] = 7,V6,V7加入到最小樹。
parent[8] = 6,V8,V6加入到最小樹。
所以所有頂點都加入到最小樹中,所以的連線如下圖所示:
最小生成樹
代碼實現
#define MAXEDGE 20
#define MAXVEX 20
#define INFINITYC 65535
typedef int Status;
typedef struct
{
int arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
/* 對邊集數組Edge結構的定義 */
typedef struct
{
int begin;
int end;
int weight;
}Edge ;
/*9.1 創建鄰接矩陣*/
void CreateMGraph(MGraph *G)
{
int i, j;
/* printf("請輸入邊數和頂點數:"); */
G->numEdges=15;
G->numVertexes=9;
for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */
{
for ( j = 0; j < G->numVertexes; j++)
{
if (i==j)
G->arc[i][j]=0;
else
G->arc[i][j] = G->arc[j][i] = INFINITYC;
}
}
G->arc[0][1]=10;
G->arc[0][5]=11;
G->arc[1][2]=18;
G->arc[1][8]=12;
G->arc[1][6]=16;
G->arc[2][8]=8;
G->arc[2][3]=22;
G->arc[3][8]=21;
G->arc[3][6]=24;
G->arc[3][7]=16;
G->arc[3][4]=20;
G->arc[4][7]=7;
G->arc[4][5]=26;
G->arc[5][6]=17;
G->arc[6][7]=19;
for(i = 0; i < G->numVertexes; i++)
{
for(j = i; j < G->numVertexes; j++)
{
G->arc[j][i] =G->arc[i][j];
}
}
}
/* 交換權值以及頭和尾 */
void Swapn(Edge *edges,int i, int j)
{
int tempValue;
//交換edges[i].begin 和 edges[j].begin 的值
tempValue = edges[i].begin;
edges[i].begin = edges[j].begin;
edges[j].begin = tempValue;
//交換edges[i].end 和 edges[j].end 的值
tempValue = edges[i].end;
edges[i].end = edges[j].end;
edges[j].end = tempValue;
//交換edges[i].weight 和 edges[j].weight 的值
tempValue = edges[i].weight;
edges[i].weight = edges[j].weight;
edges[j].weight = tempValue;
}
/* 對權值進行排序 */
void sort(Edge edges[],MGraph *G)
{
//對權值進行排序(從小到大)
int i, j;
for ( i = 0; i < G->numEdges; i++)
{
for ( j = i + 1; j < G->numEdges; j++)
{
if (edges[i].weight > edges[j].weight)
{
Swapn(edges, i, j);
}
}
}
printf("邊集數組根據權值排序之后的為:\n");
for (i = 0; i < G->numEdges; i++)
{
printf("(%d, %d) %d\n", edges[i].begin, edges[i].end, edges[i].weight);
}
}
/* 查找連線頂點的尾部下標 */
//根據頂點f以及parent 數組,可以找到當前頂點的尾部下標; 幫助我們判斷2點之間是否存在閉環問題;
int Find(int *parent, int f)
{
while ( parent[f] > 0)
{
f = parent[f];
}
return f;
}
/* 生成最小生成樹 */
void MiniSpanTree_Kruskal(MGraph G)
{
int i, j, n, m;
int sum = 0;
int k = 0;
/* 定義一數組用來判斷邊與邊是否形成環路
用來記錄頂點間的連接關系. 通過它來防止最小生成樹產生閉環;*/
int parent[MAXVEX];
/* 定義邊集數組,edge的結構為begin,end,weight,均為整型 */
Edge edges[MAXEDGE];
/*1. 用來構建邊集數組*/
for ( i = 0; i < G.numVertexes-1; i++)
{
for (j = i + 1; j < G.numVertexes; j++)
{
//如果當前路徑權值 != ∞
if (G.arc[i][j]<INFINITYC)
{
//將路徑對應的begin,end,weight 存儲到edges 邊集數組中.
edges[k].begin = i;
edges[k].end = j;
edges[k].weight = G.arc[i][j];
//邊集數組計算器k++;
k++;
}
}
}
//2. 對邊集數組排序
sort(edges, &G);
//3.初始化parent 數組為0. 9個頂點;
// for (i = 0; i < G.numVertexes; i++)
for (i = 0; i < MAXVEX; i++)
parent[i] = 0;
//4. 計算最小生成樹
printf("打印最小生成樹:\n");
/* 循環每一條邊 G.numEdges 有15條邊*/
for (i = 0; i < G.numEdges; i++)
{
//獲取begin,end 在parent 數組中的信息;
//如果n = m ,將begin 和 end 連接,就會產生閉合的環.
n = Find(parent,edges[i].begin);
m = Find(parent,edges[i].end);
//printf("n = %d,m = %d\n",n,m);
/* 假如n與m不等,說明此邊沒有與現有的生成樹形成環路 */
if (n != m)
{
/* 將此邊的結尾頂點放入下標為起點的parent中。 */
/* 表示此頂點已經在生成樹集合中 */
parent[n] = m;
/*打印最小生成樹路徑*/
printf("(%d, %d) %d\n", edges[i].begin, edges[i].end, edges[i].weight);
sum += edges[i].weight;
}
}
printf("sum = %d\n",sum);
}
int main(int argc, const char * argv[]) {
printf("Hello,最小生成樹_Kruskal算法\n");
MGraph G;
CreateMGraph(&G);
MiniSpanTree_Kruskal(G);
return 0;
}