Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ycz1234
Copy link

@ycz1234 ycz1234 commented Aug 7, 2024

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

  • Added description of change
  • Added file name matches File name guidelines
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

#include <vector>
#include <algorithm>

const int inf = 1e8;
Copy link
Collaborator

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();?

Comment on lines +72 to +88
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);
Copy link
Collaborator

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

@realstealthninja realstealthninja added the requested changes changes required label Aug 31, 2024
* 0 3 7 5
* 2 0 6 4
* 3 1 0 5
* 5 3 2 0
Copy link
Collaborator

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

@realstealthninja realstealthninja added the Proper Documentation Required requested to write the documentation properly label Aug 31, 2024
}
}

void floyd(const std::vector<std::vector<int>>& graph, std::vector<std::vector<int>>& dist) {
Copy link

@aminegh20 aminegh20 Sep 4, 2024

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;

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.

Copy link
Author

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++
*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
* @details


const int inf = 1e8;

void display(const std::vector<std::vector<int>>& graph) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the functions too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proper Documentation Required requested to write the documentation properly requested changes changes required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants