Repository navigation
used to find the shorthest paths among all pairs of nodes in a graph, #389
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import java.util.Scanner; | ||
|
|
||
|
|
||
|
|
||
| public class FloydWarshall | ||
|
|
||
| { | ||
|
|
||
| private int distancematrix[][]; | ||
|
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. Naming conventions: Use camel case |
||
|
|
||
| private int numberofvertices; | ||
|
|
||
| public static final int INFINITY = 999; | ||
|
|
||
|
|
||
|
|
||
| public FloydWarshall(int numberofvertices) | ||
|
|
||
| { | ||
|
|
||
| distancematrix = new int[numberofvertices + 1][numberofvertices + 1]; | ||
|
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. Please initialize the array 'distancematrix' with zeros. for safety. |
||
|
|
||
| this.numberofvertices = numberofvertices; | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| public void floydwarshall(int adjacencymatrix[][]) | ||
|
|
||
| { | ||
|
|
||
| for (int source = 1; source <= numberofvertices; source++) | ||
|
|
||
| { | ||
|
|
||
| for (int destination = 1; destination <= numberofvertices; destination++) | ||
|
|
||
| { | ||
|
|
||
| distancematrix[source][destination] = adjacencymatrix[source][destination]; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| for (int intermediate = 1; intermediate <= numberofvertices; intermediate++) | ||
|
|
||
| { | ||
|
|
||
| for (int source = 1; source <= numberofvertices; source++) | ||
|
|
||
| { | ||
|
|
||
| for (int destination = 1; destination <= numberofvertices; destination++) | ||
|
|
||
| { | ||
|
|
||
| if (distancematrix[source][intermediate] + distancematrix[intermediate][destination] | ||
|
|
||
| < distancematrix[source][destination]) | ||
|
|
||
| distancematrix[source][destination] = distancematrix[source][intermediate] | ||
|
|
||
| + distancematrix[intermediate][destination]; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| for (int source = 1; source <= numberofvertices; source++) | ||
|
|
||
| System.out.print("\t" + source); | ||
|
|
||
|
|
||
|
|
||
| System.out.println(); | ||
|
|
||
| for (int source = 1; source <= numberofvertices; source++) | ||
|
|
||
| { | ||
|
|
||
| System.out.print(source + "\t"); | ||
|
|
||
| for (int destination = 1; destination <= numberofvertices; destination++) | ||
|
|
||
| { | ||
|
|
||
| System.out.print(distancematrix[source][destination] + "\t"); | ||
|
|
||
| } | ||
|
|
||
| System.out.println(); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| public static void main(String... arg) | ||
|
|
||
| { | ||
|
|
||
| int adjacency_matrix[][]; | ||
|
|
||
| int numberofvertices; | ||
|
|
||
|
|
||
|
|
||
| Scanner scan = new Scanner(System.in); | ||
|
|
||
| System.out.println("Enter the number of vertices"); | ||
|
|
||
| numberofvertices = scan.nextInt(); | ||
|
|
||
|
|
||
|
|
||
| adjacency_matrix = new int[numberofvertices + 1][numberofvertices + 1]; | ||
|
|
||
| System.out.println("Enter the Weighted Matrix for the graph"); | ||
|
|
||
| for (int source = 1; source <= numberofvertices; source++) | ||
|
|
||
| { | ||
|
|
||
| for (int destination = 1; destination <= numberofvertices; destination++) | ||
|
|
||
| { | ||
|
|
||
| adjacency_matrix[source][destination] = scan.nextInt(); | ||
|
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. Spare the user for unnecessary inputs if the inputs will be overwrite. |
||
|
|
||
| if (source == destination) | ||
|
|
||
| { | ||
|
|
||
| adjacency_matrix[source][destination] = 0; | ||
|
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. Please use camel case. |
||
|
|
||
| continue; | ||
|
|
||
| } | ||
|
|
||
| if (adjacency_matrix[source][destination] == 0) | ||
|
|
||
| { | ||
|
|
||
| adjacency_matrix[source][destination] = INFINITY; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
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. Put in comments in your code. Makes the program readable for others. |
||
|
|
||
| System.out.println("The Transitive Closure of the Graph"); | ||
|
|
||
| FloydWarshall floydwarshall = new FloydWarshall(numberofvertices); | ||
|
|
||
| floydwarshall.floydwarshall(adjacency_matrix); | ||
|
|
||
| scan.close(); | ||
|
|
||
| } | ||
|
|
||
| } | ||
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.
Remove unnecessary empty lines.