-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS_algo.java
More file actions
71 lines (60 loc) · 2.18 KB
/
Copy pathBFS_algo.java
File metadata and controls
71 lines (60 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Queue;
/**
*
* @author Boaz Sharabi
*
*This class represent BFS search algo
*https://en.wikipedia.org/wiki/Breadth-first_search
*/
public class BFS_algo extends search_algo{
HashMap<String, tile> closelist= new HashMap<>(); // make closelist
LinkedHashMap<String, tile> frontier= openlist; // make frontier
Queue<tile> queue= new LinkedList<>(); // queue
public BFS_algo(tile start, tile goal, boolean with_open, boolean with_time) {
super(start,goal,with_open,with_time);
}
@Override
public void run() {
StartTime = System.nanoTime();
if(!checkValid(start)) {
saveToFile();
return;
}
// make queue and hashmap (frontier) with initial tile
frontier.put(start.getKey(),start);
queue.add(start);
tile state=null;
while(!queue.isEmpty()) {
// if(ColorTilePuzzle.count>8000000) System.out.println(false);
// print openlist to console if needed
if(withOpen) PrintOpenList();
// state = queue.remove_front , remove state from frontier and insert to closelist
state= queue.poll();
frontier.remove(state.getKey());
closelist.put(state.getKey(), state);
//for each allowed operator on state do: {
for(int i=1;i<5;i++) {
tile child= state.move(i); // child= legal_operator(state) or null
// if child is legal_operator(state) and not in frontier and not in closelist
if(child!=null&&!closelist.containsKey(child.getKey())&&!frontier.containsKey(child.getKey())) {
// if child tile is the goal child return child as solution
if(child.equals(goal)) {
path=path(child);
cost =child.getCost();
saveToFile();
return;
}
// if child isnt the goal tile - add tile to frontier and to queue
frontier.put(child.getKey(), child);
queue.add(child);
}
//end for each allowed operator }
}
}
// there is no solution - check how much tiles generated and return "no path"
saveToFile();
}
}