Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions Data Structures/Graphs/FloydWarshall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import java.util.Scanner;



public class FloydWarshall

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove unnecessary empty lines.

{

private int distancematrix[][];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Naming conventions: Use camel case
For example distanceMatrix


private int numberofvertices;

public static final int INFINITY = 999;



public FloydWarshall(int numberofvertices)

{

distancematrix = new int[numberofvertices + 1][numberofvertices + 1];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;

}

}

}


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

}

}