Skip to content

Commit ec260b6

Browse files
author
Navjinder Virdee
committed
Cleaned the code and added comments.
1 parent 992ceff commit ec260b6

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

A-Star/A star/DistWithCoords.class

-4 Bytes
Binary file not shown.

A-Star/A star/DistWithCoords.java

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1+
//Program to Implement A-Start Agorithm.
2+
13
import java.util.Scanner;
24
import java.util.ArrayList;
35
import java.util.Arrays;
46
import java.util.PriorityQueue;
57
import java.lang.Math;
68

79
public class DistWithCoords {
10+
//class for a Vertex in the Graph.
811
static class Vertex{
9-
int vertexNum;
10-
int x;
11-
int y;
12-
long distance;
13-
long potential;
14-
long distwithPotential;
15-
int queuePos;
16-
boolean processed;
17-
ArrayList<Integer> adjList;
18-
ArrayList<Integer> costList;
12+
int vertexNum; //id of the vertex.
13+
int x; //x co-ordinate of the vertex.
14+
int y; //y co-ordinate of the vertex.
15+
long distance; //distance of the vertex from the source.
16+
long potential; //euclidean distance of the vertex and target vertex.
17+
long distwithPotential; //combining i.e suming distance and potential.
18+
int queuePos; //pos of the vertex in the PriorityQueue.
19+
boolean processed; //check if the vertex is processed while traversing the graph.
20+
ArrayList<Integer> adjList; //list of adjacent vertices from this vertex.
21+
ArrayList<Integer> costList; //list of costs or distances of adjacent vertices from this vertex.
1922

2023
public Vertex(){
2124
}
@@ -30,7 +33,9 @@ public Vertex(int vertexNum, int x, int y){
3033

3134
}
3235

36+
//Implementing PriorityQueue data structure by myself for this program. (using Min-Heap property.)
3337
static class PriorityQueue{
38+
//function to swap elements int the priorityQ.
3439
public void swap(Vertex [] graph, int [] priorityQ, int index1, int index2){
3540
int temp = priorityQ[index1];
3641

@@ -40,11 +45,13 @@ public void swap(Vertex [] graph, int [] priorityQ, int index1, int index2){
4045
priorityQ[index2]=temp;
4146
graph[temp].queuePos=index2;
4247
}
43-
48+
49+
//function to swap the source vertex with the first element in the priorityQ.
4450
public void makeQueue(Vertex [] graph,int [] forwpriorityQ, int source, int target){
4551
swap(graph, forwpriorityQ,0,source);
4652
}
4753

54+
//function to extract the min element from the priorityQ. based on the distwithPotential attribute.
4855
public int extractMin(Vertex [] graph, int [] priorityQ, int extractNum){
4956
int vertex = priorityQ[0];
5057
int size = priorityQ.length-1-extractNum;
@@ -53,6 +60,7 @@ public int extractMin(Vertex [] graph, int [] priorityQ, int extractNum){
5360
return vertex;
5461
}
5562

63+
//function to siftdown the element at the given index in the priorityQ.
5664
public void siftDown(int index, Vertex [] graph, int [] priorityQ, int size){
5765
int min = index;
5866
if(2*index+1<size && graph[priorityQ[index]].distwithPotential > graph[priorityQ[2*index+1]].distwithPotential){
@@ -67,19 +75,23 @@ public void siftDown(int index, Vertex [] graph, int [] priorityQ, int size){
6775
}
6876
}
6977

78+
//function to change the priority of an element in the priorityQ. (priority can only decrease).
7079
public void changePriority(Vertex [] graph, int [] priorityQ, int index){
7180
if((index-1)/2 > -1 && graph[priorityQ[index]].distwithPotential < graph[priorityQ[(index-1)/2]].distwithPotential){
7281
swap(graph,priorityQ,index,(index-1)/2);
7382
changePriority(graph,priorityQ,(index-1)/2);
7483
}
7584
}
7685
}
77-
86+
87+
88+
//function to calculate the euclidean distance between two vertices.
7889
private static long calcPotential(Vertex [] graph, int vertex1, int vertex2){
7990
long potential = (long)Math.sqrt(Math.pow((graph[vertex1].x - graph[vertex2].x),2) + Math.pow((graph[vertex1].y - graph[vertex2].y),2));
8091
return potential;
8192
}
8293

94+
//function to initialize the graph.
8395
public static void initialize(Vertex [] graph, int [] forwpriorityQ, int source, int target){
8496
for(int i=0;i<graph.length;i++){
8597
graph[i].processed = false;
@@ -96,6 +108,8 @@ public static void initialize(Vertex [] graph, int [] forwpriorityQ, int source,
96108

97109
}
98110

111+
112+
//function to relax the edges i.e process every adjacent edge of the given vertex.
99113
private static void relaxEdges(Vertex [] graph, int [] priorityQ, int vertex, PriorityQueue queue){
100114
ArrayList<Integer> vertexList = graph[vertex].adjList;
101115
ArrayList<Integer> costList = graph[vertex].costList;
@@ -113,51 +127,59 @@ private static void relaxEdges(Vertex [] graph, int [] priorityQ, int vertex, Pr
113127
}
114128
}
115129

130+
131+
//function to compute the distance between soure and the target.
116132
public static long computeDist(Vertex [] graph, int source, int target){
117-
133+
//create priorityQ.
118134
int [] forwpriorityQ = new int[graph.length];
135+
136+
//initialize the graph.
119137
initialize(graph,forwpriorityQ,source,target);
120138

121139
PriorityQueue queue = new PriorityQueue();
122140
queue.makeQueue(graph, forwpriorityQ,source,target);
123141

124142
for(int i=0;i<graph.length;i++){
143+
//extact the element with the min
125144
int vertex1 = queue.extractMin(graph,forwpriorityQ,i);
126145

127146
if(graph[vertex1].distance == (Long.MAX_VALUE)){
128147
return -1;
129148
}
130149

150+
//if target vertex found return the distance.
131151
if(vertex1==target){
132152
return graph[vertex1].distance;
133153
}
134154

155+
//else relax the edges of the extracted vertex.
135156
relaxEdges(graph,forwpriorityQ,vertex1,queue);
136157

137158
}
159+
160+
//if no path between source and target vertex.
138161
return -1;
139162
}
140163

141164

165+
//main function to run the program.
142166
public static void main(String args[]) {
143167
Scanner in = new Scanner(System.in);
144-
int n = in.nextInt();
145-
int m = in.nextInt();
168+
int n = in.nextInt(); //number of vertices in the graph.
169+
int m = in.nextInt(); //number of edges in the graph.
146170

147-
Vertex [] graph = new Vertex[n];
148-
149-
150-
171+
//create the graph.
172+
Vertex [] graph = new Vertex[n];
173+
174+
//get the co-ordinates of every vertex.
151175
for (int i = 0; i < n; i++) {
152176
int x, y;
153177
x = in.nextInt();
154178
y = in.nextInt();
155179
graph[i] = new Vertex(i,x,y);
156-
157180
}
158181

159-
160-
182+
//get the edges in the graph.
161183
for (int i = 0; i < m; i++) {
162184
int x, y, c;
163185
x = in.nextInt();
@@ -169,6 +191,7 @@ public static void main(String args[]) {
169191

170192
}
171193

194+
//number of queries.
172195
int q = in.nextInt();
173196

174197
for (int i = 0; i < q; i++) {
@@ -178,4 +201,4 @@ public static void main(String args[]) {
178201
System.out.println(computeDist(graph,s,t));
179202
}
180203
}
181-
}
204+
}

0 commit comments

Comments
 (0)