闲来无聊,前两天看到一篇关于算法实现的文章。里面又关于图的各种算法介绍,正好上学期还学过图论,现在还记得一点点,先来实现个prim算法:

表示图的文件的内容大体上是这样的:

 2.0    1.0    1.0
3.0 1.0 1.0
4.0 1.0 1.0
5.0 1.0 1.0
6.0 1.0 1.0
7.0 1.0 1.0
8.0 1.0 1.0
9.0 1.0 1.0
11.0 1.0 1.0
12.0 1.0 1.0
13.0 1.0 1.0
14.0 1.0 1.0
18.0 1.0 1.0
20.0 1.0 1.0
22.0 1.0 1.0
32.0 1.0 1.0
34.0 10.0 1.0
34.014. 1.0
33.0 15.0 1.0
34.0 15.0 1.0
33.0 16.0 1.0
34.0 16.0 1.0
33.0 19.0 1.0
34.0 19.0 1.0
3.0 2.0 1.0
4.0 2.0 1.0
8.0 2.0 1.0
14.0 2.0 1.0
18.0 2.0 1.0
20.0 2.0 1.0
22.0 2.0 1.0
31.0 2.0 1.0
34.0 20.0 1.0
33.0 21.0 1.0
34.0 21.0 1.0
33.0 23.0 1.0
34.0 23.0 1.0
26.0 24.0 1.0
28.0 24.0 1.0
30.0 24.0 1.0
33.0 24.0 1.0
34.0 24.0 1.0
26.0 25.0 1.0
28.0 25.0 1.0
32.0 25.0 1.0
32.0 26.0 1.0
30.0 27.0 1.0
34.0 27.0 1.0
34.0 28.0 1.0
32.0 29.0 1.0
34.0 29.0 1.0
4.0 3.0 1.0
8.0 3.0 1.0
9.0 3.0 1.0
10.0 3.0 1.0
14.0 3.0 1.0
28.0 3.0 1.0
29.0 3.0 1.0
33.0 3.0 1.0
33.0 30.0 1.0
34.0 30.0 1.0
33.0 31.0 1.0
34.0 31.0 1.0
33.0 32.0 1.0
34.0 32.0 1.0
34.0 33.0 1.0
8.0 4.0 1.0
13.0 4.0 1.0
14.0 4.0 1.0
7.0 5.0 1.0
11.0 5.0 1.0
7.0 6.0 1.0
11.0 6.0 1.0
17.0 6.0 1.0
17.0 7.0 1.0
31.0 9.0 1.0
33.0 9.0 1.0
34.0 9.0 1.0

注意,从左到右分别是当前节点,连接的节点,边的权重,下面首先就是设计数据结构了:

 class Pair {        //pair代表了与某个点相连的一条边的权重
private: //,以及和这条变相连的另一个顶点是哪个
double edge_weight;
int adacent_vertex;
public:
Pair(int, double);
double weight() const;
int vertex() const;
};

上面的pair代表一个点相邻的边的权重以及这条边与哪一个顶点是相连的。

 class Node { //代表了一个节点,其包含的信息有与其相连的
private: //某一条边的权重以及和这条边相连的另一个顶点。
Pair element;
Node *next_node;
public:
Node(Pair e, Node * = NULL);
Pair retrieve() const;
Node *next() const;
};

代表一个节点,注意这个节点的next_node的值与相邻节点没有任何关系,只是代表链表的下一个节点,下面介绍的是链表:

 class List { //List中存放的是每个具体的节点,
private: //每个节点后面的链表代表的是与其相邻接的节点
Node *list_head;
public:
List();
// Accessors
bool empty() const;
Pair front() const;
Node *head() const;
void push_front(Pair);
Pair pop_front();
void print();
};

下面的cell实际上代表的就是一颗生成树了:

 class Cell { //cell代表的就是一个具体的生成树了
private:
bool visited;
double distance;
int parent;
public:
Cell(bool = false, double = INFTY, int = );
bool isvisited() const;
double get_distance() const;
int get_parent() const;
};

