See Data structure write code (41) Strong connected component

Source: Internet
Author: User

First, the concept problem is introduced, in the direction graph, if vertex v1 to v2 and v2 to v1, then Vertex v1 and V2 are strongly connected. If any two nodes of the graph are strongly connected, they are called strong connected graphs. The strongly connected sub-graph of a non-strongly connected graph is a strong connected component.

In particular, the concept of connectivity belongs to undirected graphs, and strong connectivity belongs to the undirected graph. For example: undirected graph: Connected graph, connected component, spanning tree, undirected graph: Strongly connected graph, strongly connected component.

The data structure book simply says a way to find strong connected components, in detail as follows:


In fact, I do not quite understand the principle of this algorithm. Fortunately, there is nothing wrong with implementing this algorithm.

In addition to this method there are three other methods:Kosaraju,Tarjan and Gabow. You can implement it later.


Full Source code:

Source code project file network address: Click to open the link


CrossLinkGraph.cpp: Defines the entry point of the console application. Cross-linked list notation for a graph # include "stdafx.h" #include <cstdlib> #define Max_vex_num 20enum e_state{e_state_error = 0,e_state_ Ok = 1,};struct arcnode//arc Node {int tailindex;//arc tail position int headindex;//Arc Header position Arcnode * tailnext;//Next ARC same arc Arcnode * headnext;/ /Next Arc head same arc};typedef struct Vnode{char vexname;//vertex name Arcnode * firstin; Arcnode * FIRSTOUT;} Graphlist[max_vex_num];//struct graph{graphlist list;//vertex array. int vexnum,arcnum;};/ /Gets the head node of the arc Arcnode * Getheadnode () {Arcnode * Pnode = (Arcnode *) malloc (sizeof (Arcnode)); if (pnode) {Pnode->headindex = PN Ode->tailindex = -1;pnode->headnext = Pnode->tailnext = NULL;} return pnode;} Arcnode * Getarcnode (int tailindex,int headindex) {Arcnode * Pnode = Getheadnode (); if (pnode) {Pnode->headindex = HeadIn Dex;pnode->tailindex = Tailindex;} return pnode;} int vexlocation (Graph G,char vex) {for (int i = 0; i < G.vexnum; i++) {if (G.list[i].vexname = vex) {return i;}} return-1;} void creategrahp (graph * g) {printf ("number of vertices and edges (arcs) of the input graph \n "); scanf ("%d%d%*c ", &g->vexnum,&g->arcnum);      Constructs the vertex set printf ("Enter the vertex set \ n"); for (int i = 0; i < g->vexnum; i++) {char name;scanf ("%c", &name); g->list[i].vexname = Name;g->lis      T[i].firstin = G->list[i].firstout = Getheadnode ();//Build head node and have head pointer pointing to head node}//construct vertex relationship fflush (stdin);      printf ("Please enter the relationship of vertices \ n");  for (int i = 0; i < g->arcnum; i++) {char vex1,vex2;scanf ("%c%c%*c", &vex1,&vex2); int location1 = vexlocation (*g,vex1); int location2 = Vexlocation (*G,VEX2); Arcnode * Pnode = Getarcnode (location1,location2);p Node->tailnext = g->list[location1].firstout->tailnext; G->list[location1].firstout->tailnext = Pnode;pnode->headnext = g->list[location2].firstin->    Headnext;g->list[location2].firstin->headnext = Pnode;  }//as long as the arc tail (or arc Head) node of all vertices is deleted,//delete the arc tail at the same time, memory error void Destorygraph (Graph * g) {for (int i = 0; i < g->vexnum; i++) {Arcnode * Next = g->list[i].firstout;//Delete all arc tails WHILe (next! = NULL) {Arcnode * freenode = Next;next = Next->tailnext;free (freenode);} G->list[i].firstin = G->list[i].firstout = Null;g->list[i].vexname = "; g->vexnum = G->arcNum = 0;}} int Firstoutadj (Graph g,int location) {Arcnode * next = g.list[location].firstout->tailnext;if (next! = NULL) {return NE Xt->headindex;} return-1;} int Nextoutadj (Graph g,int location1,int location2) {Arcnode * next = G.list[location1].firstout->tailnext;while ( Next! = NULL) {//Find to vex2if (Next->headindex = = Location2) {next = Next->tailnext;break;} Next = Next->tailnext;} if (next! = NULL) {return next->headindex;} return-1;} int Firstinadj (Graph g,int location) {Arcnode * next = g.list[location].firstin->headnext;if (next! = NULL) {return next ->tailindex;} return-1;} int Nextinadj (Graph g,int location1,int location2) {Arcnode * next = G.list[location1].firstin->headnext;while (next!) = NULL) {//Find to vex2if (Next->tailindex = = Location2) {next = Next->headnext;break;} Next = Next->headnext;} if (next! = NULL) {return next->tailindex;} return-1;} void Outdfs (Graph g,int i,bool * isvisited,int * finished,int * count) {Isvisited[i] = true;for (int next = Firstoutadj (g,i ); Next! =-1; Next = Nextoutadj (G,i,next)) {if (isvisited[next] = = False) {Outdfs (G,next,isvisited,finished,count);}} finished[(*count) + +] = i;//adds the node to the array to complete all the adjacency point searches.} void Outdfstraver (Graph g,int * finished) {//ARC-oriented depth first traverse bool Isvisited[max_vex_num] = {false};//tag array. int count = 0;for ( int i = 0; i < G.vexnum; i++) {if (isvisited[i] = = False) {Outdfs (G,i,isvisited,finished,&count);}}} void Indfs (Graph g,int i,bool * isvisited) {printf ("%c", g.list[i].vexname); Isvisited[i] = true;for (int next = Firstinadj ( G,I); Next! =-1; Next = Nextinadj (G,i,next)) {if (isvisited[next] = = False) {Indfs (g,next,isvisited);}}} Strongly connected graph void SCC (Graph g) {//bool Isvisited[max_vex_num] = {false};//tag array. int Finished[max_vex_num] = { -1};//Complete search Array. outdfstraver (g,finished);//The finished array is deposited according to the order of the search completion. int times = 0;//record Strong connectionThe number of pass components. for (int i = g.vexnum-1; I >= 0; i--) {int next = finished[i];if (Isvisited[next] = = False) {printf ("%d strong connected component:", times); Indfs (g,next,isvisited);p rintf ("\ n"); times++;}}} void Printgrahp (Graph g) {for (int i = 0; i < G.vexnum; i++) {printf ("vertices with%c arc tail are:", g.list[i].vexname); Arcnode * Next = g.list[i].firstout->tailnext;//Delete all arc tails while (next! = NULL) {printf ("%c", G.list[next->headindex]. Vexname); next = Next->tailnext;} printf ("\ n the vertex with%c as the arc head is:", g.list[i].vexname); next = g.list[i].firstin->headnext;//Delete all arc tails while (next! = NULL) {printf ("%c", g.list[next->tailindex].vexname); next = Next->headnext;} printf ("\ n");}} int _tmain (int argc, _tchar* argv[]) {graph g;creategrahp (&g);p rintgrahp (g);p rintf ("Vertices of the graph:%d, edge (ARC) Tree:%d\n", G.vexnum,g.arcnum); SCC (g);//Find the strongly connected component of the graph. Destorygraph (&g); return 0;}
Run:



See Data structure write code (41) Strong connected component

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.