-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented Floyd-Warshall algorithm in the graph module #2708
Conversation
#include <vector> | ||
#include <algorithm> | ||
|
||
const int inf = 1e8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use numeric_limits <int> ::max();?
int N, M; | ||
std::cin >> N >> M; // N - number of vertices; M - number of edges. | ||
std::vector<std::vector<int>> graph(N, std::vector<int>(N, inf)); | ||
for (int i = 0; i < N; i++) { | ||
graph[i][i] = 0; | ||
} | ||
for (int i = 0; i < M; i++) { | ||
int from, to, length; | ||
std::cin >> from >> to >> length; | ||
graph[from][to] = length; | ||
} | ||
|
||
// min_len[a][b] : the shortest distance from a to b. | ||
std::vector<std::vector<int>> min_len = graph; | ||
|
||
floyd(graph, min_len); | ||
display(min_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this to a function outside main called test
* 0 3 7 5 | ||
* 2 0 6 4 | ||
* 3 1 0 5 | ||
* 5 3 2 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add yourself as the author
} | ||
} | ||
|
||
void floyd(const std::vector<std::vector<int>>& graph, std::vector<std::vector<int>>& dist) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we name this floyd_warshall
?
graph[i][i] = 0; | ||
} | ||
for (int i = 0; i < M; i++) { | ||
int from, to, length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe using the variable name "cost" is more accurate than "length". This is because it more accurately represents the value associated with traversing an edge, typically in terms of a resource such as distance, time, money...etc. "Length" might imply physical distance, which could be misleading if the graph is representing something other than spatial relationships, like network latency or financial expenses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
/** | ||
* @file floyd_warshall.cpp | ||
* @brief Implementation of the Floyd-Warshall algorithm in C++ | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* | |
* @details |
|
||
const int inf = 1e8; | ||
|
||
void display(const std::vector<std::vector<int>>& graph) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the functions too!
This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions! |
This pull request implements the Floyd-Warshall algorithm in the graph module. The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices in a weighted graph. This implementation provides an efficient way to solve the shortest path problem for all pairs of vertices.
Checklist
Notes: