-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++] Graph Algorithms: Flyod-Warshall Algorithm (Shortest Path)
- Loading branch information
1 parent
372f42e
commit 8ba5aba
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |