-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
63 lines (52 loc) · 1.4 KB
/
graph.cpp
File metadata and controls
63 lines (52 loc) · 1.4 KB
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
/* graph.cpp */
/* Vladimir Kolmogorov (vnk@cs.cornell.edu), 2001. */
#include <stdio.h>
#include "graph.h"
Graph::Graph(void (*err_function)(char *))
{
error_function = err_function;
node_block = new Block<node>(NODE_BLOCK_SIZE, error_function);
arc_block = new Block<arc>(NODE_BLOCK_SIZE, error_function);
flow = 0;
}
Graph::~Graph()
{
delete node_block;
delete arc_block;
}
Graph::node_id Graph::add_node()
{
node *i = node_block -> New();
i -> first = NULL;
i -> tr_cap = 0;
return (node_id) i;
}
void Graph::add_edge(node_id from, node_id to, captype cap, captype rev_cap)
{
arc *a, *a_rev;
a = arc_block -> New(2);
a_rev = a + 1;
a -> sister = a_rev;
a_rev -> sister = a;
a -> next = ((node*)from) -> first;
((node*)from) -> first = a;
a_rev -> next = ((node*)to) -> first;
((node*)to) -> first = a_rev;
a -> head = (node*)to;
a_rev -> head = (node*)from;
a -> r_cap = cap;
a_rev -> r_cap = rev_cap;
}
void Graph::set_tweights(node_id i, captype cap_source, captype cap_sink)
{
flow += (cap_source < cap_sink) ? cap_source : cap_sink;
((node*)i) -> tr_cap = cap_source - cap_sink;
}
void Graph::add_tweights(node_id i, captype cap_source, captype cap_sink)
{
register captype delta = ((node*)i) -> tr_cap;
if (delta > 0) cap_source += delta;
else cap_sink -= delta;
flow += (cap_source < cap_sink) ? cap_source : cap_sink;
((node*)i) -> tr_cap = cap_source - cap_sink;
}