普利姆(Prim)算法求最小生成树,也就是在包含 n个顶点的连通图中,找出只有(n-1)条边包含所有 n个顶点的连通子图,也就是所谓的极小连通子图

最小生成树:给定一个带权的无向连通图,如何选取一棵生成树,使树上所有边上权的总和为最小,这叫最小生成树

步骤:(1)从第一个点开始,找它和其它点的路径,如果不连通则设为最大,然后找出最小的路径,将此点连上

(2)如果新连入的点到某个点的路径小于原来的路径,则更新,然后继续找最小的,连上

(3)重复这几个步骤

迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。 
它的主要特点是以起始点为中心向外层层扩展(广度优先思想),直到扩展到终点为止。

迪杰斯特拉算法和普利姆算法的区别:迪杰斯特拉是从原点到各点的路径,包括中途经过的顶点的路径也要算上,而普利姆则只算各点和它所连最短的路径那一条路径

总代码

#include<stdio.h>
#include<stdlib.h>/***建立网*/typedef struct Net{int **weights;int numNodes;
} *NetPtr;/***初始化*/NetPtr initNet(int paraSize, int **paraData) {int i, j;NetPtr resultPtr = (NetPtr)malloc(sizeof(struct Net));resultPtr->numNodes = paraSize;resultPtr->weights = (int**)malloc(sizeof(int*) * paraSize);for (i = 0; i < paraSize; i ++) {resultPtr->weights[i] = (int*)malloc(sizeof(int) * sizeof(int));for (j = 0; j < paraSize; j ++) {resultPtr->weights[i][j] = paraData[i][j];}}return resultPtr;
}/***普利姆算法和迪杰斯特拉算法 */int dijkstraOrPrim(NetPtr paraPtr, int paraAlgorithm) {int i, j, minDistance, tempBestNode, resultCost;int source = 0;int numNodes = paraPtr->numNodes;int *distanceArray = (int*)malloc(sizeof(int) * numNodes);int *parentArray = (int*)malloc(sizeof(int) * numNodes);int *visitedArray = (int*)malloc(sizeof(int) * numNodes);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 = 10000;for (j = 0; j < numNodes; j ++) {if (visitedArray[j] == 1) {continue;}if (minDistance > distanceArray[j]) {minDistance = distanceArray[j];tempBestNode = j;}}visitedArray[tempBestNode] = 1;for (j = 0; j < numNodes; j ++) {if (visitedArray[j] == 1) {continue;}if (paraPtr->weights[tempBestNode][j] >= 10000) {continue;}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: \n");for (i = 0; i < numNodes; i ++) {printf("%d ", parentArray[i]);}printf("\n");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\n", i, distanceArray[i], resultCost);}printf("Finally, the total cost is %d.\n", resultCost);}printf("\n");return resultCost;
}/***建立网*/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\n");tempPtr = (int**)malloc(sizeof(int*) * numNodes);for (i = 0; i < numNodes; i ++) {tempPtr[i] = (int*)malloc(sizeof(int) * numNodes);}for (i = 0; i < numNodes; i ++) {for (j = 0; j < numNodes; j ++) {if (myGraph[i][j] == 0) {tempPtr[i][j] = 10000;} else {tempPtr[i][j] = myGraph[i][j];}}}printf("Data ready\n");NetPtr resultNetPtr = initNet(numNodes, tempPtr);return resultNetPtr;
}void testPrim() {NetPtr tempNetPtr = constructSampleNet();printf("=====Dijkstra algorithm=====\n");dijkstraOrPrim(tempNetPtr, 0);printf("=====Prim algorithm=====\n");dijkstraOrPrim(tempNetPtr, 1);
}int main() {testPrim();return 0;
} 

运行结果

PreParing data
Data ready
=====Dijkstra algorithm=====
the parent of each node:
-1 0 0 0 2 2
From node 0, path length to all nodes are: 0 (0), 1 (6), 2 (1), 3 (5), 4 (7), 5 (5),
=====Prim algorithm=====
the parent of each node:
-1 2 0 5 1 2
cost of node 0 is 0, total = 0
cost of node 1 is 5, total = 5
cost of node 2 is 1, total = 6
cost of node 3 is 2, total = 8
cost of node 4 is 3, total = 11
cost of node 5 is 4, total = 15
Finally, the total cost is 15.

