Matrik.java
package Greedy_TSP;
*
* @author Rezaakhmadg
*/
public class Matrik {
int graph[][]=new int[8][8];
Matrik(){
graph[1][1]=0;graph[1][2]=75;graph[1][3]=60;graph[1][4]=50;graph[1][5]=215;graph[1][6]=130;graph[1][7]=95;
graph[2][1]=60;graph[2][2]=0;graph[2][3]=60;graph[2][4]=70;graph[2][5]=85;graph[2][6]=50;graph[2][7]=135;
graph[3][1]=40;graph[3][2]=55;graph[3][3]=0;graph[3][4]=130;graph[3][5]=95;graph[3][6]=100;graph[3][7]=55;
graph[4][1]=40;graph[4][2]=75;graph[4][3]=140;graph[4][4]=0;graph[4][5]=45;graph[4][6]=60;graph[4][7]=135;
graph[5][1]=20;graph[5][2]=85;graph[5][3]=100;graph[5][4]=40;graph[5][5]=0;graph[5][6]=90;graph[5][7]=95;
graph[6][1]=120;graph[6][2]=55;graph[6][3]=110;graph[6][4]=60;graph[6][5]=95;graph[6][6]=0;graph[6][7]=65;
graph[7][1]=80;graph[7][2]=135;graph[7][3]=60;graph[7][4]=130;graph[7][5]=95;graph[7][6]=60;graph[7][7]=0;
}
}
*
* @author Rezaakhmadg
*/
public class Matrik {
int graph[][]=new int[8][8];
Matrik(){
graph[1][1]=0;graph[1][2]=75;graph[1][3]=60;graph[1][4]=50;graph[1][5]=215;graph[1][6]=130;graph[1][7]=95;
graph[2][1]=60;graph[2][2]=0;graph[2][3]=60;graph[2][4]=70;graph[2][5]=85;graph[2][6]=50;graph[2][7]=135;
graph[3][1]=40;graph[3][2]=55;graph[3][3]=0;graph[3][4]=130;graph[3][5]=95;graph[3][6]=100;graph[3][7]=55;
graph[4][1]=40;graph[4][2]=75;graph[4][3]=140;graph[4][4]=0;graph[4][5]=45;graph[4][6]=60;graph[4][7]=135;
graph[5][1]=20;graph[5][2]=85;graph[5][3]=100;graph[5][4]=40;graph[5][5]=0;graph[5][6]=90;graph[5][7]=95;
graph[6][1]=120;graph[6][2]=55;graph[6][3]=110;graph[6][4]=60;graph[6][5]=95;graph[6][6]=0;graph[6][7]=65;
graph[7][1]=80;graph[7][2]=135;graph[7][3]=60;graph[7][4]=130;graph[7][5]=95;graph[7][6]=60;graph[7][7]=0;
}
}
Greedy.java
package Greedy_TSP;
/**
*
* @Rezaakhmadg
*/
public class Greedy {
Matrik M=new Matrik();
int[][] hasil=new int[9][9];
int[] solusi=new int[10];
// char[] record=new char[100];
// public char ruteKota() {
//
//}
public Greedy(){
}
public void showHasil(){
String temp="";
int row=1;
int col=1;
int cc=0;
int rr=0;
int com=0;
int cek=2;
int jarak=0;
int i=0;
int j=0;
int ce=0;
solusi[1]=1;
for (i=1;i<=8;i++) {
for (j=1;j<=8;j++) {
hasil[i][j]=1;
}
}
while (cek<=8) {
if (cek>7) break;
com=1000;
col=2;
while (col<8) {
if (col>7) break;
ce=0;
if (M.graph[row][col]<com && row!=col && hasil[row][col]==1) {
com=M.graph[row][col];
cc=col;
ce=1;
}
col++;
}
// System.out.print("Solusi hasil colom: " + cc + " com : " + com +" cek: "+ col + " row: " + row +" \n");
jarak+=com;
for (i=1;i<8;i++) {
for (j=1;j<8;j++) {
if (j==cc) hasil[i][j]=0;
}
}
solusi[cek]=cc;
row=cc;
cek++;
}
jarak+=M.graph[cc][1];
solusi[cek]=1;
for (i=1; i<cek; i++) {
switch (solusi[i]) {
case 1: temp = temp+ "A ";
break;
case 2: temp = temp+ "B ";
break;
case 3: temp = temp+ "C ";
break;
case 4: temp = temp+ "D ";
break;
case 5: temp = temp+ "E ";
break;
case 6: temp = temp+ "F ";
break;
case 7: temp = temp+ "G ";
break;
}
if (i==cek-1){
System.out.print("Solusi : " + temp + "total waktu yang dibutuhkan " + jarak + " \n");
}
}
}
}
Main.java
package Greedy_TSP;
/**
*
* @Rezaakhmadg
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// Matrik baru =new Matrik();
//
// for (char x='A';x<'H';x++){
// for (char y='A';y<'H';y++){
// System.out.print(BF.graph[x][y]+" ");
// }
// System.out.println('\n');
// }
Greedy g = new Greedy();
// bb keluaran = new bb();
// keluaran = b.Solusi();
System.out.print("Solusi studi kasus dengan algoritma Greedy : \n");
System.out.print("****************************************************** \n");
g.showHasil();
}
}
/**
*
* @Rezaakhmadg
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// Matrik baru =new Matrik();
//
// for (char x='A';x<'H';x++){
// for (char y='A';y<'H';y++){
// System.out.print(BF.graph[x][y]+" ");
// }
// System.out.println('\n');
// }
Greedy g = new Greedy();
// bb keluaran = new bb();
// keluaran = b.Solusi();
System.out.print("Solusi studi kasus dengan algoritma Greedy : \n");
System.out.print("****************************************************** \n");
g.showHasil();
}
}
No comments:
Post a Comment