一、代码块
网的构建
typedef struct Net{
int** weights;
int numNodes;
} *NetPtr;
创造一个范例网
NetPtr constructSampleNet(){
int i, j;
int myGraph[6][6] = {
{0, 6, 1, 5, 0, 0},
{6, 0, 5, 0, 3, 0},
{1, 5, 0, 5, 6, 4},
{5, 0, 5, 0, 0, 2},
{0, 3, 6, 0, 0, 6},
{0, 0, 4, 2, 6, 0}};
int** tempPtr;
int numNodes = 6;
printf("Preparing data\r\n");
tempPtr = (int**) malloc (numNodes * sizeof (int*));
for(i = 0; i < numNodes; i++){
tempPtr[i] = (int*)malloc(numNodes * sizeof(int));
}
for(i = 0; i < numNodes; i++){
for(j = 0; j < numNodes; j++){
if(myGraph[i][j] == 0){
tempPtr[i][j] = MAX_DISTANCE;
} else {
tempPtr[i][j] = myGraph[i][j];
}
}
}
printf("Data ready\r\n");
NetPtr resultNetPtr = initNet(numNodes, tempPtr);
return resultNetPtr;
}
初始化网
NetPtr initNet(int paraSize, int** paraData){
int i, j;
NetPtr resultPtr = (NetPtr) malloc (sizeof (Net));
resultPtr -> numNodes = paraSize;
//开辟二维空间
resultPtr->weights = (int**) malloc (paraSize * sizeof (int*));
for(i = 0; i < paraSize; i++){
resultPtr -> weights[i] = (int*) malloc (paraSize * sizeof (int));
for (j = 0; j < paraSize; j++){
//储存值
resultPtr -> weights[i][j] = paraData[i][j];
}
}
return resultPtr;
}
生成树的Prim算法,或最近路径的Dijkstra算法
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm){
int i, j, minDistance, tempBestNode, resultCost;
int source = 0;
int numNodes = paraPtr->numNodes;
int *distanceArray = (int*) malloc (numNodes * sizeof (int));
int *parentArray = (int*) malloc (numNodes * sizeof (int));
//本质上是布尔型的
int *visitedArray = (int*) malloc (numNodes * sizeof (int));
//初始化。任何结点都可当作起点
for(i = 0; i < numNodes; i++){
distanceArray[i] = paraPtr->weights[source][i];
parentArray[i] = source;
visitedArray[i] = 0;
}
distanceArray[source] = 0;
parentArray[source] = -1;
visitedArray[source] = 1;
//主回路
tempBestNode = -1;
for(i = 0; i < numNodes - 1; i++){
//找到最近的结点
minDistance = MAX_DISTANCE;
for(j = 0; j < numNodes; j++){
//判断结点是否已访问
if(visitedArray[j]){
continue;
}
if(minDistance > distanceArray[j]){
minDistance = distanceArray[j];
tempBestNode = j;
}
}
visitedArray[tempBestNode] = 1;
//准备下一轮循环
for(j = 0; j < numNodes; j++){
//判断结点是否已访问
if(visitedArray[j]){
continue;
}
//判断结点是否能到达
if(paraPtr->weights[tempBestNode][j] >= MAX_DISTANCE){
continue;
}
//注意:Dijkstra和Prim算法之间的差异
if(paraAlgorithm == 0){
if(distanceArray[j] > distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j]){
//更改与起点距离
distanceArray[j] = distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j];
//更改父级
parentArray[j] = tempBestNode;
}
} else {
if(distanceArray[j] > paraPtr->weights[tempBestNode][j]){
//更改与新起点距离
distanceArray[j] = paraPtr->weights[tempBestNode][j];
//更改父级
parentArray[j] = tempBestNode;
}
}
}
}
printf("the parent of each node: ");
for(i = 0; i < numNodes; i++){
printf("%d, ", parentArray[i]);
}
if(paraAlgorithm == 0){
printf("From node 0, path length to all nodes are: ");
for(i = 0; i < numNodes; i++){
printf("%d (%d), ", i, distanceArray[i]);
}
} else {
resultCost = 0;
for(i = 0; i < numNodes; i++){
resultCost += distanceArray[i];
printf("cost of node %d is %d, total = %d\r\n", i, distanceArray[i], resultCost);
}
printf("Finally, the total cost is %d.\r\n ", resultCost);
}
printf("\r\n");
return resultCost;
}
二、全部代码
//
// Prim 算法与 Dijkstra 算法.c
// 网
//
// Created on 2022/6/1.
//
#include <stdio.h>
#include <malloc.h>
#define MAX_DISTANCE 10000
//网的构建
typedef struct Net{
int** weights;
int numNodes;
} *NetPtr;
//创造一个范例网
NetPtr constructSampleNet(){
int i, j;
int myGraph[6][6] = {
{0, 6, 1, 5, 0, 0},
{6, 0, 5, 0, 3, 0},
{1, 5, 0, 5, 6, 4},
{5, 0, 5, 0, 0, 2},
{0, 3, 6, 0, 0, 6},
{0, 0, 4, 2, 6, 0}};
int** tempPtr;
int numNodes = 6;
printf("Preparing data\r\n");
tempPtr = (int**) malloc (numNodes * sizeof (int*));
for(i = 0; i < numNodes; i++){
tempPtr[i] = (int*)malloc(numNodes * sizeof(int));
}
for(i = 0; i < numNodes; i++){
for(j = 0; j < numNodes; j++){
if(myGraph[i][j] == 0){
tempPtr[i][j] = MAX_DISTANCE;
} else {
tempPtr[i][j] = myGraph[i][j];
}
}
}
printf("Data ready\r\n");
NetPtr resultNetPtr = initNet(numNodes, tempPtr);
return resultNetPtr;
}
//初始化网
NetPtr initNet(int paraSize, int** paraData){
int i, j;
NetPtr resultPtr = (NetPtr) malloc (sizeof (Net));
resultPtr -> numNodes = paraSize;
//开辟二维空间
resultPtr->weights = (int**) malloc (paraSize * sizeof (int*));
for(i = 0; i < paraSize; i++){
resultPtr -> weights[i] = (int*) malloc (paraSize * sizeof (int));
for (j = 0; j < paraSize; j++){
//储存值
resultPtr -> weights[i][j] = paraData[i][j];
}
}
return resultPtr;
}
//生成树的Prim算法,或最近路径的Dijkstra算法
int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm){
int i, j, minDistance, tempBestNode, resultCost;
int source = 0;
int numNodes = paraPtr->numNodes;
int *distanceArray = (int*) malloc (numNodes * sizeof (int));
int *parentArray = (int*) malloc (numNodes * sizeof (int));
//本质上是布尔型的
int *visitedArray = (int*) malloc (numNodes * sizeof (int));
//初始化。任何结点都可当作起点
for(i = 0; i < numNodes; i++){
distanceArray[i] = paraPtr->weights[source][i];
parentArray[i] = source;
visitedArray[i] = 0;
}
distanceArray[source] = 0;
parentArray[source] = -1;
visitedArray[source] = 1;
//主回路
tempBestNode = -1;
for(i = 0; i < numNodes - 1; i++){
//找到最近的结点
minDistance = MAX_DISTANCE;
for(j = 0; j < numNodes; j++){
//判断结点是否已访问
if(visitedArray[j]){
continue;
}
if(minDistance > distanceArray[j]){
minDistance = distanceArray[j];
tempBestNode = j;
}
}
visitedArray[tempBestNode] = 1;
//准备下一轮循环
for(j = 0; j < numNodes; j++){
//判断结点是否已访问
if(visitedArray[j]){
continue;
}
//判断结点是否能到达
if(paraPtr->weights[tempBestNode][j] >= MAX_DISTANCE){
continue;
}
//注意:Dijkstra和Prim算法之间的差异
if(paraAlgorithm == 0){
if(distanceArray[j] > distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j]){
//更改与起点距离
distanceArray[j] = distanceArray[tempBestNode] + paraPtr->weights[tempBestNode][j];
//更改父级
parentArray[j] = tempBestNode;
}
} else {
if(distanceArray[j] > paraPtr->weights[tempBestNode][j]){
//更改与新起点距离
distanceArray[j] = paraPtr->weights[tempBestNode][j];
//更改父级
parentArray[j] = tempBestNode;
}
}
}
}
printf("the parent of each node: ");
for(i = 0; i < numNodes; i++){
printf("%d, ", parentArray[i]);
}
if(paraAlgorithm == 0){
printf("From node 0, path length to all nodes are: ");
for(i = 0; i < numNodes; i++){
printf("%d (%d), ", i, distanceArray[i]);
}
} else {
resultCost = 0;
for(i = 0; i < numNodes; i++){
resultCost += distanceArray[i];
printf("cost of node %d is %d, total = %d\r\n", i, distanceArray[i], resultCost);
}
printf("Finally, the total cost is %d.\r\n ", resultCost);
}
printf("\r\n");
return resultCost;
}
//测试
void testPrim(){
NetPtr tempNetPtr = constructSampleNet();
printf("=====Dijkstra algorithm=====\r\n");
dijkstraOrPrim(tempNetPtr, 0);
printf("=====Prim algorithm=====\r\n");
dijkstraOrPrim(tempNetPtr, 1);
}
//main
int main(){
testPrim();
return 0;
}
三、小结
都是使用贪心算法和线性规划,每一步都是选择权值/花费最小的边。
贪心算法:一个局部最有解也是全局最优解;
线性规划:主问题包含n个子问题,而且其中有重叠的子问题。
Dijkstra算法通过线性规划缓存了最优子路径的解,每一步也通过贪心算法来选择最小的边。
Prim算法通过贪心算法来选择最小的边,而Prim的每个子树都是最小生成树说明满足线性规划的两个条件。