Write a program to check the cycle in the graph.
Example 1:
Input: n=6 , edges={{1,2},{2,3},{3,1},{4,3},{5,3},{6,2}}
Output: Cycle is present
Example 2:
Input: n=6 , edges={{1,2},{2,3},{4,3},{5,3},{6,2}}
Output: Cycle is not present
Approach:
Java
import java.util.ArrayList;import java.util.HashMap;import java.util.LinkedList;import java.util.Queue;public class CycleInGraph {// visited listpublic static ArrayList<Integer> visited = new ArrayList<>();// traversal listpublic static ArrayList<Integer> ans = new ArrayList<>();// store the indegree of all the nodesstatic HashMap<Integer, Integer> indegre = new HashMap<>();public static void main(String[] args) {// Adjacency ListsArrayList<ArrayList<Integer>> adj = new ArrayList<>();int v = 7;for (int i = 0; i < v; i++)adj.add(new ArrayList<>());addEdge(adj, 1, 2);addEdge(adj, 2, 3);addEdge(adj, 3, 1);addEdge(adj, 4, 3);addEdge(adj, 5, 3);addEdge(adj, 6, 2);checkCycle(adj, v);for (Integer key : indegre.keySet()) {if (indegre.get(key) > 0) {System.out.println("Cycle present for " + key);}}}private static void checkCycle(ArrayList<ArrayList<Integer>> adj,int v) {// create queueQueue<Integer> queue = new LinkedList<>();// if indegree of the vertex is// zero then insert into the queuefor (int i = 1; i < v; i++) {if (indegre.get(i) == null) {queue.add(i);}}// Iterate till the queue is not emptywhile (!queue.isEmpty()) {// current node from the queueint curr = queue.poll();// insert the current data into the resultans.add(curr);// iterate for all the adjacency// of the cuurent nodefor (int node : adj.get(curr)) {// decrease the indegre of the nodeindegre.put(node, indegre.getOrDefault(node, 0) - 1);// if indegree becomes zero// add into the queueif (indegre.get(node) == null || indegre.get(node) == 0) {queue.add(node);}}}}/*** @Desc : Method used for add edges* @param u* @param v*/private static void addEdge(ArrayList<ArrayList<Integer>> g,int u, int v) {// directed graphg.get(u).add(v);// check and add indegreeindegre.put(v, indegre.getOrDefault(v, 0) + 1);}}
C++
#include <bits/stdc++.h>using namespace std;//adjacency list of the//graphvector<int> arr[100001];//count the indegree of the nodeint indegree[100001];//function to add the edges//of the graphvoid addEdge(int u,int v){//directed grapharr[u].push_back(v);//increment the indegreeindegree[v]++;}//function to check the//cycle in graphbool isCycle(int n){//queue to store the nodesqueue<int> q;//store the resultvector<int> res;for(int i=1;i<=n;i++){//if indegree of node is zero//push into to the queueif(indegree[i]==0)q.push(i);}while(!q.empty()){//get current node from//queueint curr=q.front();//popped out the current nodeq.pop();//push the current node into the resultres.push_back(curr);for(int child:arr[curr]){//decrement the indegree//of the nodeindegree[child]--;//if indegree becomes zero//push into the queueif(indegree[child]==0)q.push(child);}}//result contains same no//of elements as n then theie is no//cycleif(res.size()==n)return false;//else their is a cycleelse{return true;}}int main(){int n=6;int edges=6;addEdge(1,2);addEdge(2,3);addEdge(3,1);addEdge(6,2);addEdge(4,3);addEdge(5,3);if(isCycle(n))cout<<"Cycle is present\n";elsecout<<"Cycle is not present\n";return 0;}
No comments:
Post a Comment