Java uses adjacency matrix to achieve breadth first

Source: Internet
Author: User

To define a node class:

//一个节点class Vertex{    char label;    boolean wasVisited;    publicVertex(char label){        this.label = label;        false;     }}

Figure:

Class graph{Private Final intMax_verts = -;PrivateVertex vertexlist[];//Node list    Private intAdjmat[][];//adjacency matrix    Private intNverts;//Number of nodes    PrivateQueue Thequeue;//Assistance Queue     Public Graph() {vertexlist =NewVertex[max_verts]; Adjmat =New int[Max_verts]        [Max_verts]; Nverts =0; for(inti =0; i < max_verts; i++) { for(intj =0; J < Max_verts; J + +) Adjmat[i][j] =0; } Thequeue =NewArraydeque (); } Public void Addvertex(CharLab) {vertexlist[nverts++] =NewVertex (Lab); } Public void Addedge(intStartintEnd) {Adjmat[start][end] =1; Adjmat[end][start] =1; } Public void Displayvertex(intV) {System.out.print (Vertexlist[v].label); }//Gets the unreachable node that is adjacent to the V node     Public int Getadjunvisiedvertex(intV) { for(inti =0; i < nverts; i++) {if(Adjmat[v][i] = =1&& vertexlist[i].wasvisited = =false){returnI }        }return-1; }//Breadth-First search    /** * Rule 1: Access next neighbor of future access (if present), this vertex must be the adjacency point of the current vertex, marked as visited, and inserted into the queue * * Rule 2: If you cannot execute rule 1 because no vertices have been accessed, the queue The header takes a vertex (if present) and makes it * called the current vertex * Rule 3: If rule 2 cannot be executed because the queue is empty, the search ends */     Public void BFS() {vertexlist[0].wasvisited =true; Displayvertex (0);; Thequeue.offer (0);intv2; while(!thequeue.isempty ()) {intV1 = (int) Thequeue.remove (); while((v2 = Getadjunvisiedvertex (v1))! =-1) {vertexlist[v2].wasvisited =true;                Displayvertex (v2);            Thequeue.offer (v2); }        } for(inti =0; i < nverts; i++) {vertexlist[i].wasvisited =false; }    }   }

Test:

Graph thegraph = new graph ();Thegraph. Addvertex(' A ');Thegraph. Addvertex(' B ');Thegraph. Addvertex(' C ');Thegraph. Addvertex(' D ');Thegraph. Addvertex(' E ');Thegraph. Addvertex(' F ');Thegraph. Addvertex(' G ');Thegraph. Addvertex(' H ');Thegraph. Addvertex(' I ');Thegraph. Addedge(0,1);//abThegraph. Addedge(0,2);//acThegraph. Addedge(0,3);//adThegraph. Addedge(0,4);//aeThegraph. Addedge(1,5);//BFThegraph. Addedge(5,6);//FGThegraph. Addedge(3,6);//DGThegraph. Addedge(6,7);//giThegraph. BFS();

Breadth First search: ABCDEFGH

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java uses adjacency matrix to achieve breadth first

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.