c++实现prim算法(最小生成树)

解读代码:

1.
从图中任选任选一个顶点作为初始点开始寻找最小生成树,定义的lowcost数组用来存放顶点间的权值,初始化为初始顶点与其他顶点的权值,之后再将初始顶点在lowcost数组中对应的值置为0,代表此顶点已经纳入最小生成树中,此后每次lowcost数组中的值被置为0就代表该顶点已经纳入最小生成树

2.
遍历找到lowcost数组中非0的最小值,输出该顶点再纳入最小生成树(在lowcost数组中置为0)。

3.
将刚刚纳入最小生成树的顶点与其他顶点的权值lowcost数组的非0值比较,若前者有数值比后者小,就将小的数值赋给lowcost,此举是为了保证当前树中的顶点与其他顶点的权值最小,然后adjvex数组保存的是代价最小对应的顶点的下标,例如下图,假如以顶点v0为初始点,此时最小生成树(因为这时最小生成树就只有v0一个顶点,所以也也可以看成是v0到这三个顶点的权值)到v2,v8,v6的权值应该为无穷值,但是当v1纳入最小生成树之后,最小生成树到这三个顶点的权值就可以分别变为18,12,16(也可以看成是v1到这三个顶点的权值),所以此时adjvex数组下标为2,6,8的值就应该变为1,之后再用比较完的lowcost数组重复步骤2,直到所有顶点都纳入最小生成树为止。
c++实现prim算法(最小生成树)_第1张图片

代码:

#include
using namespace std;

const int MAX = 10;
const int _INFINITY = 65535;  //初始化最小值为一个不可能的数值
class Graph
{
private:
	int vertex_num; //顶点数
	int edge_num; //边数
	int weight; //权值
	char vertex[MAX]; //顶点数组
	int edge[MAX][MAX]; //邻接矩阵
	int locate(char ch); //定位

public:
	Graph(int v_n, int e_n); //构造函数
	void print_graph(); //打印
	void prim(char ch); //prim算法
	char get_leter(); //获取字母
};

//定位(找到字母的下标)
int Graph::locate(char ch)
{
	int index;
	for (index = 0; index < this->vertex_num; index++)
	{
		if (this->vertex[index] == ch)
		{
			break;
		}
	}
	return index;
}

//构造函数
Graph::Graph(int v_n, int e_n) : vertex_num(v_n), edge_num(e_n)
{
	int i, j, k;
	cout << "现在请输入这" << this->vertex_num << "个顶点:" << endl;
	for (i = 0; i < this->vertex_num; i++)
	{
		cin >> this->vertex[i];
	}

	//初始化邻接矩阵
	for (i = 0; i < this->vertex_num; i++)
	{
		for (j = 0; j < this->vertex_num; j++)
		{
			if (i == j)
			{
				this->edge[i][j] = 0;
			}
			else
			{
				this->edge[i][j] = _INFINITY;
			}
			
		}
	}

	cout << "请依次输入边相邻的两个顶点及边的权值:" << endl;
	for (k = 0; k < this->edge_num; k++)
	{
		char first, second;
		cin >> first >> second >> this->weight;
		i = this->locate(first);
		j = this->locate(second);

		edge[i][j] = this->weight;
		edge[j][i] = this->weight;
	}
}

//打印邻接矩阵
void Graph::print_graph()
{
	cout << "邻接矩阵为:" << endl;
	for (int i = 0; i < this->vertex_num; i++)
	{
		for (int j = 0; j < this->vertex_num; j++)
		{
			cout << this->edge[i][j] << "\t";
		}
		cout << endl;
	}
}

//prim算法
void Graph::prim(char ch)
{
	int v = this->locate(ch);  //找到字母的下标
	int i, j, k, min;
	int *adjvex = new int[this->vertex_num]; //保存相关顶点下标
	int *lowcost = new int[this->vertex_num]; //保存相关顶点间的权值
	for (i = 0; i < this->vertex_num; i++)
	{
		lowcost[i] = this->edge[v][i];
		adjvex[i] = v;
	}
	lowcost[v] = 0; //凡是lowcost数组中的值被设置为0,就是表示此下标的顶点被纳入最小生成树
	for (i = 0; i < this->vertex_num - 1; i++)
	{
		min = _INFINITY;  //初始化最小权值
		j = 0;
		k = 0;
		while (j < this->vertex_num)
		{
			if (lowcost[j] != 0 && lowcost[j] < min) 
			{
				min = lowcost[j];//如果权值不为0且权值小于min,则让当前权值成为最小值
				k = j;  //将最小值的下标存入k
			}
			j++;
		}
		cout << "(" << this->vertex[adjvex[k]] << "," << this->vertex[k] << ")" << "\t权值为:" << this->edge[adjvex[k]][k] << endl;
		lowcost[k] = 0; //将此顶点纳入最小生成树
		for (j = 0; j < this->vertex_num; j++) //循环所有顶点
		{
			if (lowcost[j] != 0 && this->edge[k][j] < lowcost[j])
			{
				//若下标为k顶点各边权值小于此前这些为被加入生成树的顶点的权值
				//则将最小权值存入lowcost
				lowcost[j] = this->edge[k][j];
				adjvex[j] = k;
			}
		}
	}
	delete[]adjvex;
	delete[]lowcost;
}

//获取字母
char Graph::get_leter()
{
	return this->vertex[0];
}


int main()
{
	int v = 0, e = 0; 
	cout << "请依次输入图的顶点数和边数:" << endl;
	cin >> v >> e;

	Graph g(v, e);
	g.print_graph();
	
	cout << "最小生成树为:" << endl;
	g.prim(g.get_leter());

	system("pause");
	return 0;
}

你可能感兴趣的:(算法)