Skip to content

Commit

Permalink
[C++] Graph Algorithms: Transitive Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmehara committed Mar 31, 2024
1 parent d49dd9c commit 372f42e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Graph Algorithms
* Topological Sort _(Depth-First Search)_
* Topological Sort _(Kahn's Algorithm)_
* Transitive Closure

## Mathematics
### Linear Algebra
Expand Down
4 changes: 3 additions & 1 deletion C++/graph_algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
target_sources(programming
PRIVATE
topological_sort.h
topological_sort.cpp)
topological_sort.cpp
transitive_closure.h
transitive_closure.cpp)
46 changes: 46 additions & 0 deletions C++/graph_algorithms/transitive_closure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 Aman Mehara
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "transitive_closure.h"

#include <vector>

namespace mehara::graph {

using namespace std;

vector<vector<bool>>
transitive_closure(const vector<vector<int>>& adjacency_list)
{
int n = adjacency_list.size();
vector<vector<bool>> tc(n, vector<bool>(n, false));
for (int i = 0; i < n; i++) {
for (const auto& neighbour : adjacency_list[i]) {
tc[i][neighbour] = true;
}
}
for (int i = 0; i < n; i++) {
tc[i][i] = true;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
tc[i][j] = tc[i][j] || (tc[i][k] && tc[k][j]);
}
}
}
return tc;
}

} // namespace mehara::graph
27 changes: 27 additions & 0 deletions C++/graph_algorithms/transitive_closure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Aman Mehara
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MEHARA_TRANSITIVE_CLOSURE_H_
#define MEHARA_TRANSITIVE_CLOSURE_H_

#include <vector>

namespace mehara::graph {

std::vector<std::vector<bool>>
transitive_closure(const std::vector<std::vector<int>>& adjacency_list);

} // namespace mehara::graph

#endif // MEHARA_TRANSITIVE_CLOSURE_H_

0 comments on commit 372f42e

Please sign in to comment.