Dijkstra.java

  1. package org.drip.sample.graph;

  2. import java.util.Set;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.spaces.graph.DijkstraScheme;
  6. import org.drip.spaces.graph.Edge;
  7. import org.drip.spaces.graph.Topography;
  8. import org.drip.spaces.graph.ShortestPathVertex;
  9. import org.drip.spaces.graph.ShortestPathTree;

  10. /*
  11.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  12.  */

  13. /*!
  14.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  18.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  19.  *      credit, commodity, equity, FX, and structured products.
  20.  *  
  21.  *      https://lakshmidrip.github.io/DROP/
  22.  *  
  23.  *  DROP is composed of three modules:
  24.  *  
  25.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  26.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  27.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  28.  *
  29.  *  DROP Analytics Core implements libraries for the following:
  30.  *  - Fixed Income Analytics
  31.  *  - Asset Backed Analytics
  32.  *  - XVA Analytics
  33.  *  - Exposure and Margin Analytics
  34.  *
  35.  *  DROP Portfolio Core implements libraries for the following:
  36.  *  - Asset Allocation Analytics
  37.  *  - Transaction Cost Analytics
  38.  *
  39.  *  DROP Numerical Core implements libraries for the following:
  40.  *  - Statistical Learning
  41.  *  - Numerical Optimizer
  42.  *  - Spline Builder
  43.  *  - Algorithm Support
  44.  *
  45.  *  Documentation for DROP is Spread Over:
  46.  *
  47.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  48.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  49.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  50.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  51.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  52.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  53.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  54.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  55.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  56.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  57.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  58.  *
  59.  *  Licensed under the Apache License, Version 2.0 (the "License");
  60.  *      you may not use this file except in compliance with the License.
  61.  *  
  62.  *  You may obtain a copy of the License at
  63.  *      http://www.apache.org/licenses/LICENSE-2.0
  64.  *  
  65.  *  Unless required by applicable law or agreed to in writing, software
  66.  *      distributed under the License is distributed on an "AS IS" BASIS,
  67.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  68.  *  
  69.  *  See the License for the specific language governing permissions and
  70.  *      limitations under the License.
  71.  */

  72. /**
  73.  * <i>Dijkstra</i> illustrates the Execution of the Dijkstra Algorithm. The References are:
  74.  *  
  75.  * <br><br>
  76.  *  <ul>
  77.  *      <li>
  78.  *          Wikipedia (2018a): Graph (Abstract Data Type)
  79.  *              https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
  80.  *      </li>
  81.  *      <li>
  82.  *          Wikipedia (2018b): Graph Theory https://en.wikipedia.org/wiki/Graph_theory
  83.  *      </li>
  84.  *      <li>
  85.  *          Wikipedia (2018c): Graph (Discrete Mathematics)
  86.  *              https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)
  87.  *      </li>
  88.  *      <li>
  89.  *          Wikipedia (2018d): Dijkstra's Algorithm https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
  90.  *      </li>
  91.  *      <li>
  92.  *          Wikipedia (2018e): Bellman-Ford Algorithm
  93.  *              https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
  94.  *      </li>
  95.  *  </ul>
  96.  * <br><br>
  97.  *  <ul>
  98.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  99.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AlgorithmSupportLibrary.md">Algorithm Support Library</a></li>
  100.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  101.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/graph/README.md">Graph Builder and Navigator</a></li>
  102.  *  </ul>
  103.  * <br><br>
  104.  *
  105.  * @author Lakshmi Krishnamurthy
  106.  */

  107. public class Dijkstra
  108. {

  109.     private static final Topography SetTopography()
  110.         throws Exception
  111.     {
  112.         String[] vertexArray = new String[]
  113.         {
  114.             "Delhi     ",
  115.             "Bombay    ",
  116.             "Madras    ",
  117.             "Calcutta  ",
  118.             "Bangalore ",
  119.             "Hyderabad ",
  120.             "Cochin    ",
  121.             "Pune      ",
  122.             "Ahmedabad ",
  123.             "Jaipur    "
  124.         };

  125.         Topography topography = new Topography();

  126.         for (String vertexName : vertexArray)
  127.         {
  128.             topography.addVertex (vertexName);
  129.         }

  130.         topography.addEdge (
  131.             new Edge (
  132.                 vertexArray[0], // Delhi
  133.                 vertexArray[1], // Bombay
  134.                 1388.
  135.             )
  136.         );

  137.         topography.addEdge (
  138.             new Edge (
  139.                 vertexArray[0], // Delhi
  140.                 vertexArray[2], // Madras
  141.                 2191.
  142.             )
  143.         );

  144.         topography.addEdge (
  145.             new Edge (
  146.                 vertexArray[1], // Bombay
  147.                 vertexArray[2], // Madras
  148.                 1279.
  149.             )
  150.         );

  151.         topography.addEdge (
  152.             new Edge (
  153.                 vertexArray[0], // Delhi
  154.                 vertexArray[3], // Calcutta
  155.                 1341.
  156.             )
  157.         );

  158.         topography.addEdge (
  159.             new Edge (
  160.                 vertexArray[1], // Bombay
  161.                 vertexArray[3], // Calcutta
  162.                 1968.
  163.             )
  164.         );

  165.         topography.addEdge (
  166.             new Edge (
  167.                 vertexArray[2], // Madras
  168.                 vertexArray[3], // Calcutta
  169.                 1663.
  170.             )
  171.         );

  172.         topography.addEdge (
  173.             new Edge (
  174.                 vertexArray[2], // Madras
  175.                 vertexArray[4], // Bangalore
  176.                 361.
  177.             )
  178.         );

  179.         topography.addEdge (
  180.             new Edge (
  181.                 vertexArray[2], // Madras
  182.                 vertexArray[5], // Hyderabad
  183.                 784.
  184.             )
  185.         );

  186.         topography.addEdge (
  187.             new Edge (
  188.                 vertexArray[2], // Madras
  189.                 vertexArray[6], // Cochin
  190.                 697.
  191.             )
  192.         );

  193.         topography.addEdge (
  194.             new Edge (
  195.                 vertexArray[1], // Bombay
  196.                 vertexArray[7], // Pune
  197.                 192.
  198.             )
  199.         );

  200.         topography.addEdge (
  201.             new Edge (
  202.                 vertexArray[1], // Bombay
  203.                 vertexArray[8], // Ahmedabad
  204.                 492.
  205.             )
  206.         );

  207.         topography.addEdge (
  208.             new Edge (
  209.                 vertexArray[0], // Delhi
  210.                 vertexArray[9], // Jaipur
  211.                 308.
  212.             )
  213.         );

  214.         return topography;
  215.     }

  216.     private static final String PathVertexes (
  217.         final String source,
  218.         final String destination,
  219.         final ShortestPathTree vertexPeripheryMap)
  220.         throws Exception
  221.     {
  222.         String path = "";
  223.         String vertex = destination;

  224.         ShortestPathVertex vertexPeriphery = vertexPeripheryMap.shortestPathVertex (vertex);

  225.         while (!source.equalsIgnoreCase (vertexPeriphery.current()))
  226.         {
  227.             path = path + vertexPeriphery.current() + " <- ";

  228.             vertexPeriphery = vertexPeripheryMap.shortestPathVertex (vertexPeriphery.preceeding());
  229.         }

  230.         path = path + source;
  231.         return path;
  232.     }

  233.     public static void main (
  234.         final String[] inputArray)
  235.         throws Exception
  236.     {
  237.         EnvManager.InitEnv ("");

  238.         Topography topography = SetTopography();

  239.         DijkstraScheme dijkstraScheme = new DijkstraScheme (topography);

  240.         Set<String> vertexNameSet = topography.vertexNameSet();

  241.         for (String source : vertexNameSet)
  242.         {
  243.             ShortestPathTree vertexPeripheryMap = dijkstraScheme.spf (source).vertexPeripheryMap();

  244.             System.out.println ("\t||------------------------------------------------------------||");

  245.             for (String vertex : vertexNameSet)
  246.             {
  247.                 if (!vertex.equalsIgnoreCase(source))
  248.                 {
  249.                     ShortestPathVertex vertexPeriphery = vertexPeripheryMap.shortestPathVertex (vertex);

  250.                     System.out.println (
  251.                         "\t|| " + source + " to " + vertex + " is " +
  252.                         FormatUtil.FormatDouble (vertexPeriphery.weightFromSource(), 4, 0, 1.) +
  253.                         " | Previous is " + vertexPeriphery.preceeding() + " || " + PathVertexes (
  254.                             source,
  255.                             vertex,
  256.                             vertexPeripheryMap
  257.                         )
  258.                     );
  259.                 }
  260.             }

  261.             System.out.println ("\t||------------------------------------------------------------||\n");
  262.         }

  263.         EnvManager.TerminateEnv();
  264.     }
  265. }