-
Notifications
You must be signed in to change notification settings - Fork 300
[Algo] floyd warshall algorithm for finding all pair shortest path in a connected graph [CPP] #287
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4c70db5
floyd warshall algorithm for finding all pair shortest path in a conn…
vivekcrux 732e3bb
Added whitespaces
vivekcrux 4efed9c
Deleted extra trailing whitespaces
vivekcrux 57bffc2
Updated comments
vivekcrux c578bd6
Added whitespace
vivekcrux 3f7560f
renamed file from floyd-warshall algorithm.cpp to floyd_warshall_algo…
vivekcrux eeed8d3
added input.txt
vivekcrux 1f2b74a
graph matrix size fix
vivekcrux 590cd8e
Removed whitespace
vivekcrux 85f6af2
fix bus error
vivekcrux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,66 @@ | ||
| // Floyd-Warshall Algorithm: all pair shortest path | ||
| #include <iostream> | ||
| #include <climits> | ||
| using namespace std; | ||
|
|
||
| #define INF 999999999 | ||
|
|
||
| int main() { | ||
| freopen("input.txt", "r", stdin); | ||
| int graph[1001][1001], i, j, k, n, m; | ||
| cin >> n >> m; // number of vertices and edges respectively | ||
|
|
||
| for(i = 1; i <= n; i++) { | ||
| for(j = 1; j <= n; j++) | ||
| if(i == j) graph[i][j] = 0; // Shortest distance between (i,i) is 0 | ||
| else | ||
| graph[i][j] = INF; // Initializing other distances as infinite | ||
| } | ||
|
|
||
| for(i = 1; i <= m; i++) { | ||
| int u, v, w; | ||
| cin>> u >> v >> w; | ||
| graph[u][v] = w; // input graph | ||
| graph[v][u] = w; // Shortest distance between (i,j) using atmost one edge | ||
| } | ||
|
|
||
|
|
||
| for(k = 1; k <= n; k++) { | ||
| for(int u = 1; u <= n; u++) { | ||
| for(int v = 1; v <= n; v++) { | ||
| // Shortest distance between (i,j) using atmost k edges | ||
| graph[u][v] = min(graph[u][v], graph[u][k] + graph[k][v]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Resulting all pair shortest path in given graph | ||
| for(int u = 1; u <= n; u++) { | ||
| for(int v = 1; v <= n; v++) { | ||
| cout << graph[u][v] << " "; | ||
| } | ||
| cout << endl; | ||
| } | ||
| cout << endl; | ||
| return 0; | ||
| } | ||
|
|
||
| /* | ||
| Input:- | ||
| First line contain two space separated integer n and m | ||
| where n is the number of vertices and m is the number | ||
| of edges.Next m lines contains three space seprated integers | ||
| u, v and w which represents that there is an edge between u and v | ||
| and weight of the edge is w | ||
| Sample Input:- | ||
| 6 9 | ||
| 1 2 1 | ||
| 1 3 2 | ||
| 1 4 5 | ||
| 2 4 5 | ||
| 2 5 2 | ||
| 3 4 3 | ||
| 4 5 1 | ||
| 4 6 1 | ||
| 5 6 4 | ||
| */ |
Binary file not shown.
This file contains hidden or 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,69 @@ | ||
| // Floyd-Warshall Algorithm: all pair shortest path | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <climits> | ||
| using namespace std; | ||
|
|
||
| #define INF 999999999 | ||
| #define MAXN 1001 | ||
|
|
||
| int main() { | ||
| freopen("input.txt", "r", stdin); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] Origin: CPPLintBear, Section: |
||
| int i, j, k, n, m; | ||
| cin >> n >> m; // number of vertices and edges respectively | ||
| int graph[MAXN][MAXN]; | ||
|
|
||
| for(i = 1; i <= n; i++) { | ||
| for(j = 1; j <= n; j++) | ||
| if(i == j) graph[i][j] = 0; // Shortest distance between (i,i) is 0 | ||
| else | ||
| graph[i][j] = INF; // Initializing other distances as infinite | ||
| } | ||
|
|
||
| for(i = 1; i <= m; i++) { | ||
| int u, v, w; | ||
| cin>> u >> v >> w; | ||
| graph[u][v] = w; // input graph | ||
| graph[v][u] = w; // Shortest distance between (i,j) using atmost one edge | ||
| } | ||
|
|
||
|
|
||
| for(k = 1; k <= n; k++) { | ||
| for(int u = 1; u <= n; u++) { | ||
| for(int v = 1; v <= n; v++) { | ||
| // Shortest distance between (i,j) using atmost k edges | ||
| graph[u][v] = min(graph[u][v], graph[u][k] + graph[k][v]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Resulting all pair shortest path in given graph | ||
| for(int u = 1; u <= n; u++) { | ||
| for(int v = 1; v <= n; v++) { | ||
| cout << graph[u][v] << " "; | ||
| } | ||
| cout << endl; | ||
| } | ||
| cout << endl; | ||
| return 0; | ||
| } | ||
|
|
||
| /* | ||
| Input:- | ||
| First line contain two space separated integer n and m | ||
| where n(1 <= n <= 1000) is the number of vertices and m is the number | ||
| of edges.Next m lines contains three space seprated integers | ||
| u, v and w which represents that there is an edge between u and v | ||
| and weight of the edge is w | ||
| Sample Input:- | ||
| 6 9 | ||
| 1 2 1 | ||
| 1 3 2 | ||
| 1 4 5 | ||
| 2 4 5 | ||
| 2 5 2 | ||
| 3 4 3 | ||
| 4 5 1 | ||
| 4 6 1 | ||
| 5 6 4 | ||
| */ | ||
This file contains hidden or 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,10 @@ | ||
| 6 9 | ||
| 1 2 1 | ||
| 1 3 2 | ||
| 1 4 5 | ||
| 2 4 5 | ||
| 2 5 2 | ||
| 3 4 3 | ||
| 4 5 1 | ||
| 4 6 1 | ||
| 5 6 4 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.