Program 6 Cn
Program 6 Cn
// Inner class Edge to represent an edge in the graph with source, destination, and weight
class Edge {
int source, destination, weight;
// Bellman-Ford algorithm to find the shortest path from startVertex to all other vertices
void bellmanFord(int startVertex) {
// Initialize distance array with "infinity" (MAX_VALUE) for all vertices
int[] distances = new int[vertices];
Arrays.fill(distances, Integer.MAX_VALUE);
distances[startVertex] = 0; // Distance to start vertex is set to 0
// Check for negative-weight cycles by iterating through all edges once more
for (int j = 0; j < edges; j++) {
int u = edgeList[j].source; // Source vertex of edge
int v = edgeList[j].destination; // Destination vertex of edge
int weight = edgeList[j].weight; // Weight of edge
// Distance Vector Routing method to find shortest paths from startVertex using iterative updates
void distanceVectorRouting(int[][] graph, int startVertex) {
int[] distances = new int[vertices];
Arrays.fill(distances, Integer.MAX_VALUE); // Initialize distances to "infinity" for all vertices
distances[startVertex] = 0; // Set start vertex distance to 0