Skip to content

Commit 1d86d80

Browse files
authored
Implemented adjacencyListIn member variable: Fixes #406, Fixes #501 (#526)
* Added visual studio to .gitignore * 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 * Implemented inNotOutEdges/Neighbors and associated tests for completeness with outNotInEdges/Neighbors * Renamed adjacency matrix in/out -> adjacency list in/out to better reflect their implementation
1 parent 4737a59 commit 1d86d80

29 files changed

Lines changed: 676 additions & 481 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ packaging/
3939
.vscode
4040
.code-workspace
4141
CXXGraph.code-workspace
42+
# ignore visual studio files
43+
.vs

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.getAdjListOut();
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->getAdjListOut();
138138
}
139139
state.SetComplexityN(2);
140140
}

examples/DijkstraExample/dijkstra_example.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ int main() {
2828

2929
CXXGraph::Graph<int> graph(edgeSet);
3030
auto res = graph.dijkstra(node0, node3);
31-
31+
3232
std::cout << "Dijkstra Result: " << res.result << "\n";
33-
for(auto& name : res.path) {
33+
for (auto& name : res.path) {
3434
std::cout << "->" << name << std::endl;
3535
}
3636

@@ -39,7 +39,7 @@ int main() {
3939

4040
std::cout << "CP Result: " << res.result << "\n";
4141
std::cout << "CP path size: " << res.path.size() << "\n";
42-
for(auto& name : res.path) {
42+
for (auto& name : res.path) {
4343
std::cout << "->" << name << std::endl;
4444
}
4545

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 (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
73+
for (const auto &elem : cachedAdjListOut->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 (cachedAdjListOut->count(level_tracker[i])) {
194+
for (const auto &elem : cachedAdjListOut->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 (cachedAdjListOut->find(node) != cachedAdjListOut->end()) {
54+
for (const auto &elem : cachedAdjListOut->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 < (*cachedAdjListOut)[source].size(); i++) {
4747
shared<const Node<T>> neighbor =
48-
(*cachedAdjMatrix)[source].at(i).first;
48+
(*cachedAdjListOut)[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 < (*cachedAdjListOut)[source].size(); i++) {
8787
shared<const Node<T>> neighbor =
88-
(*cachedAdjMatrix)[source].at(i).first;
88+
(*cachedAdjListOut)[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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ bool Graph<T>::isCyclicDirectedGraphDFS() const {
5454
// node as it has already been checked for presence of cycle.
5555
if (state[node->getId()] == not_visited) {
5656
// Check for cycle.
57-
std::function<bool(const std::shared_ptr<AdjacencyMatrix<T>>,
57+
std::function<bool(const std::shared_ptr<AdjacencyList<T>>,
5858
std::unordered_map<CXXGraph::id_t, nodeStates> &,
5959
shared<const Node<T>>)>
6060
isCyclicDFSHelper;
6161
isCyclicDFSHelper =
6262
[&isCyclicDFSHelper](
63-
const std::shared_ptr<AdjacencyMatrix<T>> adjMatrix,
63+
const std::shared_ptr<AdjacencyList<T>> adjMatrix,
6464
std::unordered_map<CXXGraph::id_t, nodeStates> &states,
6565
shared<const Node<T>> node) {
6666
// Add node "in_stack" state.
@@ -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(cachedAdjListOut, 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 : (*cachedAdjListOut)) {
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 = cachedAdjListOut->find(solved);
215+
if (it != cachedAdjListOut->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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ const std::vector<Node<T>> Graph<T>::depth_first_search(
4141
if (start_node_it == nodeSet.end()) {
4242
return visited;
4343
}
44-
std::function<void(const std::shared_ptr<AdjacencyMatrix<T>>,
44+
std::function<void(const std::shared_ptr<AdjacencyList<T>>,
4545
shared<const Node<T>>, std::vector<Node<T>> &)>
4646
explore;
47-
explore = [&explore](const std::shared_ptr<AdjacencyMatrix<T>> adj,
47+
explore = [&explore](const std::shared_ptr<AdjacencyList<T>> adj,
4848
shared<const Node<T>> node,
4949
std::vector<Node<T>> &visited) -> void {
5050
visited.push_back(*node);
@@ -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(cachedAdjListOut, *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 : (*cachedAdjListOut)[u]) {
8888
auto v = i.first;
8989
int weight = 0;
9090
if (i.second->isWeighted().has_value() &&

0 commit comments

Comments
 (0)