``` 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 ``` ```// Exercise 4.2.3 (Solution published at http://algs4.cs.princeton.edu/) package algs42; import stdlib.*; import algs13.Bag; /** * The Digraph class represents an directed graph of vertices * named 0 through V-1. * It supports the following operations: add an edge to the graph, * iterate over all of the neighbors incident to a vertex. * Parallel edges and self-loops are permitted. *

* For additional documentation, * see Section 5.2 of * Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. */ public class Digraph { private final int V; private int E; private final Bag[] adj; /** * Create an empty digraph with V vertices. */ @SuppressWarnings("unchecked") public Digraph(int V) { if (V < 0) throw new IllegalArgumentException("Number of vertices in a Digraph must be nonnegative"); this.V = V; this.E = 0; adj = new Bag[V]; for (int v = 0; v < V; v++) { adj[v] = new Bag<>(); } } /** * Return the number of vertices in the digraph. */ public int V() { return V; } /** * Return the number of edges in the digraph. */ public int E() { return E; } /** * Add the directed edge v->w to the digraph. * @throws java.lang.IndexOutOfBoundsException unless both 0 <= v < V and 0 <= w < V */ public void addEdge(int v, int w) { if (v < 0 || v >= V) throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1)); if (w < 0 || w >= V) throw new IndexOutOfBoundsException("vertex " + w + " is not between 0 and " + (V-1)); adj[v].add(w); E++; } /** * Return the list of vertices pointed to from vertex v as an Iterable. * @throws java.lang.IndexOutOfBoundsException unless 0 <= v < V */ public Iterable adj(int v) { if (v < 0 || v >= V) throw new IndexOutOfBoundsException(); return adj[v]; } /** * 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; } /** * Return a string representation of the digraph. */ public String toString() { StringBuilder s = new StringBuilder(); String NEWLINE = System.getProperty("line.separator"); s.append(V + " vertices, " + E + " edges " + NEWLINE); for (int v = 0; v < V; v++) { s.append(String.format("%d: ", v)); for (int w : adj[v]) { s.append(String.format("%d ", w)); } s.append(NEWLINE); } return s.toString(); } /** * Save a graphviz representation of the graph. * See graphviz.org. */ public void toGraphviz(String filename) { GraphvizBuilder gb = new GraphvizBuilder (); for (int v = 0; v < V; v++) { gb.addNode (v); for (int w : adj[v]) gb.addEdge (v, w); } gb.toFile (filename); } /** * Test client. */ public static void main(String[] args) { //args = new String[] { "data/mediumDG.txt" }; args = new String[] { "data/tinyDG.txt" }; //args = new String[] { "data/tinyDGeuler1.txt" }; //args = new String[] { "data/tinyDGeuler2.txt" }; //args = new String[] { "data/tinyDGeuler3.txt" }; //args = new String[] { "data/tinyDGeuler4.txt" }; //args = new String[] { "data/tinyDGeuler5.txt" }; //args = new String[] { "data/tinyDGeuler6.txt" }; //args = new String[] { "data/tinyDGex2.txt" }; //args = new String [] { "10", "20" }; Digraph G; if (args.length == 1) { In in = new In(args[0]); G = DigraphGenerator.fromIn(in); } else { int V = Integer.parseInt (args[0]); int E = Integer.parseInt (args[1]); G = DigraphGenerator.simple(V, E); } StdOut.println(G); G.toGraphviz ("g.png"); } } ```