Skip to content

Commit

Permalink
[C++] Graph Algorithms: Flyod-Warshall Algorithm (Shortest Path)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmehara committed Apr 1, 2024
1 parent 372f42e commit 8ba5aba
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Segment Tree

# Graph Algorithms
* Flyod-Warshall Algorithm _(Shortest Path)_
* Topological Sort _(Depth-First Search)_
* Topological Sort _(Kahn's Algorithm)_
* Transitive Closure
Expand Down
2 changes: 2 additions & 0 deletions C++/graph_algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
target_sources(programming
PRIVATE
shortest_path.h
shortest_path.cpp
topological_sort.h
topological_sort.cpp
transitive_closure.h
Expand Down
47 changes: 47 additions & 0 deletions C++/graph_algorithms/shortest_path.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 "shortest_path.h"

#include <algorithm>
#include <climits>
#include <vector>

namespace mehara::graph {

using namespace std;

vector<vector<int>> flyod_warshall_algorithm(int n,
const std::vector<edge>& edges)
{
vector<vector<int>> dist(n, vector<int>(n, INT_MAX));
for (const auto& e : edges) {
dist[e.to][e.from] = e.weight;
}
for (int i = 0; i < n; i++) {
dist[i][i] = true;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][k] < INT_MAX && dist[k][j]) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
return dist;
}

} // namespace mehara::graph
33 changes: 33 additions & 0 deletions C++/graph_algorithms/shortest_path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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_SHORTEST_PATH_H_
#define MEHARA_SHORTEST_PATH_H_

#include <vector>

namespace mehara::graph {

struct edge {
int from;
int to;
int weight;
};

std::vector<std::vector<int>>
flyod_warshall_algorithm(int n, const std::vector<edge>& edges);

} // namespace mehara::graph

#endif // MEHARA_SHORTEST_PATH_H_

0 comments on commit 8ba5aba

Please sign in to comment.