Skip to content

Commit 2f73b59

Browse files
committed
Implemented cachedAdjMatrixIn functionality, resolving #406:
- Implemented cachedAdjMatrixIn member variable - Renamed cachedAdjMatrix -> cachedAdjMatrixOut and cachedAdjMatrix -> cachedAdjMatricies to reflect changes - Renamed inNeighbors/Edges -> inNotOutNeighbors/Edges and inOutNeighbors/Edges -> inOrOutNeighbors/Edges for clarity - Fixed inOrOutNeighbors/Edges, resolving #406 - Added #406 replicator as a test in DirectedEdgeTest
1 parent 4b6161e commit 2f73b59

21 files changed

Lines changed: 311 additions & 225 deletions

benchmark/Graph_BM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static void getAdjMatrixX(benchmark::State &state) {
122122
g.addEdge(&(*e.second));
123123
}
124124
for (auto _ : state) {
125-
auto adjMatrix = g.getAdjMatrix();
125+
auto adjMatrix = g.getAdjMatrixOut();
126126
}
127127
state.SetComplexityN(2);
128128
}
@@ -134,7 +134,7 @@ BENCHMARK(getAdjMatrixX)
134134

135135
static void getAdjMatrixCitHep(benchmark::State &state) {
136136
for (auto _ : state) {
137-
auto adjMatrix = cit_graph_ptr->getAdjMatrix();
137+
auto adjMatrix = cit_graph_ptr->getAdjMatrixOut();
138138
}
139139
state.SetComplexityN(2);
140140
}

