06-Figure 1 List Components, 06-components

Source: Internet
Author: User

06-Figure 1 List Components, 06-components

This topic mainly involves the queue, the representation of an undirected graph's Adjacent matrix, and the depth and breadth of the graph are preferentially searched. It is rough brother, refer to his program (http://www.cnblogs.com/liangchao/p/4288807.html), mainly BFS that piece, the courseware does not understand. I don't quite understand. Why can't I use the pointer to the graph instead of the function return value to initialize the graph? The code and question are as follows:

  1 #include <stdio.h>  2 #include <stdlib.h>  3 #include <string.h>  4 #include <stdbool.h>  5   6 typedef struct MGraph  7 {  8     int vertice;  9     int * edge; 10     bool * visited; 11 }MGraph, * pMGraph; 12 typedef struct Queue 13 { 14     int * elem; 15     int head; 16     int tail; 17     int size; 18 }Queue, * pQueue; 19  20 pMGraph initGraph(int vn); 21 void link(pMGraph pG, int v1, int v2); 22 void DFS(pMGraph pG, int v); 23  24 void BFS(pMGraph pG, int v); 25 pQueue createQueue(int vn); 26 bool isEmpty(pQueue pQ); 27 void inQueue(pQueue, int v); 28 int outQueue(pQueue); 29  30 int main() 31 { 32 //    freopen("in.txt", "r", stdin); // for test 33     int i, N, E; 34     scanf("%d%d", &N, &E); 35     pMGraph pG; 36     pG = initGraph(N); 37      38     int v1, v2; 39     for(i = 0; i < E; i++) 40     { 41         scanf("%d%d", &v1, &v2); 42         link(pG, v1, v2); 43     } 44      45     for(i = 0; i < N; i++) 46     { 47         if(!pG->visited[i]) 48         { 49             printf("{ "); 50             DFS(pG, i); 51             printf("}\n"); 52         } 53     } 54     memset(pG->visited, false, pG->vertice * sizeof(bool)); 55     for(i = 0; i < N; i++) 56     { 57         if(!pG->visited[i]) 58         { 59             printf("{ "); 60             BFS(pG, i); 61             printf("}\n"); 62         } 63     } 64 //    fclose(stdin); // for test 65     return 0; 66 } 67  68 pMGraph initGraph(int vn) 69 { 70     int len; 71     len = vn * (vn - 1) / 2; 72      73     pMGraph pG; 74     pG = (pMGraph)malloc(sizeof(MGraph)); 75     pG->vertice = vn; 76     pG->edge = (int *)malloc(len * sizeof(int)); 77     memset(pG->edge, 0, len * sizeof(int)); 78     pG->visited = (bool *)malloc(vn * sizeof(bool)); 79     memset(pG->visited, false, vn * sizeof(bool)); 80      81     return pG; 82 } 83  84 void link(pMGraph pG, int v1, int v2) 85 { 86     int index; 87      88     if(v1 > v2) 89     { 90         v1 += v2; 91         v2 = v1 - v2; 92         v1 -= v2; 93     } 94     index = v2 * (v2 - 1) / 2 + v1; 95     pG->edge[index] = 1; 96 } 97  98 void DFS(pMGraph pG, int v) 99 {100     int row, col, index;101     102     pG->visited[v] = true;103     printf("%d ", v);104     for(col = 0; col < v; col++)105     {106         index = v * (v - 1) / 2 + col;107         if(pG->edge[index] && pG->visited[col] == false)108             DFS(pG, col);109     }110     for(row = v + 1; row < pG->vertice; row++)111     {112         index = row * (row - 1) / 2 + v;113         if(pG->edge[index] && pG->visited[row] == false)114             DFS(pG, row);115     }116 }117 118 void BFS(pMGraph pG, int v)119 {120     pQueue pQ;121     pQ = createQueue(pG->vertice);122     123     int row, col, index;124     pG->visited[v] = true;125     printf("%d ", v);126     inQueue(pQ, v);127     while(!isEmpty(pQ))128     {129         v = outQueue(pQ);130         for(col = 0; col < v; col++)131         {132             index = v * (v - 1) / 2 + col;133             if(pG->edge[index] && pG->visited[col] == false)134             {135                 pG->visited[col] = true;136                 printf("%d ", col);137                 inQueue(pQ, col);138             }139         }140         for(row = v + 1; row < pG->vertice; row++)141         {142             index = row * (row - 1) / 2 + v;143             if(pG->edge[index] && pG->visited[row] == false)144             {145                 pG->visited[row] = true;146                 printf("%d ", row);147                 inQueue(pQ, row);148             }149         }150     }151 }152 153 pQueue createQueue(int vn)154 {155     pQueue pQ;156     pQ = (pQueue)malloc(sizeof(Queue));157     pQ->size = vn + 1;158     pQ->head = pQ->tail = 0;159     pQ->elem = (int *)malloc(pQ->size * sizeof(int));160     161     return pQ;162 }163 164 bool isEmpty(pQueue pQ)165 {166     if(pQ->head != pQ->tail)167         return false;168     else169         return true;170 }171 172 void inQueue(pQueue pQ, int v)173 {174     pQ->tail = (pQ->tail + 1) % pQ->size;175     pQ->elem[pQ->tail] = v;176 }177 178 int outQueue(pQueue pQ)179 {180     pQ->head = (pQ->head + 1) % pQ->size;181     182     return pQ->elem[pQ->head];183 }

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

Input Specification:

Each input file contains one test case. for each case, the first line gives two integers N (0 <N <= 10) and E, which are the number of vertices and the number of edges, respectively. then E lines follow, each described an edge by giving the two ends. all the numbers in a line are 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 }

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.