Compare commits

...

3 Commits

Author SHA1 Message Date
ladventure 1e51037595 test 2016-07-02 22:58:20 +08:00
lili 9e031ce6ed finish DFS 2016-04-08 15:25:06 +08:00
lili e136853077 delete codecraft 2016-04-01 13:52:31 +08:00
26 changed files with 1057 additions and 59 deletions

262
DFS.cpp
View File

@ -1,13 +1,259 @@
/**
**/
#include<iostream.h>
struct Tree{
int v; // number of childrens
struct Tree* next;
};
/**
create_tree(){
#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 Normal file
View File

@ -0,0 +1,22 @@
#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();

BIN
bin/Debug/mycodecraft.exe Normal file

Binary file not shown.

@ -1 +0,0 @@
Subproject commit bdfd132dcd2d97fb4d5054bfa298da012994431e

View File

@ -216,27 +216,123 @@ struct Path* dijkstra(struct Graph* graph, int src) {
}
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, 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);
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);
dijkstra(graph, 1);
print_path(dijkstra(graph, 1),V,1);
set<int> s;
s.insert(3);
// print_graph(graph);
@ -250,5 +346,5 @@ int main() {
test();
return 0;
}
*/
*/

View File

@ -1,2 +1,6 @@
#include <iostream>
struct Path* dijkstra(struct Graph* graph, int src);
#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);

BIN
dijstra_MinHeap.o Normal file

Binary file not shown.

View File

@ -21,11 +21,12 @@ BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUEN
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;
/*
@ -50,10 +51,118 @@ void print_path(struct Path* p,int V,int src){
}
*/
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;
@ -76,10 +185,11 @@ struct Path* dijkstra_fibonacci(struct Graph* graph, int src) {
// minHeap->size = V;
// 这个循环中minHeap包含的是所有未在SPT中的顶点
while (!h.isEmpty()) {
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;
@ -97,33 +207,40 @@ struct Path* dijkstra_fibonacci(struct Graph* graph, int src) {
pCrawl = pCrawl->next;
}
}
// print_path(p,V,src);
//print_path(p,V,src);
return p;
}
/*
void test() {
int V = 9;
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, 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);
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;
}
*/

View File

@ -0,0 +1,246 @@
/*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;
}

View File

@ -1,3 +1,7 @@
#include <iostream>
#include <iostream.h>
#include<set>
using namespace std;
struct Path* dijkstra_fibonacci(struct Graph* graph, int src);
//print_path(struct Path* p,int V,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);

View File

@ -243,8 +243,17 @@ private:
parent=n->parent;
n->parent=NULL;
}
if(parent!=NULL && parent->parent!=NULL)parent->marked=true;
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;
}

View File

@ -2,9 +2,7 @@
define graph
**/
#include "graph.h"
#include<set>
using namespace std;
//´´½¨ÁÚ½Ó±íµÄ½Úµã
struct AdjListNode* newAdjListNode(int dest, int weight) {
struct AdjListNode* newNode = (struct AdjListNode*) malloc(
@ -19,7 +17,7 @@ struct AdjListNode* newAdjListNode(int dest, int weight) {
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
graph->E=0;
//graph->E=0;
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; ++i)
@ -34,7 +32,7 @@ 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++;
//graph->E++;
//newNode = newAdjListNode(src, weight);
//newNode->next = graph->array[dest].head;
//graph->array[dest].head = newNode;
@ -42,17 +40,21 @@ void addEdge(struct Graph* graph, int src, int dest, int weight) {
//drop some vertexes from graph
struct Graph* drop_ver(struct Graph* graph,set<int> set_v){
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;
@ -67,6 +69,7 @@ struct Graph* drop_ver(struct Graph* graph,set<int> set_v){
}
}
*/
}
}
@ -84,7 +87,7 @@ void print_path(struct Path* p,int V,int src){
{
printf("v%d - v%d : ",src,i);
int j=i;
while(p[j].pre!=0)
while(p[j].pre!=src)
{
printf("%d ",p[j].pre);
j=p[j].pre;
@ -92,6 +95,21 @@ void print_path(struct Path* p,int V,int src){
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;
}
}
@ -108,4 +126,4 @@ void print_graph(Graph* graph){
}
printf("\n");
printf("\n");
}
}

View File

@ -34,9 +34,9 @@ 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_path_ver(struct Path* p,int src,int dest);
void print_graph(Graph* graph);

55
mycodecraft.cbp Normal file
View File

@ -0,0 +1,55 @@
<?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>

138
mycodecraft.depend Normal file
View File

@ -0,0 +1,138 @@
# 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>

44
mycodecraft.layout Normal file
View File

@ -0,0 +1,44 @@
<?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 Normal file

Binary file not shown.

BIN
obj/Debug/Genetic.o Normal file

Binary file not shown.

Binary file not shown.

BIN
obj/Debug/dijstra_MinHeap.o Normal file

Binary file not shown.

Binary file not shown.

BIN
obj/Debug/graph.o Normal file

Binary file not shown.

BIN
obj/Debug/header_file.o Normal file

Binary file not shown.

BIN
obj/Debug/init.o Normal file

Binary file not shown.

BIN
obj/Debug/main.o Normal file

Binary file not shown.

BIN
obj/Debug/shortpath.o Normal file

Binary file not shown.