[Graph Theory] directed graph strongly connected components (kosaraju algorithm, Tarjan algorithm), kosarajutarjan

Source: Internet
Author: User

[Graph Theory] directed graph strongly connected components (kosaraju algorithm, Tarjan algorithm), kosarajutarjan

Record your own ideas: In a directed graph, if any two vertices in some vertices can reach each other (indirectly or directly), These vertices constitute a strongly connected component, if a vertex has no degree of output, that is, it cannot reach any other vertex, then the vertex itself is a strongly connected component. When the kosaraju algorithm and the Tarjan algorithm are used to calculate the strongly connected component, it is to group and dye all vertices of the same color in the same strongly connected component, record the colors of each vertex (the number of strongly connected components) and the color of each vertex (the strongly connected component of each vertex ). All vertices in the same strongly connected component can be reduced to one vertex, and then a DAG (directed acyclic graph) is constructed based on the contraction point to record the inbound and outbound degrees of each vertex.



The templates of the following two algorithms are based on the typical questions of POJ 1236.









I. better information about kosaraju algorithms:

Http://blog.sina.com.cn/s/blog_4dff87120100r58c.html
Http://blog.csdn.net/michealtx/article/details/8233814
Http://blog.csdn.net/acceptedxukai/article/details/6961171
POJ http://www.cnblogs.com/Fatedayt/archive/2011/09/16/2178890.html 2186
Http://www.xuebuyuan.com/562460.html
Http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591370.html
Http://blog.csdn.net/dm_vincent/article/details/8554244


Template:

Maxn records the maximum number of vertices. The number of vertices ranges from 1 to 1 ~ N. The number of strongly connected components in the color record. The color value calculated in the Code is 1 greater than the actual number of strongly connected components. Therefore, the actual number of strongly connected components is color-1, belong [I] indicates that the vertex I belongs to the strongly connected components, and ans [I] indicates the number of points contained in the strongly connected components.

# Include <iostream> # include <algorithm> # include <stdio. h> # include <string. h ># include <vector> using namespace std; const int maxn = 102; vector <int> g [maxn], gre [maxn]; // store forward and inverse graphs int ord [maxn]; // forward search, vertex number bool vis [maxn]; int out [maxn]; // convert to the degree int in [maxn] for each contraction point after the DAG; // convert to the degree int belong [maxn] for each contraction point after the DAG; // The set of the current vertex, which is equivalent to dyeing and the color of the current vertex, int ans [maxn]. // how many vertices are included in each color, that is, each strongly connected component contains a certain number of int color; // represents a different color, how many strongly connected components int no; // Int n in the forward search order; // Number of vertices void dfs1 (int u) // DFS {vis [u] = 1 starting from the current u vertex; for (int I = 0; I <g [u]. size (); I ++) {int v = g [u] [I]; if (! Vis [v]) dfs1 (v);} ord [no ++] = u; // ID of each vertex} void dfs2 (int u) {vis [u] = 1; belong [u] = color; // The current vertex u is color for (int I = 0; I <gre [u]. size (); I ++) {int v = gre [u] [I]; if (! Vis [v]) {dfs2 (v) ;}} void kosaraju () {color = 1, no = 1; memset (in, 0, sizeof (vis )); memset (out, 0, sizeof (out); memset (vis, 0, sizeof (vis); for (int I = 1; I <= n; I ++) if (! Vis [I]) dfs1 (I); memset (vis, 0, sizeof (vis); for (int I = no-1; I> = 1; I --) // after the number is compiled, search for {int v = ord [I]; if (! Vis [v]) {dfs2 (v); color ++ ;}// construct a DAG for (int I = 1; I <= n; I ++) {for (int j = 0; j <g [I]. size (); j ++) {if (belong [I] = belong [g [I] [j]) continue; out [belong [I] ++; in [belong [g [I] [j] ++ ;}} int inzero = 0, outzero = 0; for (int I = 1; I <color; I ++) {if (! In [I]) inzero ++; if (! Out [I]) outzero ++;} if (color = 2) printf ("1 \ n0 \ n "); else printf ("% d \ n", inzero, max (inzero, outzero);} int main () {scanf ("% d ", & n); int to; for (int I = 1; I <= n; I ++) {while (scanf ("% d", &) {g [I]. push_back (to); gre [to]. push_back (I) ;}} kosaraju (); return 0 ;}

