ShortestPathFirstWengert.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>ShortestPathFirstWengert maintains</i> the Intermediate Wengert Objects generated during a Single
  77.  * Sequence of the Scheme Run. 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 ShortestPathFirstWengert
  113. {
  114.     private org.drip.spaces.graph.ShortestPathTree _vertexPeripheryMap = null;

  115.     /**
  116.      * Generate a ShortestPathFirstWengert from the Topography and the Source using the Dijkstra Scheme
  117.      *
  118.      * @param topography The Topography Map
  119.      * @param source The Source Vertex
  120.      *
  121.      * @return The Dijkstra ShortestPathFirstWengert Instance
  122.      */

  123.     public static final ShortestPathFirstWengert Dijkstra (
  124.         final org.drip.spaces.graph.Topography topography,
  125.         final java.lang.String source)
  126.     {
  127.         if (null == topography)
  128.         {
  129.             return null;
  130.         }

  131.         java.util.Set<java.lang.String> vertexNameSet = topography.vertexNameSet();

  132.         if (!vertexNameSet.contains (source))
  133.         {
  134.             return null;
  135.         }

  136.         java.util.Map<java.lang.String, java.lang.Double> connectionMap = topography.connectionMap();

  137.         java.util.Map<java.lang.String, java.lang.Double> egressMap = topography.vertex (source).egressMap();

  138.         org.drip.spaces.graph.ShortestPathTree vertexPeripheryMap = new
  139.             org.drip.spaces.graph.ShortestPathTree();

  140.         for (java.util.Map.Entry<java.lang.String, java.lang.Double> egressEntry : egressMap.entrySet())
  141.         {
  142.             java.lang.String egressVertex = egressEntry.getKey();

  143.             org.drip.spaces.graph.ShortestPathVertex vertexPeriphery = null;
  144.             java.lang.String sourceToEgressVertexKey = source + "_" + egressVertex;

  145.             try
  146.             {
  147.                 vertexPeriphery = new org.drip.spaces.graph.ShortestPathVertex (egressVertex);
  148.             }
  149.             catch (java.lang.Exception e)
  150.             {
  151.                 e.printStackTrace();

  152.                 return null;
  153.             }

  154.             vertexPeriphery.setPreceeding (source);

  155.             vertexPeriphery.setWeightFromSource (connectionMap.get (sourceToEgressVertexKey));

  156.             vertexPeripheryMap.addShortestPathVertex (vertexPeriphery);
  157.         }

  158.         for (java.lang.String vertexName : vertexNameSet)
  159.         {
  160.             if (!vertexPeripheryMap.containsVertex (vertexName))
  161.             {
  162.                 vertexPeripheryMap.addUnitializedShortestPathVertex (vertexName);
  163.             }
  164.         }

  165.         try
  166.         {
  167.             return new ShortestPathFirstWengert (vertexPeripheryMap);
  168.         }
  169.         catch (java.lang.Exception e)
  170.         {
  171.             e.printStackTrace();
  172.         }

  173.         return null;
  174.     }

  175.     /**
  176.      * Generate a ShortestPathFirstWengert from the Topography and the Source using the Bellman-Ford Scheme
  177.      *
  178.      * @param topography The Topography Map
  179.      * @param source The Source Vertex
  180.      *
  181.      * @return The Bellman-Ford ShortestPathFirstWengert Instance
  182.      */

  183.     public static final ShortestPathFirstWengert BellmanFord (
  184.         final org.drip.spaces.graph.Topography topography,
  185.         final java.lang.String source)
  186.     {
  187.         if (null == topography)
  188.         {
  189.             return null;
  190.         }

  191.         java.util.Set<java.lang.String> vertexNameSet = topography.vertexNameSet();

  192.         if (!vertexNameSet.contains (source))
  193.         {
  194.             return null;
  195.         }

  196.         org.drip.spaces.graph.ShortestPathTree vertexPeripheryMap = new
  197.             org.drip.spaces.graph.ShortestPathTree();

  198.         org.drip.spaces.graph.ShortestPathVertex sourceVertexPeriphery = null;

  199.         try
  200.         {
  201.             sourceVertexPeriphery = new org.drip.spaces.graph.ShortestPathVertex (source);
  202.         }
  203.         catch (java.lang.Exception e)
  204.         {
  205.             e.printStackTrace();

  206.             return null;
  207.         }

  208.         sourceVertexPeriphery.setPreceeding (source);

  209.         sourceVertexPeriphery.setWeightFromSource (0.);

  210.         vertexPeripheryMap.addShortestPathVertex (sourceVertexPeriphery);

  211.         for (java.lang.String vertexName : vertexNameSet)
  212.         {
  213.             if (!vertexName.equalsIgnoreCase (source))
  214.             {
  215.                 vertexPeripheryMap.addUnitializedShortestPathVertex (vertexName);
  216.             }
  217.         }

  218.         try
  219.         {
  220.             return new ShortestPathFirstWengert (vertexPeripheryMap);
  221.         }
  222.         catch (java.lang.Exception e)
  223.         {
  224.             e.printStackTrace();
  225.         }

  226.         return null;
  227.     }

  228.     /**
  229.      * ShortestPathFirstWengert Constructor
  230.      *
  231.      * @param vertexPeripheryMap The Vertex Periphery Map
  232.      *
  233.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  234.      */

  235.     public ShortestPathFirstWengert (
  236.         final org.drip.spaces.graph.ShortestPathTree vertexPeripheryMap)
  237.         throws java.lang.Exception
  238.     {
  239.         if (null == (_vertexPeripheryMap = vertexPeripheryMap))
  240.         {
  241.             throw new java.lang.Exception ("ShortestPathFirstWengert Constructor => Invalid Inputs");
  242.         }
  243.     }

  244.     /**
  245.      * Retrieve the Vertex Periphery Map
  246.      *
  247.      * @return The Vertex Periphery Map
  248.      */

  249.     public org.drip.spaces.graph.ShortestPathTree vertexPeripheryMap()
  250.     {
  251.         return _vertexPeripheryMap;
  252.     }
  253. }