TopographyEdgeMap.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>TopographyEdgeMap</i> maintains a Map of the Topography Connection Edges. 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 TopographyEdgeMap
  112. {
  113.     private java.util.Map<java.lang.String, org.drip.spaces.graph.Edge> _edgeMap = new
  114.         org.drip.analytics.support.CaseInsensitiveHashMap<org.drip.spaces.graph.Edge>();

  115.     /**
  116.      * Empty TopographyEdgeMap Constructor
  117.      */

  118.     public TopographyEdgeMap()
  119.     {
  120.     }

  121.     /**
  122.      * Add an Edge
  123.      *
  124.      * @param edge The Edge
  125.      *
  126.      * @return TRUE - The Edge successfully added
  127.      */

  128.     public boolean addEdge (
  129.         final org.drip.spaces.graph.Edge edge)
  130.     {
  131.         if (null == edge)
  132.         {
  133.             return false;
  134.         }

  135.         java.lang.String source = edge.source();

  136.         java.lang.String destination = edge.destination();

  137.         _edgeMap.put (
  138.             source + "_" + destination,
  139.             edge
  140.         );

  141.         try
  142.         {
  143.             _edgeMap.put (
  144.                 destination + "_" + source,
  145.                 new org.drip.spaces.graph.Edge (
  146.                     destination,
  147.                     source,
  148.                     edge.weight()
  149.                 )
  150.             );

  151.             return true;
  152.         }
  153.         catch (java.lang.Exception e)
  154.         {
  155.             e.printStackTrace();
  156.         }

  157.         return false;
  158.     }

  159.     /**
  160.      * Retrieve the Edge Map
  161.      *
  162.      * @return The Edge Map
  163.      */

  164.     public java.util.Map<java.lang.String, org.drip.spaces.graph.Edge> edgeMap()
  165.     {
  166.         return _edgeMap;
  167.     }

  168.     /**
  169.      * Retrieve the Edge connecting the Source and the Destination
  170.      *
  171.      * @param source The Source
  172.      * @param destination The Destination
  173.      *
  174.      * @return The Edge connecting the Source and the Destination
  175.      */

  176.     public org.drip.spaces.graph.Edge edge (
  177.         final java.lang.String source,
  178.         final java.lang.String destination)
  179.     {
  180.         if (null == source || source.isEmpty() ||
  181.             null == destination || destination.isEmpty())
  182.         {
  183.             return null;
  184.         }

  185.         java.lang.String key = source + "_" + destination;

  186.         return _edgeMap.containsKey (key) ? _edgeMap.get (key) : null;
  187.     }

  188.     /**
  189.      * Retrieve all the Edges corresponding to the Source Vertex
  190.      *
  191.      * @param source The Source Vertex
  192.      *
  193.      * @return Edges corresponding to the Source Vertex
  194.      */

  195.     public java.util.List<org.drip.spaces.graph.Edge> edgeList (
  196.         final java.lang.String source)
  197.     {
  198.         if (null == source || source.isEmpty())
  199.         {
  200.             return null;
  201.         }

  202.         java.util.List<org.drip.spaces.graph.Edge> edgeList = new
  203.             java.util.ArrayList<org.drip.spaces.graph.Edge>();

  204.         for (java.util.Map.Entry<java.lang.String, org.drip.spaces.graph.Edge> edgeMapEntry :
  205.             _edgeMap.entrySet())
  206.         {
  207.             java.lang.String key = edgeMapEntry.getKey();

  208.             if (key.startsWith (source))
  209.             {
  210.                 edgeList.add (edgeMapEntry.getValue());
  211.             }
  212.         }

  213.         return edgeList;
  214.     }
  215. }