Compare commits
2 Commits
lili_branc
...
master
| Author | SHA1 | Date |
|---|---|---|
|
|
90647bc3d1 | |
|
|
3991b1cd4c |
13
DFS.cpp
13
DFS.cpp
|
|
@ -1,13 +0,0 @@
|
|||
/**
|
||||
用深度优先搜索的方式遍历所以可能路径,找到最优解
|
||||
**/
|
||||
#include<iostream.h>
|
||||
struct Tree{
|
||||
int v; // number of childrens
|
||||
struct Tree* next;
|
||||
|
||||
};
|
||||
/**
|
||||
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;
|
||||
}
|
||||
*/
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <iostream.h>
|
||||
|
||||
|
|
@ -1 +0,0 @@
|
|||
Subproject commit bdfd132dcd2d97fb4d5054bfa298da012994431e
|
||||
|
|
@ -1,254 +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;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void test(){
|
||||
// 创建图
|
||||
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);
|
||||
|
||||
dijkstra(graph, 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,2 +0,0 @@
|
|||
#include <iostream>
|
||||
struct Path* dijkstra(struct Graph* graph, int src);
|
||||
|
|
@ -1,129 +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_fibonacci.h"
|
||||
#include "graph.h"
|
||||
#include "fibonacci.h"
|
||||
#define INT_MAX 2147483647
|
||||
//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");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
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;
|
||||
//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()) {
|
||||
// 取得堆顶节点,即最小距离的顶点
|
||||
// struct MinHeapNode* minHeapNode = extractMin(minHeap);
|
||||
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;
|
||||
}
|
||||
/*
|
||||
void test() {
|
||||
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);
|
||||
dijkstra_fibonacci(graph, 1);
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
test();
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
#include <iostream>
|
||||
struct Path* dijkstra_fibonacci(struct Graph* graph, int src);
|
||||
//print_path(struct Path* p,int V,int src);
|
||||
262
fibonacci.h
262
fibonacci.h
|
|
@ -1,262 +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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
111
graph.cpp
111
graph.cpp
|
|
@ -1,111 +0,0 @@
|
|||
/**
|
||||
define graph
|
||||
**/
|
||||
#include "graph.h"
|
||||
#include<set>
|
||||
|
||||
using namespace std;
|
||||
//创建邻接表的节点
|
||||
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,set<int> set_v){
|
||||
struct Graph* graph_copy=createGraph(graph->V);
|
||||
graph_copy->V=graph->V;
|
||||
|
||||
for(int i=0;i<graph->V;i++){
|
||||
if(set_v.count(i)>0){
|
||||
|
||||
graph_copy->array[i].head=NULL;
|
||||
}
|
||||
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!=0)
|
||||
{
|
||||
printf("%d ",p[j].pre);
|
||||
j=p[j].pre;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 打印图
|
||||
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 AdjListNode* newAdjListNode(int dest, int weight);
|
||||
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_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,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