-----------------------------------------------------------------------------------------------------------

下面是数据结构的具体定义了:

 #include "structure.h"

 Pair::Pair(int e, double m) :
edge_weight(m),
adacent_vertex(e) {
// empty constructor
} double Pair::weight()const {
return edge_weight;
}
int Pair::vertex()const {
return adacent_vertex;
} Node::Node(Pair e, Node *n) :
element(e),
next_node(n) {
// empty constructor
} Pair Node::retrieve() const{
return element;
}
Node *Node::next() const {
return next_node;
} List::List() :list_head(NULL) {
// empty constructor
} bool List::empty() const {
if (list_head == NULL) {
return true;
}
else {
return false;
}
} Node *List::head() const {
return list_head; } Pair List::front() const { if (empty()) {
cout << "error! the list is empty";
}
return head()->retrieve();
} void List::push_front(Pair e) {
if (empty()) {
list_head = new Node(e, NULL);
}
else {
list_head = new Node(e, head());
} } Pair List::pop_front() {
if (empty()) {
cout << "error! the list is empty";
}
Pair e = front();
Node *ptr = list_head;
list_head = list_head->next();
delete ptr;
return e; } void List::print() {
if (empty()) {
cout << "empty" << endl;
}
else {
for (Node *ptr = head(); ptr != NULL; ptr = ptr->next())
{
cout << "<" << ptr->retrieve().vertex() << " " << ptr->retrieve().weight() << "> ";
}
cout << endl;
}
} Cell::Cell(bool v, double d, int p) :
visited(v),
distance(d),
parent(p) {
// empty constructor
} bool Cell::isvisited() const {
return visited;
} double Cell::get_distance()const {
return distance;
}
int Cell::get_parent()const {
return parent;
}

好了有了上面的数据结构,实现Prim算法就比较简单了:

 Cell* Prim(List * graph, int n, int start)  //传入一个邻接数组,以及数组的大小,
{ //以及邻接矩阵的起始点,求一个最小生成树
Cell* Table = new Cell[n + ]; //n+1的目的是节点是从1开始数的,所以要多添加一个
//Table[start]=Cell(false,0,0);//这里的false是否换成True会好一点?
Table[start] = Cell(true, , );
/* 实现prim算法*/
int currMinNode, currParent = ;
double currMin;
for (;;){
currMin = INFTY; //注意这个的类型是double类型
currMinNode = ;
currParent = ;
for (int i = ; i <= n; ++i){
if (Table[i].isvisited()){//已经被访问过了
Node * currNode = graph[i].head();
while (currNode != NULL){ //从该节点开始,然后访问其所有的邻接的节点
int tmpNode = currNode->retrieve().vertex();
double tmpWeight = currNode->retrieve().weight();
if (!Table[tmpNode].isvisited() && tmpWeight < currMin){
currMin = tmpWeight;
currMinNode = tmpNode;
currParent = i;
}
currNode = currNode->next(); //取下一个邻接的节点
}
}
else
continue; }
Table[currMinNode] = Cell(true, currMin, currParent);//找到下一个节点,将其置为True
if (currMinNode == ) //如果所有的节点都已经遍历完毕的话,就停止下一次的寻找
break; }
return Table;
}

顺手写个打印生成树的函数:

 void PrintTable(Cell* Table, int n)
{
for (int i = ; i <= n; i++)
cout << Table[i].isvisited() << " " <<
Table[i].get_distance() << " " <<
Table[i].get_parent() << endl;
}

主函数如下所示:

 #include "structure.h"
#include "Prim.h"
int main()
{
List * graph = new List[N];
char *inputfile = "primTest.txt";
ifstream fin; //输入文件 .join后结果
int n = ;
double x, y, w;
fin.open(inputfile);
while (!fin.eof())
{
fin >> x >> y >> w;
Pair a(y, w);
Pair b(x, w);
graph[int(x)].push_front(a);
graph[int(y)].push_front(b);
if (n <= x)
n = x;
if (n <= y)
n = y;
}
fin.close();
cout << "The total Node number is "
<< n << endl;
for (int i = ; i <= n; i++)
graph[i].print(); Cell* Table = Prim(graph, n, );
cout << "-----------------------\n";
PrintTable(Table, n); return ;
}

