0% found this document useful (0 votes)
40 views8 pages

Shortest Path Algorithms in Java

The document describes a Java program that implements the Bellman-Ford routing algorithm to find the shortest path between nodes in a graph. It allows users to input the number of nodes, an adjacency matrix, and distances between connected nodes, and provides options to view routing table information or the entire routing table. The program uses a menu-driven approach to facilitate user interactions and updates the routing table based on user inputs.

Uploaded by

Hemanth BR
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)
40 views8 pages

Shortest Path Algorithms in Java

The document describes a Java program that implements the Bellman-Ford routing algorithm to find the shortest path between nodes in a graph. It allows users to input the number of nodes, an adjacency matrix, and distances between connected nodes, and provides options to view routing table information or the entire routing table. The program uses a menu-driven approach to facilitate user interactions and updates the routing table based on user inputs.

Uploaded by

Hemanth BR
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

1.

Develop a program to find the shortest path between vertices using the
bellman-ford and Path vector routing algorithm
import [Link].*; // Import the Java utility package for Scanner and Arrays

class bellman { // Class to implement Bellman-Ford Routing Algorithm


public static class node {
int distance; // Stores the shortest known distance to a destination
char phop; // Stores the previous hop node
}

public static void main(String[] args) {


int i, j, n; // Variables for loop iteration and number of nodes
Scanner in = new Scanner([Link]); // Scanner object for user input

[Link]("Enter the number of nodes");


n = [Link](); // Read the number of nodes

node[][] rt = new node[n][n]; // Routing table matrix


int[][] g = new int[n][n]; // Adjacency matrix to represent graph
char[] ch = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'}; // Node labels

// Initialize routing table with new node objects


for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
rt[i][j] = new node();

// Read adjacency matrix (1 for connected nodes, 0 for no connection)


[Link]("Enter the adjacency matrix:");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
g[i][j] = [Link]();

// Initialize the routing table distances


[Link]("Enter the distance:");
for (i = 0; i < n; i++) {
[Link]("The distance between node " + ch[i] + " and ");
for (j = 0; j < n; j++) {
if (g[i][j] == 1) { // If nodes are connected
[Link]("node " + ch[j] + " is:");
rt[i][j].distance = [Link](); // User input distance
} else {
rt[i][j].distance = 999; // Set a large value for unconnected nodes
}
rt[i][j].phop = ch[i]; // Initialize previous hop as itself
}
}

int[] ad = new int[n]; // Array to store neighbor nodes


int adc = 0, choice; // `adc` keeps track of the number of neighbors

// Menu-driven approach for user actions


do {
adc = 0; // Reset neighbor count
[Link]("1. Routing table information\n2. Routing
table\n3. Exit");
choice = [Link](); // User input for menu option

switch (choice) {
case 1: // Construct the routing table for a specific node
[Link]("Enter the node for which routing table
should be constructed:");
int id = [Link]();
id--; // Adjust for 0-based indexing

[Link]("The neighbours of " + ch[id] + " are:");


for (i = 0; i < n; i++) {
if (g[id][i] == 1) { // If there's a connection
ad[adc++] = i; // Store neighbor's index
[Link](ch[i]); // Print neighbor node
}
}

// Bellman-Ford's distance vector update


for (i = 0; i < n; i++) {
if (id != i) { // Exclude self-loop
int small = rt[id][i].distance; // Start with known distance
int chosen = id; // Initially, direct connection

// Check if going through neighbors results in a shorter path


for (j = 0; j < adc; j++) {
int total = rt[id][ad[j]].distance + rt[ad[j]][i].distance;
if (total < small) { // If a shorter path is found
small = total;
chosen = ad[j]; // Update previous hop
rt[id][i].phop = ch[chosen]; // Update previous hop
character
}
rt[id][i].distance = small; // Update distance
}

// Print shortest path result


[Link]("The smallest distance from " + ch[id] + " to " + ch[i]
+ " is " + small);
[Link]("The previous hop is " + rt[id][i].phop);
} else {
rt[id][i].distance = 0; // Distance to itself is 0
}
}
break;

case 2: // Print the entire routing table


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
[Link](rt[i][j].distance + "," + rt[i][j].phop + "\t");
// Display distance and hop
[Link]("\n");
}
break;
}
} while (choice != 3); // Exit loop when user selects option 3
}
}
OUTPUT:

(base) ksit@cnlab4:~/lab$ javac [Link]


(base) ksit@cnlab4:~/lab$ java bellman
Enter the number of nodes
4
Enter the adjacency matrix:
0110
1001
1001
0110
Enter the distance:
The distance between node a and
node b is:
2
node c is:
6
The distance between node b and
node a is:
2
node d is:
1
The distance between node c and
node a is:
6
node d is:
2
The distance between node d and
node b is:
1
node c is:
2

1. Routing table information


2. Routing table
3. Exit
2

999,a 2,a 6,a 999,a

2,b 999,b 999,b 1,b


6,c 999,c 999,c 2,c

999,d 1,d 2,d 999,d

1. Routing table information


2. Routing table
3. Exit
1

Enter the node for which routing table should be constructed:


1
The neighbors of a are:
b
c
The smallest distance from a to b is 2
The previous hop is a
The smallest distance from a to c is 6
The previous hop is a
The smallest distance from a to d is 3
The previous hop is b

1. Routing table information


2. Routing table
3. Exit
2

0,a 2,a 6,a 3,b

2,b 999,b 999,b 1,b

6,c 999,c 999,c 2,c

999,d 1,d 2,d 999,d

1. Routing table information


2. Routing table
3. Exit
1

Enter the node for which routing table should be constructed:


2
The neighbors of b are:
a
d
The smallest distance from b to a is 2
The previous hop is b
The smallest distance from b to c is 3
The previous hop is d
The smallest distance from b to d is 1
The previous hop is b

1. Routing table information


2. Routing table
3. Exit
2

0,a 2,a 6,a 3,b


2,b 0,b 3,d 1,b
6,c 999,c 999,c 2,c
999,d 1,d 2,d 999,d

1. Routing table information


2. Routing table
3. Exit
1

Enter the node for which routing table should be constructed:


1
The neighbors of a are:
b
c
The smallest distance from a to b is 2
The previous hop is a
The smallest distance from a to c is 5
The previous hop is b
The smallest distance from a to d is 3
The previous hop is b

1. Routing table information


2. Routing table
3. Exit
2

0,a 2,a 5,b 3,b

2,b 0,b 3,d 1,b

6,c 999,c 999,c 2,c

999,d 1,d 2,d 999,d

1. Routing table information


2. Routing table
3. Exit
3

You might also like