include/CXXGraph/Graph/Algorithm/BestFirstSearch_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ BestFirstSearchResult<T> Graph<T>::best_first_search(
6969
if (*currentNode == target) {
7070
break;
7171
}
72-
if (cachedAdjMatrix->find(currentNode) != cachedAdjMatrix->end()) {
73-
for (const auto &elem : cachedAdjMatrix->at(currentNode)) {
72+
if (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
73+
for (const auto &elem : cachedAdjMatrixOut->at(currentNode)) {
7474
if (elem.second->isWeighted().has_value()) {
7575
if (elem.second->isDirected().has_value()) {
7676
shared<const DirectedWeightedEdge<T>> dw_edge =
@@ -190,8 +190,8 @@ const std::vector<Node<T>> Graph<T>::concurrency_breadth_first_search(
190190
}
191191

192192
for (int i = start_index; i < end_index; ++i) {
193-
if (cachedAdjMatrix->count(level_tracker[i])) {
194-
for (const auto &elem : cachedAdjMatrix->at(level_tracker[i])) {
193+
if (cachedAdjMatrixOut->count(level_tracker[i])) {
194+
for (const auto &elem : cachedAdjMatrixOut->at(level_tracker[i])) {
195195
int index = (int)node_to_index[elem.first];
196196
if (visited[index] == 0) {
197197
visited[index] = 1;

include/CXXGraph/Graph/Algorithm/BreadthFirstSearch_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const std::vector<Node<T>> Graph<T>::breadth_first_search(
5050
while (!tracker.empty()) {
5151
shared<const Node<T>> node = tracker.front();
5252
tracker.pop();
53-
if (cachedAdjMatrix->find(node) != cachedAdjMatrix->end()) {
54-
for (const auto &elem : cachedAdjMatrix->at(node)) {
53+
if (cachedAdjMatrixOut->find(node) != cachedAdjMatrixOut->end()) {
54+
for (const auto &elem : cachedAdjMatrixOut->at(node)) {
5555
// if the node is not visited then mark it as visited
5656
// and push it to the queue
5757
if (std::find(visited.begin(), visited.end(), *(elem.first)) ==

include/CXXGraph/Graph/Algorithm/BronKerbosch_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const BronKerboschResult<T> Graph<T>::bron_kerbosch() const {
4949
auto it = P.begin();
5050
while (it != P.end()) {
5151
const auto v = *it;
52-
T_NodeSet<T> X2, R2(R), P2, nbd(inOutNeighbors(v));
52+
T_NodeSet<T> X2, R2(R), P2, nbd(inOrOutNeighbors(v));
5353
R2.insert(v);
5454
for (const auto &u : nbd) {
5555
if (X.count(u) > 0 && u != v) {

include/CXXGraph/Graph/Algorithm/Connectivity_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ bool Graph<T>::isConnectedGraph() const {
4343
visited[source->getId()] = true;
4444

4545
// travel the neighbors
46-
for (size_t i = 0; i < (*cachedAdjMatrix)[source].size(); i++) {
46+
for (size_t i = 0; i < (*cachedAdjMatrixOut)[source].size(); i++) {
4747
shared<const Node<T>> neighbor =
48-
(*cachedAdjMatrix)[source].at(i).first;
48+
(*cachedAdjMatrixOut)[source].at(i).first;
4949
if (visited[neighbor->getId()] == false) {
5050
// make recursive call from neighbor
5151
dfs_helper(neighbor);
@@ -83,9 +83,9 @@ bool Graph<T>::isStronglyConnectedGraph() const {
8383
visited[source->getId()] = true;
8484

8585
// travel the neighbors
86-
for (size_t i = 0; i < (*cachedAdjMatrix)[source].size(); i++) {
86+
for (size_t i = 0; i < (*cachedAdjMatrixOut)[source].size(); i++) {
8787
shared<const Node<T>> neighbor =
88-
(*cachedAdjMatrix)[source].at(i).first;
88+
(*cachedAdjMatrixOut)[source].at(i).first;
8989
if (visited[neighbor->getId()] == false) {
9090
// make recursive call from neighbor
9191
dfs_helper(neighbor);

include/CXXGraph/Graph/Algorithm/CycleDetection_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool Graph<T>::isCyclicDirectedGraphDFS() const {
9494
// Return that current node didn't result in any cycles.
9595
return false;
9696
};
97-
if (isCyclicDFSHelper(cachedAdjMatrix, state, node)) {
97+
if (isCyclicDFSHelper(cachedAdjMatrixOut, state, node)) {
9898
return true;
9999
}
100100
}
@@ -184,7 +184,7 @@ bool Graph<T>::isCyclicDirectedGraphBFS() const {
184184
indegree[node->getId()] = 0;
185185
}
186186
// Calculate the indegree i.e. the number of incident edges to the node.
187-
for (auto const &list : (*cachedAdjMatrix)) {
187+
for (auto const &list : (*cachedAdjMatrixOut)) {
188188
auto children = list.second;
189189
for (auto const &child : children) {
190190
indegree[std::get<0>(child)->getId()]++;
@@ -211,8 +211,8 @@ bool Graph<T>::isCyclicDirectedGraphBFS() const {
211211
remain--;
212212

213213
// Visit all the children of the visited node.
214-
auto it = cachedAdjMatrix->find(solved);
215-
if (it != cachedAdjMatrix->end()) {
214+
auto it = cachedAdjMatrixOut->find(solved);
215+
if (it != cachedAdjMatrixOut->end()) {
216216
for (const auto &child : it->second) {
217217
// Check if we can visited the node safely.
218218
if (--indegree[std::get<0>(child)->getId()] == 0) {

include/CXXGraph/Graph/Algorithm/DepthFirstSearch_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const std::vector<Node<T>> Graph<T>::depth_first_search(
5757
}
5858
}
5959
};
60-
explore(cachedAdjMatrix, *start_node_it, visited);
60+
explore(cachedAdjMatrixOut, *start_node_it, visited);
6161

6262
return visited;
6363
}

include/CXXGraph/Graph/Algorithm/Dial_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const DialResult Graph<T>::dial(const Node<T> &source, int maxWeight) const {
8484

8585
// Process all adjacents of extracted vertex 'u' and
8686
// update their distanced if required.
87-
for (const auto &i : (*cachedAdjMatrix)[u]) {
87+
for (const auto &i : (*cachedAdjMatrixOut)[u]) {
8888
auto v = i.first;
8989
int weight = 0;
9090
if (i.second->isWeighted().has_value() &&

include/CXXGraph/Graph/Algorithm/Dijkstra_impl.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T>& source,
5252
}
5353
// n denotes the number of vertices in graph
5454
// unused
55-
// auto n = cachedAdjMatrix->size();
55+
// auto n = cachedAdjMatrixOut->size();
5656

5757
// setting all the distances initially to INF_DOUBLE
5858
std::unordered_map<shared<const Node<T>>, double, nodeHash<T>> dist;
@@ -88,8 +88,8 @@ const DijkstraResult Graph<T>::dijkstra(const Node<T>& source,
8888

8989
// for all the reachable vertex from the currently exploring vertex
9090
// we will try to minimize the distance
91-
if (cachedAdjMatrix->find(currentNode) != cachedAdjMatrix->end()) {
92-
for (const auto& elem : cachedAdjMatrix->at(currentNode)) {
91+
if (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
92+
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
9393
// minimizing distances
9494
if (elem.second->isWeighted().has_value() &&
9595
elem.second->isWeighted().value()) {
@@ -177,7 +177,7 @@ const DijkstraResult Graph<T>::dijkstra_deterministic(
177177
}
178178
// n denotes the number of vertices in graph
179179
// unused
180-
// auto n = cachedAdjMatrix->size();
180+
// auto n = cachedAdjMatrixOut->size();
181181

182182
// setting all the distances initially to INF_DOUBLE
183183
std::unordered_map<shared<const Node<T>>, double, nodeHash<T>> dist;
@@ -232,8 +232,8 @@ const DijkstraResult Graph<T>::dijkstra_deterministic(
232232

233233
// for all the reachable vertex from the currently exploring vertex
234234
// we will try to minimize the distance
235-
if (cachedAdjMatrix->find(currentNode) != cachedAdjMatrix->end()) {
236-
for (const auto& elem : cachedAdjMatrix->at(currentNode)) {
235+
if (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
236+
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
237237
// minimizing distances
238238
if (elem.second->isWeighted().has_value() &&
239239
elem.second->isWeighted().value()) {
@@ -325,7 +325,7 @@ const DijkstraResult Graph<T>::dijkstra_deterministic2(
325325
}
326326
// n denotes the number of vertices in graph
327327
// unused
328-
// auto n = cachedAdjMatrix->size();
328+
// auto n = cachedAdjMatrixOut->size();
329329

330330
// setting all the distances initially to INF_DOUBLE
331331
std::unordered_map<shared<const Node<T>>, double, nodeHash<T>> dist;
@@ -388,8 +388,8 @@ const DijkstraResult Graph<T>::dijkstra_deterministic2(
388388

389389
// for all the reachable vertex from the currently exploring vertex
390390
// we will try to minimize the distance
391-
if (cachedAdjMatrix->find(currentNode) != cachedAdjMatrix->end()) {
392-
for (const auto& elem : cachedAdjMatrix->at(currentNode)) {
391+
if (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
392+
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
393393
// minimizing distances
394394
if (elem.second->isWeighted().has_value() &&
395395
elem.second->isWeighted().value()) {
@@ -481,7 +481,7 @@ const DijkstraResult Graph<T>::criticalpath_deterministic(
481481
}
482482
// n denotes the number of vertices in graph
483483
// unused
484-
// auto n = cachedAdjMatrix->size();
484+
// auto n = cachedAdjMatrixOut->size();
485485

486486
// setting all the distances initially to -INF_DOUBLE
487487
std::unordered_map<shared<const Node<T>>, double, nodeHash<T>> dist;
@@ -536,8 +536,8 @@ const DijkstraResult Graph<T>::criticalpath_deterministic(
536536

537537
// for all the reachable vertex from the currently exploring vertex
538538
// we will try to minimize the distance
539-
if (cachedAdjMatrix->find(currentNode) != cachedAdjMatrix->end()) {
540-
for (const auto& elem : cachedAdjMatrix->at(currentNode)) {
539+
if (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
540+
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
541541
// minimizing distances
542542
if (elem.second->isWeighted().has_value() &&
543543
elem.second->isWeighted().value()) {

include/CXXGraph/Graph/Algorithm/HopcroftKarp_impl.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ namespace CXXGraph {
7474
auto current = queue.front();
7575
queue.pop();
7676

77-
auto neighbors = this->inOutNeighbors(current);
77+
auto neighbors = this->inOrOutNeighbors(current);
7878
for(const auto& neighbor : neighbors) {
7979
if(color.find(neighbor->getUserId()) == color.end()) {
8080
// assign opposite color
@@ -180,7 +180,7 @@ namespace CXXGraph {
180180
// stop if shortest path already found
181181
if (path_len != -1 && dist.at(u) >= path_len) continue;
182182

183-
auto neighbors = this->inOutNeighbors(u);
183+
auto neighbors = this->inOrOutNeighbors(u);
184184
for (const auto& v : neighbors) {
185185
if (match.find(v) == match.end()) {
186186
// found unmatched node - augmenting path exists
@@ -202,7 +202,7 @@ namespace CXXGraph {
202202
auto dfs = [&](auto&& self, shared<const Node<T>> u) -> bool {
203203
if (dist.find(u) == dist.end()) return false;
204204

205-
auto neighbors = this->inOutNeighbors(u);
205+
auto neighbors = this->inOrOutNeighbors(u);
206206
for (const auto& v : neighbors) {
207207
if (match.find(v) == match.end()) {
208208
// found unmatched node at correct distance

0 commit comments

Comments
 (0)