Java implementation Breadth-first search and depth-first search

Source: Internet
Author: User

Java implementation Breadth-first search and depth-first search

1. Overview

Review the algorithm data structure, use Java to achieve breadth-first search and depth-first search.

2. Code

The graph to search for is:

Java code:

1  PackageCn.edu.tju.scs;2 3 Importjava.util.LinkedList;4 ImportJava.util.Queue;5 6  Public classSearch {7      Public Static Boolean[] Accessed =New Boolean[8];8      Public Static Boolean[] Accessed2 =New Boolean[8];9      Public StaticArcnode [] heads =NewArcnode[8];Ten      Public Staticqueue<integer> queue =NewLinkedlist<integer>(); One      A      Public Static voidmain (String [] args) { -          for(inti = 0; i < heads.length; i++) { -Heads[i] =NewArcnode (i + 1);//If the node number is also starting from 0, there is a lot less to add a minus one operation the         } -          for(inti = 0; i < accessed.length; i++) { -Accessed[i] =false;  -         } +Heads[0].nextarc =NewArcnode (2); -Heads[0].nextarc.nextarc =NewArcnode (3); +          AHeads[1].nextarc =NewArcnode (1); atHeads[1].nextarc.nextarc =NewArcnode (4); -Heads[1].nextarc.nextarc.nextarc =NewArcnode (5); -          -Heads[2].nextarc =NewArcnode (1); -Heads[2].nextarc.nextarc =NewArcnode (6); -Heads[2].nextarc.nextarc.nextarc =NewArcnode (7); in          -Heads[3].nextarc =NewArcnode (2); toHeads[3].nextarc.nextarc =NewArcnode (8); +          -Heads[4].nextarc =NewArcnode (2); theHeads[4].nextarc.nextarc =NewArcnode (8); *          $Heads[5].nextarc =NewArcnode (3);Panax Notoginseng          -Heads[6].nextarc =NewArcnode (3); the          +Heads[7].nextarc =NewArcnode (4); AHeads[7].nextarc.nextarc =NewArcnode (5); the          + Broadfirstsearch (heads); - Depthfirstsearch (heads); $     } $  -     Private Static voidBroadfirstsearch (arcnode[] heads) { -Broadfirstsearch (heads[0]); the     } - Wuyi     Private Static voidBroadfirstsearch (Arcnode arcnode) { the Queue.add (Arcnode.adjvex); -ACCESSED2[ARCNODE.ADJVEX-1] =true; Wu          while(Queue.size () > 0) { -Integer Vex =Queue.remove (); AboutArcnode Curnode = heads[vex-1].nextarc; $              while(Curnode! =NULL) { -                 if(!accessed2[curnode.adjvex-1]) { - Queue.add (Curnode.adjvex); -ACCESSED2[CURNODE.ADJVEX-1] =true; A                 } +Curnode =Curnode.nextarc; the             } - System.out.print (vex); $             if(Queue.size () > 0) theSystem.out.print ("--"); the             Else theSystem.out.println (""); the         } -     } in  the     Private Static voidDepthfirstsearch (arcnode[] heads) { theDepthfirstsearch (heads[0]); About     } the  the     Private Static voidDepthfirstsearch (Arcnode arcnode) { theArcnode Curnode = heads[arcnode.adjvex-1]; + System.out.print (curnode); -ACCESSED[CURNODE.ADJVEX-1] =true; the         BayiCurnode =Curnode.nextarc; the          while(Curnode! =NULL) { the             if(!accessed[curnode.adjvex-1]){ -System.out.print ("--"); - Depthfirstsearch (curnode); the             } theCurnode =Curnode.nextarc; the         } the     } -}

Output Result:

(Guang Search:) 1--2, 3, 4, 5, 6--7
(Deep Search:) 1--2, 4, 8, 5, 3--6

Java implementation Breadth-first search and depth-first search

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.