PAT 05-1 List components (simple DFS vs. BFs)

Source: Internet
Author: User

It's too complicated for him to get this question.


Clearly is the maximum length of 10 of the order structure can solve the problem, it is preferred to use linked list.


BFS is involved in a queue operation, which is much better in order structure in this small-scale data


The topics are as follows:


For a given undirected graph with N vertices and E edges, please list all of the connected components by both DFS and BFS. Assume that all the vertices is numbered from 0 to N-1. While searching, assume. We always start from the vertex with the smallest index, and visit it adjacent vertices in a Scending order of their indices.

Input Specification:

Each input file contains the one test case. For each case, the first line gives integers N (0<n<=10) and E, which is the number of vertices and the number of edges, respectively. Then E-lines follow, each described a edge by giving the both ends. All the numbers in a line is separated by a space.

Output Specification:

For each test case, print in each line a connected component in the format "{v1 v2 ... vk}". First print the result obtained by DFS, then by BFS.

Sample Input:
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}
Topic Link: Dot i dot i dot I


The code is as follows:

# include <stdio.h># include <stdlib.h>void DFS (int i); void BFS (int i); int vertice[10][10];          int flag[10];               int n,e;    int main () {int i,j,k;    scanf ("%d%d", &n,&e);    int A, B;        for (i=0;i<e;i++) {scanf ("%d%d", &a,&b);    VERTICE[A][B] = Vertice[b][a] = 1;        } for (i=0;i<n;i++) {if (flag[i]==1) continue;       else printf ("{"), DFS (i), printf ("}\n");    } for (i=0;i<10;i++) flag[i] = 0;        for (i=0;i<n;i++) {if (flag[i]==1) continue;       else printf ("{"), BFS (i), printf ("}\n");    }}void DFS (int i) {printf ("%d", I), flag[i] = 1;    Int J;          for (j=0;j<n;j++) if (vertice[i][j]==1&&flag[j]==0) {DFS (j);   }}void BFS (int i) {int start,end,q[100];   Start = end = 0;   q[end++] = i;flag[i] = 1;   int temp,j;       while (start<end) {temp = q[start++];       printf ("%d", temp); for (j=0;j<n;j++) if (vertice[temp][j]==1&&flag[j]==0) {q[end++] = j;          FLAG[J] = 1; }   }}


PAT 05-1 List components (simple DFS vs. BFs)

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.