The representation of the graph and the graph in the programming is almost the same, this question introduces the Adjacency table representation method.
The code outlines for the map object are as follows:
public class Digraph {public Digraph (int v) { } //create side public void Addedge (int v, int w) for V to W) { } Gets the vertex that the V can reach directly public iterable<integer> adj (int v) { } //Gets the number of vertices of the entire graph public int V () { } //Gets the number of edges of the entire graph public int E () { } //Reverses the direction of the full graph to public Digraph reverse () { } @Override Public String toString () { }}
Code
This time using the Adjacency table method to implement, the code and the graph is almost the same.
Import Java.util.linkedlist;import java.util.List; public class Digraph {private list<integer>[] adj; private int v; public Digraph (int v) {adj = (list<integer>[]) new LINKEDLIST[V]; THIS.V = v; }//create Edge of V to w public void Addedge (int v, int w) {adj[v].add (w); }//Gets the vertex that the V can reach directly public iterable<integer> adj (int v) {return adj[v]; }//Gets the number of vertices of the entire graph public int V () {return V; }//Gets the number of edges of the entire graph public int E () {int result = 0; for (list<integer> E:adj) {result + = E.size (); } return result; }//Reverses the direction of the full graph to public Digraph reverse () {Digraph G = new Digraph (THIS.V); for (int v = 0; v < this.v; v++) {for (int W:adj (v)) {G.addedge (w, v); }} return G; } @Override Public String toString () {String result = ""; for (int v = 0; v < this.v; v++) { Result + = v + ":"; for (int W:adj (v)) {result + = "" + W; } result + = "\ n"; } return result; }}
Algorithm 7-8: A forward Graph interface