0% found this document useful (0 votes)
2 views

Program 6 Cn

The document provides a Java implementation of a program that finds the shortest path between vertices using the Bellman-Ford algorithm and distance vector routing. It defines a Graph class with methods to add edges, execute the Bellman-Ford algorithm, and perform distance vector routing. The program includes functionality to detect negative-weight cycles and print the shortest distances from a specified source vertex.

Uploaded by

lokuop196
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)
2 views

Program 6 Cn

The document provides a Java implementation of a program that finds the shortest path between vertices using the Bellman-Ford algorithm and distance vector routing. It defines a Graph class with methods to add edges, execute the Bellman-Ford algorithm, and perform distance vector routing. The program includes functionality to detect negative-weight cycles and print the shortest distances from a specified source vertex.

Uploaded by

lokuop196
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/ 4

Develop a program to find the shortest path between vertices using the Bellman-Ford and path

vector routing algorithm.

import java.util.Arrays; // Import Arrays class for utility functions

// Graph class to represent a graph using edge list


class Graph {

// Inner class Edge to represent an edge in the graph with source, destination, and weight
class Edge {
int source, destination, weight;

// Constructor for Edge


Edge(int source, int destination, int weight) {
this.source = source;
this.destination = destination;
this.weight = weight;
}
}

int vertices, edges; // Number of vertices and edges in the graph


Edge[] edgeList; // Array to store all edges in the graph

// Constructor for Graph


Graph(int vertices, int edges) {
this.vertices = vertices;
this.edges = edges;
edgeList = new Edge[edges]; // Initialize edge list with the specified number of edges
}

// Method to add an edge to the edge list


void addEdge(int edgeIndex, int source, int destination, int weight) {
edgeList[edgeIndex] = new Edge(source, destination, weight); // Add edge to edgeList array
}

// 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

// Relax edges |V| - 1 times to find shortest paths


for (int i = 1; i < vertices; i++) {
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

// If current distance to source + weight is less than distance to destination, update it


if (distances[u] != Integer.MAX_VALUE && distances[u] + weight < distances[v]) {
distances[v] = distances[u] + weight;
}
}
}

// 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

// If we find a shorter path, then a negative-weight cycle exists


if (distances[u] != Integer.MAX_VALUE && distances[u] + weight < distances[v]) {
System.out.println("Graph contains a negative-weight cycle");
return; // Exit if a negative cycle is found
}
}

// Print the computed shortest distances from the source vertex


printSolution(distances, startVertex);
}

// Utility method to print the distances from the start vertex


void printSolution(int[] distances, int startVertex) {
System.out.println("Vertex distances from source vertex " + startVertex + ":");
for (int i = 0; i < vertices; i++) {
System.out.println("To Vertex " + i + " is " + distances[i]);
}
}

// 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

boolean updated; // Flag to check if any distance was updated in an iteration

// Repeat updates until no changes occur (convergence)


do {
updated = false;

// Iterate over each vertex u and its adjacent vertices v


for (int u = 0; u < vertices; u++) {
for (int v = 0; v < vertices; v++) {
// If there's an edge between u and v and distance can be minimized, update it
if (graph[u][v] != Integer.MAX_VALUE && distances[u] != Integer.MAX_VALUE &&
distances[u] + graph[u][v] < distances[v]) {
distances[v] = distances[u] + graph[u][v];
updated = true; // Set flag if a distance was updated
}
}
}
} while (updated); // Repeat until no more updates

// Print the computed shortest distances from the source vertex

You might also like