0% found this document useful (0 votes)
44 views3 pages

Source Code Algoritma BFS

The document summarizes an algorithm for breadth-first search (BFS) on a 2D map represented as an integer array. It defines classes for nodes on the map (MapNode) and directions of movement. The BFS method takes a starting and target point, initializes the open and closed lists, and iteratively explores neighbors until the target is found or open list is empty. It returns the lowest cost path from the start to target coordinates by tracking parent nodes.
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)
44 views3 pages

Source Code Algoritma BFS

The document summarizes an algorithm for breadth-first search (BFS) on a 2D map represented as an integer array. It defines classes for nodes on the map (MapNode) and directions of movement. The BFS method takes a starting and target point, initializes the open and closed lists, and iteratively explores neighbors until the target is found or open list is empty. It returns the lowest cost path from the start to target coordinates by tracking parent nodes.
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/ 3

Nama: M Hendro Junawarko

NPM: 18312215

Source Code Algoritma BFS

package com.hendro;
import java.awt.Point;
import java.util.LinkedList;

public class BFS {


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}
};

/*
{1, 1, 2, 1, 1},
{1, 1, -1, -1, -1},
{1, 1, -1, 1, -1},
{1, 1, -1, 1, -1},
{1, 1, 1, 1, 1}
*/

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 BFS() {
Point start = new Point(3, 4);
Point target = new Point(3, 0);

LinkedList<MapNode> path = findPath(start, target);


System.out.println("Hasil Pencarian BFS");
if (path == null) {
System.out.println("Alamat Tidak Ditemukan");
} else {
int cost = 0;
for (MapNode node : path) {
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);
}
}

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{
for(Point nb: getNeighbors(cNode.getLocation())){
MapNode child = new MapNode (nb);
if(!openList.contains(child)&& !closedList.contains(child)){
openList.add(child);
cNode.addChild(child);
}
}
}
}
if(leaves != null) {
return getPath(leaves);
}
return null;
}

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 BFS();
}
}

You might also like