Depth and breadth-first traversal algorithm for Java implementation graphs

Source: Internet
Author: User

Overview:

Recently to learn to write web crawler. So the depth and breadth of the map to search all over again.


Figure structure Show:



implementation process:first, let's look at the implementation of the graph structure in the code. There are three blocks of logic:
1. The nodes in the diagram:

public class Graphnode {public list<graphedge> edgelist = null;private string label = "";p ublic graphnode (String lab EL) {This.label = label;if (edgelist = = null) {edgelist = new arraylist<graphedge> ();}} /** * Add an edge to the current node * Graphnode * @param edge * Join the Edge */public void Addedgelist (Graphedge edge) {Edgelist.add (edge);} Public String Getlabel () {return label;}}

2. Edges in the diagram:

public class Graphedge {private Graphnode nodeleft;private graphnode noderight;/** * @param nodeleft * left side of Edge * @param noder Ight * Side right end */public Graphedge (Graphnode nodeleft, Graphnode noderight) {this.nodeleft = Nodeleft;this.noderight = NodeRig HT;} Public Graphnode Getnodeleft () {return nodeleft;} Public Graphnode Getnoderight () {return noderight;}}

3. Combine nodes and edges into one graph structure:

public class Mygraph {private list<graphnode> nodes = Null;public void initgraph (int n) {if (nodes = = null) {nodes = New arraylist<graphnode> ();} Graphnode node = null;for (int i = 0; i < n; i++) {node = new Graphnode (string.valueof (i)); Nodes.Add (node);}} public void initgraph (int n, Boolean b) {initgraph (n); Graphedge edge01 = new Graphedge (nodes.get (0), Nodes.get (1)); Graphedge edge02 = new Graphedge (nodes.get (0), Nodes.get (2)); Graphedge edge13 = new Graphedge (Nodes.get (1), Nodes.get (3)); Graphedge edge14 = new Graphedge (Nodes.get (1), Nodes.get (4)); Graphedge edge25 = new Graphedge (Nodes.get (2), Nodes.get (5)); Graphedge edge26 = new Graphedge (Nodes.get (2), Nodes.get (6)); Graphedge edge37 = new Graphedge (Nodes.get (3), Nodes.get (7)); Graphedge edge47 = new Graphedge (Nodes.get (4), Nodes.get (7)); Graphedge edge56 = new Graphedge (Nodes.get (5), Nodes.get (6)), Nodes.get (0). Addedgelist (EDGE01); nodes.get (0). Addedgelist (EDGE02); Nodes.get (1). Addedgelist (EDGE13); Nodes.get (1). Addedgelist (EdgeNodes.get (2). Addedgelist (Edge25); Nodes.get (2). Addedgelist (Edge26); Nodes.get (3). Addedgelist (EDGE37); Nodes.get (4). Addedgelist (edge47); Nodes.get (5). Addedgelist (edge56);} public void Initgraph () {Initgraph (8, false);} Public list<graphnode> Getgraphnodes () {return nodes;}}

With the structure of the graph, we are able to do some practical work.
Depth-First search:

public class Dfsearch {/** * Depth traversal * Dfsearch * @param node * Current node * @param visited * interviewed node list */public void Searchtravers ing (graphnode node, list<graphnode> visited) {//infer whether to traverse the if (Visited.contains (node)) {return;} Visited.add (node); System.out.println ("node:" + Node.getlabel ()); for (int i = 0; i < node.edgeList.size (); i++) {searchtraversing (Node.edge List.get (i). Getnoderight (), visited);}}

Breadth-First search:

public class Bfsearch {/** * Breadth First search * Bfsearch * @param node * Search for portal nodes */public void searchtraversing (Graphnode node) {Lis T<graphnode> visited = new arraylist<graphnode> (); The elements that have been interviewed queue<graphnode> q = new linkedlist<graphnode> (); Use the queue to hold the element Q.offer (node) to traverse sequentially, while (!q.isempty ()) {Graphnode Currnode = Q.poll (); if (!visited.contains (Currnode)) { Visited.add (Currnode); System.out.println ("node:" + Currnode.getlabel ()); for (int i = 0; i < currNode.edgeList.size (); i++) {Q.offer (currnode.e Dgelist.get (i). Getnoderight ());}}}}

Execution Result:



source code Download:

http://download.csdn.net/detail/u013761665/8968443

Depth and breadth-first traversal algorithm for Java implementation graphs

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.