Connected graph algorithm of data structure

Source: Internet
Author: User

#include <stdio.h>#include<string.h>#include<math.h>#defineMAXN 110using namespaceStd;typedefCharVertextype;typedefintEdgetype;intK;intS[MAXN];intVis[maxn];typedefstruct{    CharVEXES[MAXN]; intMAPS[MAXN][MAXN]; intnumvexes, Numedge;} Mgraph;///creating diagrams with adjacency matricesvoidCreategraph (Mgraph &G) {    intu, v; printf ("Enter the number of vertices and the number of edges:"); scanf ("%d%d", &g.numvexes, &G.numedge);    GetChar ();  for(intI=0; i<g.numvexes; i++) scanf ("%c", &G.vexes[i]);  for(intI=0; i<g.numvexes; i++)         for(intj=0; j<g.numvexes; J + +) G.maps[i][j]=0;  for(intI=0; i<g.numedge; i++) {scanf ("%d%d", &u, &v); G.MAPS[U][V]=g.maps[v][u]=1; }}///Depth-first traversal from vertex ivoidDFS (Mgraph G,intu) {Vis[u]=1;    Putchar (G.vexes[u]); S[k++]=u;  for(intI=0; i<g.numvexes; i++)        if(g.maps[u][i]&&!Vis[i]) DFS (G, i);}///Depth-First traversalintDFSM (mgraph G) {intCnt=0; memset (Vis,0,sizeof(VIS));  for(intI=0; i<g.numvexes; i++)    {        if(!Vis[i]) {k=0; memset (s),0,sizeof(s)); CNT++;            DFS (G, i); printf (":");  for(intz=0; z<k; z++) printf ("%d", S[z]); printf ("--%d\n", K); }    }    returnCNT;}///to find the number of connected componentsintcount_conn (mgraph G) {intCnt=0; memset (Vis,0,sizeof(VIS));  for(intv=0; v<g.numvexes; v++)    {        if(!Vis[v]) {CNT++;        DFS (G, v); }    }    returnCNT;}intMain () {mgraph G;    Creategraph (G); intCount=DFSM (G); printf ("%d\n", Count); return 0;}/*
Sample input:13ABCDEFGHIJKLM0 10 20 50 111 123 46 76 86 109 119 1211 127 10 Sample output: abmjlcf:0 1 9 2 5--7de:3 4--2ghki:6 7 8--*/

Connected graph algorithm of data structure

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.