Skip to content

Commit b7a4f3f

Browse files
committed
Renamed adjacency matrix in/out -> adjacency list in/out to better reflect their implementation
1 parent ed09ca8 commit b7a4f3f

27 files changed

Lines changed: 407 additions & 400 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.getAdjMatrixOut();
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->getAdjMatrixOut();
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 (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
73-
for (const auto &elem : cachedAdjMatrixOut->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 (cachedAdjMatrixOut->count(level_tracker[i])) {
194-
for (const auto &elem : cachedAdjMatrixOut->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 (cachedAdjMatrixOut->find(node) != cachedAdjMatrixOut->end()) {
54-
for (const auto &elem : cachedAdjMatrixOut->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/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 < (*cachedAdjMatrixOut)[source].size(); i++) {
46+
for (size_t i = 0; i < (*cachedAdjListOut)[source].size(); i++) {
4747
shared<const Node<T>> neighbor =
48-
(*cachedAdjMatrixOut)[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 < (*cachedAdjMatrixOut)[source].size(); i++) {
86+
for (size_t i = 0; i < (*cachedAdjListOut)[source].size(); i++) {
8787
shared<const Node<T>> neighbor =
88-
(*cachedAdjMatrixOut)[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(cachedAdjMatrixOut, 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 : (*cachedAdjMatrixOut)) {
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 = cachedAdjMatrixOut->find(solved);
215-
if (it != cachedAdjMatrixOut->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(cachedAdjMatrixOut, *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 : (*cachedAdjMatrixOut)[u]) {
87+
for (const auto &i : (*cachedAdjListOut)[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: 13 additions & 14 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 = cachedAdjMatrixOut->size();
55+
// auto n = cachedAdjListOut->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 (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
92-
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
91+
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
92+
for (const auto& elem : cachedAdjListOut->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 = cachedAdjMatrixOut->size();
180+
// auto n = cachedAdjListOut->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 (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
236-
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
235+
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
236+
for (const auto& elem : cachedAdjListOut->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 = cachedAdjMatrixOut->size();
328+
// auto n = cachedAdjListOut->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 (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
392-
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
391+
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
392+
for (const auto& elem : cachedAdjListOut->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 = cachedAdjMatrixOut->size();
484+
// auto n = cachedAdjListOut->size();
485485

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

516-
std::priority_queue<VertexInfo, std::vector<VertexInfo>, VertexInfoLesser>
517-
pq;
516+
std::priority_queue<VertexInfo, std::vector<VertexInfo>, VertexInfoLesser> pq;
518517

519518
// pushing the source vertex 's' with 0 distance in min heap
520519
pq.push(VertexInfo{0.0, stableIds[*source_node_it], *source_node_it});
@@ -536,8 +535,8 @@ const DijkstraResult Graph<T>::criticalpath_deterministic(
536535

537536
// for all the reachable vertex from the currently exploring vertex
538537
// we will try to minimize the distance
539-
if (cachedAdjMatrixOut->find(currentNode) != cachedAdjMatrixOut->end()) {
540-
for (const auto& elem : cachedAdjMatrixOut->at(currentNode)) {
538+
if (cachedAdjListOut->find(currentNode) != cachedAdjListOut->end()) {
539+
for (const auto& elem : cachedAdjListOut->at(currentNode)) {
541540
// minimizing distances
542541
if (elem.second->isWeighted().has_value() &&
543542
elem.second->isWeighted().value()) {

0 commit comments

Comments
 (0)