0% found this document useful (0 votes)
7 views3 pages

22bce7873 Asg7

The document contains a Java program that implements the Floyd-Warshall algorithm to find the shortest paths between every pair of nodes in a graph. It initializes a cost matrix, processes the input to determine paths, and outputs the shortest paths. The program prompts the user for the number of nodes and the adjacency matrix, displaying the results accordingly.
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
7 views3 pages

22bce7873 Asg7

The document contains a Java program that implements the Floyd-Warshall algorithm to find the shortest paths between every pair of nodes in a graph. It initializes a cost matrix, processes the input to determine paths, and outputs the shortest paths. The program prompts the user for the number of nodes and the adjacency matrix, displaying the results accordingly.
Copyright
© © All Rights Reserved
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/ 3

NAME VEMURI PRAVEENA

REGISTRATION NUMBER 22BCE7873


LAB SLOT L51+52
ASSIGNMENT NUMBER 07

>>WRITE A JAVA PROGRAM TO DISPLAY SHORTEST PATH BETWEEN EVERY PAIR OF NODES.

CODE:

import java.util.Scanner;

public class FloydWarshallWithPath {

final static int INF = 99999;

static void allPairs(int n, int cost[][]) {

int[][] A = new int[n][n];

int[][] V = new int[n][n];

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

A[i][j] = cost[i][j];

V[i][j] = (cost[i][j] != INF && i != j) ? i : -1;

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] != INF && A[k][j] != INF && A[i][j] > A[i][k] + A[k][j]) {

A[i][j] = A[i][k] + A[k][j];

V[i][j] = k;
}

System.out.println("Shortest paths between nodes:");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

if (i != j) {

System.out.print(i + " -> ");

printPath(V, i, j);

System.out.println(j);

static void printPath(int[][] V, int i, int j) {

if (V[i][j] == i) return;

printPath(V, i, V[i][j]);

System.out.print(V[i][j] + " -> ");

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of nodes: ");

int n = scanner.nextInt();
int[][] cost = new int[n][n];

System.out.println("Enter adjacency matrix (Use " + INF + " for no direct path):");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

cost[i][j] = scanner.nextInt();

scanner.close();

allPairs(n, cost);

OUTPUT:

Enter number of nodes: 4

Enter adjacency matrix (Use 99999 for no direct path):

0 3 99999 7

8 0 2 99999

5 99999 0 1

2 99999 99999 0

Shortest paths between nodes:

0 -> 1

0 -> 2 -> 3

0 -> 3

1 -> 2

1 -> 2 -> 3

2 -> 3

3 -> 0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy