1- //Program to implement Contraction Hierarchies Algorithm
1+ //Program to implement Contraction Hierarchies Algorithm.
22
33import java .util .Scanner ;
44import java .util .ArrayList ;
@@ -38,7 +38,7 @@ public Distance(){
3838}
3939 }
4040
41- //in this ids are made for the same reason, to not have to reinitialize processed variable. for every query in bidirectional dijkstra.
41+ //in this ids are made for the same reason, to not have to reinitialize processed variable for every query in bidirectional dijkstra.
4242 static class Processed {
4343boolean forwProcessed ; //is processed in forward search.
4444boolean revProcessed ; //is processed in backward search.
@@ -51,7 +51,7 @@ public Processed(){
5151}
5252 }
5353
54- //class for Vertex
54+ //class for Vertex of a graph.
5555 static class Vertex {
5656int vertexNum ;//id of the vertex.
5757ArrayList <Integer > inEdges ; //list of incoming edges to this vertex.
@@ -66,10 +66,10 @@ static class Vertex{
6666Distance distance ;
6767Processed processed ;
6868
69- //parameters for computing importance.
70- int edgeDiff ; //egdediff = sE - inE - outE.
71- long delNeighbors ; // number of contracted neighbors.
72- int shortcutCover ; // number of shotcuts to be introduces if this vertex is contracted.
69+ //parameters for computing importance according to which we will contract the vertices. Vertex wih least importance wil be contracted first .
70+ int edgeDiff ; //egdediff = sE - inE - outE. (sE=inE*outE , i.e number of shortcuts that we may have to add.)
71+ long delNeighbors ; //number of contracted neighbors.
72+ int shortcutCover ; //number of shortcuts to be introduced if this vertex is contracted.
7373
7474long importance ; //total importance = edgediff + shortcutcover + delneighbors.
7575
@@ -155,8 +155,9 @@ private void computeImportance(Vertex [] graph, Vertex vertex){
155155
156156while (PQImp .size ()!=0 ){
157157Vertex vertex = (Vertex )PQImp .poll ();
158- computeImportance (graph ,vertex );//recompute importance before contractingthe vertex.
158+ computeImportance (graph ,vertex );//recompute importance before contracting the vertex.
159159
160+ //if the vertex's recomputed importance is still minimum then contract it.
160161if (PQImp .size ()!=0 && vertex .importance > PQImp .peek ().importance ){
161162PQImp .add (vertex );
162163continue ;
@@ -166,13 +167,14 @@ private void computeImportance(Vertex [] graph, Vertex vertex){
166167vertex .orderPos = extractNum ;
167168extractNum = extractNum + 1 ;
168169
169- contractNode (graph ,vertex ,extractNum -1 );//contraction part.
170+ //contraction part.
171+ contractNode (graph ,vertex ,extractNum -1 );
170172}
171173return nodeOrdering ;
172174}
173175
174176
175- //update the neighbors of the contracted vertex that this this vertex is contracted.
177+ //update the neighbors of the contracted vertex that this vertex is contracted.
176178private void calNeighbors (Vertex [] graph ,ArrayList <Integer > inEdges , ArrayList <Integer > outEdges ){
177179for (int i =0 ;i <inEdges .size ();i ++){
178180int temp =inEdges .get (i );
@@ -218,7 +220,7 @@ private void contractNode(Vertex [] graph, Vertex vertex, int contractId){
218220}
219221}
220222
221- long max = inMax +outMax ; //total max distance.
223+ long max = inMax +outMax ; //total max distance.
222224
223225for (int i =0 ;i <inEdges .size ();i ++){
224226int inVertex = inEdges .get (i );
@@ -300,7 +302,7 @@ private boolean checkId(Vertex [] graph,int source,int target){
300302
301303//main function of this class.
302304public int [] processing (Vertex [] graph ){
303- computeImportance (graph );//find initial importance by traversing all vertices.
305+ computeImportance (graph );//find initial importance by traversing all vertices.
304306int [] nodeOrdering = preProcess (graph );
305307return nodeOrdering ;
306308}
@@ -491,4 +493,4 @@ public static void main(String args[]) {
491493 System .out .println (bd .computeDist (graph ,u ,v ,i ,nodeOrdering ));
492494 }
493495 }
494- }
496+ }
0 commit comments