类 Digraph


  • public class Digraph
    extends java.lang.Object
    The Digraph class represents a directed graph of vertices named 0 through V - 1. It supports the following two primary operations: add an edge to the digraph, iterate over all of the vertices adjacent from a given vertex. Parallel edges and self-loops are permitted.

    This implementation uses an adjacency-lists representation, which is a vertex-indexed array of Bag objects. All operations take constant time (in the worst case) except iterating over the vertices adjacent from a given vertex, which takes time proportional to the number of such vertices.

    For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 构造器概要

      构造器 
      构造器 说明
      Digraph​(int V)
      Initializes an empty digraph with V vertices.
      Digraph​(Digraph G)
      Initializes a new digraph that is a deep copy of the specified digraph.
      Digraph​(In in)
      Initializes a digraph from the specified input stream.
    • 方法概要

      修饰符和类型 方法 说明
      void addEdge​(int v, int w)
      Adds the directed edge v→w to this digraph.
      java.lang.Iterable<java.lang.Integer> adj​(int v)
      Returns the vertices adjacent from vertex v in this digraph.
      int E()
      Returns the number of edges in this digraph.
      int indegree​(int v)
      Returns the number of directed edges incident to vertex v.
      static void main​(java.lang.String[] args)
      Unit tests the Digraph data type.
      int outdegree​(int v)
      Returns the number of directed edges incident from vertex v.
      Digraph reverse()
      Returns the reverse of the digraph.
      java.lang.String toString()
      Returns a string representation of the graph.
      int V()
      Returns the number of vertices in this digraph.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 构造器详细资料

      • Digraph

        public Digraph​(int V)
        Initializes an empty digraph with V vertices.
        参数:
        V - the number of vertices
        抛出:
        java.lang.IllegalArgumentException - if V < 0
      • Digraph

        public Digraph​(In in)
        Initializes a digraph from the specified input stream. The format is the number of vertices V, followed by the number of edges E, followed by E pairs of vertices, with each entry separated by whitespace.
        参数:
        in - the input stream
        抛出:
        java.lang.IllegalArgumentException - if the endpoints of any edge are not in prescribed range
        java.lang.IllegalArgumentException - if the number of vertices or edges is negative
        java.lang.IllegalArgumentException - if the input stream is in the wrong format
      • Digraph

        public Digraph​(Digraph G)
        Initializes a new digraph that is a deep copy of the specified digraph.
        参数:
        G - the digraph to copy
    • 方法详细资料

      • V

        public int V()
        Returns the number of vertices in this digraph.
        返回:
        the number of vertices in this digraph
      • E

        public int E()
        Returns the number of edges in this digraph.
        返回:
        the number of edges in this digraph
      • addEdge

        public void addEdge​(int v,
                            int w)
        Adds the directed edge v→w to this digraph.
        参数:
        v - the tail vertex
        w - the head vertex
        抛出:
        java.lang.IllegalArgumentException - unless both 0 <= v < V and 0 <= w < V
      • adj

        public java.lang.Iterable<java.lang.Integer> adj​(int v)
        Returns the vertices adjacent from vertex v in this digraph.
        参数:
        v - the vertex
        返回:
        the vertices adjacent from vertex v in this digraph, as an iterable
        抛出:
        java.lang.IllegalArgumentException - unless 0 <= v < V
      • outdegree

        public int outdegree​(int v)
        Returns the number of directed edges incident from vertex v. This is known as the outdegree of vertex v.
        参数:
        v - the vertex
        返回:
        the outdegree of vertex v
        抛出:
        java.lang.IllegalArgumentException - unless 0 <= v < V
      • indegree

        public int indegree​(int v)
        Returns the number of directed edges incident to vertex v. This is known as the indegree of vertex v.
        参数:
        v - the vertex
        返回:
        the indegree of vertex v
        抛出:
        java.lang.IllegalArgumentException - unless 0 <= v < V
      • reverse

        public Digraph reverse()
        Returns the reverse of the digraph.
        返回:
        the reverse of the digraph
      • toString

        public java.lang.String toString()
        Returns a string representation of the graph.
        覆盖:
        toString 在类中 java.lang.Object
        返回:
        the number of vertices V, followed by the number of edges E, followed by the V adjacency lists
      • main

        public static void main​(java.lang.String[] args)
        Unit tests the Digraph data type.
        参数:
        args - the command-line arguments