06-Figure 1 lists the connected sets

Source: Internet
Author: User

Given an undirected graph with N vertices and E edges, list all its connected sets with DFS and BFS, respectively. Assume that vertices are numbered from 0 to N-1 . When searching, it is assumed that we always start from the lowest numbered vertices and access the adjacency points sequentially in ascending numbers.

Input format:

Enter the 1th line to give 2 integersN (< Span class= "katex-html" >< Span class= "Mord mathrm" >0<n≤10" and e < Span class= "Mord mathit", respectively, is the number of vertices and the number of sides of the graph. Subsequently e line, each row gives two endpoints of an edge. The numbers in each row are separated by 1 spaces.

Output format:

Follow the {V?1??v ? 2??  ... < Span class= "Strut bottom" >v< Span class= "Vlist" >? k??  } "format, each line outputs a connected set. Outputs the results of the DFS and then outputs the BFS results.

Input Sample:
8 60 70 12 04 12 43 5
Sample output:
{ 0 1 4 2 7 }{ 3 5 }{ 6 }{ 0 1 2 7 4 }{ 3 5 }{ 6 }
1 /*a simplified method for adjacency matrix representation of graphs*/2#include <iostream>3#include <cstdio>4#include <queue>5 using namespacestd;6 7 #defineMaxSize 108  9 intGraph[maxsize][maxsize], Nv, Ne;//NV vertex number NE edge numberTen intCheck[maxsize]; One voidbuildgraph () A { -     intV1,v2; -scanf"%d%d", &AMP;NV, &Ne); the      for(inti =0; i < Nv; i++){ -Check[i] =0; -          for(intj =0; J < Ne; J + +) -GRAPH[I][J] =0; +     }         -      +      for(inti =0; i < Ne; i++) { Ascanf"%d%d", &AMP;V1, &v2); atGRAPH[V1][V2] =1;//1 with side -GRAPH[V2][V1] =1; -     } - } - intcheckvisited () - { in     inti; -      for(i =0; i < Nv; i + +){ to         if( !Check[i]) +              Break; -     } the     if(i = = Nv)//if it's all been accessed, *         return-1; $     returni;Panax Notoginseng } -  the voidClearcheck () + { A      for(inti =0; i < Nv; i++) theCheck[i] =0; + } -  $ intBFS () $ { -queue<int>queue; -     inti,j; thei =checkvisited (); -     if(i = =-1 )Wuyi         return-1; the Queue.push (i); -Check[i] =1;//the vertex has been accessed Wuprintf"{%d", i); -      while( !Queue.empty ()) { About         intTempi =Queue.front (); $ Queue.pop (); -          for(j =0; J < Nv; J + +)  -             if(Graph[tempi][j] = =1&&!Check[j]) { -CHECK[J] =1;//the vertex has been accessed Aprintf"%d", j); + Queue.push (j); the             } -     }     $printf"}\n"); the     returnBFS (); the } the  the voidDFS (intVi) - { inCHECK[VI] =1; theprintf"%d", Vi); the      for(intj =0; J < Nv; J + +) { About         if(Graph[vi][j] = =1&&!Check[j]) the DFS (j); the     } the } + intListdfs () - { the     if(checkvisited () = =-1 )Bayi         return-1; theprintf"{ "); the DFS (checkvisited ()); -printf"}\n"); -     returnListdfs (); the } the intMain () the { the buildgraph (); - Listdfs (); the Clearcheck (); the BFS (); the     return 0;94}



06-Figure 1 lists the connected sets

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.