let's start Code

Showing posts with label Articulation Points and Bridges. Show all posts
Showing posts with label Articulation Points and Bridges. Show all posts

315 - Network

315 - Network

Topic : Graph Theory(AP)

Solution 01:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;

#define Max 100000
vector<int> graph[Max];
int parent[Max];
int low[Max];
int d[Max];
int visited[Max];
bool isArticulationPoint[Max];
int Time = 0;

void dfs(int u, int root)
{
    Time = Time + 1;
    visited[u] = Time;
    d[u] = low[u] = Time;
    int noOfChildren = 0;
    for(int i = 0; i <graph[u].size(); i++){
        int v = graph[u][i];
        if(v == parent[u])continue;
        parent[v] = u;
        if(visited[v]) low[u] = min(low[u], d[v]);
        else{
            noOfChildren = noOfChildren + 1;
            dfs(v, root);
            low[u] = min(low[u], low[v]);
            if(low[v] >= d[u] and u != root)isArticulationPoint[u] = true;
        }
    }
    if(u == root and noOfChildren > 1)isArticulationPoint[u] = true;
}

int main()
{
    int n,x,y;
    char c;
    while(cin >> n && n)
    {
        Time = 0;
        for(int t = 0; t <= n; t++){
            graph[t].clear();
            parent[t]= low[t] = d[t] = visited[t] = 0;
            isArticulationPoint[t] = false;
        }
        while(scanf("%d", &x) == 1 && x)
        {
            while(scanf("%d%c", &y, &c) == 2)
            {
                graph[x].push_back(y);
                    graph[y].push_back(x);
                if(c == '\n') break;
            }
        }
        dfs(1, 1);
        cout << count(isArticulationPoint+1, isArticulationPoint+n+1, true)<<endl;
    }
    return 0;
}

Share:

Articulation Points and Bridges

Resource:

বাইকানেক্টেড কম্পোনেন্ট , ব্রিজ, আরটিকুলেশন পয়েন্ট [ থিওরি ]

গ্রাফ থিওরিতে হাতেখড়ি ১৩: আর্টিকুলেশন পয়েন্ট এবং ব্রিজ

Visualization

CF blog 

Finding bridges in a graph in O(N+M)

Finding articulation points in a graph in O(N+M)

Articulation Points:01


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;

#define Max 100000
vector<int> graph[Max];
int parent[Max];
int low[Max];
int d[Max];
int visited[Max];
bool isArticulationPoint[Max];
int Time = 0;

void dfs(int u, int root)
{
    Time = Time + 1;
    visited[u] = Time;
    d[u] = low[u] = Time;
    int noOfChildren = 0;
    for(int i = 0; i <graph[u].size(); i++){
        int v = graph[u][i];
        if(v == parent[u])continue;
        parent[v] = u;
        if(visited[v]) low[u] = min(low[u], d[v]);
        else{
            noOfChildren = noOfChildren + 1;
            dfs(v, root);
            low[u] = min(low[u], low[v]);
            if(low[v] >= d[u] and u != root)isArticulationPoint[u] = true;
        }
    }
    if(u == root and noOfChildren > 1)isArticulationPoint[u] = true;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  //  freopen("in.txt", "r", stdin);
    int n,e;
    cin >> n >> e;
    for(int i = 0; i < e; i++){
        int u,v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    dfs(1,1);
    for(int i = 1; i <= n; i++){
        if(isArticulationPoint[i])cout << i<<endl;
    }
    return 0;
}

Bridge 01:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;

#define Max 100000
vector<int> graph[Max];
vector<pair<int, int> > Bridge;
int parent[Max];
int low[Max];
int d[Max];
int visited[Max];
bool isArticulationPoint[Max];
int Time = 0;

void dfs(int u, int root)
{
    int v;
    Time = Time + 1;
    visited[u] = Time;
    d[u] = low[u] = Time;
    int noOfChildren = 0;
    for(int i = 0; i <graph[u].size(); i++){
         v = graph[u][i];
        if(v == parent[u])continue;
        parent[v] = u;
        if(visited[v]) low[u] = min(low[u], d[v]);
        else{
            noOfChildren = noOfChildren + 1;
            dfs(v, root);
            low[u] = min(low[u], low[v]);
            if(low[v] > d[u])
            {
                isArticulationPoint[u] = true;
             //    cout << u << "-------"<<v<<endl;
                 Bridge.push_back({u,v});
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt", "r", stdin);
    int n,e;
    cin >> n >> e;
    for(int i = 0; i < e; i++){
        int u,v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    dfs(1,1);
    for(int i = 0; i < Bridge.size(); i++){
        cout << Bridge[i].first << " "<<Bridge[i].second<<endl;
    }
    return 0;
}

Share:

About

let's start CODE

Popular Posts

Labels

Algorithm (31) Articulation Points and Bridges (2) Atcoder (3) BFS (6) Binary Search (2) Code Repository (71) Codechef (4) Codeforces (5) Contest Problem (5) Datastructure (27) Devskill (1) DFS (5) Dijkstra (2) Divide and Conquer (5) DP (22) DSU (4) Game Theory (1) GeeksForGeeks (8) Geometry (6) Graph Theory (20) Greedy (8) HackerEarth (4) Hackerrank (7) Home (1) ICPC (2) Kruskal (1) LeetCode (17) LightOJ (19) Math (5) MST (1) Number Theory (21) Online Judge (84) PDBS (2) Probability (1) Resource (7) Searching (1) Segment Tree (1) Sorting (6) Spoj (8) STL (9) String (11) Trie Tree (5) two pointer (1) Upsolve (3) USACO (3) UVA (15)

Categories