Strongly Connected Component

Source: Internet
Author: User

The kosaraju algorithm requires directed graph strongly connected components.

Take a look, here
Detailed description.

# Include <stdio. h> # include <string. h ># include <vector> using namespace STD; /* num [] records access vertex sequence STC [] records connected branch E1 of vertex [] source image e2 [] Reverse graph visited [] hash accessed vertex */const int maxn = 101; typedef vector <int> edge; edgee1 [maxn], E2 [maxn]; int num [maxn], STC [maxn], CNT; bool visited [maxn]; void DFS (INT Vex) {visited [Vex] = true; For (INT I = 0; I <e1 [Vex]. size (); I ++) {If (! Visited [e1 [Vex] [I]) {DFS (e1 [Vex] [I]);} num [CNT ++] = Vex ;} void DFS (INT Vex, const Int & ANC) {visited [Vex] = true; STC [Vex] = ANC; For (INT I = 0; I <e2 [Vex]. size (); I ++) {If (! Visited [e2 [Vex] [I]) {DFS (E2 [Vex] [I], ANC) ;}} void Init () {CNT = 0; int I, j, n, m, a, B; scanf ("% d", & N, & M); for (I = 0; I <n; I ++) {e1 [I]. clear (); E2 [I]. clear (); visited [I] = 0 ;}for (I = 0; I <m; I ++) {scanf ("% d", &, & B); e1 [A]. push_back (B); E2 [B]. push_back (a) ;}for (I = 0; I <n; I ++) {If (! Visited [I]) {DFS (I) ;}} memset (visited, 0, sizeof (visited); for (I = n-1; I> = 0; I --) {If (! Visited [num [I]) DFS (Num [I], num [I]) ;}} void _ test () {Init (); puts ("strongly connected components of each vertex:"); For (INT I = 0; I <CNT; I ++) {printf ("% d ", STC [I]);} puts ("") ;}int main () {__ test ();} /* 9 80 11 21 42 70 33 60 84 70 10 32 02 12 33 03 2 */

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.