Beispiel #1
0
 /**
  * Returns the reverse of the digraph. 反向图
  *
  * @return the reverse of the digraph
  */
 public Digraph reverse() { // 反向图
   Digraph R = new Digraph(V);
   for (int v = 0; v < V; v++) {
     for (int w : adj(v)) { // 反向
       R.addEdge(w, v);
     }
   }
   return R;
 }
Beispiel #2
0
 /**
  * Initializes a digraph from the specified input stream. The format is the number of vertices
  * <em>V</em>, followed by the number of edges <em>E</em> , followed by <em>E</em> pairs of
  * vertices, with each entry separated by whitespace.
  *
  * @param in the input stream
  * @throws IndexOutOfBoundsException if the endpoints of any edge are not in prescribed range
  * @throws IllegalArgumentException if the number of vertices or edges is negative
  */
 public Digraph(In in) {
   try {
     this.V = in.readInt();
     if (V < 0)
       throw new IllegalArgumentException("Number of vertices in a Digraph must be nonnegative");
     indegree = new int[V]; // 点
     adj = (Bag<Integer>[]) new Bag[V];
     for (int v = 0; v < V; v++) {
       adj[v] = new Bag<Integer>();
     }
     int E = in.readInt(); // 边
     if (E < 0)
       throw new IllegalArgumentException("Number of edges in a Digraph must be nonnegative");
     for (int i = 0; i < E; i++) {
       int v = in.readInt();
       int w = in.readInt();
       addEdge(v, w);
     }
   } catch (NoSuchElementException e) {
     throw new InputMismatchException("Invalid input format in Digraph constructor");
   }
 }