Data structure experiment Graph Theory 1: breadth-first search traversal based on the adjacent matrix

Source: Internet
Author: User
Data structure experiment Graph Theory 1: breadth-first search traversal based on the adjacent matrix Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ The topic description gives a undirected connected graph with vertex numbers ranging from 0 to n-1. it traverses through the breadth-first search (BFS) and outputs The traversal Sequence starting from a certain vertex. (The adjacent contacts of the same node and smaller node numbers are preferentially traversed.) input the first behavior integer N (0 <n <100), indicating the number of data groups.
For each group of data, the first row is three integers K, M, T (0 <k <m <(k-1) * k/<t <K ), it indicates that there are m edges and k vertices. t indicates the starting vertex of traversal.
In the following m rows, each row is a space-separated integer U, V, indicating a undirected edge connecting u and v vertex. The output contains N rows, which correspond to N sets of outputs. Each row is a K integer separated by spaces and corresponds to a group of data, which indicates the traversal result of BFS. Sample Input
16 7 00 30 41 41 52 32 43 5
Sample output
0 3 4 2 5 1
The prompt shows that the storage structure is an adjacent matrix. Source sample program


#include <stdio.h>#include <string.h>#include <stdlib.h>int map[110][110];int dis[110];int vis[110];int k,m,t;int in=1;int out=0;void Bfs(int u){    int i=0;    out++;    for(i=0;i<k;i++)    {        if(map[u][i]==1&&vis[i]==0)        {            dis[in++]=i;            vis[i]=1;        }    }    if(out<=in)        Bfs(dis[out]);}int main(){    int n,i,j;    int u,v;    scanf("%d",&n);    for(i=0;i<n;i++)    {        scanf("%d %d %d",&k,&m,&t);        memset(map,0,sizeof(map));        memset(dis,0,sizeof(dis));        memset(vis,0,sizeof(vis));        for(j=0;j<m;j++)        {            scanf("%d %d",&u,&v);            map[u][v]=map[v][u]=1;        }        dis[0]=t;        vis[t]=1;        Bfs(t);        for(j=0;j<k-1;j++)        printf("%d ",dis[j]);        printf("%d\n",dis[j]);    }    return 0;}


Zookeeper

Data structure experiment Graph Theory 1: breadth-first search traversal based on the adjacent matrix

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.