Algorithm 7-8: A forward Graph interface

Source: Internet
Author: User
Tags iterable

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.