Strongly Connected Components in graph
Example 1:
Input: n=6,edges={{1,2},{2,3},{3,1},{3,5},{5,4},{4,6},{6,5}}
Output: SCC={2,3,1},{4,6,5}
Approach:
Java
import java.util.ArrayList;public class StronglyConnectedComponentsGraph {// Adjacency Lists //original graphstatic ArrayList<ArrayList<Integer>> adj= new ArrayList<>();// Adjacency Lists //transpose graphstatic ArrayList<ArrayList<Integer>> transpose= new ArrayList<>();// visited listpublic static int visited[];public static ArrayList<Integer> order= new ArrayList<>();public static ArrayList<Integer> scc= new ArrayList<>();public static void main(String[] args) {int n = 6;for (int i = 0; i <= n; i++) {adj.add(new ArrayList<>());transpose.add(new ArrayList<>());}visited = new int[n + 1];addEdge(1, 2);addEdge(2, 3);addEdge(3, 1);addEdge(3, 5);addEdge(5, 4);addEdge(4, 6);addEdge(6, 5);dfs(1);visited = new int[n + 1];for (int i = 1; i <= n; i++) {if (visited[order.get(n - i)] == 0) {scc = new ArrayList<Integer>();dfsscc(order.get(n - i));System.out.println(scc);}}}// dfs to find the orderprivate static void dfs(Integer start) {// set element as visitedvisited[start] = 1;// add element in traversal listfor (int i : adj.get(start)) {// if not visited then call dfsif (visited[i] == 0) {dfs(i);}}order.add(start);}// to find the sscprivate static void dfsscc(Integer start) {// set element as visitedvisited[start] = 1;// add element in traversal listscc.add(start);for (int i : transpose.get(start)) {// if not visited then call dfsif (visited[i] == 0) {dfsscc(i);}}}private static void addEdge(int u, int v) {adj.get(u).add(v);transpose.get(v).add(u);}}
C++
#include <bits/stdc++.h>using namespace std;//adjacency list of the graph and//the transpose of the graphvector<int> arr[10001],tr[10001];//vector order and SCCvector<int> order,SCC;//visited array to hold//the which are visitedint vis[10001];//function of the dfsvoid dfs(int node){//marks current node as vistedvis[node]=1;//iterate for the adjacent of the//current nodefor(int child:arr[node]){//if child is not visited//call dfs for childif(vis[child]==0)dfs(child);}//store the orderorder.push_back(node);}//funuction of dfsvoid dfs1(int node){//mark the current node as//visitedvis[node]=1;//iterate for the adjacent//of the current nodefor(int child:tr[node]){//if child is not visited//call dfs for itif(vis[child]==0)dfs1(child);}//store the SCCSCC.push_back(node);}int main(){int n=6;//original grapharr[1].push_back(2);arr[2].push_back(3);arr[3].push_back(1);arr[3].push_back(5);arr[5].push_back(4);arr[4].push_back(6);arr[6].push_back(5);//transpose graphtr[2].push_back(1);tr[3].push_back(2);tr[1].push_back(3);tr[5].push_back(3);tr[4].push_back(5);tr[6].push_back(4);tr[5].push_back(6);//run dfs for all the nodes to//decide the orderfor(int i=1;i<=n;i++){if(vis[i]==0)dfs(i);}//mark all nodes as unvisited//for second dfsfor(int i=1;i<=n;i++)vis[i]=0;//now iterate for all//nodesfor(int i=1;i<=n;i++){//if vis of that order is unvisitedif(vis[order[n-i]]==0){//claer SCC for next roundSCC.clear();//call the dfsdfs1(order[n-i]);cout<<"SCC from "<<order[n-i]<<" is\n";for(int node:SCC)cout<<node<<" ";cout<<"\n";}}return 0;}
No comments:
Post a Comment