Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
psco-2019-gp
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Heinrichs
psco-2019-gp
Commits
772efa7c
Commit
772efa7c
authored
5 years ago
by
p-hamann
Browse files
Options
Downloads
Patches
Plain Diff
Adjust member identifiers and datatype of edge indices
parent
68420fe1
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bnb/edmonds_karp.cpp
+30
-30
30 additions, 30 deletions
bnb/edmonds_karp.cpp
include/gp-bnb/edmonds_karp.hpp
+7
-7
7 additions, 7 deletions
include/gp-bnb/edmonds_karp.hpp
with
37 additions
and
37 deletions
bnb/edmonds_karp.cpp
+
30
−
30
View file @
772efa7c
...
...
@@ -5,9 +5,9 @@
#include
<gp-bnb/edmonds_karp.hpp>
edmonds_karp
::
edmonds_karp
(
const
graph
&
g
,
node_id
source
,
node_id
sink
)
:
g
(
g
),
source
(
source
),
sink
(
sink
)
{
assert
(
source
>
0
);
// Nodes have ids: 1, .., n
assert
(
source
<=
g
.
num_nodes
());
:
g
_
(
g
),
source
_
(
source
),
sink
_
(
sink
)
{
assert
(
source
_
>
0
);
// Nodes have ids: 1, .., n
assert
(
source
_
<=
g
.
num_nodes
());
};
/* Indexes edges of the graph
...
...
@@ -15,22 +15,22 @@ Every edge in the graph is mapped to its unique id
indexed_edges std::map consists of key: pair<node_id, node_id> value: edge_id */
void
edmonds_karp
::
index_edges
()
{
unsigned
int
num_nodes
=
g
.
num_nodes
();
unsigned
int
num_nodes
=
g
_
.
num_nodes
();
unsigned
int
eid
=
0
;
for
(
node_id
u
=
1
;
u
<=
num_nodes
;
u
++
)
{
std
::
vector
<
node_id
>
neighbors
=
g
.
get_adjacency
(
u
);
std
::
vector
<
node_id
>
neighbors
=
g
_
.
get_adjacency
(
u
);
for
(
node_id
v
:
neighbors
)
{
auto
node_pair
=
std
::
make_pair
(
u
,
v
);
// edge was already listed
if
(
indexed_edges
.
count
(
std
::
make_pair
(
v
,
u
))
==
1
)
{
indexed_edges
[
node_pair
]
=
indexed_edges
.
at
(
std
::
make_pair
(
v
,
u
));
if
(
indexed_edges
_
.
count
(
std
::
make_pair
(
v
,
u
))
==
1
)
{
indexed_edges
_
[
node_pair
]
=
indexed_edges
_
.
at
(
std
::
make_pair
(
v
,
u
));
}
else
{
indexed_edges
[
node_pair
]
=
eid
;
indexed_edges
_
[
node_pair
]
=
eid
;
eid
++
;
}
}
...
...
@@ -42,32 +42,32 @@ When the sink is reached gain is added to gain[sink] */
int
edmonds_karp
::
bfs
(
std
::
vector
<
int
>
&
resid_flow
,
std
::
vector
<
unsigned
int
>
&
pred
)
const
{
pred
.
clear
();
pred
.
resize
(
g
.
num_nodes
()
+
1
,
-
1
);
// undiscovered nodes are marked with -1
std
::
vector
<
int
>
gain
(
g
.
num_nodes
(),
0.0
);
pred
.
resize
(
g
_
.
num_nodes
()
+
1
,
-
1
);
// undiscovered nodes are marked with -1
std
::
vector
<
int
>
gain
(
g
_
.
num_nodes
(),
0.0
);
std
::
queue
<
node_id
>
q
;
q
.
push
(
source
);
pred
[
source
]
=
source
;
gain
[
source
]
=
std
::
numeric_limits
<
int
>::
max
();
q
.
push
(
source
_
);
pred
[
source
_
]
=
source
_
;
gain
[
source
_
]
=
std
::
numeric_limits
<
int
>::
max
();
while
(
!
q
.
empty
())
{
node_id
u
=
q
.
front
();
q
.
pop
();
bool
sink_reached
=
false
;
std
::
vector
<
node_id
>
neighbors
=
g
.
get_adjacency
(
u
);
std
::
vector
<
node_id
>
neighbors
=
g
_
.
get_adjacency
(
u
);
// iterate through neighbors of u
for
(
node_id
v
:
neighbors
)
{
unsigned
int
edge_id
=
indexed_edges
.
at
(
std
::
make_pair
(
u
,
v
));
unsigned
int
edge_id
=
indexed_edges
_
.
at
(
std
::
make_pair
(
u
,
v
));
int
edge_weight
=
1
;
// unweighted graph
if
(((
u
>=
v
&&
flow
[
edge_id
]
<
edge_weight
)
if
(((
u
>=
v
&&
flow
_
[
edge_id
]
<
edge_weight
)
||
(
u
<
v
&&
resid_flow
[
edge_id
]
<
edge_weight
))
&&
pred
[
v
]
==
(
unsigned
int
)
-
1
)
{
// only add those neighbors with rest capacity and which were not discovered yet
pred
[
v
]
=
u
;
gain
[
v
]
=
std
::
min
(
gain
[
u
],
edge_weight
-
(
u
>=
v
?
flow
[
edge_id
]
:
resid_flow
[
edge_id
]));
if
(
v
!=
sink
&&
!
sink_reached
)
{
gain
[
v
]
=
std
::
min
(
gain
[
u
],
edge_weight
-
(
u
>=
v
?
flow
_
[
edge_id
]
:
resid_flow
[
edge_id
]));
if
(
v
!=
sink
_
&&
!
sink_reached
)
{
q
.
push
(
v
);
}
else
{
sink_reached
=
true
;
...
...
@@ -76,7 +76,7 @@ int edmonds_karp::bfs(std::vector<int> &resid_flow, std::vector<unsigned int> &p
}
if
(
sink_reached
)
{
return
gain
[
sink
];
return
gain
[
sink
_
];
}
}
return
0.0
;
...
...
@@ -92,13 +92,13 @@ Step 3: Add gain that was calculated during BFS to the flow value. Break from th
Step 4: Update flow and residual flow values for each node */
void
edmonds_karp
::
run
()
{
index_edges
();
int
num_edges
=
indexed_edges
.
size
()
/
2
;
int
num_edges
=
indexed_edges
_
.
size
()
/
2
;
flow
.
clear
();
flow
.
resize
(
num_edges
,
0.0
);
flow
_
.
clear
();
flow
_
.
resize
(
num_edges
,
0.0
);
std
::
vector
<
int
>
resid_flow
(
num_edges
,
0.0
);
flow_value
=
0
;
flow_value
_
=
0
;
while
(
true
)
{
std
::
vector
<
node_id
>
pred
;
...
...
@@ -109,18 +109,18 @@ void edmonds_karp::run() {
if
(
gain
==
0
)
break
;
// Add gain that was calculated during BFS to the flow value
flow_value
+=
gain
;
node_id
v
=
sink
;
flow_value
_
+=
gain
;
node_id
v
=
sink
_
;
// Update flow and residual flow values for each edge
while
(
v
!=
source
)
{
while
(
v
!=
source
_
)
{
node_id
u
=
pred
[
v
];
int
edge_id
=
indexed_edges
[
std
::
make_pair
(
u
,
v
)];
int
edge_id
=
indexed_edges
_
[
std
::
make_pair
(
u
,
v
)];
if
(
u
>=
v
)
{
flow
[
edge_id
]
+=
gain
;
flow
_
[
edge_id
]
+=
gain
;
resid_flow
[
edge_id
]
-=
gain
;
}
else
{
flow
[
edge_id
]
-=
gain
;
flow
_
[
edge_id
]
-=
gain
;
resid_flow
[
edge_id
]
+=
gain
;
}
v
=
u
;
...
...
@@ -129,5 +129,5 @@ void edmonds_karp::run() {
};
int
edmonds_karp
::
get_max_flow
()
const
{
return
flow_value
;
return
flow_value
_
;
};
This diff is collapsed.
Click to expand it.
include/gp-bnb/edmonds_karp.hpp
+
7
−
7
View file @
772efa7c
...
...
@@ -7,15 +7,15 @@
class
edmonds_karp
{
private:
const
graph
&
g
;
const
graph
&
g
_
;
std
::
map
<
std
::
pair
<
node_id
,
node_id
>
,
int
>
indexed_edges
;
std
::
map
<
std
::
pair
<
node_id
,
node_id
>
,
unsigned
int
>
indexed_edges
_
;
node_id
source
;
node_id
sink
;
node_id
source
_
;
node_id
sink
_
;
int
flow_value
;
std
::
vector
<
int
>
flow
;
int
flow_value
_
;
std
::
vector
<
int
>
flow
_
;
/**
* Indexes edges of the graph
...
...
@@ -53,4 +53,4 @@ public:
int
get_max_flow
()
const
;
};
#endif
/* EDMONDSKARP_H_ */
\ No newline at end of file
#endif
/* EDMONDSKARP_H_ */
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment