-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.h
More file actions
104 lines (83 loc) · 2.93 KB
/
Network.h
File metadata and controls
104 lines (83 loc) · 2.93 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
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
102
103
104
#ifndef NETWORK_H_INCLUDED
#define NETWORK_H_INCLUDED
#include <cstddef>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <tuple>
#include "Edge.h"
#include "Circle.h"
//custom comparator for (nodeId, nodeId, isResidual)
//needed for edge storage
struct custComp {
bool operator() (const std::tuple<size_t, size_t, bool>& edge0, const std::tuple<size_t, size_t, bool>& edge1) const {
if (std::get<0>(edge0) != std::get<0>(edge1)) {
return std::get<0>(edge0) < std::get<0>(edge1);
}
if (std::get<1>(edge0) != std::get<1>(edge1)) {
return std::get<1>(edge0) < std::get<1>(edge1);
}
return std::get<2>(edge0) < std::get<2>(edge1);
}
};
struct Node {
size_t id;
intmax_t b_value;
//defined by (the other) nodeID
//always incoming and outgoing due to residual edges
std::set<size_t> neighbours;
Node (size_t _id, intmax_t _b_value) : id(_id), b_value(_b_value) {};
};
//no parallel (nonresidual) edges are allowed
class Network {
public:
Network(size_t noOfNodes);
void print();
//adds randomized values to all capacities
void randomNoise(double phi);
//return 0 if there’s a parallel edge
//adds residual edge
bool addEdge(Edge e);
//returns nodeID
//used to add sources and sinks
size_t addNode(intmax_t b_value = 0);
//changes b_value
bool changeBvalue(size_t a, intmax_t b);
size_t getNoOfNodes () const;
size_t getNoOfEdges () const;
const std::map<std::tuple<size_t, size_t, bool>, Edge, custComp>& getEdges() const;
const std::map<size_t, Node, std::less<size_t>>& getNodes() const;
//fails and returns 0 if there’s flow left
bool deleteEdge(size_t node0, size_t node1);
//fails and returns 0 if there’s flow on an edge to this node left
bool deleteNode(size_t nodeID);
//both functions fail and return 0 if not possible
//takes a path from a source to a sink
//doesn't use residual edges
bool addFlow(std::vector<size_t>& path, intmax_t flow);
//takes a circle
bool changeFlow(Circle& c, intmax_t flow);
//toggles all edges
void toggleCost();
//clears network from all flow
void clean();
//following both functions are used to get a random entry
//gets the real NodeId of the n-th node
size_t getNode(size_t index);
//gets the n-th Edge
Edge getEdge(size_t index);
intmax_t getFlow() {return flow;}
intmax_t getCost() {return cost;}
//private:
intmax_t sumSource = 0, sumSink = 0;
size_t largestNodeID;
std::vector<size_t> sources, sinks, transit;
private:
intmax_t flow = 0, cost = 0;
std::map<std::tuple<size_t, size_t, bool>, Edge, custComp> edges;
std::map<size_t, Node, std::less<size_t>> nodes;
void deleteEdge(std::tuple<size_t, size_t, bool>& e);
std::tuple<size_t, size_t, bool> invertKey (const std::tuple<size_t, size_t, bool>& key);
};
#endif // NETWORK_H_INCLUDED