1. 使用C++STL实现邻接链表构建图
下面这种方法可能更适合于打比赛之类的,方便之处在于可以很快的创建图,其中vector G[maxn]用于模拟邻接链表,里面存储的是每个from顶点关联的边在vector edges中的下标。
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 100;//最大顶点数
const int maxm = 100;//最大边数
const int inf = 0x7fff;
struct Edge {
int from;//起始点
int to; //终止点
int dist;//权值 距离
Edge(int from, int to, int dist) :from(from), to(to), dist(dist) {}
}; //边结构体
vector<Edge>edges; //存储边的信息
vector<int> G[maxn]; //存储图的信息
int vexs[maxn];//存储顶点值
int n;//顶点数
int m; //边数
/*辅助数组 记录具最小代价的边*/
struct closedge
{
int adjvex;
int lowcost;
};
void AddEdge(int from, int to, int dist)
{
edges.push_back(Edge(from, to, dist));
int s = edges.size() - 1; //边的下标
//填写邻接链表信息
G[from].push_back(s);//只需要将边的信息存储在G[from]中即可
}
/*
求出辅助数组中代价最小的边
返回最小代价边在V-U集合中的顶点
V = 所有顶点值的集合
U = 顶点集V的非空子集 也就是在寻找MST时加入的顶点
*/
int minimum(closedge cls[])
{
int min_ = inf;
int flag = 0;
for (int i = 0; i < n; i++)
{
if (cls[i].lowcost < min_ && cls[i].lowcost != 0)//必须是V-U中的点
{
min_ = cls[i].lowcost;
flag = i;
}
}
return flag;
}
/*获取from-to关联的边的权值*/
int get_w(int u, int v)
{
vector<int>::iterator iter;
for (iter = G[u].begin(); iter != G[u].end(); iter++)
{
if (edges[*iter].to == v)
return edges[*iter].dist;
}
return inf;
}
/*定位顶点的索引*/
int LocateVertex(int v)
{
for(int i = 0; i < n; i++)
{
if(vexs[i] == v)
return i;
}
return -1;
}
/*最小生成树Prim算法*/
void MinSpanTree_Prim(int u0)
{
cout << "加入顶点 " << u0 << endl;
closedge cls[maxn];
int l= LocateVertex(u0);
cls[l].lowcost = 0;
for (int i = 0; i < n; i++)
{
if (i != l)
{
cls[i].adjvex = u0;
cls[i].lowcost = get_w(u0, vexs[i]);
}
}
for (int i = 1; i < n; i++) {
int k = minimum(cls);
cout << "加入顶点 " << vexs[k] << ", " << "最小代价边(" << cls[k].adjvex << "," << vexs[k] << ") = "
<< cls[k].lowcost << endl;
cls[k].adjvex = -1;
cls[k].lowcost = 0;//加入U集合
// n为顶点数目
for (int j = 0; j < n; j++) {
if (get_w(vexs[k], vexs[j]) < cls[j].lowcost) {
cls[j].lowcost = get_w(vexs[k], vexs[j]);
cls[j].adjvex = vexs[k];
}
}
}
}
/*初始化顶点数组(i + 1)和向量*/
void init()
{
for (int i = 0; i < maxn; i++)
{
G[i].clear();
vexs[i] = i + 1;
}
edges.clear();
}
int main()
{
cout << "请输入顶点数:";
cin >> n;
cout << "请输入边数:";
cin >> m;
cout << "请输入边依附的顶点及权值信息:" << endl;
init();
for (int i = 0; i < m; i++)
{
int from, to, dist;
cin >> from >> to >> dist;
AddEdge(from, to, dist);
AddEdge(to, from, dist);//无向图
}
cout << "请输入开始顶点:";
int u0;
cin >> u0;
MinSpanTree_Prim(u0);
return 0;
}
输出结果如下
请输入顶点数:6
请输入边数:10
请输入边依附的顶点及权值信息:
1 2 6
1 3 1
1 4 5
2 3 5
2 5 3
3 4 5
3 5 6
5 6 6
3 6 4
4 6 2
请输入开始顶点:2
加入顶点 2
加入顶点 5, 最小代价边(2,5) = 3
加入顶点 3, 最小代价边(2,3) = 5
加入顶点 1, 最小代价边(3,1) = 1
加入顶点 6, 最小代价边(3,6) = 4
加入顶点 4, 最小代价边(6,4) = 2
2. 自定义结构体实现邻接矩阵构建图
#define MAX_VERTEX_NUM 20
#define INF 65535
#include<iostream>
#include<cstring>
using namespace std;
typedef int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef enum { DG, DN, UDG, UDN } GrapgKind;
typedef int VertexType;
typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];//顶点数组
AdjMatrix arcs; //邻接矩阵 用于记录顶点之间的弧 信息
int vexnum, arcnum;
int GraphKind;
} MGraph;
/*辅助数组 记录具最小代价的边*/
struct closedge
{
VertexType adjvex;
int lowcost;
} closedge[MAX_VERTEX_NUM]; //结构体数组 定义了一个变量
/*定位顶点 顶点在数组存储的下标*/
int LocateVertex(MGraph& G, VertexType v)
{
for (int i = 0; i < G.vexnum; i++)
{
if (G.vexs[i] == v)
return i;
}
return -1;
}
/*创建图*/
void CreateGraph(MGraph& G)
{
cout << "请输入顶点数:";
cin >> G.vexnum;
cout << "请输入边数:";
cin >> G.arcnum;
for (int i = 0; i < G.vexnum; i++)
{
G.vexs[i] = i + 1;//每个顶点用数字表示 1 2 3 4 ....
}
//邻接矩阵初始化为无穷大
for (int i = 0; i < G.vexnum; i++)
{
for (int j = 0; j < G.vexnum; j++)
{
G.arcs[i][j] = INF;
}
}
VertexType u, v;
int w;
cout <<"请输入边依附的顶点及权值信息:"<<endl;
for (int k = 0; k < G.arcnum; k++)
{
cin >> u >> v >> w;
int i = LocateVertex(G, u);
int j = LocateVertex(G, v);
//创建无向图
G.arcs[i][j] = w;
G.arcs[j][i] = w;
}
}
/*求出辅助数组中代价最小的边
返回最小代价边在V-U集合中的顶点
V = 所有顶点值的集合
U = 顶点集V的非空子集 也就是在寻找MST时加入的顶点
*/
int minimum(MGraph& G)
{
int min_ = INF;
int flag = 0;
for (int i = 0; i < G.vexnum; i++)
{
if (closedge[i].lowcost < min_ && closedge[i].lowcost != 0)//必须是V-U中的点
{
min_ = closedge[i].lowcost;
flag = i;
}
}
return flag;
}
/*
Prim算法求最小生成树
u-初始顶点 U = {u} 初始顶点加入U
1. 先定位第一个顶点在数组中的下标k
2. 初始化 closedge 数组—邻接矩阵中顶点u与其他顶点的距离 closedge[k] = 0;
3. 循环,次数为 顶点数-1 寻找MST的边及顶点
1. 求出最小代价边(u,v)依附在V-U中依附的顶点
2. 将该顶点加入集合U
3. 更新closedge数组中的最小代价边和依附的V-U集合中的顶点
*/
void MinSpanTree_Prim(MGraph& G, VertexType u)
{
cout << "加入顶点 "<< u <<endl;
//1. 定位顶点
int k = LocateVertex(G, u);
//2. 初始化closedge数组
//memset(closedge,{-1, INF},sizeof(closedge));
closedge[k].lowcost = 0;
for (int i = 0; i < G.vexnum; i++)
{
if (k != i)
{
closedge[i].lowcost = G.arcs[k][i];
closedge[i].adjvex = u;//邻接点是初始顶点
}
}
//3. 循环寻找最小生成树的边
for (int i = 1; i < G.vexnum; i++)
{
//求出代价最小的边依附的另一个顶点
int j = minimum(G);
cout << "加入顶点 "<< G.vexs[j] <<", "<< "最小代价边(" << closedge[j].adjvex << "," << G.vexs[j] << ") = "
<< closedge[j].lowcost << endl;
//将该顶点加入集合U
closedge[j].lowcost = 0;
closedge[j].adjvex = -1;
// 更新closedge数组中的最小代价边和依附的U集合中的顶点
for (int m = 0; m < G.vexnum; m++)
{
if (G.arcs[j][m] < closedge[m].lowcost)
{
closedge[m].adjvex = G.vexs[j];
closedge[m].lowcost = G.arcs[j][m];
}
}
}
}
int main()
{
MGraph G;
CreateGraph(G);
cout << "请输入开始顶点:";
int u0;
cin >> u0;
MinSpanTree_Prim(G, u0);
return 0;
}
输出结果如下
请输入顶点数:6
请输入边数:10
请输入边依附的顶点及权值信息:
1 2 6
1 3 1
1 4 5
2 3 5
2 5 3
3 4 5
3 5 6
5 6 6
3 6 4
4 6 2
请输入开始顶点:1
加入顶点 1
加入顶点 3, 最小代价边(1,3) = 1
加入顶点 6, 最小代价边(3,6) = 4
加入顶点 4, 最小代价边(6,4) = 2
加入顶点 2, 最小代价边(3,2) = 5
加入顶点 5, 最小代价边(2,5) = 3
3. 需要注意的几点
在定义结构体时typedef struct
的用法
-
typedef struct { }Mystruct
-
上面这种方法在C 和 C++中都可以定义一个结构体类型Mystruct;
-
typedef struct Struct { } Mystruct
-
上面这种方法在C和C++中定义了一个结构体类型Struct并起别名Mystruct;
-
struct Struct { };
-
上面这种方法在C++中定义了一个结构体类型Struct;
-
struct { } StructVar;
-
上面这种方法定义了一个结构体变量StructVar,编译器会为它分配内存,该结构体是匿名结构体;
-
struct Struct { } StructArr[10];
-
上面这种方法定义了一个Struct类型的结构体数组(变量)StructArr;