/****************************************************************************
 *   This file is part of the aGrUM/pyAgrum library.                        *
 *                                                                          *
 *   Copyright (c) 2005-2026 by                                             *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *                                                                          *
 *   The aGrUM/pyAgrum library is free software; you can redistribute it    *
 *   and/or modify it under the terms of either :                           *
 *                                                                          *
 *    - the GNU Lesser General Public License as published by               *
 *      the Free Software Foundation, either version 3 of the License,      *
 *      or (at your option) any later version,                              *
 *    - the MIT license (MIT),                                              *
 *    - or both in dual license, as here.                                   *
 *                                                                          *
 *   (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html)    *
 *                                                                          *
 *   This aGrUM/pyAgrum library is distributed in the hope that it will be  *
 *   useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,          *
 *   INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
 *   FOR A PARTICULAR PURPOSE  AND NONINFRINGEMENT. IN NO EVENT SHALL THE   *
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,        *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  *
 *   OTHER DEALINGS IN THE SOFTWARE.                                        *
 *                                                                          *
 *   See LICENCES for more details.                                         *
 *                                                                          *
 *   SPDX-FileCopyrightText: Copyright 2005-2026                            *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *   SPDX-License-Identifier: LGPL-3.0-or-later OR MIT                      *
 *                                                                          *
 *   Contact  : info_at_agrum_dot_org                                       *
 *   homepage : http://agrum.gitlab.io                                      *
 *   gitlab   : https://gitlab.com/agrumery/agrum                           *
 *                                                                          *
 ****************************************************************************/


/** @file
 * @brief Base classes for oriented graphs
 *
 * @author Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_AMU)
 */
#ifndef GUM_DIGRAPH_H
#define GUM_DIGRAPH_H

#include <iostream>
#include <optional>
#include <sstream>
#include <utility>

#include <agrum/agrum.h>

#include <agrum/base/core/sequence.h>
#include <agrum/base/graphs/graphConcepts.h>
#include <agrum/base/graphs/parts/arcGraphPart.h>
#include <agrum/base/graphs/parts/nodeGraphPart.h>

namespace gum {

  /* ===========================================================================
   */
  /* ===           BASE CLASS FOR MANIPULATING ALL DIRECTED GRAPHS           ===
   */
  /* ===========================================================================
   */
  /** @class DiGraph
   * @brief Base class for all oriented graphs
   *
   * \ingroup graph_group
   *
   *
   * This is the base class for graphs containing directed edges (so-called
   *arcs)
   * *
   * @par Usage example:
   * @code
   * // creating empty graphs
   * DiGraph g1,g2;
   *
   * // adding nodes and arcs to g1
   * NodeId i1=g1.addNode();
   * NodeId i2=g1.addNode();
   * NodeId i3=g1.addNode();
   * g1.addArc( i1,i2 );
   * g1.addArc( i1,i3 );
   * g1.addArc( i2,i3 );
   *
   * //throw an InvalidNode
   * // g1.addArc( i1+i2+i3,i1 );
   *
   * // copying graphs
   * DiGraph g3 = g1;
   * g2 = g1;
   * DiGraph g4=g1;
   *
   * // check if a graph has no node
   * if ( g1.empty() ) cerr << "graph g1 is empty" << endl;
   *
   * // remove all the nodes (as well as their adjacent arcs)
   * g1.clear();
   *
   * // remove some arc
   * g4.eraseArc( Arc ( i1,i3 ) );
   *
   * // remove node
   * g2.eraseNode( i2 );
   *
   * // parse a graph
   * for ( const auto node : g3.nodes() )
   *   cerr << node<< endl;
   *
   * for ( const auto & arc : g3.arcs())
   *   cerr << arc << endl;
   *
   * const ArcSet& a=g3.parents( 3 );
   *
   * for ( const auto & par : g3.parents(3))
   *   cerr << "  -  "<< par;
   *
   * cerr<<endl;
   *
   * // remove all the arcs that are parent of a given node
   * g3.eraseParents( 2 );
   * @endcode
   */
  /* ===========================================================================
   */
  class DiGraph: public virtual NodeGraphPart, public ArcGraphPart {
    public:
    // ############################################################################
    /// @name Constructors / Destructors
    // ############################################################################
    /// @{

