The document provides a Java implementation of Floyd's algorithm to solve the All-Pairs Shortest Paths problem. It prompts the user to input the number of vertices and a cost matrix, then computes the shortest paths between all pairs of vertices. Finally, it prints the resulting shortest path matrix.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views1 page
Floyd's Algo
The document provides a Java implementation of Floyd's algorithm to solve the All-Pairs Shortest Paths problem. It prompts the user to input the number of vertices and a cost matrix, then computes the shortest paths between all pairs of vertices. Finally, it prints the resulting shortest path matrix.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
10. Implement All-Pairs Shortest Paths Problem using Floyds algorithm.
import java.util.Scanner; public class Floyds { static int a[][]; static int n;
public static void main(String args[])
{ System.out.println("Enter the number of vertices\n"); Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); a = new int[n][n]; System.out.println("Enter the Cost Matrix (999 for infinity) \n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = scanner.nextInt(); } } getPath(); PrintMatrix(); scanner.close(); }
public static void getPath()
{ for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if ((a[i][k] + a[k][j]) < a[i][j]) a[i][j] = a[i][k] + a[k][j]; } }