Class DirectedGraph

  • All Implemented Interfaces:
    IGraph

    public class DirectedGraph
    extends Object
    implements IGraph
    Directed graph implementation : arcs are indexed per endpoints
    Author:
    Jean-Guillaume Fages, Xavier Lorca
    • Constructor Summary

      Constructors 
      Constructor Description
      DirectedGraph​(int n, SetType type, boolean allNodes)
      Creates an empty graph.
      DirectedGraph​(Model model, int n, SetType type, boolean allNodes)
      Creates an empty backtrable graph of n nodes Allocates memory for n nodes (but they should then be added explicitly, unless allNodes is true).
    • Constructor Detail

      • DirectedGraph

        public DirectedGraph​(int n,
                             SetType type,
                             boolean allNodes)
        Creates an empty graph. Allocates memory for n nodes (but they should then be added explicitly, unless allNodes is true).
        Parameters:
        n - maximum number of nodes
        type - data structure to use for representing node successors and predecessors
        allNodes - true iff all nodes must always remain present in the graph. i.e. The node set is fixed to [0,n-1] and will never change
      • DirectedGraph

        public DirectedGraph​(Model model,
                             int n,
                             SetType type,
                             boolean allNodes)
        Creates an empty backtrable graph of n nodes Allocates memory for n nodes (but they should then be added explicitly, unless allNodes is true).
        Parameters:
        model - model providing the backtracking environment
        n - maximum number of nodes
        type - data structure to use for representing node successors and predecessors
        allNodes - true iff all nodes must always remain present in the graph
    • Method Detail

      • getNbMaxNodes

        public int getNbMaxNodes()
        Description copied from interface: IGraph
        The maximum number of nodes in the graph Vertices of the graph belong to [0,getNbMaxNodes()-1] This quantity is fixed at the creation of the graph
        Specified by:
        getNbMaxNodes in interface IGraph
        Returns:
        the maximum number of nodes of the graph
      • getNodes

        public ISet getNodes()
        Specified by:
        getNodes in interface IGraph
        Returns:
        the collection of nodes present in the graph
      • getType

        public SetType getType()
        Description copied from interface: IGraph
        Get the type of data structures used in the graph
        Specified by:
        getType in interface IGraph
        Returns:
        the type of data structures used in the graph
      • addNode

        public boolean addNode​(int x)
        Description copied from interface: IGraph
        Adds node x to the node set of the graph
        Specified by:
        addNode in interface IGraph
        Parameters:
        x - a node index
        Returns:
        true iff x was not already present in the graph
      • removeNode

        public boolean removeNode​(int x)
        Description copied from interface: IGraph
        Remove node x from the graph
        Specified by:
        removeNode in interface IGraph
        Parameters:
        x - a node index
        Returns:
        true iff x was present in the graph
      • removeArc

        public boolean removeArc​(int from,
                                 int to)
        remove arc (from,to) from the graph
        Parameters:
        from - a node index
        to - a node index
        Returns:
        true iff arc (from,to) was in the graph
      • arcExists

        public boolean arcExists​(int from,
                                 int to)
        Test whether arc (from,to) exists or not in the graph
        Parameters:
        from - a node index
        to - a node index
        Returns:
        true iff arc (from,to) exists in the graph
      • isArcOrEdge

        public boolean isArcOrEdge​(int from,
                                   int to)
        Description copied from interface: IGraph
        If this is directed returns true if and only if arc (x,y) exists Else, if this is undirected returns true if and only if edge (x,y) exists

        This method enables to capitalize some code but should be called with care

        Specified by:
        isArcOrEdge in interface IGraph
        Parameters:
        from - a node index
        to - a node index
      • isDirected

        public boolean isDirected()
        Specified by:
        isDirected in interface IGraph
        Returns:
        true if and only if this is a directed graph
      • addArc

        public boolean addArc​(int from,
                              int to)
        add arc (from,to) to the graph
        Parameters:
        from - a node index
        to - a node index
        Returns:
        true iff arc (from,to) was not already in the graph
      • getSuccOf

        public ISet getSuccOf​(int x)
        Get successors of node x
        Parameters:
        x - node index
        Returns:
        successors of x
      • getSuccOrNeighOf

        public ISet getSuccOrNeighOf​(int x)
        Description copied from interface: IGraph
        Get either x's successors or neighbors.

        This method enables to capitalize some code but should be called with care

        Specified by:
        getSuccOrNeighOf in interface IGraph
        Parameters:
        x - a node index
        Returns:
        x's successors if this is directed x's neighbors otherwise
      • getPredOf

        public ISet getPredOf​(int x)
        Get predecessors of node x
        Parameters:
        x - node index
        Returns:
        predecessors of x
      • getPredOrNeighOf

        public ISet getPredOrNeighOf​(int x)
        Description copied from interface: IGraph
        Get either x's predecessors or neighbors.

        This method enables to capitalize some code but should be called with care

        Specified by:
        getPredOrNeighOf in interface IGraph
        Parameters:
        x - a node index
        Returns:
        x's predecessors if this is directed x's neighbors otherwise