Analysis Software
Documentation for
sPHENIX
simulation software
Home page
Related Pages
Modules
Namespaces
Classes
Files
Examples
External Links
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
bellman_ford.cpp
Go to the documentation of this file.
Or view
the newest version in sPHENIX GitHub for file bellman_ford.cpp
1
/* This software is distributed under the GNU Lesser General Public License */
2
//==========================================================================
3
//
4
// bellman_ford.cpp
5
//
6
//==========================================================================
7
// $Id: bellman_ford.cpp,v 1.4 2003/01/30 17:50:56 raitner Exp $
8
9
#include <
GTL/bellman_ford.h
>
10
11
#ifdef __GTL_MSVCC
12
# ifdef _DEBUG
13
# ifndef SEARCH_MEMORY_LEAKS_ENABLED
14
# error SEARCH NOT ENABLED
15
# endif
16
# define new DEBUG_NEW
17
# undef THIS_FILE
18
static
char
THIS_FILE[] = __FILE__;
19
# endif // _DEBUG
20
#endif // __GTL_MSVCC
21
22
__GTL_BEGIN_NAMESPACE
23
24
bellman_ford::bellman_ford
()
25
{
26
vars_set
=
false
;
27
preds
= 0;
28
}
29
30
bellman_ford::~bellman_ford
()
31
{
32
if
(
preds
)
delete
preds
;
33
}
34
35
void
bellman_ford::store_preds
(
bool
set
)
36
{
37
if
(
set
&& !
preds
) {
38
preds
=
new
node_map<edge>
;
39
}
else
if
(!
set
&&
preds
) {
40
delete
preds
;
41
preds
= 0;
42
}
43
}
44
45
46
int
bellman_ford::check
(
graph
& G)
47
{
48
if
(!
vars_set
)
49
{
50
return
algorithm::GTL_ERROR
;
51
}
52
53
if
(G.
nodes_begin
() == G.
nodes_end
())
54
{
55
return
algorithm::GTL_ERROR
;
56
}
57
58
return
algorithm::GTL_OK
;
59
}
60
61
int
bellman_ford::run
(
graph
& G)
62
{
63
if
(
s
==
node
())
64
{
65
s
= *(G.
nodes_begin
());
66
}
67
68
//----------------------------------------------------------------------
69
// initialize
70
//----------------------------------------------------------------------
71
72
inf
.
init
(G,
true
);
73
74
if
(
preds
) {
75
preds
->
init
(G,
edge
());
76
}
77
78
inf
[
s
] =
false
;
79
d
[
s
] = 0;
80
cycle
=
false
;
81
82
//----------------------------------------------------------------------
83
// relax
84
//----------------------------------------------------------------------
85
86
graph::edge_iterator
it
,
end
;
87
88
for
(
int
i
= 1;
i
< G.
number_of_nodes
(); ++
i
)
89
{
90
for
(it = G.
edges_begin
(), end = G.
edges_end
(); it !=
end
; ++
it
)
91
{
92
relax
(*it,
true
);
93
94
if
(G.
is_undirected
())
95
{
96
relax
(*it,
false
);
97
}
98
}
99
}
100
101
//----------------------------------------------------------------------
102
// cycle detection
103
//----------------------------------------------------------------------
104
105
for
(it = G.
edges_begin
(), end = G.
edges_end
(); it !=
end
; ++
it
)
106
{
107
node
u
= (*it).source();
108
node
v
= (*it).target();
109
110
if
(!
inf
[u] && !
inf
[v])
111
{
112
if
(
d
[v] >
d
[u] +
w
[*it])
113
{
114
cycle
=
true
;
115
}
116
}
117
}
118
119
return
algorithm::GTL_OK
;
120
}
121
122
void
bellman_ford::reset
()
123
{
124
}
125
126
void
bellman_ford::relax
(
const
edge
&
e
,
bool
dir )
127
{
128
node
u
= e.
source
();
129
node
v
= e.
target
();
130
131
if
(!dir) {
132
node
tmp
=
u
;
133
u =
v
;
134
v =
tmp
;
135
}
136
137
if
(!
inf
[u] && (
inf
[v] || (
d
[v] >
d
[u] +
w
[e])))
138
{
139
d
[
v
] =
d
[
u
] +
w
[
e
];
140
inf
[
v
] =
false
;
141
142
if
(
preds
)
143
{
144
(*preds)[
v
] =
e
;
145
}
146
}
147
}
148
149
150
151
__GTL_END_NAMESPACE
JETSCAPE
blob
main
external_packages
gtl
src
bellman_ford.cpp
Built by
Jin Huang
. updated:
Sat Feb 17 2024 22:18:23
using
1.8.2 with
sPHENIX GitHub integration