0% found this document useful (0 votes)
54 views5 pages

Source Code Algoritma Greedy: Nama: M Hendro Junawarko NPM: 18312215

The document contains the source code for an algorithm that finds the shortest path between a starting and ending point on a 2D grid map using greedy and modified greedy best-first search approaches. It defines methods for finding the path, getting neighbors of a point, and calculating heuristics to estimate distance to the target. The main method runs tests that find and output paths and costs using both greedy and modified greedy algorithms.
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)
54 views5 pages

Source Code Algoritma Greedy: Nama: M Hendro Junawarko NPM: 18312215

The document contains the source code for an algorithm that finds the shortest path between a starting and ending point on a 2D grid map using greedy and modified greedy best-first search approaches. It defines methods for finding the path, getting neighbors of a point, and calculating heuristics to estimate distance to the target. The main method runs tests that find and output paths and costs using both greedy and modified greedy algorithms.
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/ 5

Nama: M Hendro Junawarko

NPM: 18312215

Source Code Algoritma Greedy

package com.hendro;

import java.awt.Point;
import java.util.LinkedList;

public class Greedy {

int maps[][] = {
{1, 3, 2, 3, 3},
{2, 3, -1, -1, -1},
{2, 4, -1, 1, -1},
{5, 2, -1, 3, -1},
{2, 3, 5, 1, 4}
};

Point direction[] = {
new Point(1, 0), new Point(1, 1),
new Point(0, 1), new Point(-1, 1),
new Point(-1, 0), new Point(-1, -1),
new Point(0, -1), new Point(1, -1),};

public Greedy() {
Point start = new Point(3, 4);
Point target = new Point(3, 0);
System.out.println("Hasil Pencarian Greedy");
LinkedList<MapNode> path1 = findPath(start, target);

if (path1 == null) {
System.out.println("Alamat Tidak Ditemukan");
} else {
int cost = 0;
for (MapNode node : path1) {
Point loc = node.getLocation();
cost += maps[loc.y][loc.x];
if (!node.getLocation().equals(target)) {
System.out.println(node.getId() + " -> ");
} else {
System.out.println("(" + node.getId() + ")");
}
}
System.out.println("\nTotal cost : " + cost);
}
System.out.println("Hasil pencarian Modified Greedy BFS");
LinkedList<MapNode> path2 = findPathModified(start, target);
if (path2 == null) {
System.out.println("Alaamt tidak ditemukan");
} else {
int cost = 0;
for (MapNode node : path2) {
Point loc = node.getLocation();
cost += maps[loc.y][loc.x];
if (!node.getLocation().equals(target)) {
System.out.print(node.getId() + " -> ");
} else {
System.out.print("(" + node.getId() + ")");
}
}
System.out.println("\nTotal cost : " + cost);

}
}

public LinkedList<MapNode> findPath(Point src, Point dst) {


LinkedList<MapNode> openList = new LinkedList<MapNode>();
LinkedList<MapNode> closedList = new LinkedList<MapNode>();

openList.add(new MapNode(src));
MapNode leaves = null;
while (openList.size() > 0) {
MapNode cNode = openList.removeFirst();
closedList.add(cNode);
if (cNode.equals(dst)) {
leaves = cNode;
break;
} else {
Point bestPoint = null;
double bestDistance = Double.MAX_VALUE;
for (Point nb : getNeighbors(cNode.getLocation())) {
double tmpDistance = getSLDHeuristic(nb, dst);
if (bestPoint == null || tmpDistance < bestDistance) {
bestPoint = nb;

bestDistance = tmpDistance;
}
}
if (bestPoint != null) {
MapNode child = new MapNode(bestPoint);
if (!openList.contains(child)
&& !closedList.contains(child)) {
openList.add(child);
cNode.addChild(child);
}
}
}
}
if (leaves != null) {
return getPath(leaves);
}
return null;
}

public LinkedList<MapNode> findPathModified(Point src, Point dst) {


LinkedList<MapNode> openList = new LinkedList<MapNode>();
LinkedList<MapNode> closedList = new LinkedList<MapNode>();
openList.add(new MapNode(src));
MapNode leaves = null;
while (openList.size() > 0) {

MapNode cNode = openList.removeFirst();


closedList.add(cNode);
if (cNode.equals(dst)) {
leaves = cNode;
break;
} else {
for (Point nb : getNeighbors(cNode.getLocation())) {
MapNode child = new MapNode(nb);
if (!openList.contains(child)
&& !closedList.contains(child)) {
addToList(openList, dst, child);
cNode.addChild(child);
}
}
}
}
if (leaves != null) {
return getPath(leaves);
}
return null;
}

private void addToList(LinkedList<MapNode> openList, Point dst, MapNode node) {


int i = 0;
double hValue = getSLDHeuristic(node.getLocation(), dst);
for (i = 0; i < openList.size(); i++) {
double tmpH = getSLDHeuristic(openList.get(i).getLocation(), dst);
if (hValue < tmpH) {
break;
}
}
openList.add(i, node);

private double getSLDHeuristic(Point src, Point dst) {


double a = src.x - dst.x;
double b = src.y - dst.y;
return Math.sqrt(a * a + b * b);

public LinkedList<MapNode> getPath(MapNode node) {


LinkedList<MapNode> path = new LinkedList<MapNode>();
do {
path.addFirst(node);
node = (MapNode) node.getParent();
} while (node != null);
return path;
}

public LinkedList<Point> getNeighbors(Point src) {


LinkedList<Point> neighbors = new LinkedList<Point>();
for (Point dir : direction) {
Point ttg = new Point(src.x + dir.x, src.y + dir.y);
if (ttg.x >= 0 && ttg.x < maps[0].length && ttg.y >= 0 && ttg.y < maps.length &&
maps[ttg.y][ttg.x] > -1) {

neighbors.add(ttg);
}
}
return neighbors;
}

public static void main(String arg[]) {


new Greedy();
}
}

You might also like