Skip to content

Commit a63cb70

Browse files
Merge pull request TheAlgorithms#1350 from MarcosVillacanas/marcosvillacanas-A-Star
A-Star Algorithm Included
2 parents 4e058de + c21ae9a commit a63cb70

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

DataStructures/Graphs/A_Star.java

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
Time Complexity = O(E), where E is equal to the number of edges
3+
*/
4+
5+
package A_Star;
6+
7+
import java.util.*;
8+
9+
public class A_Star {
10+
11+
private static class Graph {
12+
//Graph's structure can be changed only applying changes to this class.
13+
private ArrayList<Edge> [] graph;
14+
15+
//Initialise ArrayLists in Constructor
16+
public Graph(int size) {
17+
this.graph = new ArrayList[size];
18+
for (int i = 0; i < size; i++) {
19+
this.graph[i] = new ArrayList<>();
20+
}
21+
}
22+
23+
private ArrayList<Edge> getNeighbours(int from) { return this.graph[from]; }
24+
25+
//Graph is bidirectional, for just one direction remove second instruction of this method.
26+
private void addEdge (Edge edge) {
27+
this.graph[edge.getFrom()].add(new Edge(edge.getFrom(), edge.getTo(), edge.getWeight()));
28+
this.graph[edge.getTo()].add(new Edge(edge.getTo(), edge.getFrom(), edge.getWeight()));
29+
}
30+
}
31+
32+
private static class Edge {
33+
private int from;
34+
private int to;
35+
private int weight;
36+
37+
public Edge(int from, int to, int weight) {
38+
this.from = from;
39+
this.to = to;
40+
this.weight = weight;
41+
}
42+
43+
public int getFrom() { return from; }
44+
45+
public int getTo() { return to; }
46+
47+
public int getWeight() { return weight; }
48+
}
49+
50+
//class to iterate during the algorithm execution, and also used to return the solution.
51+
private static class PathAndDistance {
52+
private int distance; //distance advanced so far.
53+
private ArrayList<Integer> path; //list of visited nodes in this path.
54+
private int estimated; //heuristic value associated to the last node od the path (current node).
55+
56+
public PathAndDistance(int distance, ArrayList<Integer> path, int estimated) {
57+
this.distance = distance;
58+
this.path = path;
59+
this.estimated = estimated;
60+
}
61+
62+
public int getDistance() { return distance; }
63+
64+
public ArrayList<Integer> getPath() { return path; }
65+
66+
public int getEstimated() { return estimated; }
67+
68+
private void printSolution () {
69+
if (this.path != null)
70+
System.out.println("Optimal path: " + this.path.toString() +
71+
", distance: " + this.distance);
72+
else
73+
System.out.println("There is no path available to connect the points");
74+
}
75+
}
76+
77+
private static void initializeGraph(Graph graph, ArrayList<Integer> data) {
78+
for (int i = 0; i < data.size(); i+=4) {
79+
graph.addEdge(new Edge(data.get(i), data.get(i + 1), data.get(i + 2)));
80+
}
81+
/*
82+
.x. node
83+
(y) cost
84+
- or | or / bidirectional connection
85+
86+
( 98)- .7. -(86)- .4.
87+
|
88+
( 85)- .17. -(142)- .18. -(92)- .8. -(87)- .11.
89+
|
90+
. 1. -------------------- (160)
91+
| \ |
92+
(211) \ .6.
93+
| \ |
94+
. 5. (101)-.13. -(138) (115)
95+
| | | /
96+
( 99) ( 97) | /
97+
| | | /
98+
.12. -(151)- .15. -(80)- .14. | /
99+
| | | | /
100+
( 71) (140) (146)- .2. -(120)
101+
| | |
102+
.19. -( 75)- . 0. .10. -(75)- .3.
103+
| |
104+
(118) ( 70)
105+
| |
106+
.16. -(111)- .9.
107+
*/
108+
}
109+
110+
public static void main(String[] args) {
111+
//heuristic function optimistic values
112+
int[] heuristic = {366, 0, 160, 242, 161, 178, 77, 151, 226,
113+
244, 241, 234, 380, 98, 193, 253, 329, 80, 199, 374};
114+
115+
Graph graph = new Graph(20);
116+
ArrayList<Integer> graphData = new ArrayList<>(Arrays.asList(0, 19, 75, null,
117+
0, 15, 140, null, 0, 16, 118, null, 19, 12, 71, null, 12, 15, 151, null,
118+
16, 9, 111, null, 9, 10, 70, null, 10, 3, 75, null, 3, 2, 120, null,
119+
2, 14, 146, null, 2, 13, 138, null, 2, 6, 115, null, 15, 14, 80, null,
120+
15, 5, 99, null, 14, 13, 97, null, 5, 1, 211, null, 13, 1, 101, null,
121+
6, 1, 160, null, 1, 17, 85, null, 17, 7, 98, null, 7, 4, 86, null,
122+
17, 18, 142, null, 18, 8, 92, null, 8, 11, 87));
123+
initializeGraph(graph, graphData);
124+
125+
PathAndDistance solution = aStar(3, 1, graph, heuristic);
126+
solution.printSolution();
127+
128+
}
129+
130+
public static PathAndDistance aStar(int from, int to, Graph graph, int[] heuristic) {
131+
//nodes are prioritised by the less value of the current distance of their paths, and the estimated value
132+
//given by the heuristic function to reach the destination point from the current point.
133+
PriorityQueue<PathAndDistance> queue = new PriorityQueue<>
134+
(Comparator.comparingInt(a -> (a.getDistance() + a.getEstimated())));
135+
136+
//dummy data to start the algorithm from the beginning point
137+
queue.add(new PathAndDistance(0, new ArrayList<>(Arrays.asList(from)), 0));
138+
139+
boolean solutionFound = false;
140+
PathAndDistance currentData = new PathAndDistance(-1, null, -1);
141+
while (!queue.isEmpty() && !solutionFound) {
142+
currentData = queue.poll(); //first in the queue, best node so keep exploring.
143+
int currentPosition = currentData.getPath().get(currentData.getPath().size() - 1); //current node.
144+
if (currentPosition == to)
145+
solutionFound = true;
146+
else
147+
for (Edge edge : graph.getNeighbours(currentPosition))
148+
if (!currentData.getPath().contains(edge.getTo())) { //Avoid Cycles
149+
ArrayList<Integer> updatedPath = new ArrayList<>(currentData.getPath());
150+
updatedPath.add(edge.getTo()); //Add the new node to the path, update the distance,
151+
// and the heuristic function value associated to that path.
152+
queue.add(new PathAndDistance(currentData.getDistance() + edge.getWeight(),
153+
updatedPath, heuristic[edge.getTo()]));
154+
}
155+
}
156+
return (solutionFound) ? currentData : new PathAndDistance(-1, null, -1);
157+
//Out of while loop, if there is a solution, the current Data stores the optimal path, and its distance
158+
}
159+
}

0 commit comments

Comments
 (0)