ShortestPathTree.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>ShortestPathTree</i> holds the Map of Vertex Peripheries by Weight and Vertex Name. The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Wikipedia (2018a): Graph (Abstract Data Type)
  82.  *              https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
  83.  *      </li>
  84.  *      <li>
  85.  *          Wikipedia (2018b): Graph Theory https://en.wikipedia.org/wiki/Graph_theory
  86.  *      </li>
  87.  *      <li>
  88.  *          Wikipedia (2018c): Graph (Discrete Mathematics)
  89.  *              https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)
  90.  *      </li>
  91.  *      <li>
  92.  *          Wikipedia (2018d): Dijkstra's Algorithm https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
  93.  *      </li>
  94.  *      <li>
  95.  *          Wikipedia (2018e): Bellman-Ford Algorithm
  96.  *              https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
  97.  *      </li>
  98.  *  </ul>
  99.  *
  100.  * <br><br>
  101.  *  <ul>
  102.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  103.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  104.  *      <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>
  105.  *      <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>
  106.  *  </ul>
  107.  * <br><br>
  108.  *
  109.  * @author Lakshmi Krishnamurthy
  110.  */

  111. public class ShortestPathTree
  112. {
  113.     private java.util.Map<java.lang.String, org.drip.spaces.graph.ShortestPathVertex> _nameIndex = new
  114.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.spaces.graph.ShortestPathVertex>();

  115.     private java.util.Map<java.lang.Double, java.util.List<org.drip.spaces.graph.ShortestPathVertex>>
  116.         _weightIndex = new java.util.TreeMap<java.lang.Double,
  117.             java.util.List<org.drip.spaces.graph.ShortestPathVertex>>();

  118.     private org.drip.spaces.graph.ShortestPathVertex getUnvisited (
  119.         final java.util.List<org.drip.spaces.graph.ShortestPathVertex> shortestPathVertexList)
  120.     {
  121.         for (org.drip.spaces.graph.ShortestPathVertex shortestPathVertex : shortestPathVertexList)
  122.         {
  123.             if (!shortestPathVertex.visited())
  124.             {
  125.                 return shortestPathVertex;
  126.             }
  127.         }

  128.         return null;
  129.     }

  130.     /**
  131.      * Empty ShortestPathTree Constructor
  132.      */

  133.     public ShortestPathTree()
  134.     {
  135.     }

  136.     /**
  137.      * Add a shortestPathVertex
  138.      *
  139.      * @param shortestPathVertex The shortestPathVertex
  140.      *
  141.      * @return TRUE - The shortestPathVertex successfully added
  142.      */

  143.     public boolean addShortestPathVertex (
  144.         final org.drip.spaces.graph.ShortestPathVertex shortestPathVertex)
  145.     {
  146.         if (null == shortestPathVertex)
  147.         {
  148.             return false;
  149.         }

  150.         _nameIndex.put (
  151.             shortestPathVertex.current(),
  152.             shortestPathVertex
  153.         );

  154.         double weightFromSource = shortestPathVertex.weightFromSource();

  155.         if (_weightIndex.containsKey (weightFromSource))
  156.         {
  157.             _weightIndex.get (shortestPathVertex.weightFromSource()).add (shortestPathVertex);
  158.         }
  159.         else
  160.         {
  161.             java.util.List<org.drip.spaces.graph.ShortestPathVertex> shortestPathVertexList = new
  162.                 java.util.ArrayList<org.drip.spaces.graph.ShortestPathVertex>();

  163.             shortestPathVertexList.add (shortestPathVertex);

  164.             _weightIndex.put (
  165.                 weightFromSource,
  166.                 shortestPathVertexList
  167.             );
  168.         }

  169.         return true;
  170.     }

  171.     /**
  172.      * Add an Uninitialized ShortestPathVertex
  173.      *
  174.      * @param vertexName The Vertex Name
  175.      *
  176.      * @return TRUE - An Uninitialized ShortestPathVertex successfully added
  177.      */

  178.     public boolean addUnitializedShortestPathVertex (
  179.         final java.lang.String vertexName)
  180.     {
  181.         try
  182.         {
  183.             return addShortestPathVertex (new org.drip.spaces.graph.ShortestPathVertex (vertexName));
  184.         }
  185.         catch (java.lang.Exception e)
  186.         {
  187.             e.printStackTrace();
  188.         }
  189.         return false;
  190.     }

  191.     /**
  192.      * Indicate of the Vertex is available in the Periphery Map
  193.      *
  194.      * @param vertexName The Vertex Name
  195.      *
  196.      * @return TRUE - The Vertex is available in the Periphery Map
  197.      */

  198.     public boolean containsVertex (
  199.         final java.lang.String vertexName)
  200.     {
  201.         return null != vertexName && !vertexName.isEmpty() && _nameIndex.containsKey (vertexName);
  202.     }

  203.     /**
  204.      * Retrieve the Vertex Periphery by Name
  205.      *
  206.      * @param vertexName The Vertex Name
  207.      *
  208.      * @return The Vertex Periphery by Name
  209.      */

  210.     public org.drip.spaces.graph.ShortestPathVertex shortestPathVertex (
  211.         final java.lang.String vertexName)
  212.     {
  213.         return _nameIndex.containsKey (vertexName) ? _nameIndex.get (vertexName) : null;
  214.     }

  215.     /**
  216.      * Retrieve the Vertex Periphery with the least Weight
  217.      *
  218.      * @return The Vertex Periphery with the least Weight
  219.      */

  220.     public org.drip.spaces.graph.ShortestPathVertex greedyShortestPathVertex()
  221.     {
  222.         for (java.util.Map.Entry<java.lang.Double, java.util.List<org.drip.spaces.graph.ShortestPathVertex>>
  223.             weightIndexEntry : _weightIndex.entrySet())
  224.         {
  225.             org.drip.spaces.graph.ShortestPathVertex vertexPeriphery = getUnvisited
  226.                 (weightIndexEntry.getValue());

  227.             if (null == vertexPeriphery)
  228.             {
  229.                 continue;
  230.             }

  231.             vertexPeriphery.setVisited (true);

  232.             return vertexPeriphery;
  233.         }

  234.         return null;
  235.     }

  236.     /**
  237.      * Retrieve the Name Indexed Vertex Periphery Map
  238.      *
  239.      * @return The Name Indexed Vertex Periphery Map
  240.      */

  241.     public java.util.Map<java.lang.String, org.drip.spaces.graph.ShortestPathVertex> nameIndex()
  242.     {
  243.         return _nameIndex;
  244.     }

  245.     /**
  246.      * Retrieve the Weight Indexed Vertex Periphery Map
  247.      *
  248.      * @return The Weight Indexed Vertex Periphery Map
  249.      */

  250.     public java.util.Map<java.lang.Double, java.util.List<org.drip.spaces.graph.ShortestPathVertex>>
  251.         weightIndex()
  252.     {
  253.         return _weightIndex;
  254.     }
  255. }