1.
Develop a program to find the shortest path between vertices using the
bellman-ford and Path vector routing algorithm
import java.util.*; // 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(System.in); // Scanner object for user input
System.out.println("Enter the number of nodes");
n = in.nextInt(); // 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)
System.out.println("Enter the adjacency matrix:");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
g[i][j] = in.nextInt();
// Initialize the routing table distances
System.out.println("Enter the distance:");
for (i = 0; i < n; i++) {
System.out.println("The distance between node " + ch[i] + " and ");
for (j = 0; j < n; j++) {
if (g[i][j] == 1) { // If nodes are connected
System.out.println("node " + ch[j] + " is:");
rt[i][j].distance = in.nextInt(); // 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
System.out.println("1. Routing table information\n2. Routing
table\n3. Exit");
choice = in.nextInt(); // User input for menu option
switch (choice) {
case 1: // Construct the routing table for a specific node
System.out.println("Enter the node for which routing table
should be constructed:");
int id = in.nextInt();
id--; // Adjust for 0-based indexing
System.out.println("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
System.out.println(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
System.out.println("The smallest distance from " + ch[id] + " to " + ch[i]
+ " is " + small);
System.out.println("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++)
System.out.print(rt[i][j].distance + "," + rt[i][j].phop + "\t");
// Display distance and hop
System.out.println("\n");
}
break;
}
} while (choice != 3); // Exit loop when user selects option 3
}
}
OUTPUT:
(base) ksit@cnlab4:~/lab$ javac lb6.java
(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