-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadj_list.h
213 lines (173 loc) · 5.03 KB
/
adj_list.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Created by yoonsikjung on 2022/03/21.
//
#ifndef NETWORK_OPTIMIZATION_ADJ_LIST_H
#define NETWORK_OPTIMIZATION_ADJ_LIST_H
#include <vector>
#include <tuple>
// assume all parameters are integer.
class Edge;
class Node;
class Edge {
public:
int head;
int cost;
int lb;
int ub;
Edge *next;
};
class Node {
public:
int nodeId;
int capacity;
int order;
Edge *next;
};
class AdjacencyList {
std::vector<Node *> nodes;
// int N; // number of nodes
// int M; // number of edges
public:
AdjacencyList() = default;
// AdjacencyList(int N);
AdjacencyList(std::vector<std::tuple<int, int>> edges, bool isDirected) {
if (isDirected) {
for (auto i: edges) {
addDirectedEdge(std::get<0>(i), std::get<1>(i));
}
} else {
for (auto i: edges) {
addUnDirectedEdge(std::get<0>(i), std::get<1>(i));
}
}
};
Edge *findLastEdge(Edge *edge) {
if (edge->next == NULL) {
return edge;
} else {
return findLastEdge((edge)->next);
}
}
void addEdgeToNode(Node *node, Edge *edge) {
if (node->next == NULL) {
node->next = edge;
} else {
auto end = findLastEdge((node->next));
end->next = edge;
}
}
Node *findNode(int nodeId) {
auto first = nodes.begin();
auto last = nodes.end();
for (; first != last; ++first) {
if ((**first).nodeId == nodeId) {
return *first;
}
}
return NULL;
}
void addDirectedEdge(int tail, int head, int cost = 1, int lb = 0, int ub = 0) {
// if node_t not exist, add node_t
Node *node_t = findNode(tail);
Node *node_h = findNode(head);
if (node_t == NULL) {
node_t = new Node{tail, 0, NULL};
nodes.push_back(node_t);
}
if (node_h == NULL) {
node_h = new Node{head, 0, NULL};
nodes.push_back(node_h);
}
// generate edge instance
auto edge_t = new Edge{head, cost, lb, ub, NULL};
// connect to node_t
addEdgeToNode(node_t, edge_t);
}
void addUnDirectedEdge(int u, int v, int cost = 1, int lb = 0, int ub = 0) {
Node *node_u = findNode(u);
Node *node_v = findNode(v);
// if node not exist, add node
if (node_u == NULL) {
node_u = new Node{u, 0, NULL};
nodes.push_back(node_u);
}
if (node_v == NULL) {
node_v = new Node{v, 0, NULL};
nodes.push_back(node_v);
}
// u -> v
// generate edge instance
auto edge_u = new Edge{v, cost, lb, ub, NULL};
// connect to node_u
node_u->next = edge_u;
// v -> u
// generate edge instance
auto edge_v = new Edge{v, cost, lb, ub, NULL};
// connect to node
node_v->next = edge_v;
}
void printNodes() {
for (auto i: nodes) {
std::cout << (*i).nodeId << "'s t_order :" << (*i).order << std::endl;
}
}
void printEdges() {
for (auto i: nodes) {
auto cursor = i->next;
std::cout << (*i).nodeId << ":";
while (cursor != NULL) {
std::cout << (*cursor).head << " -> ";
cursor = cursor->next;
}
std::cout << std::endl;
}
}
void topologicalOrdering() {
std::vector<int> bag; // node container that already ordered
int CUR = 1; // topological ordering starts from 1
size_t n_size = nodes.size();
for (int i = 0; i < n_size; i++) {
for (auto n: nodes) {
if (n->order != 0)
continue;
if (checkIndegree(n->nodeId)) { // indegree exists
continue;
} else { // indegree not exists
n->order = CUR;
n->next = NULL; // temporally handling method required
bag.push_back(n->nodeId);
CUR++;
}
}
if (n_size + 1 == CUR)
break;
}
if (bag.size() == n_size) {
std::cout << "Topological Ordering Complete" << std::endl;
} else {
std::cout << "Topological Ordering Failed : " << "# of unordered nodes " << n_size - bag.size()
<< std::endl;
}
printNodes();
}
bool checkIndegree(int nodeId) {
bool isExist = false;
for (auto n: nodes) {
Edge *temp = n->next;
while (temp != NULL) {
if (temp->head == nodeId) {
isExist = true;
break;
} else {
temp = temp->next;
}
}
if (isExist)
break;
}
return isExist;
}
void graphSearch(){
}
};
#endif //NETWORK_OPTIMIZATION_ADJ_LIST_H