Saturday, March 26, 2011

Dijk's Algorithms in Java

/*Implemented Dijk algo in Java.
*Here I have given simple static example...but you can make it dynamic by removing comment.
*
*/import java.io.*;

public class Dijk {
public static void main(String[] arg) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i, j, k, nodes;
// int[][] adjMat = new int[10][10];
//
//
//
// System.out.println("Enter No of Nodes");
// nodes = Integer.parseInt(br.readLine());
// for (i = 0; i < nodes; i++) {
// for (j = i + 1; j < nodes; j++) {
// System.out.println("Is there Edge between node " + (i + 1) + "and node " + (j + 1) + " (1=yes)/(0=no)");
// int b = Integer.parseInt(br.readLine());
// if (b == 0) {
// adjMat[i][j] = 0;
// adjMat[j][i] = 0;
// continue;
// } else {
// System.out.print("Enter weight between node " + (i + 1) + "and node " + (j + 1) + ":");
// adjMat[i][j] = Integer.parseInt(br.readLine());
// adjMat[j][i] = adjMat[i][j];
// }
// }
// }
nodes = 5;
int[][] adjMat = {{0, 50, 30, 100, 10},
{50, 0, 5, 20, 0},
{30, 5, 0, 50, 0},
{100, 20, 50, 0, 10},
{10, 0, 0, 10, 0}};
k = 0;
int[][] D = new int[2][nodes];
//int[] cost = new int[nodes];
System.out.println("Enter Staring node from the range 1 to" + nodes);
int st = Integer.parseInt(br.readLine());
for (i = 0; i < nodes; i++) {
if ((i + 1) == st) {
continue;
} else {
D[k][i] = i;
if (adjMat[st - 1][i] > 0) {
D[k + 1][i] = adjMat[st - 1][i];
} else {
D[k + 1][i] = 9999;
}
}
}
int[][] finalCost = new int[2][nodes];
printD(D,nodes);
for (i = 0; i < nodes-1; i++) {
int index = findminIndex(D, nodes);
System.out.println("Min Index "+index);
int cost = findminCost(D, nodes);
System.out.println("Min Cost "+cost);
for (j = 0; j < nodes; j++) {
if (j == index) {
D[0][j] = 0;

continue;
} else {
if (((adjMat[index][j] + cost) < D[1][j]) && adjMat[index][j]>0) {
D[1][j] = adjMat[index][j] + cost;
} else {
continue;
}
}
}
copyD(D,finalCost,nodes,index,st);
st=index;
D[1][index]=0;
printD(D, nodes);
}
 
// for (i = 0; i < nodes; i++) {
// if (D[0][i] == index) {
// D[0][i] = 0;
// }
// }
// printD(D, nodes);
printD(D, nodes);
for(i=0;i<2;i++)
{
for(j=0;j
System.out.print((finalCost[i][j]) + " ");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printD(int[][] D, int nodes) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < nodes; j++) {
System.out.print(D[i][j] + " ");
}
System.out.println("");
}
}
private static int findminIndex(int[][] D, int nodes) {
int index = 0, min = D[1][1];
for (int i = 1; i < nodes; i++) {
if (min > D[1][i] && D[1][i]!=0) {
min = D[1][i];
index = i;
}
}
return index;
}
private static int findminCost(int[][] D, int nodes) {
int min = D[1][1];
for (int i = 1; i < nodes; i++) {
if (min > D[1][i] && D[1][i]!=0) {
min = D[1][i];
}
}
return min;
}

private static void copyD(int[][] D, int[][] finalCost,int nodes,int index,int st) {
for(int i=0;i
{
if(D[1][i] != 0)
{
finalCost[1][i] = D[1][i];
}
}
finalCost[0][index] = st;
}
}

No comments:

Post a Comment