scribble

Adrian Statescu

About Projects Posts

17 Apr 2017
Detect cycle in a directed graph

Given a directed graph your task is to complete the method isCycle to detect if there is a cycle in the graph or not. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.

Input (only to be used for Expected Output): The first line of the input contains an integer ‘T’ denoting the number of test cases. Then ‘T’ test cases follow. Each test case consists of two lines. Description of test cases is as follows: The First line of each test case contains two integers ‘N’ and ‘M’ which denotes the no of vertices and no of edges respectively. The Second line of each test case contains ‘M’ space separated pairs u and v denoting that there is an unidirected edge from u to v .

Output: The method should return true if there is a cycle else it should return false.

Constraints:
1 <= T <= 100
1 <= N, M <= 100
0 <=u , v <= N-1

Example:

Input: 2
2 2
0 1 0 0
4 3
0 1 1 2 2 3

Output:
1
0

In above first test case there is a graph with 2 vertices and 2 edges the first edge is from 0 to 1 and other edge is from 0 to 0 . In the second test case there is a graph with 3 vertices and 3 edges from 0 to 1 , 1 to 2 and 2 to 3

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
#define N 100
#define WHITE 0
#define GREY 1
#define BLACK 2

using namespace std;

class Graph {

      int V;
      list<int> *adj;
        
      public:
      Graph(int V);
      void addEdge(int v, int w);
      bool isCyclic();         
};

Graph::Graph(int V) {

      this->V = V;

      adj = new list<int>[V]; 
};

void Graph::addEdge(int v, int w){

       adj[v].push_back(w);
};

bool hasCycle(std::list<int> adj[], int colours[], int u) {

     colours[u] = GREY;

     for( const auto&v:adj[u] ) {

          switch( colours[v] ) {
            
                  case GREY: 

                       return true;         
                  case WHITE: 

                       if(hasCycle(adj, colours, v))   
                       return true;         
          }
     }

     colours[ u ] = BLACK;

     return false;     
}

bool Graph::isCyclic() {

    static int colours[N];

    for (int i = 0; i < V; i++) {

        colours[i] = WHITE;
    }

    for (int i = 0; i < V; i++) {

        if (colours[i] == 0) {

            if (hasCycle(adj, colours, i)) {

                return true;
            }
        }
    }

    return false;    
}


int main() {

     int nodes, edges, T;

     cin>>T;

     while(T--) {
 
     cin>>nodes>>edges;

     Graph *g = new Graph(nodes); 

     for(int i = 0; i < edges; ++i) {

             int u,v;

             cin>>u>>v;

             g->addEdge(u,v);
     } 

     cout<<g->isCyclic()<<endl; 

     }

return(0);

About Projects Posts