-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathford_bellman.cpp
101 lines (98 loc) · 1.94 KB
/
ford_bellman.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include "constant.h"
/*
void init_d(double d[], int trace[], int n, pGraph graph, int S)
{
pNode temp=graph->lists[S]->next;
for (int i=1; i<=n; i++) d[i]=MAXC;
d[S]=0;
while (temp!=NULL)
{
d[temp->vertex]=temp->cost;//distance firstly is cost
temp=temp->next;
}
for (int i=1; i<=n; i++)
{
trace[i]=S;//initial path to other vertices is from starting vertex
}
}
int Ford_Bellman(double d[], int trace[], pGraph graph, int S)
{
int n=graph->numVertices;
int CountLoop = 0;
int i, j, Stop;
pNode temp;
init_d(d,trace,n,graph,S);
do {
Stop = 1;
for (i = 1; i <= n; i++)
{
temp=graph->lists[i]->next;
while (temp!=NULL)
{
j=temp->vertex;
if (d[j] > (d[i] + temp->cost))
{
d[j] = d[i] + temp->cost;
trace[j] = i;
Stop = 0;
}
temp=temp->next;
}
}
CountLoop++;
} while (!Stop && CountLoop < n - 2);
//check for negative circle
for (i = 1; i <= n; i++)
{
temp=graph->lists[i]->next;
while (temp!=NULL)
{
j=temp->vertex;
if (d[j] > (d[i] + temp->cost)) return 0;
temp=temp->next;
}
}
return 1;
}
*/
int Ford_Bellman2(double d[], int trace[], pGraph graph, int S)
{
int n=graph->numVertices;
int u,v,front=0,rear=0,count=0;
int Queue[n];
int InQueue[n+1]={0};//check if u exists in Queue
int check[n+1]={0};//check how many times a path is upgraded
pNode temp;
for (int i=1; i<=n; i++) d[i]=MAXC;
d[S]=0;
Queue[0]=S;//Firstly add S to Queue
InQueue[S]=1;
do
{
u=Queue[front];//DeQueue
InQueue[u]=0;
front=(front+1)%n;
temp=graph->lists[u]->next;
while (temp!=NULL)
{
v=temp->vertex;
if (d[v] > (d[u] + temp->cost))
{
d[v] = d[u] + temp->cost;
trace[v] = u;
if (check[v]==n) return 0;//check for negative circle
else ++check[v];
if (InQueue[v]==0)
{
//EnQueue
rear=(rear+1)%n;
Queue[rear]=v;
InQueue[v]=1;
}
}
temp=temp->next;
}
} while (front != (rear+1)%n);
return 1;
}