Program 8 (Java)



8.      Write a program to find the shortest path between vertices using bellman-ford algorithm.

Distance Vector Algorithm is a decentralized routing algorithm that requires that each router simply inform its neighbors of its routing table. For each network path, the receiving routers pick the neighbor advertising the lowest cost, then add this entry into its routing table for re-advertisement. To find the shortest path, Distance Vector Algorithm is based on one of two basic algorithms: the Bellman-Ford and the Dijkstra algorithms.
Routers that use this algorithm have to maintain the distance tables (which is a one- dimension array -- "a vector"), which tell the distances and shortest path to sending packets to each node in the network. The information in the distance table is always up date by exchanging information with the neighboring nodes. The number of data in the table equals to that of all nodes in networks (excluded itself). The columns of table represent the directly attached neighbors whereas the rows represent all destinations in the network. Each data contains the path for sending packets to each destination in the network and distance/or time to transmit on that path (we call this as "cost"). The measurements in this algorithm are the number of hops, latency, the number of outgoing packets, etc.
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. Negative edge weights are found in various applications of graphs, hence the usefulness of this algorithm. If a graph contains a "negative cycle" (i.e. a cycle whose edges sum to a negative value) that is reachable from the source, then there is no cheapest path: any path that has a point on the negative cycle can be made cheaper by one more walk around the negative cycle. In such a case, the Bellman–Ford algorithm can detect negative cycles and report their existence.

 

Source code:
package bellmanford;

import java.util.Scanner;

public class BellmanFord {

    public static void main(String[] args) {
        int MAX_VALUE = 999;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of vertices");
        int num_ver = scan.nextInt();
        int A[][] = new int[num_ver + 1][num_ver + 1];
        System.out.println("Enter the cost adjacency matrix, 0 for self loop, 999 for no edge");
        for (int sn = 1; sn <= num_ver; sn++) {
            for (int dn = 1; dn <= num_ver; dn++) {
                A[sn][dn] = scan.nextInt();
            }
        }
        System.out.println("Enter the source vertex");
        int source = scan.nextInt();
      
       /*Create Distance Matrix D and initialize it*/
        int D[] = new int[num_ver + 1];
        for (int node = 1; node <= num_ver; node++) {
            D[node] = MAX_VALUE;
        }
        D[source] = 0;
               /*Relaxation of edges n-1 times*/
        for (int i = 1; i < num_ver; i++) {
            for (int sn = 1; sn <= num_ver; sn++) {
                for (int dn = 1; dn <= num_ver; dn++) {
                    if (D[dn] > D[sn] + A[sn][dn]) {
                        D[dn] = D[sn] + A[sn][dn];
                    }
                }
            }
        }
               /*To check for negative weight cycle*/
        for (int sn = 1; sn <= num_ver; sn++) {
            for (int dn = 1; dn <= num_ver; dn++) {

                if (D[dn] > D[sn] + A[sn][dn]) {
                    System.out.println("The Graph contains negative edge cycle");
                    return;
                }

            }
        }
        for (int vertex = 1; vertex <= num_ver; vertex++) {
            System.out.println("distance of source " + source + " to " + vertex + " is " + D[vertex]);
        }
    }

}




/*
Run1:
 Enter the number of vertices
 4
 Enter the cost adjacency matrix, 0 for self loop, 999 for no edge
 0            4             999        5
 999       0             999        999
 999       -10         0             999
 999       999        3             0
 Enter the source vertex
 1
 distance of source 1 to 1 is 0
 distance of source 1 to 2 is -2
 distance of source 1 to 3 is 8
 distance of source 1 to 4 is 5

 Run 2:
 Enter the number of vertices
 4
 Enter the cost adjacency matrix, 0 for self loop, 999 for no edge
 0            4       999              5
 999       0             999        5
 999       -10         0             999
 999       999        3             0
 Enter the source vertex
 1
 The Graph contains negative edge cycle
 */





No comments:

Post a Comment