Java graph traversal (deep traversal and breadth traversal)

Source: Internet
Author: User

Import java. util. *;/*** in this example, we use the graph Traversal method, let me understand the graph traversal * Created on 2013-11-18 * @ version 0.1 */public class DeptSearch {public static void main (String args []) {// construct the Node object NodeT a = new NodeT ("a"); NodeT B = new NodeT ("B"); NodeT c = new NodeT ("c "); nodeT d = new NodeT ("d"); NodeT e = new NodeT ("e"); NodeT f = new NodeT ("f "); nodeT g = new NodeT ("g"); NodeT h = new NodeT ("h"); ArcT AB = new ArcT (a, B ); arcT ac = new ArcT (a, c); ArcT Ad = new ArcT (a, d); ArcT ah = new ArcT (a, h); ArcT bc = new ArcT (B, c); ArcT de = new ArcT (d, e); ArcT ef = new ArcT (e, f); ArcT eg = new ArcT (e, g); ArcT hg = new ArcT (h, g ); // establish their relationship. outgoing. add (AB);. outgoing. add (ac);. outgoing. add (ad);. outgoing. add (ah); B. outgoing. add (bc); d. outgoing. add (de); e. outgoing. add (ef); e. outgoing. add (eg); h. outgoing. add (hg); // construct the object DeptSearch search = new DeptSearch (); // traverse the System in breadth. out. println ("Breadth Traversal: "); search. widthSearch (a); // traverse System in depth. out. println ("Deep traversal:"); List <NodeT> visited = new ArrayList <NodeT> (); search. deptFisrtSearch (a, visited);}/** method of deep sorting * the method of this method is as follows: keep searching for one node in depth, until it has no node * the set of elements accessed by the current element * visited of the cur */void deptFisrtSearch (NodeT cur, List <NodeT> visited) {// accessed, if (visited. contains (cur) return; visited. add (cur); System. out. println ("This traversal is:" + cur. word); for (int I = 0; I <c Ur. outgoing. size (); I ++) {// access the end point of the current vertex deptFisrtSearch (cur. outgoing. get (I ). end, visited) ;}}/*** method of sorting by breadth * The method is used to access the graph by hierarchy. The first layer is followed by the second layer, and so on * @ param start from which to sort the logs */void widthSearch (NodeT start) {// record all accessed elements Set <NodeT> visited = new HashSet <NodeT> (); // use a Queue to store all the elements to be accessed in sequence: Queue <NodeT> q = new Queue list <NodeT> (); // Add the current element to the end of the Queue q. offer (start); while (! Q. isEmpty () {NodeT cur = q. poll (); // if (! Visited. contains (cur) {visited. add (cur); System. out. println ("the queried node is:" + cur. word); for (int I = 0; I <cur. outgoing. size (); I ++) {// Add its next layer to the queue q. offer (cur. outgoing. get (I ). end) ;}}}}/*** set of all links of vertex */class NodeT {/*/List <ArcT> outgoing; // String word; public NodeT (String word) {this. word = word; outgoing = new ArrayList <ArcT> () ;}/ *** relationship between a single graph vertex */class ArcT {NodeT start, end;/* start Point, end Point */public ArcT (NodeT start, NodeT end) {this. start = start; this. end = end ;}}

 

Related Article

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.