You are on page 1of 6

WeightedGraph.

java
1 package bab3;
2
3 public class WeightedGraph {
4
5 private int[][] edges;
6 private Object[] labels;
7
8 public WeightedGraph(int n) {
9 edges = new int[n][n];
10 labels = new Object[n];
11 }
12
13 public void setLabel(int vertex, Object label) {
14 labels[vertex] = label;
15 }
16
17 public Object getLabel(int vertex) {
18 return labels[vertex];
19 }
20
21 public void addEdge(int source, int target, int w) {
22 edges[source][target] = w;
23 edges[target][source] = w;
24 }
25
26 public int getWeight(int source, int target) {
27 return edges[source][target];
28 }
29
30 public int[] neighbors(int vertex) {
31 int count = 0;
32 for (int i = 0; i < edges[vertex].length; i++) {
33 if (edges[vertex][i] > 0) {
34 count++;
35 }
36 }
37 final int[] answer = new int[count];
38 count = 0;
39 for (int i = 0; i < edges[vertex].length; i++) {
40 if (edges[vertex][i] > 0) {
41 answer[count++] = i;
42 }
43 }
44 return answer;
45 }
46 }

Sequence.java
1 package bab3;
2
3 import java.util.ArrayList;
4
5 public class Sequence {
6
7 public ArrayList<Integer> seq;
8 public double cost;
9
10 public Sequence(int i) {
11 this.seq = new ArrayList();
12 this.seq.add(i);
13 }
14
15 public Sequence(Sequence s) {
16 this.seq = new ArrayList(s.seq);
17 this.cost = s.cost;
18 }
19
20 public void addNext(int i, double cost) {
21 seq.add(i);
22 this.cost += cost;
23 }
24
25 public int getLast() {
26 return (int) seq.get(seq.size() - 1);
27 }
28 }

AStarSearch.java
1 package bab3;
2
3 import java.util.ArrayList;
4 import java.util.Scanner;
5
6 public class AStarSearch {
7
8 public WeightedGraph graph;
9 public ArrayList<Sequence> list;
10 public int goalIndex;
11 public int[] heuristicValue;
12
13 public AStarSearch(WeightedGraph graph) {
14 this.list = new ArrayList<>();
15 this.graph = graph;
16 }
17
18 public void setGoal(int goal) {
19 this.goalIndex = goal;
20 }
21
22 public void setHeuristic(int[] heuristic) {
23 this.heuristicValue = heuristic;
24 }
25
26 public void findRoute(int startIndex) {
27 if (startIndex == goalIndex) {
28 System.out.println("Already at goal");
29 return;
30 }
31 int minIndex = -1, node = startIndex;
32 double costValue, fn, min, sequenceCost;
33 Sequence sequence = new Sequence(startIndex), sn;
34 list.add(sequence);
35 printList();
36 System.out.print("Expanded Node : ");
37 printSequence(sequence);
38 do {
39 for (int n : graph.neighbors(node)) {
40 sn = new Sequence(sequence);
41 costValue = graph.getWeight(node, n);
42 fn = costValue + heuristicValue[n];
43 sn.addNext(n, fn);
44 list.add(sn);
45 }
46 list.remove(sequence);
47 min = Integer.MAX_VALUE;
48 for (int i = 0; i < list.size(); i++) {
49 sequenceCost = list.get(i).cost;
50 if (sequenceCost < min) {
51 min = sequenceCost;
52 minIndex = i;
53 }
54 }
55 sequence = list.get(minIndex);
56 node = sequence.getLast();
57 if (list.isEmpty()) {
58 System.out.println("No Possible Route
59 Found!");
60 return;
61 }
62 printList();
63 System.out.print("Expanded Node : ");
64 printSequence(sequence);
65 }while (node != goalIndex);
66 System.out.println("Shortest Path Found!");
67 printSequence(sequence);
68 }
69
70 public void printList() {
71 System.out.println("Current Queue : ");
72 System.out.println("======================");
73 for (int i = 0; i < list.size(); i++) {
74 printSequence(list.get(i));
75 }
76 System.out.println("======================");
77 }
78
79 public void printSequence(Sequence s) {
80 for (int i = 0; i < s.seq.size(); i++) {
81 int num = s.seq.get(i);
82 System.out.print(graph.getLabel(num) + " ");
83 }
84 System.out.println("(" + s.cost + ")");
85 }
86
87 public static void main(String[] args) {
88 WeightedGraph t = new WeightedGraph(7);
89 t.setLabel(0, "A");
90 t.setLabel(1, "B");
91 t.setLabel(2, "C");
92 t.setLabel(3, "D");
93 t.setLabel(4, "E");
94 t.setLabel(5, "F");
95 t.setLabel(6, "G");
96 t.addEdge(0, 1, 21);
97 t.addEdge(0, 5, 21);
98 t.addEdge(0, 6, 19);
99 t.addEdge(1, 2, 24);
100 t.addEdge(1, 3, 35);
101 t.addEdge(1, 6, 13);
102 t.addEdge(2, 3, 14);
103 t.addEdge(3, 4, 14);
104 t.addEdge(3, 5, 35);
105 t.addEdge(4, 5, 24);
106 t.addEdge(5, 6, 13);
107
108 Scanner input = new Scanner(System.in);
109 System.out.print("Select Initial Node : ");
110 int start = input.next().codePointAt(0) % 65;
111 int[] heuristicValue = {52, 35, 14, 0, 14, 35, 27};
112 AStarSearch astar = new AStarSearch(t);
113 astar.setGoal(3);
114 astar.setHeuristic(heuristicValue);
115 astar.findRoute(start);
116 }
117 }

PEMBAHASAN :

Kelas WeightedGraph, untuk merepresentasikan graf dalam bentuk matrix adjacency


berbobot dimana bobotnya didapat dari nilai g(n) atau jarak suatu node ke tetangganya.

Kelas Sequence, untuk merepresentasikan node-node yang telah dilalui beserta total
cost yang dibutuhkan.

Kelas AStarSearch, kelas utama yang melakukan pencarian rute terpendek dari graf
menggunakan metode A* Search dengan memanfaatkan WeightedGraph dan Sequence.

- Konstruktor, memerlukan WeightedGraph yang telah diset node dan edge nya
- setGoal, menentukan tujuan akhir dari pencarian
- setHeuristic, menentukan jarak heuristic masing-masing ke node ke tujuan
- findRoute, melakukan pencarian A*, dimana node dengan cost terkecil akan
terus diekspan hingga node dengan cost terkecil itu sama dengan goal.
- printLit, mencetak isi list yaitu sequence
- printSequence, mencetak isi sequence yaitu urutan node dan total costnya
- main, dilakukan penginisialisasian graf dengan menentukan node dan edge nya,
penerimaan input node start, nilai heuristic dan pemanggilan findRoute.

You might also like