├── Graph_M.java ├── Heap.java ├── LICENSE ├── README.md └── Requirements.md /Graph_M.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | 5 | public class Graph_M 6 | { 7 | public class Vertex 8 | { 9 | HashMap nbrs = new HashMap<>(); 10 | } 11 | 12 | static HashMap vtces; 13 | 14 | public Graph_M() 15 | { 16 | vtces = new HashMap<>(); 17 | } 18 | 19 | public int numVetex() 20 | { 21 | return this.vtces.size(); 22 | } 23 | 24 | public boolean containsVertex(String vname) 25 | { 26 | return this.vtces.containsKey(vname); 27 | } 28 | 29 | public void addVertex(String vname) 30 | { 31 | Vertex vtx = new Vertex(); 32 | vtces.put(vname, vtx); 33 | } 34 | 35 | public void removeVertex(String vname) 36 | { 37 | Vertex vtx = vtces.get(vname); 38 | ArrayList keys = new ArrayList<>(vtx.nbrs.keySet()); 39 | 40 | for (String key : keys) 41 | { 42 | Vertex nbrVtx = vtces.get(key); 43 | nbrVtx.nbrs.remove(vname); 44 | } 45 | 46 | vtces.remove(vname); 47 | } 48 | 49 | public int numEdges() 50 | { 51 | ArrayList keys = new ArrayList<>(vtces.keySet()); 52 | int count = 0; 53 | 54 | for (String key : keys) 55 | { 56 | Vertex vtx = vtces.get(key); 57 | count = count + vtx.nbrs.size(); 58 | } 59 | 60 | return count / 2; 61 | } 62 | 63 | public boolean containsEdge(String vname1, String vname2) 64 | { 65 | Vertex vtx1 = vtces.get(vname1); 66 | Vertex vtx2 = vtces.get(vname2); 67 | 68 | if (vtx1 == null || vtx2 == null || !vtx1.nbrs.containsKey(vname2)) { 69 | return false; 70 | } 71 | 72 | return true; 73 | } 74 | 75 | public void addEdge(String vname1, String vname2, int value) 76 | { 77 | Vertex vtx1 = vtces.get(vname1); 78 | Vertex vtx2 = vtces.get(vname2); 79 | 80 | if (vtx1 == null || vtx2 == null || vtx1.nbrs.containsKey(vname2)) { 81 | return; 82 | } 83 | 84 | vtx1.nbrs.put(vname2, value); 85 | vtx2.nbrs.put(vname1, value); 86 | } 87 | 88 | public void removeEdge(String vname1, String vname2) 89 | { 90 | Vertex vtx1 = vtces.get(vname1); 91 | Vertex vtx2 = vtces.get(vname2); 92 | 93 | //check if the vertices given or the edge between these vertices exist or not 94 | if (vtx1 == null || vtx2 == null || !vtx1.nbrs.containsKey(vname2)) { 95 | return; 96 | } 97 | 98 | vtx1.nbrs.remove(vname2); 99 | vtx2.nbrs.remove(vname1); 100 | } 101 | 102 | public void display_Map() 103 | { 104 | System.out.println("\t Delhi Metro Map"); 105 | System.out.println("\t------------------"); 106 | System.out.println("----------------------------------------------------\n"); 107 | ArrayList keys = new ArrayList<>(vtces.keySet()); 108 | 109 | for (String key : keys) 110 | { 111 | String str = key + " =>\n"; 112 | Vertex vtx = vtces.get(key); 113 | ArrayList vtxnbrs = new ArrayList<>(vtx.nbrs.keySet()); 114 | 115 | for (String nbr : vtxnbrs) 116 | { 117 | str = str + "\t" + nbr + "\t"; 118 | if (nbr.length()<16) 119 | str = str + "\t"; 120 | if (nbr.length()<8) 121 | str = str + "\t"; 122 | str = str + vtx.nbrs.get(nbr) + "\n"; 123 | } 124 | System.out.println(str); 125 | } 126 | System.out.println("\t------------------"); 127 | System.out.println("---------------------------------------------------\n"); 128 | 129 | } 130 | 131 | public void display_Stations() 132 | { 133 | System.out.println("\n***********************************************************************\n"); 134 | ArrayList keys = new ArrayList<>(vtces.keySet()); 135 | int i=1; 136 | for(String key : keys) 137 | { 138 | System.out.println(i + ". " + key); 139 | i++; 140 | } 141 | System.out.println("\n***********************************************************************\n"); 142 | } 143 | 144 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// 145 | 146 | public boolean hasPath(String vname1, String vname2, HashMap processed) 147 | { 148 | // DIR EDGE 149 | if (containsEdge(vname1, vname2)) { 150 | return true; 151 | } 152 | 153 | //MARK AS DONE 154 | processed.put(vname1, true); 155 | 156 | Vertex vtx = vtces.get(vname1); 157 | ArrayList nbrs = new ArrayList<>(vtx.nbrs.keySet()); 158 | 159 | //TRAVERSE THE NBRS OF THE VERTEX 160 | for (String nbr : nbrs) 161 | { 162 | 163 | if (!processed.containsKey(nbr)) 164 | if (hasPath(nbr, vname2, processed)) 165 | return true; 166 | } 167 | 168 | return false; 169 | } 170 | 171 | 172 | private class DijkstraPair implements Comparable 173 | { 174 | String vname; 175 | String psf; 176 | int cost; 177 | 178 | /* 179 | The compareTo method is defined in Java.lang.Comparable. 180 | Here, we override the method because the conventional compareTo method 181 | is used to compare strings,integers and other primitive data types. But 182 | here in this case, we intend to compare two objects of DijkstraPair class. 183 | */ 184 | 185 | /* 186 | Removing the overriden method gives us this errror: 187 | The type Graph_M.DijkstraPair must implement the inherited abstract method Comparable.compareTo(Graph_M.DijkstraPair) 188 | 189 | This is because DijkstraPair is not an abstract class and implements Comparable interface which has an abstract 190 | method compareTo. In order to make our class concrete(a class which provides implementation for all its methods) 191 | we have to override the method compareTo 192 | */ 193 | @Override 194 | public int compareTo(DijkstraPair o) 195 | { 196 | return o.cost - this.cost; 197 | } 198 | } 199 | 200 | public int dijkstra(String src, String des, boolean nan) 201 | { 202 | int val = 0; 203 | ArrayList ans = new ArrayList<>(); 204 | HashMap map = new HashMap<>(); 205 | 206 | Heap heap = new Heap<>(); 207 | 208 | for (String key : vtces.keySet()) 209 | { 210 | DijkstraPair np = new DijkstraPair(); 211 | np.vname = key; 212 | //np.psf = ""; 213 | np.cost = Integer.MAX_VALUE; 214 | 215 | if (key.equals(src)) 216 | { 217 | np.cost = 0; 218 | np.psf = key; 219 | } 220 | 221 | heap.add(np); 222 | map.put(key, np); 223 | } 224 | 225 | //keep removing the pairs while heap is not empty 226 | while (!heap.isEmpty()) 227 | { 228 | DijkstraPair rp = heap.remove(); 229 | 230 | if(rp.vname.equals(des)) 231 | { 232 | val = rp.cost; 233 | break; 234 | } 235 | 236 | map.remove(rp.vname); 237 | 238 | ans.add(rp.vname); 239 | 240 | Vertex v = vtces.get(rp.vname); 241 | for (String nbr : v.nbrs.keySet()) 242 | { 243 | if (map.containsKey(nbr)) 244 | { 245 | int oc = map.get(nbr).cost; 246 | Vertex k = vtces.get(rp.vname); 247 | int nc; 248 | if(nan) 249 | nc = rp.cost + 120 + 40*k.nbrs.get(nbr); 250 | else 251 | nc = rp.cost + k.nbrs.get(nbr); 252 | 253 | if (nc < oc) 254 | { 255 | DijkstraPair gp = map.get(nbr); 256 | gp.psf = rp.psf + nbr; 257 | gp.cost = nc; 258 | 259 | heap.updatePriority(gp); 260 | } 261 | } 262 | } 263 | } 264 | return val; 265 | } 266 | 267 | private class Pair 268 | { 269 | String vname; 270 | String psf; 271 | int min_dis; 272 | int min_time; 273 | } 274 | 275 | public String Get_Minimum_Distance(String src, String dst) 276 | { 277 | int min = Integer.MAX_VALUE; 278 | //int time = 0; 279 | String ans = ""; 280 | HashMap processed = new HashMap<>(); 281 | LinkedList stack = new LinkedList<>(); 282 | 283 | // create a new pair 284 | Pair sp = new Pair(); 285 | sp.vname = src; 286 | sp.psf = src + " "; 287 | sp.min_dis = 0; 288 | sp.min_time = 0; 289 | 290 | // put the new pair in stack 291 | stack.addFirst(sp); 292 | 293 | // while stack is not empty keep on doing the work 294 | while (!stack.isEmpty()) 295 | { 296 | // remove a pair from stack 297 | Pair rp = stack.removeFirst(); 298 | 299 | if (processed.containsKey(rp.vname)) 300 | { 301 | continue; 302 | } 303 | 304 | // processed put 305 | processed.put(rp.vname, true); 306 | 307 | //if there exists a direct edge b/w removed pair and destination vertex 308 | if (rp.vname.equals(dst)) 309 | { 310 | int temp = rp.min_dis; 311 | if(temp nbrs = new ArrayList<>(rpvtx.nbrs.keySet()); 320 | 321 | for(String nbr : nbrs) 322 | { 323 | // process only unprocessed nbrs 324 | if (!processed.containsKey(nbr)) { 325 | 326 | // make a new pair of nbr and put in queue 327 | Pair np = new Pair(); 328 | np.vname = nbr; 329 | np.psf = rp.psf + nbr + " "; 330 | np.min_dis = rp.min_dis + rpvtx.nbrs.get(nbr); 331 | //np.min_time = rp.min_time + 120 + 40*rpvtx.nbrs.get(nbr); 332 | stack.addFirst(np); 333 | } 334 | } 335 | } 336 | ans = ans + Integer.toString(min); 337 | return ans; 338 | } 339 | 340 | 341 | public String Get_Minimum_Time(String src, String dst) 342 | { 343 | int min = Integer.MAX_VALUE; 344 | String ans = ""; 345 | HashMap processed = new HashMap<>(); 346 | LinkedList stack = new LinkedList<>(); 347 | 348 | // create a new pair 349 | Pair sp = new Pair(); 350 | sp.vname = src; 351 | sp.psf = src + " "; 352 | sp.min_dis = 0; 353 | sp.min_time = 0; 354 | 355 | // put the new pair in queue 356 | stack.addFirst(sp); 357 | 358 | // while queue is not empty keep on doing the work 359 | while (!stack.isEmpty()) { 360 | 361 | // remove a pair from queue 362 | Pair rp = stack.removeFirst(); 363 | 364 | if (processed.containsKey(rp.vname)) 365 | { 366 | continue; 367 | } 368 | 369 | // processed put 370 | processed.put(rp.vname, true); 371 | 372 | //if there exists a direct edge b/w removed pair and destination vertex 373 | if (rp.vname.equals(dst)) 374 | { 375 | int temp = rp.min_time; 376 | if(temp nbrs = new ArrayList<>(rpvtx.nbrs.keySet()); 385 | 386 | for (String nbr : nbrs) 387 | { 388 | // process only unprocessed nbrs 389 | if (!processed.containsKey(nbr)) { 390 | 391 | // make a new pair of nbr and put in queue 392 | Pair np = new Pair(); 393 | np.vname = nbr; 394 | np.psf = rp.psf + nbr + " "; 395 | //np.min_dis = rp.min_dis + rpvtx.nbrs.get(nbr); 396 | np.min_time = rp.min_time + 120 + 40*rpvtx.nbrs.get(nbr); 397 | stack.addFirst(np); 398 | } 399 | } 400 | } 401 | Double minutes = Math.ceil((double)min / 60); 402 | ans = ans + Double.toString(minutes); 403 | return ans; 404 | } 405 | 406 | public ArrayList get_Interchanges(String str) 407 | { 408 | ArrayList arr = new ArrayList<>(); 409 | String res[] = str.split(" "); 410 | arr.add(res[0]); 411 | int count = 0; 412 | for(int i=1;i "+res[i+1]); 429 | i++; 430 | count++; 431 | } 432 | } 433 | else 434 | { 435 | arr.add(res[i]); 436 | } 437 | } 438 | arr.add(Integer.toString(count)); 439 | arr.add(res[res.length-1]); 440 | return arr; 441 | } 442 | 443 | public static void Create_Metro_Map(Graph_M g) 444 | { 445 | g.addVertex("Noida Sector 62~B"); 446 | g.addVertex("Botanical Garden~B"); 447 | g.addVertex("Yamuna Bank~B"); 448 | g.addVertex("Rajiv Chowk~BY"); 449 | g.addVertex("Vaishali~B"); 450 | g.addVertex("Moti Nagar~B"); 451 | g.addVertex("Janak Puri West~BO"); 452 | g.addVertex("Dwarka Sector 21~B"); 453 | g.addVertex("Huda City Center~Y"); 454 | g.addVertex("Saket~Y"); 455 | g.addVertex("Vishwavidyalaya~Y"); 456 | g.addVertex("Chandni Chowk~Y"); 457 | g.addVertex("New Delhi~YO"); 458 | g.addVertex("AIIMS~Y"); 459 | g.addVertex("Shivaji Stadium~O"); 460 | g.addVertex("DDS Campus~O"); 461 | g.addVertex("IGI Airport~O"); 462 | g.addVertex("Rajouri Garden~BP"); 463 | g.addVertex("Netaji Subhash Place~PR"); 464 | g.addVertex("Punjabi Bagh West~P"); 465 | 466 | g.addEdge("Noida Sector 62~B", "Botanical Garden~B", 8); 467 | g.addEdge("Botanical Garden~B", "Yamuna Bank~B", 10); 468 | g.addEdge("Yamuna Bank~B", "Vaishali~B", 8); 469 | g.addEdge("Yamuna Bank~B", "Rajiv Chowk~BY", 6); 470 | g.addEdge("Rajiv Chowk~BY", "Moti Nagar~B", 9); 471 | g.addEdge("Moti Nagar~B", "Janak Puri West~BO", 7); 472 | g.addEdge("Janak Puri West~BO", "Dwarka Sector 21~B", 6); 473 | g.addEdge("Huda City Center~Y", "Saket~Y", 15); 474 | g.addEdge("Saket~Y", "AIIMS~Y", 6); 475 | g.addEdge("AIIMS~Y", "Rajiv Chowk~BY", 7); 476 | g.addEdge("Rajiv Chowk~BY", "New Delhi~YO", 1); 477 | g.addEdge("New Delhi~YO", "Chandni Chowk~Y", 2); 478 | g.addEdge("Chandni Chowk~Y", "Vishwavidyalaya~Y", 5); 479 | g.addEdge("New Delhi~YO", "Shivaji Stadium~O", 2); 480 | g.addEdge("Shivaji Stadium~O", "DDS Campus~O", 7); 481 | g.addEdge("DDS Campus~O", "IGI Airport~O", 8); 482 | g.addEdge("Moti Nagar~B", "Rajouri Garden~BP", 2); 483 | g.addEdge("Punjabi Bagh West~P", "Rajouri Garden~BP", 2); 484 | g.addEdge("Punjabi Bagh West~P", "Netaji Subhash Place~PR", 3); 485 | } 486 | 487 | public static String[] printCodelist() 488 | { 489 | System.out.println("List of station along with their codes:\n"); 490 | ArrayList keys = new ArrayList<>(vtces.keySet()); 491 | int i=1,j=0,m=1; 492 | StringTokenizer stname; 493 | String temp=""; 494 | String codes[] = new String[keys.size()]; 495 | char c; 496 | for(String key : keys) 497 | { 498 | stname = new StringTokenizer(key); 499 | codes[i-1] = ""; 500 | j=0; 501 | while (stname.hasMoreTokens()) 502 | { 503 | temp = stname.nextToken(); 504 | c = temp.charAt(0); 505 | while (c>47 && c<58) 506 | { 507 | codes[i-1]+= c; 508 | j++; 509 | c = temp.charAt(j); 510 | } 511 | if ((c<48 || c>57) && c<123) 512 | codes[i-1]+= c; 513 | } 514 | if (codes[i-1].length() < 2) 515 | codes[i-1]+= Character.toUpperCase(temp.charAt(1)); 516 | 517 | System.out.print(i + ". " + key + "\t"); 518 | if (key.length()<(22-m)) 519 | System.out.print("\t"); 520 | if (key.length()<(14-m)) 521 | System.out.print("\t"); 522 | if (key.length()<(6-m)) 523 | System.out.print("\t"); 524 | System.out.println(codes[i-1]); 525 | i++; 526 | if (i == (int)Math.pow(10,m)) 527 | m++; 528 | } 529 | return codes; 530 | } 531 | 532 | public static void main(String[] args) throws IOException 533 | { 534 | Graph_M g = new Graph_M(); 535 | Create_Metro_Map(g); 536 | 537 | System.out.println("\n\t\t\t****WELCOME TO THE METRO APP*****"); 538 | // System.out.println("\t\t\t\t~~LIST OF ACTIONS~~\n\n"); 539 | // System.out.println("1. LIST ALL THE STATIONS IN THE MAP"); 540 | // System.out.println("2. SHOW THE METRO MAP"); 541 | // System.out.println("3. GET SHORTEST DISTANCE FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 542 | // System.out.println("4. GET SHORTEST TIME TO REACH FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 543 | // System.out.println("5. GET SHORTEST PATH (DISTANCE WISE) TO REACH FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 544 | // System.out.println("6. GET SHORTEST PATH (TIME WISE) TO REACH FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 545 | // System.out.print("\nENTER YOUR CHOICE FROM THE ABOVE LIST : "); 546 | BufferedReader inp = new BufferedReader(new InputStreamReader(System.in)); 547 | // int choice = Integer.parseInt(inp.readLine()); 548 | //STARTING SWITCH CASE 549 | while(true) 550 | { 551 | System.out.println("\t\t\t\t~~LIST OF ACTIONS~~\n\n"); 552 | System.out.println("1. LIST ALL THE STATIONS IN THE MAP"); 553 | System.out.println("2. SHOW THE METRO MAP"); 554 | System.out.println("3. GET SHORTEST DISTANCE FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 555 | System.out.println("4. GET SHORTEST TIME TO REACH FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 556 | System.out.println("5. GET SHORTEST PATH (DISTANCE WISE) TO REACH FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 557 | System.out.println("6. GET SHORTEST PATH (TIME WISE) TO REACH FROM A 'SOURCE' STATION TO 'DESTINATION' STATION"); 558 | System.out.println("7. EXIT THE MENU"); 559 | System.out.print("\nENTER YOUR CHOICE FROM THE ABOVE LIST (1 to 7) : "); 560 | int choice = -1; 561 | try { 562 | choice = Integer.parseInt(inp.readLine()); 563 | } catch(Exception e) { 564 | // default will handle 565 | } 566 | System.out.print("\n***********************************************************\n"); 567 | if(choice == 7) 568 | { 569 | System.exit(0); 570 | } 571 | switch(choice) 572 | { 573 | case 1: 574 | g.display_Stations(); 575 | break; 576 | 577 | case 2: 578 | g.display_Map(); 579 | break; 580 | 581 | case 3: 582 | ArrayList keys = new ArrayList<>(vtces.keySet()); 583 | String codes[] = printCodelist(); 584 | System.out.println("\n1. TO ENTER SERIAL NO. OF STATIONS\n2. TO ENTER CODE OF STATIONS\n3. TO ENTER NAME OF STATIONS\n"); 585 | System.out.println("ENTER YOUR CHOICE:"); 586 | int ch = Integer.parseInt(inp.readLine()); 587 | int j; 588 | 589 | String st1 = "", st2 = ""; 590 | System.out.println("ENTER THE SOURCE AND DESTINATION STATIONS"); 591 | if (ch == 1) 592 | { 593 | st1 = keys.get(Integer.parseInt(inp.readLine())-1); 594 | st2 = keys.get(Integer.parseInt(inp.readLine())-1); 595 | } 596 | else if (ch == 2) 597 | { 598 | String a,b; 599 | a = (inp.readLine()).toUpperCase(); 600 | for (j=0;j processed = new HashMap<>(); 622 | if(!g.containsVertex(st1) || !g.containsVertex(st2) || !g.hasPath(st1, st2, processed)) 623 | System.out.println("THE INPUTS ARE INVALID"); 624 | else 625 | System.out.println("SHORTEST DISTANCE FROM "+st1+" TO "+st2+" IS "+g.dijkstra(st1, st2, false)+"KM\n"); 626 | break; 627 | 628 | case 4: 629 | System.out.print("ENTER THE SOURCE STATION: "); 630 | String sat1 = inp.readLine(); 631 | System.out.print("ENTER THE DESTINATION STATION: "); 632 | String sat2 = inp.readLine(); 633 | 634 | HashMap processed1= new HashMap<>(); 635 | System.out.println("SHORTEST TIME FROM ("+sat1+") TO ("+sat2+") IS "+g.dijkstra(sat1, sat2, true)/60+" MINUTES\n\n"); 636 | break; 637 | 638 | case 5: 639 | System.out.println("ENTER THE SOURCE AND DESTINATION STATIONS"); 640 | String s1 = inp.readLine(); 641 | String s2 = inp.readLine(); 642 | 643 | HashMap processed2 = new HashMap<>(); 644 | if(!g.containsVertex(s1) || !g.containsVertex(s2) || !g.hasPath(s1, s2, processed2)) 645 | System.out.println("THE INPUTS ARE INVALID"); 646 | else 647 | { 648 | ArrayList str = g.get_Interchanges(g.Get_Minimum_Distance(s1, s2)); 649 | int len = str.size(); 650 | System.out.println("SOURCE STATION : " + s1); 651 | System.out.println("SOURCE STATION : " + s2); 652 | System.out.println("DISTANCE : " + str.get(len-1)); 653 | System.out.println("NUMBER OF INTERCHANGES : " + str.get(len-2)); 654 | //System.out.println(str); 655 | System.out.println("~~~~~~~~~~~~~"); 656 | System.out.println("START ==> " + str.get(0)); 657 | for(int i=1; i END"); 662 | System.out.println("\n~~~~~~~~~~~~~"); 663 | } 664 | break; 665 | 666 | case 6: 667 | System.out.print("ENTER THE SOURCE STATION: "); 668 | String ss1 = inp.readLine(); 669 | System.out.print("ENTER THE DESTINATION STATION: "); 670 | String ss2 = inp.readLine(); 671 | 672 | HashMap processed3 = new HashMap<>(); 673 | if(!g.containsVertex(ss1) || !g.containsVertex(ss2) || !g.hasPath(ss1, ss2, processed3)) 674 | System.out.println("THE INPUTS ARE INVALID"); 675 | else 676 | { 677 | ArrayList str = g.get_Interchanges(g.Get_Minimum_Time(ss1, ss2)); 678 | int len = str.size(); 679 | System.out.println("SOURCE STATION : " + ss1); 680 | System.out.println("DESTINATION STATION : " + ss2); 681 | System.out.println("TIME : " + str.get(len-1)+" MINUTES"); 682 | System.out.println("NUMBER OF INTERCHANGES : " + str.get(len-2)); 683 | //System.out.println(str); 684 | System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 685 | System.out.print("START ==> " + str.get(0) + " ==> "); 686 | for(int i=1; i END"); 691 | System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 692 | } 693 | break; 694 | default: //If switch expression does not match with any case, 695 | //default statements are executed by the program. 696 | //No break is needed in the default case 697 | System.out.println("Please enter a valid option! "); 698 | System.out.println("The options you can choose are from 1 to 6. "); 699 | 700 | } 701 | } 702 | 703 | } 704 | } 705 | -------------------------------------------------------------------------------- /Heap.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashMap; 3 | 4 | public class Heap> 5 | { 6 | ArrayList data = new ArrayList<>(); 7 | HashMap map = new HashMap<>(); 8 | 9 | public void add(T item) 10 | { 11 | data.add(item); 12 | map.put(item, this.data.size() - 1); 13 | upheapify(data.size() - 1); 14 | } 15 | 16 | private void upheapify(int ci) 17 | { 18 | int pi = (ci - 1) / 2; 19 | if (isLarger(data.get(ci), data.get(pi)) > 0) 20 | { 21 | swap(pi, ci); 22 | upheapify(pi); 23 | } 24 | } 25 | 26 | private void swap(int i, int j) 27 | { 28 | T ith = data.get(i); 29 | T jth = data.get(j); 30 | 31 | data.set(i, jth); 32 | data.set(j, ith); 33 | map.put(ith, j); 34 | map.put(jth, i); 35 | } 36 | 37 | public void display() 38 | { 39 | System.out.println(data); 40 | } 41 | 42 | public int size() 43 | { 44 | return this.data.size(); 45 | } 46 | 47 | public boolean isEmpty() 48 | { 49 | return this.size() == 0; 50 | } 51 | 52 | public T remove() 53 | { 54 | swap(0, this.data.size() - 1); 55 | T rv = this.data.remove(this.data.size() - 1); 56 | downheapify(0); 57 | 58 | map.remove(rv); 59 | return rv; 60 | } 61 | 62 | private void downheapify(int pi) 63 | { 64 | int lci = 2 * pi + 1; 65 | int rci = 2 * pi + 2; 66 | int mini = pi; 67 | 68 | if (lci < this.data.size() && isLarger(data.get(lci), data.get(mini)) > 0) 69 | { 70 | mini = lci; 71 | } 72 | 73 | if (rci < this.data.size() && isLarger(data.get(rci), data.get(mini)) > 0) 74 | { 75 | mini = rci; 76 | } 77 | 78 | if (mini != pi) 79 | { 80 | swap(mini, pi); 81 | downheapify(mini); 82 | } 83 | } 84 | 85 | public T get() 86 | { 87 | return this.data.get(0); 88 | } 89 | 90 | public int isLarger(T t, T o) 91 | { 92 | return t.compareTo(o); 93 | } 94 | 95 | public void updatePriority(T pair) 96 | { 97 | int index = map.get(pair); 98 | upheapify(index); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020, KISLAYA SRIVASTAVA 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DELHI METRO RAIL APP 2 | 3 | Please don't get confused, this is NOT an ANDROID Application! 4 | 5 | This is a simple Java program that will take information (name) of the source station and the destination station, of Delhi Metro, from the user and display the fare and shortest metro route to reach the destination. It will also be having a metro map for commuter’s better navigation. 6 | 7 | The idea is implemented using Graph and Heap data structures. 8 | The graph has nodes and edges. Nodes represent a metro station that will be containing certain information regarding that station like its name, its metro corridor, and the lines which it connects. Edges (the connection between two nodes) represent the distance between the two stations and the cost of each edge will be equal to the distance between the two of its connecting stations(nodes). 9 | 10 | By using different algorithms like Dijkstra, breadth-first search, depth-first search, etc, the shortest path between the source station and the destination station is determined, and accordingly, the fare is being calculated on the basis of the total distance between the two stations. Finally, the metro route between the two stations and the total fare is displayed. 11 | 12 | Main.java cointains all the major code and Heap.java contains heap implementation. 13 | 14 | 15 | ## REQUIREMENTS 16 | 17 | > The project can run on any online or offline Integrated Development Environment (IDE) like Eclipse, Netbeans, ideone.com, etc. 18 | > You should have at least elementary knowledge of Java Programming language to work on the project. 19 | > Knowledge of data structures like Graph and Heap and Algorithms like Dijkstra, BFS, DFS, etc is appreciated, however, it is not essential. 20 | > And lastly, some understanding of the Collection framework makes it a cakewalk journey. (If you don't know about the Collection framework it is not a problem, you can proceed without it and while working if you feel the need to know you can refer to https://www.geeksforgeeks.org/collections-in-java-2/ ). 21 | 22 | 23 | ## That was all... You are all set to work on the project!!!! 24 | 25 | -------------------------------------------------------------------------------- /Requirements.md: -------------------------------------------------------------------------------- 1 | # Requirements 2 | 3 | ## Java Collection framework 4 | 5 | The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. 6 | 7 | ![flowchart](https://user-images.githubusercontent.com/71463658/101475597-9db6f400-3972-11eb-8b9f-dec58a00104f.png) 8 | 9 | ## Collection framework used in this project 10 | 11 | ### In Graph_M.java 12 | 13 | * ArrayList 14 | * LinkedList 15 | * Stack 16 | * HashMap 17 | 18 | ### In Heap.java 19 | 20 | * HashMap 21 | * ArrayList 22 | 23 | 24 | 25 | ## Data Structures 26 | Data structure is a way of organizing data in the computer so that it can be used effectively. 27 | The data structures used in this project are: 28 | * Array 29 | * HashMap 30 | * Linked List 31 | * Heap 32 | * Stack 33 | * Graph 34 | 35 | * The array data structure is implemented using ArrayList class which is a resizable array.It stores data in contiguous memory locations. 36 | * Hashmap data structure is used to store key-value pairs. 37 | * The stack data structure required by the algorithm is implemented using linked list.It follows the principle Last In First Out(LIFO) 38 | * Heap is implemented with the help of ArrayList and HashMap.It is a tree based data structure where all elements of the tree are in a particular order. 39 | * Graph is a complex data structure containing vertices and edges connecting these vertices.This is implemented in the project with the help of ArrayList and HashMap. 40 | --------------------------------------------------------------------------------