Compare commits
2 Commits
lili_branc
...
master
| Author | SHA1 | Date |
|---|---|---|
|
|
90647bc3d1 | |
|
|
3991b1cd4c |
259
DFS.cpp
259
DFS.cpp
|
|
@ -1,259 +0,0 @@
|
|||
/**
|
||||
用深度优先搜索的方式遍历所以可能路径,找到最优解
|
||||
**/
|
||||
|
||||
#include "dijstra_fibonacci.h"
|
||||
#include "graph.h"
|
||||
#include "dijstra_MinHeap.h"
|
||||
#include<set>
|
||||
#include "DFS.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
void get_graph(){
|
||||
int V = 4;
|
||||
//v1.insert(1);
|
||||
v1.insert(3);
|
||||
v1.insert(2);
|
||||
//v1.insert(1);
|
||||
//v1.insert(5);
|
||||
//v1.insert(6);
|
||||
//v1.insert(7);
|
||||
//v1.insert(8);
|
||||
//set<int> v;
|
||||
//v.insert(3);
|
||||
//v.insert(3);
|
||||
graph = createGraph(V);
|
||||
addEdge(graph, 0, 1, 1);
|
||||
addEdge(graph, 0, 2, 2);
|
||||
addEdge(graph, 0, 3, 1);
|
||||
addEdge(graph, 2, 1, 3);
|
||||
addEdge(graph, 2, 3, 1);
|
||||
addEdge(graph, 3, 1, 1);
|
||||
addEdge(graph, 3, 2, 1);
|
||||
/*
|
||||
addEdge(graph, 0, 1, 4);
|
||||
addEdge(graph, 0, 7, 8);
|
||||
addEdge(graph, 1, 2, 8);
|
||||
addEdge(graph, 1, 7, 11);
|
||||
addEdge(graph, 2, 3, 7);
|
||||
addEdge(graph, 2, 8, 2);
|
||||
addEdge(graph, 2, 4, 131);
|
||||
addEdge(graph, 2, 5, 4);
|
||||
addEdge(graph, 3, 4, 9);
|
||||
addEdge(graph, 3, 5, 14);
|
||||
addEdge(graph, 4, 5, 10);
|
||||
addEdge(graph, 5, 6, 2);
|
||||
addEdge(graph, 6, 7, 1);
|
||||
addEdge(graph, 6, 8, 6);
|
||||
addEdge(graph, 7, 8, 7);
|
||||
*/
|
||||
src=0;
|
||||
dest=1;
|
||||
}
|
||||
class Path_v1{
|
||||
public:
|
||||
struct Path *p;
|
||||
set<int> drop_v;
|
||||
Path_v1* next;
|
||||
};
|
||||
Path_v1* path_v1;
|
||||
bool intersection_isnot_empty(set<int> a,set<int> b){
|
||||
set<int>::iterator it;
|
||||
for(it=b.begin();it!=b.end();it++){
|
||||
if(a.count(*it)==1) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
bool is_a_contain_b(set<int> a,set<int> b){
|
||||
set<int>::iterator it; //定义前向迭代器
|
||||
for(it=b.begin();it!=b.end();it++){
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
set<int> get_pass_v(Path *p,int src,int dest){
|
||||
set<int> pass_v;
|
||||
if(p[dest].dist>=INT_MAX) return pass_v;
|
||||
int temp=dest;
|
||||
while(p[temp].pre!=src)
|
||||
{
|
||||
temp=p[temp].pre;
|
||||
pass_v.insert(temp);
|
||||
|
||||
}
|
||||
// pass_v.insert(temp);
|
||||
return pass_v; //don't include src and dest
|
||||
}
|
||||
/*
|
||||
set<int> copy_insert_set(set<int> a,int n){
|
||||
set<int>::iterator it; //定义前向迭代器
|
||||
set<int> b;
|
||||
for(it=a.begin();it!=a.end();it++){
|
||||
b.insert(*it);
|
||||
}
|
||||
b.insert(n);
|
||||
return b;
|
||||
}
|
||||
*/
|
||||
set<int> merge(set<int> a,set<int> b){
|
||||
set<int>::iterator it; //定义前向迭代器
|
||||
for(it=b.begin();it!=b.end();it++){
|
||||
a.insert(*it);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
Path_v1* create_path_v1(Graph* graph)
|
||||
//Path_v1* create_path_v1(Graph* graph,set<int> v1,int src,int dest)
|
||||
{
|
||||
int v=graph->V;//number of vertexs
|
||||
Path_v1* p_v1=new Path_v1[v];
|
||||
v1.insert(src);
|
||||
v1.insert(dest);
|
||||
set<int>::iterator it; //定义前向迭代器
|
||||
|
||||
for(it=v1.begin();it!=v1.end();it++){
|
||||
/*
|
||||
p_v1->p=(struct Path*)malloc(sizeof(struct Path)*v);
|
||||
for(int i=0;i<v;i++){
|
||||
p_v1->p[i].dist=INT_MAX;
|
||||
|
||||
}
|
||||
*/
|
||||
/*
|
||||
if(*it==dest)
|
||||
{
|
||||
p_v1=NULL;
|
||||
continue;
|
||||
}
|
||||
*/
|
||||
//p_v1[*it].p=dijkstra_fibonacci_v1(graph,*it,v1);
|
||||
p_v1[*it].p=dijkstra_v1(graph,*it,v1);
|
||||
p_v1[*it].next=NULL;
|
||||
// print_path(p_v1[*it].p,v,*it);
|
||||
}
|
||||
return p_v1;
|
||||
}
|
||||
int get_child_num(Path *p){
|
||||
int num=0;
|
||||
set<int>::iterator it;
|
||||
for(it=v1.begin();it!=v1.end();it++){
|
||||
if(p[*it].dist<INT_MAX)
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
Tree_Node* create_child(Tree_Node *parent,int this_dest){
|
||||
if(parent->pass_v1.count(this_dest)==1) return NULL; //generate a circle
|
||||
set<int> one=get_pass_v(path_v1[parent->v].p,parent->v,this_dest); //parent to child acroos vertex
|
||||
if(intersection_isnot_empty(one,v1)==1) return NULL;
|
||||
|
||||
Tree_Node *temp=new Tree_Node();
|
||||
|
||||
|
||||
temp->v=this_dest;
|
||||
temp->pass_v=merge(parent->pass_v,one);
|
||||
temp->p=path_v1[parent->v].p;
|
||||
temp->pass_v1=parent->pass_v1;
|
||||
temp->pass_v1.insert(temp->v);
|
||||
//temp->drop_v=parent->drop_v;
|
||||
temp->parent=parent;
|
||||
if(intersection_isnot_empty(one,parent->pass_v)==1)
|
||||
{
|
||||
Path_v1* next=path_v1[parent->v].next;
|
||||
Path_v1* last=next;
|
||||
while(next!=NULL && next->drop_v!=parent->pass_v){
|
||||
last=next;
|
||||
next=next->next;
|
||||
|
||||
}
|
||||
if(next!=NULL)
|
||||
{
|
||||
temp->p=next->p;
|
||||
temp->pass_v=merge(parent->pass_v,get_pass_v(temp->p,parent->v,this_dest));
|
||||
|
||||
//temp->drop_v=next->drop_v;
|
||||
}
|
||||
else{
|
||||
//set<int> drop;
|
||||
//temp->p=dijkstra_fibonacci_drop_v(graph,parent->v,v1,parent->pass_v1);
|
||||
temp->p=dijkstra_drop_v(graph,parent->v,v1,parent->pass_v1);
|
||||
if(temp->p[this_dest].dist>=INT_MAX) return NULL;
|
||||
temp->pass_v=merge(parent->pass_v,get_pass_v(temp->p,parent->v,this_dest));
|
||||
Path_v1* temp_p_v1=new Path_v1();
|
||||
temp_p_v1->drop_v=temp->pass_v;
|
||||
temp_p_v1->p=temp->p;
|
||||
temp_p_v1->next=NULL;
|
||||
last->next=temp_p_v1;
|
||||
}
|
||||
}
|
||||
temp->dist=parent->dist+(temp->p)[temp->v].dist;
|
||||
if(temp->v==dest && temp->pass_v1==v1){
|
||||
if(min_road==NULL) {min_road=temp;return min_road;}
|
||||
if(min_road->dist>parent->dist){
|
||||
min_road=parent;
|
||||
return min_road;
|
||||
}
|
||||
}
|
||||
temp->num_child=get_child_num(path_v1[src].p);
|
||||
temp->child=new Tree_Node*[temp->num_child];
|
||||
//temp->child=(struct Tree_Node**)malloc(sizeof(struct Tree_Node)*(temp->num_child));
|
||||
|
||||
int j=0;
|
||||
for(int i=0;i<graph->V;i++){
|
||||
if(path_v1[temp->v].p[i].dist<INT_MAX && v1.count(i)==1 && temp->v!=i) //reach and in v'
|
||||
temp->child[j++]=create_child(temp,i);
|
||||
}
|
||||
}
|
||||
|
||||
Tree_Node* create_Tree()
|
||||
{
|
||||
|
||||
path_v1=create_path_v1(graph);
|
||||
|
||||
// for(int i=0;i<v1.size();i++)
|
||||
// print_path(path_v1[i].p,graph->V,i);
|
||||
|
||||
if(path_v1[src].p[dest].dist>=INT_MAX) return NULL;
|
||||
// printf("%d",v1.size());
|
||||
Tree_Node* root=new Tree_Node();
|
||||
// (root->pass_v).insert(2);
|
||||
root->v=src;
|
||||
root->dist=0;
|
||||
root->num_child=get_child_num(path_v1[src].p);
|
||||
root->child=new Tree_Node*[root->num_child];
|
||||
root->parent=NULL;
|
||||
root->p=path_v1[src].p;
|
||||
//root->pass_v1.clear();
|
||||
root->pass_v1.insert(src);
|
||||
// create the root's children
|
||||
int j=0;
|
||||
for(int i=0;i<graph->V;i++){
|
||||
if(path_v1[src].p[i].dist<INT_MAX && v1.count(i)==1) //reach and in v'
|
||||
root->child[j++]=create_child(root,i);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
/*
|
||||
int main(){
|
||||
get_graph();
|
||||
Tree_Node* root=create_Tree();
|
||||
if(min_road==NULL)
|
||||
{
|
||||
printf("null");
|
||||
return 0;
|
||||
}
|
||||
printf("%d\n",min_road->dist);
|
||||
Tree_Node* temp=min_road;
|
||||
while(temp->parent!=NULL){
|
||||
print_path_ver(temp->p,temp->parent->v,temp->v);
|
||||
printf("%d ",temp->parent->v);
|
||||
temp=temp->parent;
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
22
DFS.h
22
DFS.h
|
|
@ -1,22 +0,0 @@
|
|||
#include<set>
|
||||
using namespace std;
|
||||
static Graph* graph;
|
||||
static set<int> v1 ;
|
||||
static int src;
|
||||
static int dest;
|
||||
|
||||
class Tree_Node{
|
||||
public:
|
||||
struct Path *p;
|
||||
//set<int> drop_v;
|
||||
Tree_Node **child;
|
||||
Tree_Node *parent;
|
||||
int v;
|
||||
set<int> pass_v; //across vertex in v
|
||||
set<int> pass_v1; // across vertex in v'
|
||||
int dist;
|
||||
int num_child;
|
||||
};
|
||||
|
||||
Tree_Node* min_road=NULL ;
|
||||
Tree_Node* create_Tree();
|
||||
371
Genetic.cpp
371
Genetic.cpp
|
|
@ -1,371 +0,0 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#include<math.h>
|
||||
#include<time.h>
|
||||
#define cities 10 //城市的个数
|
||||
#define MAXX 100//迭代次数
|
||||
#define pc 0.8 //交配概率
|
||||
#define pm 0.05 //变异概率
|
||||
#define num 10//种群的大小
|
||||
int bestsolution;//最优染色体
|
||||
int distance[cities][cities];//城市之间的距离
|
||||
struct group //染色体的结构
|
||||
{
|
||||
int city[cities];//城市的顺序
|
||||
int adapt;//适应度
|
||||
double p;//在种群中的幸存概率
|
||||
}group[num],grouptemp[num];
|
||||
//随机产生cities个城市之间的相互距离
|
||||
void init()
|
||||
{
|
||||
int i,j;
|
||||
memset(distance,0,sizeof(distance));
|
||||
srand((unsigned)time(NULL));
|
||||
for(i=0;i<cities;i++)
|
||||
{
|
||||
for(j=i+1;j<cities;j++)
|
||||
{
|
||||
distance[i][j]=rand()%100;
|
||||
distance[j][i]=distance[i][j];
|
||||
}
|
||||
}
|
||||
//打印距离矩阵
|
||||
printf("城市的距离矩阵如下\n");
|
||||
for(i=0;i<cities;i++)
|
||||
{
|
||||
for(j=0;j<cities;j++)
|
||||
printf("%4d",distance[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
//随机产生初试群
|
||||
void groupproduce()
|
||||
{
|
||||
int i,j,t,k,flag;
|
||||
for(i=0;i<num;i++) //初始化
|
||||
for(j=0;j<cities;j++)
|
||||
group[i].city[j]=-1;
|
||||
srand((unsigned)time(NULL));
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
//产生10个不相同的数字
|
||||
for(j=0;j<cities;)
|
||||
{
|
||||
t=rand()%cities;
|
||||
flag=1;
|
||||
for(k=0;k<j;k++)
|
||||
{
|
||||
if(group[i].city[k]==t)
|
||||
{
|
||||
flag=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(flag)
|
||||
{
|
||||
group[i].city[j]=t;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
//打印种群基因
|
||||
printf("初始的种群\n");
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
for(j=0;j<cities;j++)
|
||||
printf("%4d",group[i].city[j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
//评价函数,找出最优染色体
|
||||
void pingjia()
|
||||
{
|
||||
int i,j;
|
||||
int n1,n2;
|
||||
int sumdistance,biggestsum=0;
|
||||
double biggestp=0;
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
sumdistance=0;
|
||||
for(j=1;j<cities;j++)
|
||||
{
|
||||
n1=group[i].city[j-1];
|
||||
n2=group[i].city[j];
|
||||
sumdistance+=distance[n1][n2];
|
||||
}
|
||||
group[i].adapt=sumdistance; //每条染色体的路径总和
|
||||
biggestsum+=sumdistance; //种群的总路径
|
||||
}
|
||||
//计算染色体的幸存能力,路劲越短生存概率越大
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
group[i].p=1-(double)group[i].adapt/(double)biggestsum;
|
||||
biggestp+=group[i].p;
|
||||
}
|
||||
for(i=0;i<num;i++)
|
||||
group[i].p=group[i].p/biggestp; //在种群中的幸存概率,总和为1
|
||||
//求最佳路劲
|
||||
bestsolution=0;
|
||||
for(i=0;i<num;i++)
|
||||
if(group[i].p>group[bestsolution].p)
|
||||
bestsolution=i;
|
||||
//打印适应度
|
||||
for(i=0;i<num;i++)
|
||||
printf("染色体%d的路径之和与生存概率分别为%4d %.4f\n",i,group[i].adapt,group[i].p);
|
||||
printf("当前种群的最优染色体是%d号染色体\n",bestsolution);
|
||||
}
|
||||
//选择
|
||||
void xuanze()
|
||||
{
|
||||
int i,j,temp;
|
||||
double gradient[num];//梯度概率
|
||||
double xuanze[num];//选择染色体的随机概率
|
||||
int xuan[num];//选择了的染色体
|
||||
//初始化梯度概率
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
gradient[i]=0.0;
|
||||
xuanze[i]=0.0;
|
||||
}
|
||||
gradient[0]=group[0].p;
|
||||
for(i=1;i<num;i++)
|
||||
gradient[i]=gradient[i-1]+group[i].p;
|
||||
srand((unsigned)time(NULL));
|
||||
//随机产生染色体的存活概率
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
xuanze[i]=(rand()%100);
|
||||
xuanze[i]/=100;
|
||||
}
|
||||
//选择能生存的染色体
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
for(j=0;j<num;j++)
|
||||
{
|
||||
if(xuanze[i]<gradient[j])
|
||||
{
|
||||
xuan[i]=j; //第i个位置存放第j个染色体
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//拷贝种群
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
grouptemp[i].adapt=group[i].adapt;
|
||||
grouptemp[i].p=group[i].p;
|
||||
for(j=0;j<cities;j++)
|
||||
grouptemp[i].city[j]=group[i].city[j];
|
||||
}
|
||||
//数据更新
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
temp=xuan[i];
|
||||
group[i].adapt=grouptemp[temp].adapt;
|
||||
group[i].p=grouptemp[temp].p;
|
||||
for(j=0;j<cities;j++)
|
||||
group[i].city[j]=grouptemp[temp].city[j];
|
||||
}
|
||||
//用于测试
|
||||
/*
|
||||
printf("<------------------------------->\n");
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
for(j=0;j<cities;j++)
|
||||
printf("%4d",group[i].city[j]);
|
||||
printf("\n");
|
||||
printf("染色体%d的路径之和与生存概率分别为%4d %.4f\n",i,group[i].adapt,group[i].p);
|
||||
}
|
||||
*/
|
||||
}
|
||||
//交配,对每个染色体产生交配概率,满足交配率的染色体进行交配
|
||||
void jiaopei()
|
||||
{
|
||||
int i,j,k,kk;
|
||||
int t;//参与交配的染色体的个数
|
||||
int point1,point2,temp;//交配断点
|
||||
int pointnum;
|
||||
int temp1,temp2;
|
||||
int map1[cities],map2[cities];
|
||||
double jiaopeip[num];//染色体的交配概率
|
||||
int jiaopeiflag[num];//染色体的可交配情况
|
||||
for(i=0;i<num;i++)//初始化
|
||||
jiaopeiflag[i]=0;
|
||||
//随机产生交配概率
|
||||
srand((unsigned)time(NULL));
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
jiaopeip[i]=(rand()%100);
|
||||
jiaopeip[i]/=100;
|
||||
}
|
||||
//确定可以交配的染色体
|
||||
t=0;
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
if(jiaopeip[i]<pc)
|
||||
{
|
||||
jiaopeiflag[i]=1;
|
||||
t++;
|
||||
}
|
||||
}
|
||||
t=t/2*2;//t必须为偶数
|
||||
//产生t/2个0-9交配断点
|
||||
srand((unsigned)time(NULL));
|
||||
temp1=0;
|
||||
//temp1号染色体和temp2染色体交配
|
||||
for(i=0;i<t/2;i++)
|
||||
{
|
||||
point1=rand()%cities;
|
||||
point2=rand()%cities;
|
||||
for(j=temp1;j<num;j++)
|
||||
if(jiaopeiflag[j]==1)
|
||||
{
|
||||
temp1=j;
|
||||
break;
|
||||
}
|
||||
for(j=temp1+1;j<num;j++)
|
||||
if(jiaopeiflag[j]==1)
|
||||
{
|
||||
temp2=j;
|
||||
break;
|
||||
}
|
||||
//进行基因交配
|
||||
if(point1>point2) //保证point1<=point2
|
||||
{
|
||||
temp=point1;
|
||||
point1=point2;
|
||||
point2=temp;
|
||||
}
|
||||
memset(map1,-1,sizeof(map1));
|
||||
memset(map2,-1,sizeof(map2));
|
||||
//断点之间的基因产生映射
|
||||
for(k=point1;k<=point2;k++)
|
||||
{
|
||||
map1[group[temp1].city[k]]=group[temp2].city[k];
|
||||
map2[group[temp2].city[k]]=group[temp1].city[k];
|
||||
}
|
||||
//断点两边的基因互换
|
||||
for(k=0;k<point1;k++)
|
||||
{
|
||||
temp=group[temp1].city[k];
|
||||
group[temp1].city[k]=group[temp2].city[k];
|
||||
group[temp2].city[k]=temp;
|
||||
}
|
||||
for(k=point2+1;k<cities;k++)
|
||||
{
|
||||
temp=group[temp1].city[k];
|
||||
group[temp1].city[k]=group[temp2].city[k];
|
||||
group[temp2].city[k]=temp;
|
||||
}
|
||||
//处理产生的冲突基因
|
||||
for(k=0;k<point1;k++)
|
||||
{
|
||||
for(kk=point1;kk<=point2;kk++)
|
||||
if(group[temp1].city[k]==group[temp1].city[kk])
|
||||
{
|
||||
group[temp1].city[k]=map1[group[temp1].city[k]];
|
||||
break;
|
||||
}
|
||||
}
|
||||
for(k=point2+1;k<cities;k++)
|
||||
{
|
||||
for(kk=point1;kk<=point2;kk++)
|
||||
if(group[temp1].city[k]==group[temp1].city[kk])
|
||||
{
|
||||
group[temp1].city[k]=map1[group[temp1].city[k]];
|
||||
break;
|
||||
}
|
||||
}
|
||||
for(k=0;k<point1;k++)
|
||||
{
|
||||
for(kk=point1;kk<=point2;kk++)
|
||||
if(group[temp2].city[k]==group[temp2].city[kk])
|
||||
{
|
||||
group[temp2].city[k]=map2[group[temp2].city[k]];
|
||||
break;
|
||||
}
|
||||
}
|
||||
for(k=point2+1;k<cities;k++)
|
||||
{
|
||||
for(kk=point1;kk<=point2;kk++)
|
||||
if(group[temp2].city[k]==group[temp2].city[kk])
|
||||
{
|
||||
group[temp2].city[k]=map2[group[temp2].city[k]];
|
||||
break;
|
||||
}
|
||||
}
|
||||
temp1=temp2+1;
|
||||
}
|
||||
}
|
||||
//变异
|
||||
void bianyi()
|
||||
{
|
||||
int i,j;
|
||||
int t;
|
||||
int temp1,temp2,point;
|
||||
double bianyip[num]; //染色体的变异概率
|
||||
int bianyiflag[num];//染色体的变异情况
|
||||
for(i=0;i<num;i++)//初始化
|
||||
bianyiflag[i]=0;
|
||||
//随机产生变异概率
|
||||
srand((unsigned)time(NULL));
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
bianyip[i]=(rand()%100);
|
||||
bianyip[i]/=100;
|
||||
}
|
||||
//确定可以变异的染色体
|
||||
t=0;
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
if(bianyip[i]<pm)
|
||||
{
|
||||
bianyiflag[i]=1;
|
||||
t++;
|
||||
}
|
||||
}
|
||||
//变异操作,即交换染色体的两个节点
|
||||
srand((unsigned)time(NULL));
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
if(bianyiflag[i]==1)
|
||||
{
|
||||
temp1=rand()%10;
|
||||
temp2=rand()%10;
|
||||
point=group[i].city[temp1];
|
||||
group[i].city[temp1]=group[i].city[temp2];
|
||||
group[i].city[temp2]=point;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
int main()
|
||||
{
|
||||
int i,j,t;
|
||||
init();
|
||||
groupproduce();
|
||||
//初始种群评价
|
||||
pingjia();
|
||||
t=0;
|
||||
while(t++<MAXX)
|
||||
{
|
||||
xuanze();
|
||||
//jiaopei();
|
||||
bianyi();
|
||||
pingjia();
|
||||
}
|
||||
//最终种群的评价
|
||||
printf("\n输出最终的种群评价\n");
|
||||
for(i=0;i<num;i++)
|
||||
{
|
||||
for(j=0;j<cities;j++)
|
||||
{
|
||||
printf("%4d",group[i].city[j]);
|
||||
}
|
||||
printf(" adapt:%4d, p:%.4f\n",group[i].adapt,group[i].p);
|
||||
}
|
||||
printf("最优解为%d号染色体\n",bestsolution);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
Binary file not shown.
|
|
@ -1,5 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <iostream.h>
|
||||
|
||||
|
|
@ -1,350 +0,0 @@
|
|||
/* C / C++实现的 Dijkstra最短路径,图的邻接表表示
|
||||
用addEdge(graph, 0, 1, 4);函数添加边
|
||||
调用 dijkstra(graph, 1);得到1节点到其他节点的最短路径
|
||||
返回结果保存在结构数组p[V]中
|
||||
{
|
||||
int dist
|
||||
int pre
|
||||
}
|
||||
p[i].disk表示到i节点的最短路径,0表示本身节点,很大表示不可达
|
||||
p[i].pre 表示i节点最短路径的前驱
|
||||
*/
|
||||
|
||||
#include "graph.h"
|
||||
#include "dijstra_MinHeap.h"
|
||||
#include<set>
|
||||
using namespace std;
|
||||
|
||||
// 最小堆节点
|
||||
struct MinHeapNode {
|
||||
int v; //下标
|
||||
int dist; //距离
|
||||
};
|
||||
|
||||
// 最小堆
|
||||
struct MinHeap {
|
||||
int size;
|
||||
int capacity;
|
||||
int *pos; // pos[i]表示顶点i所在的下标
|
||||
struct MinHeapNode **array;
|
||||
};
|
||||
|
||||
|
||||
// 创建一个最小堆节点
|
||||
struct MinHeapNode* newMinHeapNode(int v, int dist) {
|
||||
struct MinHeapNode* minHeapNode = (struct MinHeapNode*) malloc(
|
||||
sizeof(struct MinHeapNode));
|
||||
minHeapNode->v = v;
|
||||
minHeapNode->dist = dist;
|
||||
return minHeapNode;
|
||||
}
|
||||
|
||||
// A utility function to create a Min Heap
|
||||
struct MinHeap* createMinHeap(int capacity) {
|
||||
struct MinHeap* minHeap = (struct MinHeap*) malloc(sizeof(struct MinHeap));
|
||||
minHeap->pos = (int *) malloc(capacity * sizeof(int));
|
||||
minHeap->size = 0;
|
||||
minHeap->capacity = capacity;
|
||||
minHeap->array = (struct MinHeapNode**) malloc(
|
||||
capacity * sizeof(struct MinHeapNode*));
|
||||
return minHeap;
|
||||
}
|
||||
|
||||
// 交换两个最小堆的节点
|
||||
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b) {
|
||||
struct MinHeapNode* t = *a;
|
||||
*a = *b;
|
||||
*b = t;
|
||||
}
|
||||
|
||||
//在位置 idx 调整堆
|
||||
void minHeapify(struct MinHeap* minHeap, int idx) {
|
||||
int smallest, left, right;
|
||||
smallest = idx;
|
||||
left = 2 * idx + 1;
|
||||
right = 2 * idx + 2;
|
||||
|
||||
if (left < minHeap->size
|
||||
&& minHeap->array[left]->dist < minHeap->array[smallest]->dist)
|
||||
smallest = left;
|
||||
|
||||
if (right < minHeap->size
|
||||
&& minHeap->array[right]->dist < minHeap->array[smallest]->dist)
|
||||
smallest = right;
|
||||
|
||||
if (smallest != idx) {
|
||||
// 需要交换的节点
|
||||
MinHeapNode *smallestNode = minHeap->array[smallest];
|
||||
MinHeapNode *idxNode = minHeap->array[idx];
|
||||
|
||||
//交换下标
|
||||
minHeap->pos[smallestNode->v] = idx;
|
||||
minHeap->pos[idxNode->v] = smallest;
|
||||
|
||||
//交换节点
|
||||
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
|
||||
|
||||
minHeapify(minHeap, smallest);
|
||||
}
|
||||
}
|
||||
|
||||
// 推是否为空
|
||||
int isEmpty(struct MinHeap* minHeap) {
|
||||
return minHeap->size == 0;
|
||||
}
|
||||
|
||||
// 弹出堆顶的节点(即最小的节点)
|
||||
struct MinHeapNode* extractMin(struct MinHeap* minHeap) {
|
||||
if (isEmpty(minHeap))
|
||||
return NULL;
|
||||
|
||||
struct MinHeapNode* root = minHeap->array[0];
|
||||
|
||||
struct MinHeapNode* lastNode = minHeap->array[minHeap->size - 1];
|
||||
minHeap->array[0] = lastNode;
|
||||
|
||||
// 更新下标
|
||||
minHeap->pos[root->v] = minHeap->size - 1;
|
||||
minHeap->pos[lastNode->v] = 0;
|
||||
|
||||
// 记得减少堆的大小
|
||||
--minHeap->size;
|
||||
minHeapify(minHeap, 0);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
// 当节点v的距离更新后(变小了)调整堆
|
||||
void decreaseKey(struct MinHeap* minHeap, int v, int dist) {
|
||||
//获取节点 v 在 堆中的下标
|
||||
int i = minHeap->pos[v];
|
||||
|
||||
minHeap->array[i]->dist = dist;
|
||||
|
||||
// 因为是变小了,自下向上调整堆即可。 O(Logn)
|
||||
while (i && minHeap->array[i]->dist < minHeap->array[(i - 1) / 2]->dist) {
|
||||
minHeap->pos[minHeap->array[i]->v] = (i - 1) / 2;
|
||||
minHeap->pos[minHeap->array[(i - 1) / 2]->v] = i;
|
||||
swapMinHeapNode(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
|
||||
|
||||
i = (i - 1) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断节点v是否在堆中
|
||||
bool isInMinHeap(struct MinHeap *minHeap, int v) {
|
||||
if (minHeap->pos[v] < minHeap->size)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 打印结果
|
||||
/*
|
||||
void print_path(struct Path* p,int V,int src){
|
||||
//int V = graph->V;
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : %d\n",src,i,p[i].dist);
|
||||
|
||||
}
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : ",src,i);
|
||||
int j=i;
|
||||
while(p[j].pre!=0)
|
||||
{
|
||||
printf("%d ",p[j].pre);
|
||||
j=p[j].pre;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
*/
|
||||
//P[v]保存路径(即每个节点的前驱节点)
|
||||
|
||||
struct Path* dijkstra(struct Graph* graph, int src) {
|
||||
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
|
||||
struct MinHeap* minHeap = createMinHeap(V);
|
||||
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; ++v) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
minHeap->array[v] = newMinHeapNode(v, p[v].dist);
|
||||
minHeap->pos[v] = v;
|
||||
}
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
//minHeap->array[src] = newMinHeapNode(src, p[src].dist);
|
||||
minHeap->array[src]->dist=0;
|
||||
|
||||
|
||||
decreaseKey(minHeap, src, p[src].dist);
|
||||
|
||||
minHeap->size = V;
|
||||
|
||||
// 这个循环中,minHeap包含的是所有未在SPT中的顶点
|
||||
while (!isEmpty(minHeap)) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
struct MinHeapNode* minHeapNode = extractMin(minHeap);
|
||||
int u = minHeapNode->v;
|
||||
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (isInMinHeap(minHeap, v) && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
decreaseKey(minHeap, v, p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
|
||||
// 打印
|
||||
//print_path(p,V,src);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
struct Path* dijkstra_v1(struct Graph* graph, int src,set<int> v1) {
|
||||
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
|
||||
struct MinHeap* minHeap = createMinHeap(V);
|
||||
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; ++v) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
minHeap->array[v] = newMinHeapNode(v, p[v].dist);
|
||||
minHeap->pos[v] = v;
|
||||
}
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
//minHeap->array[src] = newMinHeapNode(src, p[src].dist);
|
||||
minHeap->array[src]->dist=0;
|
||||
|
||||
|
||||
decreaseKey(minHeap, src, p[src].dist);
|
||||
|
||||
minHeap->size = V;
|
||||
|
||||
// 这个循环中,minHeap包含的是所有未在SPT中的顶点
|
||||
while (!isEmpty(minHeap) && v1.size()!=0) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
struct MinHeapNode* minHeapNode = extractMin(minHeap);
|
||||
int u = minHeapNode->v;
|
||||
v1.erase(u);
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (isInMinHeap(minHeap, v) && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
decreaseKey(minHeap, v, p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
|
||||
// 打印
|
||||
//print_path(p,V,src);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
struct Path* dijkstra_drop_v(struct Graph* source_graph, int src,set<int> v1,set <int> v) {
|
||||
struct Graph* graph=drop_ver(source_graph,v);
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
|
||||
struct MinHeap* minHeap = createMinHeap(V);
|
||||
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; ++v) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
minHeap->array[v] = newMinHeapNode(v, p[v].dist);
|
||||
minHeap->pos[v] = v;
|
||||
}
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
//minHeap->array[src] = newMinHeapNode(src, p[src].dist);
|
||||
minHeap->array[src]->dist=0;
|
||||
|
||||
|
||||
decreaseKey(minHeap, src, p[src].dist);
|
||||
|
||||
minHeap->size = V;
|
||||
|
||||
// 这个循环中,minHeap包含的是所有未在SPT中的顶点
|
||||
while (!isEmpty(minHeap) && v1.size()!=0) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
struct MinHeapNode* minHeapNode = extractMin(minHeap);
|
||||
int u = minHeapNode->v;
|
||||
v1.erase(u);
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (isInMinHeap(minHeap, v) && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
decreaseKey(minHeap, v, p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
|
||||
// 打印
|
||||
//print_path(p,V,src);
|
||||
|
||||
return p;
|
||||
}
|
||||
/*
|
||||
void test(){
|
||||
// 创建图
|
||||
int V = 9;
|
||||
struct Graph* graph = createGraph(V);
|
||||
addEdge(graph, 0, 1, 1);
|
||||
addEdge(graph, 0, 3, 2);
|
||||
addEdge(graph, 1, 2, 1);
|
||||
addEdge(graph, 1, 3, 20);
|
||||
addEdge(graph, 2, 3, 3);
|
||||
|
||||
print_path(dijkstra(graph, 1),V,1);
|
||||
set<int> s;
|
||||
s.insert(3);
|
||||
// print_graph(graph);
|
||||
// print_graph(drop_ver(graph,s));
|
||||
//dijkstra(drop_ver(graph,s),1);
|
||||
}
|
||||
// 测试
|
||||
|
||||
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
using namespace std;
|
||||
struct Path* dijkstra(struct Graph* graph, int src);
|
||||
struct Path* dijkstra_v1(struct Graph* graph, int src,set<int> v1);
|
||||
struct Path* dijkstra_drop_v(struct Graph* source_graph, int src,set<int> v1,set <int> v);
|
||||
Binary file not shown.
|
|
@ -1,246 +0,0 @@
|
|||
/*Copyright (c) 2010, Robin Message <Robin.Message@cl.cam.ac.uk>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Univsersity of Cambridge nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF CAMBRIDGE OR ROBIN MESSAGE
|
||||
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "dijstra_MinHeap.h"
|
||||
#include "dijstra_fibonacci.h"
|
||||
#include "graph.h"
|
||||
#include "fibonacci.h"
|
||||
#define INT_MAX 2147483647
|
||||
using namespace std;
|
||||
//P[v]保存路径(即每个节点的前驱节点)
|
||||
FibonacciHeap h;
|
||||
/*
|
||||
void print_path(struct Path* p,int V,int src){
|
||||
//int V = graph->V;
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : %d\n",src,i,p[i].dist);
|
||||
|
||||
}
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : ",src,i);
|
||||
int j=i;
|
||||
while(p[j].pre!=0)
|
||||
{
|
||||
printf("%d ",p[j].pre);
|
||||
j=p[j].pre;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int isIn(set<int> v1,struct Path* p){
|
||||
set<int>::iterator it; //定义前向迭代器
|
||||
|
||||
for(it=v1.begin();it!=v1.end();it++){
|
||||
if(p[*it].dist>=INT_MAX)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
struct Path* dijkstra_fibonacci(struct Graph* graph, int src) {
|
||||
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
h.capacity=V;
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; ++v) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
h.insert(v,INT_MAX);
|
||||
}
|
||||
//h.pos[src]->value=0;
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
|
||||
|
||||
h.decreaseKey(h.pos[src],p[src].dist);
|
||||
|
||||
|
||||
|
||||
|
||||
// 这个循环中,h包含的是所有未在SPT中的顶点
|
||||
while (!h.isEmpty()) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
|
||||
int u = h.heap->v;
|
||||
h.removeMinimum();
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (h.pos[v]!=NULL && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
//decreaseKey(minHeap, v, p[v].dist);
|
||||
h.decreaseKey(h.pos[v],p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
//print_path(p,V,src);
|
||||
return p;
|
||||
}
|
||||
|
||||
struct Path* dijkstra_fibonacci_v1(struct Graph* graph, int src,set<int> v1) {
|
||||
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
h.capacity=V;
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; v++) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
h.insert(v,INT_MAX);
|
||||
}
|
||||
h.pos[src]->value=0;
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
|
||||
|
||||
h.decreaseKey(h.pos[src],p[src].dist);
|
||||
|
||||
|
||||
|
||||
|
||||
// 这个循环中,h包含的是所有未在SPT中的顶点
|
||||
while (!h.isEmpty() && v1.size()!=0) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
|
||||
int u = h.heap->v;
|
||||
v1.erase(u);
|
||||
h.removeMinimum();
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (h.pos[v]!=NULL && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
//decreaseKey(minHeap, v, p[v].dist);
|
||||
h.decreaseKey(h.pos[v],p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
//print_path(p,V,src);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
struct Path* dijkstra_fibonacci_drop_v(struct Graph* source_graph, int src,set<int> v1,set <int> v) {
|
||||
struct Graph* graph=drop_ver(source_graph,v);
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
h.capacity=V;
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; ++v) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
h.insert(v,INT_MAX);
|
||||
}
|
||||
h.pos[src]->value=0;
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
//minHeap->array[src] = newMinHeapNode(src, p[src].dist);
|
||||
// minHeap->array[src]->dist=0;
|
||||
|
||||
h.decreaseKey(h.pos[src],p[src].dist);
|
||||
// decreaseKey(minHeap, src, p[src].dist);
|
||||
|
||||
// minHeap->size = V;
|
||||
|
||||
// 这个循环中,minHeap包含的是所有未在SPT中的顶点
|
||||
while (!h.isEmpty() && v1.size()!=0) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
// struct MinHeapNode* minHeapNode = extractMin(minHeap);
|
||||
int u = h.heap->v;
|
||||
v1.erase(u);
|
||||
h.removeMinimum();
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (h.pos[v]!=NULL && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
//decreaseKey(minHeap, v, p[v].dist);
|
||||
h.decreaseKey(h.pos[v],p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
//print_path(p,V,src);
|
||||
return p;
|
||||
}
|
||||
/*
|
||||
void test() {
|
||||
int V = 4;
|
||||
set<int> v1;
|
||||
v1.insert(2);
|
||||
//v1.insert(3);
|
||||
v1.insert(4);
|
||||
v1.insert(1);
|
||||
set<int> v;
|
||||
v.insert(3);
|
||||
//v.insert(13);
|
||||
struct Graph* graph = createGraph(V);
|
||||
addEdge(graph, 0, 1, 1);
|
||||
addEdge(graph, 0, 3, 2);
|
||||
addEdge(graph, 1, 2, 1);
|
||||
addEdge(graph, 1, 3, 20);
|
||||
addEdge(graph, 2, 3, 3);
|
||||
// print_graph(graph);
|
||||
//print_graph(drop_ver(graph,v));
|
||||
//print_graph(graph);
|
||||
print_path(dijkstra_fibonacci(graph,1),V,1);
|
||||
print_path(dijkstra(graph,1),V,1);
|
||||
//dijkstra_fibonacci_v1(graph,0,v1);
|
||||
//dijkstra_fibonacci_drop_v(graph,0,v1,v);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
|
|
@ -1,246 +0,0 @@
|
|||
/*Copyright (c) 2010, Robin Message <Robin.Message@cl.cam.ac.uk>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Univsersity of Cambridge nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF CAMBRIDGE OR ROBIN MESSAGE
|
||||
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "dijstra_MinHeap.h"
|
||||
#include "dijstra_fibonacci.h"
|
||||
#include "graph.h"
|
||||
#include "fibonacci.h"
|
||||
#define INT_MAX 2147483647
|
||||
using namespace std;
|
||||
//P[v]保存路径(即每个节点的前驱节点)
|
||||
FibonacciHeap h;
|
||||
/*
|
||||
void print_path(struct Path* p,int V,int src){
|
||||
//int V = graph->V;
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : %d\n",src,i,p[i].dist);
|
||||
|
||||
}
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : ",src,i);
|
||||
int j=i;
|
||||
while(p[j].pre!=0)
|
||||
{
|
||||
printf("%d ",p[j].pre);
|
||||
j=p[j].pre;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int isIn(set<int> v1,struct Path* p){
|
||||
set<int>::iterator it; //定义前向迭代器
|
||||
|
||||
for(it=v1.begin();it!=v1.end();it++){
|
||||
if(p[*it].dist>=INT_MAX)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
struct Path* dijkstra_fibonacci(struct Graph* graph, int src) {
|
||||
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
h.capacity=V;
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; ++v) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
h.insert(v,INT_MAX);
|
||||
}
|
||||
h.pos[src]->value=0;
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
|
||||
|
||||
h.decreaseKey(h.pos[src],p[src].dist);
|
||||
|
||||
|
||||
|
||||
|
||||
// 这个循环中,h包含的是所有未在SPT中的顶点
|
||||
while (!h.isEmpty()) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
|
||||
int u = h.heap->v;
|
||||
h.removeMinimum();
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (h.pos[v]!=NULL && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
//decreaseKey(minHeap, v, p[v].dist);
|
||||
h.decreaseKey(h.pos[v],p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
//print_path(p,V,src);
|
||||
return p;
|
||||
}
|
||||
|
||||
struct Path* dijkstra_fibonacci_v1(struct Graph* graph, int src,set<int> v1) {
|
||||
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
h.capacity=V;
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; v++) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
h.insert(v,INT_MAX);
|
||||
}
|
||||
h.pos[src]->value=0;
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
|
||||
|
||||
h.decreaseKey(h.pos[src],p[src].dist);
|
||||
|
||||
|
||||
|
||||
|
||||
// 这个循环中,h包含的是所有未在SPT中的顶点
|
||||
while (!h.isEmpty() && v1.size()!=0) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
|
||||
int u = h.heap->v;
|
||||
v1.erase(u);
|
||||
h.removeMinimum();
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (h.pos[v]!=NULL && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
//decreaseKey(minHeap, v, p[v].dist);
|
||||
h.decreaseKey(h.pos[v],p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
//print_path(p,V,src);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
struct Path* dijkstra_fibonacci_drop_v(struct Graph* source_graph, int src,set<int> v1,set <int> v) {
|
||||
struct Graph* graph=drop_ver(source_graph,v);
|
||||
int V = graph->V;
|
||||
struct Path* p=new struct Path[V];
|
||||
h.capacity=V;
|
||||
// 初始化堆包含所有的顶点
|
||||
for (int v = 0; v < V; ++v) {
|
||||
p[v].dist = INT_MAX;
|
||||
p[v].pre=0;
|
||||
h.insert(v,INT_MAX);
|
||||
}
|
||||
h.pos[src]->value=0;
|
||||
|
||||
// 把 源点 src 的距离设置为0,第一个取出的点即为源点
|
||||
p[src].dist = 0;
|
||||
//minHeap->array[src] = newMinHeapNode(src, p[src].dist);
|
||||
// minHeap->array[src]->dist=0;
|
||||
|
||||
h.decreaseKey(h.pos[src],p[src].dist);
|
||||
// decreaseKey(minHeap, src, p[src].dist);
|
||||
|
||||
// minHeap->size = V;
|
||||
|
||||
// 这个循环中,minHeap包含的是所有未在SPT中的顶点
|
||||
while (!h.isEmpty() && v1.size()!=0) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
// struct MinHeapNode* minHeapNode = extractMin(minHeap);
|
||||
int u = h.heap->v;
|
||||
v1.erase(u);
|
||||
h.removeMinimum();
|
||||
// 只需要遍历和u相邻的顶点进行更新
|
||||
struct AdjListNode* pCrawl = graph->array[u].head;
|
||||
while (pCrawl != NULL) {
|
||||
int v = pCrawl->dest;
|
||||
// 松弛操作,更新距离
|
||||
if (h.pos[v]!=NULL && p[u].dist != INT_MAX
|
||||
&& pCrawl->weight + p[u].dist < p[v].dist) {
|
||||
p[v].dist = p[u].dist + pCrawl->weight;
|
||||
p[v].pre=u;
|
||||
//距离更新了之后,要调整最小堆
|
||||
//decreaseKey(minHeap, v, p[v].dist);
|
||||
h.decreaseKey(h.pos[v],p[v].dist);
|
||||
}
|
||||
pCrawl = pCrawl->next;
|
||||
}
|
||||
}
|
||||
//print_path(p,V,src);
|
||||
return p;
|
||||
}
|
||||
|
||||
void test() {
|
||||
int V = 4;
|
||||
set<int> v1;
|
||||
v1.insert(2);
|
||||
//v1.insert(3);
|
||||
v1.insert(4);
|
||||
v1.insert(1);
|
||||
set<int> v;
|
||||
v.insert(3);
|
||||
//v.insert(13);
|
||||
struct Graph* graph = createGraph(V);
|
||||
addEdge(graph, 0, 1, 1);
|
||||
addEdge(graph, 0, 3, 2);
|
||||
addEdge(graph, 1, 2, 1);
|
||||
addEdge(graph, 1, 3, 20);
|
||||
addEdge(graph, 2, 3, 3);
|
||||
// print_graph(graph);
|
||||
//print_graph(drop_ver(graph,v));
|
||||
//print_graph(graph);
|
||||
print_path(dijkstra_fibonacci(graph,1),V,1);
|
||||
print_path(dijkstra(graph,1),V,1);
|
||||
//dijkstra_fibonacci_v1(graph,0,v1);
|
||||
//dijkstra_fibonacci_drop_v(graph,0,v1,v);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
#include <iostream.h>
|
||||
#include<set>
|
||||
using namespace std;
|
||||
struct Path* dijkstra_fibonacci(struct Graph* graph, int src);
|
||||
struct Path* dijkstra_fibonacci_v1(struct Graph* graph, int src,set<int> v1);
|
||||
struct Path* dijkstra_fibonacci_drop_v(struct Graph* source_graph, int src,set<int> v1,set <int> v);
|
||||
//print_path(struct Path* p,int V,int src);
|
||||
271
fibonacci.h
271
fibonacci.h
|
|
@ -1,271 +0,0 @@
|
|||
/*Copyright (c) 2010, Robin Message <Robin.Message@cl.cam.ac.uk>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Univsersity of Cambridge nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF CAMBRIDGE OR ROBIN MESSAGE
|
||||
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include<iostream.h>
|
||||
class FibonacciHeap;
|
||||
|
||||
struct node {
|
||||
public:
|
||||
int value;
|
||||
node* prev;
|
||||
node* next;
|
||||
node* child;
|
||||
node* parent;
|
||||
int v;
|
||||
int degree;
|
||||
bool marked;
|
||||
public:
|
||||
friend class FibonacciHeap;
|
||||
node* getPrev() {return prev;}
|
||||
node* getNext() {return next;}
|
||||
node* getChild() {return child;}
|
||||
node* getParent() {return parent;}
|
||||
int getValue() {return value;}
|
||||
bool isMarked() {return marked;}
|
||||
|
||||
bool hasChildren() {return child;}
|
||||
bool hasParent() {return parent;}
|
||||
};
|
||||
|
||||
class FibonacciHeap {
|
||||
public:
|
||||
int capacity;
|
||||
node** pos;
|
||||
node *heap;
|
||||
|
||||
public:
|
||||
|
||||
FibonacciHeap() {
|
||||
pos=new node* [capacity];
|
||||
heap=_empty();
|
||||
}
|
||||
virtual ~FibonacciHeap() {
|
||||
if(heap) {
|
||||
_deleteAll(heap);
|
||||
}
|
||||
}
|
||||
node* insert(int v,int value) {
|
||||
//capacity+=1;
|
||||
node* ret=_singleton(v,value);
|
||||
pos[v]=ret;
|
||||
heap=_merge(heap,ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
void merge(FibonacciHeap& other) {
|
||||
heap=_merge(heap,other.heap);
|
||||
other.heap=_empty();
|
||||
}
|
||||
|
||||
bool isEmpty() {
|
||||
return heap==NULL;
|
||||
}
|
||||
|
||||
int getMinimum() {
|
||||
return heap->value;
|
||||
}
|
||||
|
||||
int removeMinimum() {
|
||||
node* old=heap;
|
||||
heap=_removeMinimum(heap);
|
||||
int ret=old->value;
|
||||
delete old;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void decreaseKey(node* n,int value) {
|
||||
heap=_decreaseKey(heap,n,value);
|
||||
}
|
||||
|
||||
node* find(int value) {
|
||||
return _find(heap,value);
|
||||
}
|
||||
private:
|
||||
node* _empty() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node* _singleton(int v,int value) {
|
||||
node* n=new node;
|
||||
n->value=value;
|
||||
n->prev=n->next=n;
|
||||
n->degree=0;
|
||||
n->marked=false;
|
||||
n->child=NULL;
|
||||
n->parent=NULL;
|
||||
n->v=v;
|
||||
return n;
|
||||
}
|
||||
//合并a,b两个树
|
||||
node* _merge(node* a,node* b) {
|
||||
if(a==NULL)return b;
|
||||
if(b==NULL)return a;
|
||||
if(a->value>b->value) {
|
||||
node* temp=a;
|
||||
a=b;
|
||||
b=temp;
|
||||
//pos[a->v]
|
||||
}
|
||||
node* an=a->next;
|
||||
node* bp=b->prev;
|
||||
a->next=b;
|
||||
b->prev=a;
|
||||
an->prev=bp;
|
||||
bp->next=an;
|
||||
return a;
|
||||
}
|
||||
|
||||
void _deleteAll(node* n) {
|
||||
if(n!=NULL) {
|
||||
node* c=n;
|
||||
do {
|
||||
node* d=c;
|
||||
c=c->next;
|
||||
_deleteAll(d->child);
|
||||
delete d;
|
||||
} while(c!=n);
|
||||
}
|
||||
}
|
||||
|
||||
void _addChild(node* parent,node* child) {
|
||||
child->prev=child->next=child;
|
||||
child->parent=parent;
|
||||
parent->degree++;
|
||||
parent->child=_merge(parent->child,child);
|
||||
}
|
||||
|
||||
//
|
||||
void _unMarkAndUnParentAll(node* n) {
|
||||
if(n==NULL)return;
|
||||
node* c=n;
|
||||
do {
|
||||
c->marked=false;
|
||||
c->parent=NULL;
|
||||
c=c->next;
|
||||
}while(c!=n);
|
||||
}
|
||||
|
||||
node* _removeMinimum(node* n) {
|
||||
pos[n->v]=NULL;
|
||||
_unMarkAndUnParentAll(n->child);
|
||||
if(n->next==n) {
|
||||
n=n->child;
|
||||
} else {
|
||||
n->next->prev=n->prev;
|
||||
n->prev->next=n->next;
|
||||
n=_merge(n->next,n->child);
|
||||
|
||||
}
|
||||
if(n==NULL)return n;
|
||||
node* trees[64]={NULL};
|
||||
|
||||
while(true) {
|
||||
if(trees[n->degree]!=NULL) {
|
||||
node* t=trees[n->degree];
|
||||
if(t==n)break;
|
||||
trees[n->degree]=NULL;
|
||||
if(n->value<t->value) {
|
||||
t->prev->next=t->next;
|
||||
t->next->prev=t->prev;
|
||||
_addChild(n,t);
|
||||
} else {
|
||||
t->prev->next=t->next;
|
||||
t->next->prev=t->prev;
|
||||
if(n->next==n) {
|
||||
t->next=t->prev=t;
|
||||
_addChild(t,n);
|
||||
n=t;
|
||||
} else {
|
||||
n->prev->next=t;
|
||||
n->next->prev=t;
|
||||
t->next=n->next;
|
||||
t->prev=n->prev;
|
||||
_addChild(t,n);
|
||||
n=t;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
trees[n->degree]=n;
|
||||
}
|
||||
n=n->next;
|
||||
}
|
||||
node* min=n;
|
||||
do {
|
||||
if(n->value<min->value)min=n;
|
||||
n=n->next;
|
||||
} while(n!=n);
|
||||
return min;
|
||||
}
|
||||
|
||||
node* _cut(node* heap,node* n) {
|
||||
if(n->next==n) {
|
||||
n->parent->child=NULL;
|
||||
} else {
|
||||
n->next->prev=n->prev;
|
||||
n->prev->next=n->next;
|
||||
n->parent->child=n->next;
|
||||
}
|
||||
n->next=n->prev=n;
|
||||
n->marked=false;
|
||||
return _merge(heap,n);
|
||||
}
|
||||
|
||||
node* _decreaseKey(node* heap,node* n,int value) {
|
||||
if(n->value<value)return heap;
|
||||
n->value=value;
|
||||
if(n->parent!=NULL && (n->value < n->parent->value)) {
|
||||
heap=_cut(heap,n);
|
||||
node* parent=n->parent;
|
||||
n->parent=NULL;
|
||||
while(parent!=NULL && parent->marked) {
|
||||
heap=_cut(heap,parent);
|
||||
n=parent;
|
||||
parent=n->parent;
|
||||
n->parent=NULL;
|
||||
}
|
||||
if(parent!=NULL && parent->parent!=NULL)
|
||||
parent->marked=true;
|
||||
}
|
||||
node* temp=heap->next;
|
||||
//node* min_node=temp;
|
||||
while(temp!=heap){
|
||||
if(temp->value<heap->value)
|
||||
heap=temp;
|
||||
temp=temp->next;
|
||||
}
|
||||
//heap=min_node;
|
||||
return heap;
|
||||
}
|
||||
|
||||
node* _find(node* heap,int value) {
|
||||
node* n=heap;
|
||||
if(n==NULL)return NULL;
|
||||
do {
|
||||
if(n->value==value)return n;
|
||||
node* ret=_find(n->child,value);
|
||||
if(ret)return ret;
|
||||
n=n->next;
|
||||
}while(n!=heap);
|
||||
return NULL;
|
||||
}
|
||||
};
|
||||
129
graph.cpp
129
graph.cpp
|
|
@ -1,129 +0,0 @@
|
|||
/**
|
||||
define graph
|
||||
**/
|
||||
#include "graph.h"
|
||||
|
||||
//创建邻接表的节点
|
||||
struct AdjListNode* newAdjListNode(int dest, int weight) {
|
||||
struct AdjListNode* newNode = (struct AdjListNode*) malloc(
|
||||
sizeof(struct AdjListNode));
|
||||
newNode->dest = dest;
|
||||
newNode->weight = weight;
|
||||
newNode->next = NULL;
|
||||
return newNode;
|
||||
}
|
||||
|
||||
//创建一个图,包含V的顶点
|
||||
struct Graph* createGraph(int V) {
|
||||
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
|
||||
graph->V = V;
|
||||
//graph->E=0;
|
||||
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
|
||||
|
||||
for (int i = 0; i < V; ++i)
|
||||
graph->array[i].head = NULL;
|
||||
|
||||
return graph;
|
||||
}
|
||||
|
||||
// 添加一个边(无向图)
|
||||
void addEdge(struct Graph* graph, int src, int dest, int weight) {
|
||||
|
||||
struct AdjListNode* newNode = newAdjListNode(dest, weight);
|
||||
newNode->next = graph->array[src].head;
|
||||
graph->array[src].head = newNode;
|
||||
//graph->E++;
|
||||
//newNode = newAdjListNode(src, weight);
|
||||
//newNode->next = graph->array[dest].head;
|
||||
//graph->array[dest].head = newNode;
|
||||
}
|
||||
|
||||
//drop some vertexes from graph
|
||||
|
||||
struct Graph* drop_ver(struct Graph* graph,std::set<int> set_v){
|
||||
struct Graph* graph_copy=createGraph(graph->V);
|
||||
graph_copy->V=graph->V;
|
||||
|
||||
for(int i=0;i<graph->V;i++){
|
||||
// drop the edges form vertex i to others
|
||||
if(set_v.count(i)>0){
|
||||
|
||||
graph_copy->array[i].head=NULL;
|
||||
}
|
||||
|
||||
//drop the edges form others to vertex i
|
||||
else{
|
||||
graph_copy->array[i].head=graph->array[i].head;
|
||||
/*
|
||||
struct AdjListNode* temp=graph_copy->array[i].head;
|
||||
while(temp!=NULL && set_v.count(temp->dest)>0){
|
||||
graph_copy->array[i].head=temp->next;
|
||||
temp=temp->next;
|
||||
}
|
||||
while(temp!=NULL && temp->next!=NULL){
|
||||
struct AdjListNode* temp_next=temp->next;
|
||||
if(set_v.count(temp_next->dest)>0){
|
||||
temp->next=temp_next->next;
|
||||
}else{
|
||||
temp=temp->next;
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
return graph_copy;
|
||||
}
|
||||
|
||||
void print_path(struct Path* p,int V,int src){
|
||||
//int V = graph->V;
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : %d\n",src,i,p[i].dist);
|
||||
|
||||
}
|
||||
for(int i=0;i<V;++i)
|
||||
{
|
||||
printf("v%d - v%d : ",src,i);
|
||||
int j=i;
|
||||
while(p[j].pre!=src)
|
||||
{
|
||||
printf("%d ",p[j].pre);
|
||||
j=p[j].pre;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
}
|
||||
|
||||
// print path vertex
|
||||
void print_path_ver(struct Path* p,int src,int dest){
|
||||
//int V = graph->V;
|
||||
|
||||
int j=dest;
|
||||
while(p[j].pre!=src)
|
||||
{
|
||||
printf("%d ",p[j].pre);
|
||||
j=p[j].pre;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 打印图
|
||||
void print_graph(Graph* graph){
|
||||
for (int i=0;i<graph->V;i++){
|
||||
printf("v%d:\t",i);
|
||||
struct AdjListNode* temp=graph->array[i].head;
|
||||
while(temp!=NULL){
|
||||
printf("v%d:%d\t",temp->dest,temp->weight);
|
||||
temp=temp->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
}
|
||||
42
graph.h
42
graph.h
|
|
@ -1,42 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <iostream.h>
|
||||
#include<set>
|
||||
using namespace std;
|
||||
struct AdjListNode {
|
||||
int dest;
|
||||
int weight;
|
||||
struct AdjListNode* next;
|
||||
};
|
||||
|
||||
// 邻接表 结构体
|
||||
struct AdjList {
|
||||
struct AdjListNode *head; // 指向头节点
|
||||
};
|
||||
|
||||
// 图结构体,V为顶点个数。array为所有的邻接表
|
||||
struct Graph {
|
||||
int V;
|
||||
int E;
|
||||
struct AdjList* array;
|
||||
|
||||
};
|
||||
struct Path {
|
||||
int dist;
|
||||
int pre;
|
||||
};
|
||||
//创建邻接表的节点
|
||||
struct AdjListNode* newAdjListNode(int dest, int weight);
|
||||
|
||||
//创建一个图,包含V的顶点
|
||||
struct Graph* createGraph(int V);
|
||||
|
||||
// 添加一个边(无向图)
|
||||
void addEdge(struct Graph* graph, int src, int dest, int weight) ;
|
||||
|
||||
|
||||
struct Graph* drop_ver(struct Graph* graph,set<int> set_v);
|
||||
void print_path(struct Path* p,int V,int src);
|
||||
void print_path_ver(struct Path* p,int src,int dest);
|
||||
void print_graph(Graph* graph);
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
50
init.cpp
50
init.cpp
|
|
@ -1,50 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include<iostream.h>
|
||||
#define MAX 0x01111111
|
||||
struct Graph_matri{
|
||||
int **matri;
|
||||
int V;
|
||||
int E;
|
||||
};
|
||||
/**
|
||||
struct Graph_matri* read_graph_matrix(char* file_name){
|
||||
//int** matri_graph=new int[V][V];
|
||||
FILE *fp;
|
||||
struct Graph_matri *graph;
|
||||
// int **lines=new int[600][600];
|
||||
graph->matri=lines;
|
||||
if(!(fp=fopen(file_name,"rt")))
|
||||
{
|
||||
printf("´ò¿ªÎļþʧ°Ü£¡");
|
||||
return graph;
|
||||
}
|
||||
int v=0;
|
||||
int e=0;
|
||||
char line[9];
|
||||
int src=0,dist=0,wight=0;
|
||||
while(!feof(fp))
|
||||
{
|
||||
fgets(line,9,fp);
|
||||
src=(int)(line[2]-'0');
|
||||
dist=(int)(line[4]-'0');
|
||||
wight=(int)(line[6]-'0');
|
||||
if(lines[src][dist]>wight)
|
||||
{
|
||||
lines[src][dist]=wight;
|
||||
}
|
||||
if(src>v)
|
||||
v=src+1;
|
||||
e++;
|
||||
}
|
||||
graph->E=e;
|
||||
graph->V=v;
|
||||
fclose(fp);
|
||||
return graph;
|
||||
}
|
||||
|
||||
int main(){
|
||||
struct Graph_matri *graph=read_graph_matrix("E:\\projects\\c++\\topo.csv");
|
||||
printf("successed!");
|
||||
}
|
||||
|
||||
**/
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="mycodecraft" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/mycodecraft" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/mycodecraft" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="DFS.cpp" />
|
||||
<Unit filename="Genetic.cpp" />
|
||||
<Unit filename="chose_init_operator.cpp" />
|
||||
<Unit filename="dijstra_MinHeap.cpp" />
|
||||
<Unit filename="dijstra_MinHeap.h" />
|
||||
<Unit filename="dijstra_fibonacci.cpp" />
|
||||
<Unit filename="dijstra_fibonacci.h" />
|
||||
<Unit filename="fibonacci.h" />
|
||||
<Unit filename="graph.cpp" />
|
||||
<Unit filename="graph.h" />
|
||||
<Unit filename="header_file.cpp" />
|
||||
<Unit filename="init.cpp" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Extensions>
|
||||
<code_completion />
|
||||
<envvars />
|
||||
<debugger />
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
|
|
@ -1,138 +0,0 @@
|
|||
# depslib dependency file v1.0
|
||||
1458898798 source:e:\projects\c++\mycodecraft\chose_init_operator.cpp
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<limits.h>
|
||||
<iostream.h>
|
||||
|
||||
1459301069 source:e:\projects\c++\mycodecraft\dfs.cpp
|
||||
<iostream.h>
|
||||
|
||||
1459442414 source:e:\projects\c++\mycodecraft\dijstra_fibonacci.cpp
|
||||
"dijstra_fibonacci.h"
|
||||
"graph.h"
|
||||
"fibonacci.h"
|
||||
|
||||
1459440375 e:\projects\c++\mycodecraft\dijstra_fibonacci.h
|
||||
<iostream>
|
||||
|
||||
1459442286 e:\projects\c++\mycodecraft\graph.h
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<limits.h>
|
||||
<iostream.h>
|
||||
<set>
|
||||
|
||||
1459438754 e:\projects\c++\mycodecraft\fibonacci.h
|
||||
<iostream.h>
|
||||
|
||||
1459442430 source:e:\projects\c++\mycodecraft\dijstra_minheap.cpp
|
||||
"graph.h"
|
||||
"dijstra_MinHeap.h"
|
||||
<set>
|
||||
|
||||
1459440381 e:\projects\c++\mycodecraft\dijstra_minheap.h
|
||||
<iostream>
|
||||
|
||||
1458975742 source:e:\projects\c++\mycodecraft\genetic.cpp
|
||||
<stdio.h>
|
||||
<string.h>
|
||||
<stdlib.h>
|
||||
<math.h>
|
||||
<time.h>
|
||||
|
||||
1459438976 source:e:\projects\c++\mycodecraft\graph.cpp
|
||||
"graph.h"
|
||||
<set>
|
||||
|
||||
1459432578 source:e:\projects\c++\mycodecraft\header_file.cpp
|
||||
|
||||
1459432419 source:e:\projects\c++\mycodecraft\init.cpp
|
||||
<stdio.h>
|
||||
<iostream.h>
|
||||
|
||||
1459443462 source:e:\projects\c++\mycodecraft\main.cpp
|
||||
<iostream>
|
||||
|
||||
1459443440 e:\projects\c++\mycodecraft\shortpath.cpp
|
||||
"dijstra_MinHeap.h"
|
||||
"dijstra_fibonacci.h"
|
||||
"graph.h"
|
||||
"math.h"
|
||||
|
||||
1459443440 source:e:\projects\c++\mycodecraft\shortpath.cpp
|
||||
"dijstra_MinHeap.h"
|
||||
"dijstra_fibonacci.h"
|
||||
"graph.h"
|
||||
"math.h"
|
||||
|
||||
1459489918 source:e:\projects\c++\codecraft\chose_init_operator.cpp
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<limits.h>
|
||||
<iostream.h>
|
||||
|
||||
1460099949 source:e:\projects\c++\codecraft\dfs.cpp
|
||||
"dijstra_fibonacci.h"
|
||||
"graph.h"
|
||||
"dijstra_MinHeap.h"
|
||||
<set>
|
||||
"DFS.h"
|
||||
|
||||
1460045106 source:e:\projects\c++\codecraft\dijstra_fibonacci.cpp
|
||||
"dijstra_MinHeap.h"
|
||||
"dijstra_fibonacci.h"
|
||||
"graph.h"
|
||||
"fibonacci.h"
|
||||
|
||||
1459940523 e:\projects\c++\codecraft\dijstra_fibonacci.h
|
||||
<iostream.h>
|
||||
<set>
|
||||
|
||||
1460046633 e:\projects\c++\codecraft\graph.h
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<limits.h>
|
||||
<iostream.h>
|
||||
<set>
|
||||
|
||||
1460045037 e:\projects\c++\codecraft\fibonacci.h
|
||||
<iostream.h>
|
||||
|
||||
1460035894 source:e:\projects\c++\codecraft\dijstra_minheap.cpp
|
||||
"graph.h"
|
||||
"dijstra_MinHeap.h"
|
||||
<set>
|
||||
|
||||
1460035918 e:\projects\c++\codecraft\dijstra_minheap.h
|
||||
<iostream>
|
||||
<set>
|
||||
|
||||
1459489918 source:e:\projects\c++\codecraft\genetic.cpp
|
||||
<stdio.h>
|
||||
<string.h>
|
||||
<stdlib.h>
|
||||
<math.h>
|
||||
<time.h>
|
||||
|
||||
1460046634 source:e:\projects\c++\codecraft\graph.cpp
|
||||
"graph.h"
|
||||
|
||||
1459489918 source:e:\projects\c++\codecraft\header_file.cpp
|
||||
|
||||
1459489918 source:e:\projects\c++\codecraft\init.cpp
|
||||
<stdio.h>
|
||||
<iostream.h>
|
||||
|
||||
1459489918 source:e:\projects\c++\codecraft\main.cpp
|
||||
<iostream>
|
||||
|
||||
1459489918 source:e:\projects\c++\codecraft\shortpath.cpp
|
||||
"dijstra_MinHeap.h"
|
||||
"dijstra_fibonacci.h"
|
||||
"graph.h"
|
||||
"math.h"
|
||||
|
||||
1460099945 e:\projects\c++\codecraft\dfs.h
|
||||
<set>
|
||||
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_layout_file>
|
||||
<ActiveTarget name="Debug" />
|
||||
<File name="DFS.cpp" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="1136" topLine="239" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="main.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="21" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="graph.h" open="0" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="640" topLine="22" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="dijstra_MinHeap.cpp" open="0" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="5523" topLine="199" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="dijstra_fibonacci.cpp" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="6422" topLine="178" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="graph.cpp" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="1906" topLine="61" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="fibonacci.h" open="0" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="5963" topLine="237" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="dijstra_MinHeap.h" open="0" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="210" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
||||
BIN
obj/Debug/DFS.o
BIN
obj/Debug/DFS.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
obj/Debug/init.o
BIN
obj/Debug/init.o
Binary file not shown.
BIN
obj/Debug/main.o
BIN
obj/Debug/main.o
Binary file not shown.
Binary file not shown.
|
|
@ -1,48 +0,0 @@
|
|||
#include "dijstra_MinHeap.h"
|
||||
#include "dijstra_fibonacci.h"
|
||||
#include "graph.h"
|
||||
#include "math.h"
|
||||
|
||||
|
||||
Graph* getGraph(){
|
||||
int V = 9;
|
||||
struct Graph* graph = createGraph(V);
|
||||
addEdge(graph, 0, 1, 4);
|
||||
addEdge(graph, 0, 7, 8);
|
||||
addEdge(graph, 1, 2, 8);
|
||||
addEdge(graph, 1, 7, 11);
|
||||
addEdge(graph, 2, 3, 7);
|
||||
addEdge(graph, 2, 8, 2);
|
||||
addEdge(graph, 2, 5, 4);
|
||||
addEdge(graph, 3, 4, 9);
|
||||
// addEdge(graph, 3, 5, 14);
|
||||
addEdge(graph, 4, 5, 10);
|
||||
addEdge(graph, 5, 6, 2);
|
||||
addEdge(graph, 6, 7, 1);
|
||||
addEdge(graph, 6, 8, 6);
|
||||
addEdge(graph, 7, 8, 7);
|
||||
return graph;
|
||||
}
|
||||
struct Path* shor_path(Graph *graph,int src){
|
||||
struct Path* p;
|
||||
// if E=o(V*V/lgV) then MinHeap is better! Otherwise fibonacci Heap is better!
|
||||
if (graph->E < graph->V*graph->V/log(graph->V))
|
||||
{
|
||||
p=dijkstra(graph,src);
|
||||
}
|
||||
else{
|
||||
p=dijkstra_fibonacci(graph,src);
|
||||
}
|
||||
print_path(p,graph->V,src);
|
||||
return p;
|
||||
}
|
||||
void test_shortpath(){
|
||||
shor_path(getGraph(),0);
|
||||
|
||||
}
|
||||
|
||||
int main(){
|
||||
printf("start\n");
|
||||
test_shortpath();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue