轉載請註明出處:勿在浮沙築高臺http://blog.csdn.net/luoshixian099/article/details/51908175css
關於圖的幾個概念定義:node
- 連通圖:在無向圖中,若任意兩個頂點vi」 role=」presentation」>vivi都有路徑相通,則稱該無向圖爲連通圖。
- 強連通圖:在有向圖中,若任意兩個頂點vi」 role=」presentation」>vivi都有路徑相通,則稱該有向圖爲強連通圖。
- 連通網:在連通圖中,若圖的邊具備必定的意義,每一條邊都對應着一個數,稱爲權;權表明着鏈接連個頂點的代價,稱這種連通圖叫作連通網。
- 生成樹:一個連通圖的生成樹是指一個連通子圖,它含有圖中所有n個頂點,但只有足以構成一棵樹的n-1條邊。一顆有n個頂點的生成樹有且僅有n-1條邊,若是生成樹中再添加一條邊,則一定成環。
- 最小生成樹:在連通網的全部生成樹中,全部邊的代價和最小的生成樹,稱爲最小生成樹。
下面介紹兩種求最小生成樹算法ios
1.Kruskal算法
此算法能夠稱爲「加邊法」,初始最小生成樹邊數爲0,每迭代一次就選擇一條知足條件的最小代價邊,加入到最小生成樹的邊集合裏。
1. 把圖中的全部邊按代價從小到大排序;
2. 把圖中的n個頂點當作獨立的n棵樹組成的森林;
3. 按權值從小到大選擇邊,所選的邊鏈接的兩個頂點ui,vi」 role=」presentation」>ui,viui,vi,應屬於兩顆不一樣的樹,則成爲最小生成樹的一條邊,並將這兩顆樹合併做爲一顆樹。
4. 重複(3),直到全部頂點都在一顆樹內或者有n-1條邊爲止。web
2.Prim算法
此算法能夠稱爲「加點法」,每次迭代選擇代價最小的邊對應的點,加入到最小生成樹中。算法從某一個頂點s開始,逐漸長大覆蓋整個連通網的全部頂點。算法
- 圖的全部頂點集合爲V」 role=」presentation」>VV;
- 在兩個集合u,v」 role=」presentation」>u,vu,v併入到集合u中。
- 重複上述步驟,直到最小生成樹有n-1條邊或者n個頂點爲止。
因爲不斷向集合u中加點,因此最小代價邊必須同步更新;須要創建一個輔助數組closedge,用來維護集合v中每一個頂點與集合u中最小代價邊信息,:數組
struct
{
char vertexData
UINT lowestcost
}closedge[vexCounts]
3.完整代碼
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define INFINITE 0xFFFFFFFF
#define VertexData unsigned int
#define UINT unsigned int
#define vexCounts 6
char vextex[] = {
'A',
'B',
'C',
'D',
'E',
'F' };
struct node
{
VertexData data;
unsigned int lowestcost;
}closedge[vexCounts];
typedef struct
{
VertexData u;
VertexData v;
unsigned int cost;
}Arc;
void AdjMatrix(
unsigned int adjMat[][vexCounts])
{
for (
int i =
0; i < vexCounts; i++)
for (
int j =
0; j < vexCounts; j++)
{
adjMat[i][j] = INFINITE;
}
adjMat[
0][
1] =
6; adjMat[
0][
2] =
1; adjMat[
0][
3] =
5;
adjMat[
1][
0] =
6; adjMat[
1][
2] =
5; adjMat[
1][
4] =
3;
adjMat[
2][
0] =
1; adjMat[
2][
1] =
5; adjMat[
2][
3] =
5; adjMat[
2][
4] =
6; adjMat[
2][
5] =
4;
adjMat[
3][
0] =
5; adjMat[
3][
2] =
5; adjMat[
3][
5] =
2;
adjMat[
4][
1] =
3; adjMat[
4][
2] =
6; adjMat[
4][
5] =
6;
adjMat[
5][
2] =
4; adjMat[
5][
3] =
2; adjMat[
5][
4] =
6;
}
int Minmum(
struct node * closedge)
{
unsigned int min = INFINITE;
int index = -
1;
for (
int i =
0; i < vexCounts;i++)
{
if (closedge[i].lowestcost < min && closedge[i].lowestcost !=
0)
{
min = closedge[i].lowestcost;
index = i;
}
}
return index;
}
void MiniSpanTree_Prim(
unsigned int adjMat[][vexCounts], VertexData s)
{
for (
int i =
0; i < vexCounts;i++)
{
closedge[i].lowestcost = INFINITE;
}
closedge[s].data = s;
closedge[s].lowestcost =
0;
for (
int i =
0; i < vexCounts;i++)
{
if (i != s)
{
closedge[i].data = s;
closedge[i].lowestcost = adjMat[s][i];
}
}
for (
int e =
1; e <= vexCounts -
1; e++)
{
int k = Minmum(closedge);
cout << vextex[closedge[k].data] <<
"--" << vextex[k] << endl;
closedge[k].lowestcost =
0;
for (
int i =
0; i < vexCounts;i++)
{
if ( adjMat[k][i] < closedge[i].lowestcost)
{
closedge[i].data = k;
closedge[i].lowestcost = adjMat[k][i];
}
}
}
}
void ReadArc(
unsigned int adjMat[][vexCounts],
vector<Arc> &vertexArc)
{
Arc * temp = NULL;
for (
unsigned int i =
0; i < vexCounts;i++)
{
for (
unsigned int j =
0; j < i; j++)
{
if (adjMat[i][j]!=INFINITE)
{
temp =
new Arc;
temp->u = i;
temp->v = j;
temp->cost = adjMat[i][j];
vertexArc.push_back(*temp);
}
}
}
}
bool compare(Arc A, Arc B)
{
return A.cost < B.cost ?
true :
false;
}
bool FindTree(VertexData u, VertexData v,
vector<vector<VertexData> > &Tree)
{
unsigned int index_u = INFINITE;
unsigned int index_v = INFINITE;
for (
unsigned int i =
0; i < Tree.size();i++)
{
if (find(Tree[i].begin(), Tree[i].end(), u) != Tree[i].end())
index_u = i;
if (find(Tree[i].begin(), Tree[i].end(), v) != Tree[i].end())
index_v = i;
}
if (index_u != index_v)
{
for (
unsigned int i =
0; i < Tree[index_v].size();i++)
{
Tree[index_u].push_back(Tree[index_v][i]);
}
Tree[index_v].clear();
return true;
}
return false;
}
void MiniSpanTree_Kruskal(
unsigned int adjMat[][vexCounts])
{
vector<Arc> vertexArc;
ReadArc(adjMat, vertexArc);
sort(vertexArc.begin(), vertexArc.end(), compare);
vector<vector<VertexData> > Tree(vexCounts);
for (
unsigned int i =
0; i < vexCounts; i++)
{
Tree[i].push_back(i);
}
for (
unsigned int i =
0; i < vertexArc.size(); i++)
{
VertexData u = vertexArc[i].u;
VertexData v = vertexArc[i].v;
if (FindTree(u, v, Tree))
{
cout << vextex[u] <<
"---" << vextex[v] << endl;
}
}
}
int main()
{
unsigned int adjMat[vexCounts][vexCounts] = {
0 };
AdjMatrix(adjMat);
cout <<
"Prim :" << endl;
MiniSpanTree_Prim(adjMat,
0);
cout <<
"-------------" << endl <<
"Kruskal:" << endl;
MiniSpanTree_Kruskal(adjMat);
return 0;
}
Reference:
數據結構–耿國華
算法導論–第三版markdown
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
</div>