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

  109. public class BellmanFord
  110. {

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

  127.         Topography topography = new Topography();

  128.         for (String vertexName : vertexArray)
  129.         {
  130.             topography.addVertex (vertexName);
  131.         }

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

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

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

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

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

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

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

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

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

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

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

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

  216.         return topography;
  217.     }

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

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

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

  230.             vertexPeriphery = vertexPeripheryMap.shortestPathVertex (vertexPeriphery.preceeding());
  231.         }

  232.         path = path + source;
  233.         return path;
  234.     }

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

  240.         Topography topography = SetTopography();

  241.         BellmanFordScheme bellmanFordScheme = new BellmanFordScheme (topography);

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

  243.         for (String source : vertexNameSet)
  244.         {
  245.             ShortestPathTree vertexPeripheryMap = bellmanFordScheme.spf (source).vertexPeripheryMap();

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

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

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

  263.             System.out.println ("\t||------------------------------------------------------------||\n");
  264.         }

  265.         EnvManager.TerminateEnv();
  266.     }
  267. }