2. Good Tarjan Materials

Https://www.byvoid.com/blog/scc-tarjan/


Template:

Vertex number 1 ~ N. The number of strongly connected components is represented by scc. belong [I] indicates that the vertex I belongs to the strongly connected components, num [I] indicates the number of vertices contained in the I-th strongly connected component. The subscript Of The num array is 1 ~ Scc

# Include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <algorithm> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <time. h >#include <iomanip> # include <cctype> # define ll long longusing namespace std;/* Tarjan algorithm complexity O (n + m) */const int maxn = 110; /// point const int maxm = 100010; // Number of edges struct Edge {int to, next;} edge [maxm]; // each Edge is considered as a knot Struct int head [maxn], tot; int low [maxn], dfn [maxn], Stack [maxn], belong [maxn]; /// dfn [I] indicates the access time of vertex I during search, low [I] indicates the minimum access time of the I vertex or its subtree node // Belong [I] indicates which strongly connected component the I vertex belongs, the value of the array is 1 ~ Scc: A total of scc are strongly connected components: int index, top; // index is used to add the vertex access time when searching, top stack top int scc; /// Number of strongly connected components bool instack [maxn]; // determines whether vertex I is in the stack int num [maxn]; /// the number of points in each strongly connected component, array number 1 ~ Sccint in [maxn]; // The inbound int out [maxn] of each shrinkage point after the DAG is converted; /// convert to the outbound void addedge (int u, int v) of each contraction point After DAG. // Add an edge pointing from u to v, the implementation of the adjacent table adopts the header Insertion Method {edge [tot]. to = v; edge [tot]. next = head [u]; head [u] = tot ++;} void Tarjan (int u) {int v; low [u] = dfn [u] = ++ index; // The first access time equals to the minimum time Stack [top ++] = u; /// instack [u] = true; for (int I = head [u]; I! =-1; I = edge [I]. next) // vertex v adjacent to u, pointing from u to v {v = edge [I]. to; if (! Dfn [v]) {Tarjan (v); if (low [u]> low [v]) low [u] = low [v];} else if (instack [v] & low [u]> dfn [v]) low [u] = dfn [v];} if (low [u] = dfn [u]) {scc ++; do {v = Stack [-- top]; instack [v] = false; belong [v] = scc; num [scc] ++;} while (v! = U) ;}} void init () {tot = 0; memset (head,-1, sizeof (head);} void solve (int n) {memset (dfn, 0, sizeof (dfn); memset (instack, false, sizeof (instack); memset (num, 0, sizeof (num); index = scc = top = 0; for (int I = 1; I <= n; I ++) if (! Dfn [I]) Tarjan (I) ;}int n, m; int main () {scanf ("% d", & n); init (); int; for (int I = 1; I <= n; I ++) {while (scanf ("% d", & to) {addedge (I, to) ;}} solve (n); memset (in, 0, sizeof (in); memset (in, 0, sizeof (out )); /// construct DAG for (int I = 1; I <= n; I ++) {for (int j = head [I]; j! =-1; j = edge [j]. next) {int v = edge [j]. to; if (belong [I] = belong [v]) continue; out [belong [I] ++; in [belong [v] ++ ;}} int inzero = 0, outzero = 0; for (int I = 1; I <= scc; I ++) {if (! In [I]) inzero ++; if (! Out [I]) outzero ++;} if (scc = 1) printf ("1 \ n0 \ n "); else printf ("% d \ n", inzero, max (inzero, outzero); return 0 ;}





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.