最后的结果如下所示:

写的有点乱,见谅见谅

其实这个也可以实现Dijkstra算法,那个好像没学过,看看以后有时间再来写。

C++实现Prim算法的更多相关文章

  1. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  2. 最小生成树のprim算法

    Problem A Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  3. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  4. 最小生成树——prim算法

    prim算法是选取任意一个顶点作为树的一个节点,然后贪心的选取离这棵树最近的点,直到连上所有的点并且不够成环,它的时间复杂度为o(v^2) #include<iostream>#inclu ...

  5. 洛谷 P3366 【模板】最小生成树 prim算法思路 我自己的实现

    网上有很多prim算法  用邻接矩阵 加什么lowcost数组 我觉得不靠谱 毕竟邻接矩阵本身就不是存图的好方法 所以自己写了一个邻接表(边信息表)版本的  注意我还是用了优先队列  每次新加入一个点 ...

  6. 最小生成树算法——prim算法

    prim算法:从某一点开始,去遍历相邻的边,然后将权值最短的边加入集合,同时将新加入边集中的新点遍历相邻的边更新边值集合(边值集合用来找出新的最小权值边),注意每次更新都需将cost数组中的点对应的权 ...

  7. 贪心算法-最小生成树Kruskal算法和Prim算法

    Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...

  8. Prim算法(三)之 Java详解

    前面分别通过C和C++实现了普里姆,本文介绍普里姆的Java实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http:// ...

  9. Prim算法(二)之 C++详解

    本章是普里姆算法的C++实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http://www.cnblogs.com/sk ...

  10. Prim算法(一)之 C语言详解

    本章介绍普里姆算法.和以往一样,本文会先对普里姆算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里 ...

随机推荐

  1. centos上传下载文件

    1.在windows上安装SecureCRT 2.在centos上安装rzsz软件:yum install lrzsz 3.rz命令上传文件到centos 4.sz命令发送文件到windows

  2. HTML5文件上传前本地预览

    HTML5之FileReader的使用 HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型 ...

  3. Uninstalling JIRA applications from Linux

     If you wish to re-install JIRA in 'unattended mode', do not uninstall your previous installation of ...

  4. java中数组以及集合

    java中数组: 数组在Java里是一种特殊类型,有别于普通的“类的实例”的对象.但实际数组也是一种对象类型,int[]a = new int[5]  a是在java栈中分配的引用变量,类型是int[ ...

  5. 使用C++11实现一个半同步半异步线程池

    前言 C++11之前我们使用线程需要系统提供API.posix线程库或者使用boost提供的线程库,C++11后就加入了跨平台的线程类std::thread,线程同步相关类std::mutex.std ...

  6. 简单的linux压力测试工具webbench

    wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd we ...

  7. Javascript 中的 call 和 apply

    发表于 2012年02月1日 by 愚人码头   原文链接:http://www.css88.com/archives/4431 JavaScript 中通过call或者apply用来代替另一个对象调 ...

  8. lamp编译详解

    首先确认系统环境:centos6.4 min版本 1.安装需要的开发环境 yum groupinstall "Development Tools" "Server Pla ...

  9. 20145240《网络对抗》PC平台逆向破解_advanced

    PC平台逆向破解_advanced shellcode注入 Shellcode实际是一段代码(也可以是填充数据),是用来发送到服务器利用特定漏洞的代码,一般可以获取权限.另外,Shellcode一般是 ...

  10. 广播机制的CS模型实现

    广播机制的cs模型实现如下: 首先可以使用ifconfig命令查看自己所在网段的广播地址 server.c #include<stdio.h> #include<unistd.h&g ...