    /// default constructor
    /** @param nodes_size the size of the hash table used to store all the nodes
     * @param nodes_resize_policy the resizing policy of this hash table
     * @param arcs_size the size of the hash table used to store all the arcs
     * @param arcs_resize_policy the resizing policy of this hash table */
    explicit DiGraph(Size nodes_size          = HashTableConst::default_size,
                     bool nodes_resize_policy = true,
                     Size arcs_size           = HashTableConst::default_size,
                     bool arcs_resize_policy  = true);

    /// copy constructor
    /** @param g the DiGraph to copy */
    DiGraph(const DiGraph& g);

    /// move constructor
    DiGraph(DiGraph&& g);

    /// destructor
    ~DiGraph() override;

    /**
     * @brief Build a complete DiGraph with n nodes
     * @param int n
     * @return the complete DiGraph
     */
    static DiGraph completeGraph(int n);

    /// @}

    // ############################################################################
    /// @name Operators
    // ############################################################################
    /// @{

    /// copy operator
    /** @param g the DiGraph to copy */
    DiGraph& operator=(const DiGraph& g);

    /// move assignment operator
    DiGraph& operator=(DiGraph&& g);

    /// tests whether two DiGraphs are identical (same nodes, same arcs)
    /** @param g the DiGraph with which "this" is compared */
    // not virtual : it is a feature !!! :)
    bool operator==(const DiGraph& g) const;

    /// tests whether two DiGraphs are different
    /** @param g the DiGraph with which "this" is compared */
    // not virtual : it is a feature !!! :)
    // bool operator!=(const DiGraph& g) const;

    /// @}

    // ############################################################################
    /// @name Accessors/Modifiers
    // ############################################################################
    /// @{

    /// insert a new arc into the directed graph
    /** @param tail the id of the tail of the new inserted arc
     * @param head the id of the head of the new inserted arc
     * @warning if the arc already exists, nothing is done. In particular, no
     * exception is raised.
     * @throw InvalidNode if head or tail does not belong to the graph nodes */
    void addArc(const NodeId tail, const NodeId head) override;

    /// remove a node and its adjacent arcs from the graph
    /** @param id the id of the node to be removed
     * @warning if the node does not exist, nothing is done. In particular, no
     * exception is raised.*/
    void eraseNode(const NodeId id) override;

    /// removes all the nodes and arcs from the graph
    void clear() override;

    /// to friendly display the content of the graph
    std::string toString() const override;

    /// to friendly display the content of the graph in the DOT syntax
    /** @param name The graph name in the dot syntax. Default is G.
     * @return Returns a string describing the graph in the dot syntax */
    virtual std::string toDot() const;

    /**
     * Build and return a topological order
     *
     * @throw InvalidDirectedCycle Raised if this DiGraph contains cycles.
     */
    Sequence< NodeId > topologicalOrder() const;
    /// @}

    /** checks whether there exists a directed path from \e from to \e to
     *
     * If from==to, this function checks if a directed cycle containing \e from
     * exists.
     * @param from
     * @param to
     * @return true if a directed path exists
     *
     */
    bool hasDirectedPath(NodeId from, NodeId to) const;

    /// returns a directed path from node1 to node2, or std::nullopt if none
    std::optional< std::vector< NodeId > > directedPath(NodeId node1, NodeId node2) const;

    /// returns a shortest path from node1 to node2 ignoring arc orientation,
    /// or std::nullopt if none
    std::optional< std::vector< NodeId > > directedUnorientedPath(NodeId node1, NodeId node2) const;

    /// returns the set of all ancestors of id (nodes from which id is reachable)
    NodeSet ancestors(NodeId id) const;

    /// returns the set of all descendants of id (nodes reachable from id)
    NodeSet descendants(NodeId id) const;

    /// returns { id } ∪ parents(id)
    NodeSet family(NodeId id) const;

    /// returns the union of families of all nodes in ids
    NodeSet family(const NodeSet& ids) const;

    /// returns a property {node:id of weakly connected component}
    NodeProperty< NodeId > connectedComponents() const;
  };

  /// for friendly displaying the content of directed graphs
  std::ostream& operator<<(std::ostream&, const DiGraph&);

} /* namespace gum */

static_assert(gum::GUM_DiGraphable< gum::DiGraph >);

#ifndef GUM_NO_INLINE
#  include <agrum/base/graphs/diGraph_inl.h>
#endif   // GU%_NO_INLINE

#endif   /* GUM_DIGRAPH_H */