数据结构第十二天——普利姆算法和迪杰斯特拉算法相关推荐

  1. 图 相关算法~从头学算法【广搜、 深搜、 拓扑排序、 并查集、 弗洛伊德算法、迪杰斯特拉算法】

    图的相关主流算法主要有: 广度优先搜索 深度优先搜索 拓扑排序 并查集 多源最短路径(弗洛伊德算法) 单源最短路径(迪杰斯特拉算法) 其中呢,最基本的是前两种,也就是平时常用的广搜和深搜,本文中将概要 ...

  2. 0096 克鲁斯卡尔算法,迪杰斯特拉算法

    /*  * 克鲁斯卡尔算法  * 1.用来求加权连通图的最小生成树的算法  * 2.思想:按照权值从小到大的顺序,选择n-1条边,并保证这n-1条边不构成回路  * 3.先构造一个只含n个顶点的森林, ...

  3. 【数据结构】图的应用(普利姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法、拓扑排序)

    最小生成树 什么是最小生成树 是一棵树 - 无回路 - |V|个顶点一定有|V|-1条边 是生成树 - 包含全部顶点 - |V|-1条边全在图里 贪心算法 什么是"贪":每一步都要 ...

  4. 【数据结构与算法】迪杰斯特拉算法的介绍和最短路径问题程序实现

    目录 1. 迪杰斯特拉算法的介绍 2. 迪杰斯特拉算法的原理 3. 最短路径问题介绍 1. 迪杰斯特拉算法的介绍 迪杰斯特拉(Dijkstra)算法是典型求两点之间最短路径算法.它的主要特点是以起始点 ...

  5. java迪杰斯特拉算法_迪杰斯特拉算法完整代码(Java)

    package com.rao.graph; import java.util.*; /** * @author Srao * @className Dijkstra * @date 2019/12/ ...

  6. 计算机网络课程实验4——编程实现路由算法(迪杰斯特拉算法)

    实验目的: 运用各种编程语言实现基于 Dijkstra 算法的路由软件. 实验意义: 通过本实验,使学生能够对路由原理和路由算法有进一步的理解和掌握. 实验步骤: 1, 选择合适的编程语言编程实现基于 ...

  7. 最短路径的两种算法(迪杰斯特拉算法和弗洛伊德算法)

    一.迪杰斯特拉(Dijkstra)算法 1.定义描述 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩 ...

  8. 算法系列——迪杰斯特拉算法(Dijkstra)

    本系列旨在用简单的人话讲解算法,尽可能避免晦涩的定义,读者可以短时间内理解算法原理及应用细节.我在努力! 本篇文章编程语言为Python,供参考. 迪杰斯特拉算法(Dijkstra) 典型最短路径算法 ...

  9. 最短路径算法之迪杰斯特拉算法(Dijkstra)和佛洛依德算法(Floyd)

    今天学习了这两种算法,都是用来求最小路径的算法,但是迪杰斯特拉算法只能从某个特定点到所有点的最短路径,而佛洛依德算法可以查出任意点到任意点的最小路径. 迪杰斯特拉: package dijkstra; ...

最新文章

  1. 神经网络基础:(2)损失函数
  2. 想知道什么是“成员变量”吗?
  3. tensorflow 训练权重不更新_TensorFlow模型剪枝原理
  4. 使用maven快速构建SSM项目
  5. 奇怪吸引子---NoseHoover
  6. MyBatis关键配置-接口注入使用
  7. 全部关于测试–第2部分
  8. python_标识符_帮助系统的使用_命名规则---python工作笔记018
  9. LTR学习排序 Learning to Rank 小结
  10. only has output.xml, how to check failed step.
  11. HDU2075 A|B?【水题】
  12. 01. Couchbase简介-CouchBase从0到50
  13. 电脑重置,win10怎么重置此电脑 重置电脑的方法和后果
  14. Showwindow 及参数
  15. 运行 CTS 测试命令
  16. Invalid hook call. Hooks can only be called inside of the body of a function
  17. 我的第一篇论文诞生的故事
  18. 微信小程序 界面禁止下拉 左右滑动_微信 iOS 版更新,带来了这些新功能
  19. 怎样炒美国股票 | 如何炒美股
  20. Stream流使用详解

热门文章

  1. FutureWarning: pandas.Int64Index is deprecated and will be removed ... in a future version. 解决方法
  2. 对cpu和内存的简单理解
  3. [零基础学JAVA]Java SE面向对象部分-18.面向对象高级(06)
  4. c语言考研知识,c语言考研应如何准备
  5. 行业分析-全球与中国气动播种机市场现状及未来发展趋势
  6. 今天无聊写了个自定义动态特效烟雾PHP源码
  7. 初识mybatis源码
  8. SpringBoot实现数据加密传输
  9. htc vive_HTC Vive Pro如何比原始Vive更好?
  10. 应用性能管理解决方案