-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar.java
More file actions
160 lines (132 loc) · 4.91 KB
/
AStar.java
File metadata and controls
160 lines (132 loc) · 4.91 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.*;
public class AStar {
static class Node {
int id;
List<Edge> neighbors;
public Node(int id) {
this.id = id;
this.neighbors = new ArrayList<>();
}
public void addNeighbor(Edge edge) {
neighbors.add(edge);
}
@Override
public String toString() {
return "Node-" + id;
}
}
static class Edge {
Node target;
int weight;
public Edge(Node target, int weight) {
this.target = target;
this.weight = weight;
}
}
static class PathNode implements Comparable<PathNode> {
Node node;
int gScore;
int fScore;
public PathNode(Node node, int gScore, int fScore) {
this.node = node;
this.gScore = gScore;
this.fScore = fScore;
}
@Override
public int compareTo(PathNode other) {
return Integer.compare(this.fScore, other.fScore);
}
}
public static List<Node> astar(Node[] graph, Node source, Node destination) {
Map<Node, Integer> gScores = new HashMap<>();
Map<Node, Integer> fScores = new HashMap<>();
Map<Node, Node> cameFrom = new HashMap<>();
for (Node node : graph) {
gScores.put(node, Integer.MAX_VALUE);
fScores.put(node, Integer.MAX_VALUE);
}
gScores.put(source, 0);
fScores.put(source, heuristic(source, destination));
PriorityQueue<PathNode> openSet = new PriorityQueue<>();
openSet.offer(new PathNode(source, 0, heuristic(source, destination)));
while (!openSet.isEmpty()) {
PathNode current = openSet.poll();
Node current_node = current.node;
if (current_node == destination) {
return reconstructPath(cameFrom, destination);
}
for (Edge neighborEdge : current_node.neighbors) {
Node neighbor = neighborEdge.target;
int tentative_gScore = gScores.get(current_node) + neighborEdge.weight;
if (tentative_gScore < gScores.get(neighbor)) {
cameFrom.put(neighbor, current_node);
gScores.put(neighbor, tentative_gScore);
fScores.put(neighbor, tentative_gScore + heuristic(neighbor, destination));
openSet.offer(new PathNode(neighbor, tentative_gScore, fScores.get(neighbor)));
}
}
}
return null; // No path found
}
static int heuristic(Node a, Node b) {
// Implement heuristic function here (e.g., Euclidean distance, Manhattan distance, etc.)
return 0; // Placeholder heuristic value
}
static List<Node> reconstructPath(Map<Node, Node> cameFrom, Node current) {
List<Node> path = new ArrayList<>();
while (cameFrom.containsKey(current)) {
path.add(0, current);
current = cameFrom.get(current);
}
path.add(0, current);
return path;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of nodes: ");
int numNodes = scanner.nextInt();
Node[] graph = new Node[numNodes];
System.out.print("Enter the number of edges: ");
int numEdges = scanner.nextInt();
for (int i = 0; i < numNodes; i++) {
graph[i] = new Node(i);
}
System.out.println("Enter edge details (source destination weight):");
for (int i = 0; i < numEdges; i++) {
int sourceId = scanner.nextInt();
int targetId = scanner.nextInt();
int weight = scanner.nextInt();
graph[sourceId].addNeighbor(new Edge(graph[targetId], weight));
}
System.out.print("Enter the source node id: ");
int sourceId = scanner.nextInt();
Node source = graph[sourceId];
System.out.print("Enter the destination node id: ");
int destinationId = scanner.nextInt();
Node destination = graph[destinationId];
List<Node> shortestPath = astar(graph, source, destination);
if (shortestPath != null) {
System.out.println("Shortest path: " + shortestPath);
} else {
System.out.println("No path found.");
}
scanner.close();
}
/*
* Test Case:
* Input:
* Enter the number of nodes: 4
* Enter the number of edges: 5
* Enter edge details (source destination weight):
* 0 1 4
* 0 2 2
* 1 2 1
* 1 3 5
* 2 3 8
* Enter the source node id: 0
* Enter the destination node id: 3
*
* Output:
* Shortest path: [Node-0, Node-1, Node-3]
*/
}