BellmanFordScheme.java

  1. package org.drip.spaces.graph;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

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

  75. /**
  76.  * <i>BellmanFordScheme</i> implements the Bellman Ford Algorithm for finding the Shortest Path between a
  77.  * Pair of Vertexes in a Graph. The References are:
  78.  *
  79.  * <br><br>
  80.  *  <ul>
  81.  *      <li>
  82.  *          Wikipedia (2018a): Graph (Abstract Data Type)
  83.  *              https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
  84.  *      </li>
  85.  *      <li>
  86.  *          Wikipedia (2018b): Graph Theory https://en.wikipedia.org/wiki/Graph_theory
  87.  *      </li>
  88.  *      <li>
  89.  *          Wikipedia (2018c): Graph (Discrete Mathematics)
  90.  *              https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)
  91.  *      </li>
  92.  *      <li>
  93.  *          Wikipedia (2018d): Dijkstra's Algorithm https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
  94.  *      </li>
  95.  *      <li>
  96.  *          Wikipedia (2018e): Bellman-Ford Algorithm
  97.  *              https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  * <br><br>
  102.  *  <ul>
  103.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  104.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  105.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/README.md">R<sup>1</sup> and R<sup>d</sup> Vector/Tensor Spaces (Validated and/or Normed), and Function Classes</a></li>
  106.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/graph/README.md">Graph Representation and Traversal Algorithms</a></li>
  107.  *  </ul>
  108.  * <br><br>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class BellmanFordScheme
  113. {
  114.     private org.drip.spaces.graph.Topography _topography = null;

  115.     private void visitEdge (
  116.         final org.drip.spaces.graph.Edge edge,
  117.         final org.drip.spaces.graph.ShortestPathFirstWengert spfWengert)
  118.     {
  119.         java.lang.String leftVertex = edge.source();

  120.         org.drip.spaces.graph.ShortestPathTree vertexPeripheryMap = spfWengert.vertexPeripheryMap();

  121.         org.drip.spaces.graph.ShortestPathVertex vertexPeripheryLeft = vertexPeripheryMap.shortestPathVertex
  122.             (leftVertex);

  123.         org.drip.spaces.graph.ShortestPathVertex vertexPeripheryRight = vertexPeripheryMap.shortestPathVertex
  124.             (edge.destination());

  125.         double sourceToRightThroughLeft = vertexPeripheryLeft.weightFromSource() + edge.weight();

  126.         if (sourceToRightThroughLeft < vertexPeripheryRight.weightFromSource())
  127.         {
  128.             vertexPeripheryRight.setWeightFromSource (sourceToRightThroughLeft);

  129.             vertexPeripheryRight.setPreceeding (leftVertex);
  130.         }
  131.     }

  132.     /**
  133.      * BellmanFordScheme Constructor
  134.      *
  135.      * @param topography The Topography Map
  136.      *
  137.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  138.      */

  139.     public BellmanFordScheme (
  140.         final org.drip.spaces.graph.Topography topography)
  141.         throws java.lang.Exception
  142.     {
  143.         if (null == (_topography = topography))
  144.         {
  145.             throw new java.lang.Exception ("BellmanFordScheme Constructor => Invalid Inputs");
  146.         }
  147.     }

  148.     /**
  149.      * Retrieve the Topography Map
  150.      *
  151.      * @return The Topography Map
  152.      */

  153.     public org.drip.spaces.graph.Topography topography()
  154.     {
  155.         return _topography;
  156.     }

  157.     /**
  158.      * Initialize the Bellman Ford Scheme
  159.      *
  160.      * @param source The Source Vertex
  161.      *
  162.      * @return The Initial Bellman Ford Wengert
  163.      */

  164.     public org.drip.spaces.graph.ShortestPathFirstWengert setup (
  165.         final java.lang.String source)
  166.     {
  167.         return org.drip.spaces.graph.ShortestPathFirstWengert.BellmanFord (
  168.             _topography,
  169.             source
  170.         );
  171.     }

  172.     /**
  173.      * Run the Bellman Ford SPF Algorithm
  174.      *
  175.      * @param source The Source Vertex
  176.      *
  177.      * @return The Bellman Ford Wengert
  178.      */

  179.     public org.drip.spaces.graph.ShortestPathFirstWengert spf (
  180.         final java.lang.String source)
  181.     {
  182.         org.drip.spaces.graph.ShortestPathFirstWengert spfWengert = setup (source);

  183.         if (null == spfWengert)
  184.         {
  185.             return null;
  186.         }

  187.         int vertexCount = _topography.vertexNameSet().size();

  188.         for (int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  189.         {
  190.             java.util.Map<java.lang.String, org.drip.spaces.graph.Edge> edgeMap =
  191.                 _topography.topographyEdgeMap().edgeMap();

  192.             for (java.util.Map.Entry<java.lang.String, org.drip.spaces.graph.Edge> edgeEntry :
  193.                 edgeMap.entrySet())
  194.             {
  195.                 visitEdge (
  196.                     edgeEntry.getValue(),
  197.                     spfWengert
  198.                 );
  199.             }
  200.         }

  201.         return spfWengert;
  202.     }
  203. }