Advertisement
i_love_rao_khushboo

Untitled

Aug 15th, 2022
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. // Cycle Detection in UG using O(n) DSU operations
  2.  
  3. // Here the graph is represented using EDGE LIST representation
  4.  
  5. #include<iostream>
  6. #include<list>
  7. using namespace std;
  8.  
  9. class Graph
  10. {
  11.     int v;
  12.     list<pair<int, int>> l;
  13.    
  14.     public:
  15.         Graph(int v){
  16.             this->v=v;
  17.         }
  18.        
  19.         void addEdge(int x, int y){
  20.             l.push_back({x, y});
  21.         }
  22.        
  23.         int find_set(int v, int *parent)
  24.         {
  25.             // base condition
  26.             if(parent[v]==-1)
  27.                 return v;
  28.                
  29.             return find_set(parent[v], parent);
  30.         }
  31.        
  32.         void union_sets(int a, int b, int parent [])
  33.         {
  34.             a=find_set(a, parent);
  35.             b=find_set(b, parent);
  36.            
  37.             if(a!=b) parent[b]=a;
  38.         }
  39.        
  40.         bool containsCycle()
  41.         {
  42.             int *parent = new int[v];
  43.             for(int i=0; i<v; i++){
  44.                 parent[i]=-1;
  45.             }
  46.            
  47.             for(auto p: l)
  48.             {
  49.                 int s1=find_set(p.first, parent);
  50.                 int s2=find_set(p.second, parent);
  51.                
  52.                 if(s1==s2) return true;
  53.                 else union_sets(p.first, p.second, parent);
  54.             }
  55.            
  56.             delete [] parent;
  57.             return false;
  58.         }
  59. };
  60.  
  61. int main()
  62. {
  63.     ios_base::sync_with_stdio(false);
  64.     cin.tie(nullptr); cout.tie(nullptr);
  65.  
  66.     Graph g(4);
  67.     g.addEdge(0, 1);
  68.     g.addEdge(1, 2);
  69.     g.addEdge(2, 3);
  70.     g.addEdge(3, 0);
  71.    
  72.     cout<<(g.containsCycle() ? "Cycle is present" : "Cycle isn't present");
  73.  
  74.     return 0;
  75. }
  76.  
  77. // Time complexity: O(|V| x |E|)
  78.  
  79. /***********************************************************************************************************************/
  80.  
  81. // Cycle Detection in UG using Optimised DSU operations
  82.  
  83. #include<iostream>
  84. #include<list>
  85. using namespace std;
  86.  
  87. class Graph
  88. {
  89.     int v;
  90.     list<pair<int, int>> l;
  91.    
  92.     public:
  93.         Graph(int v){
  94.             this->v=v;
  95.         }
  96.        
  97.         void addEdge(int x, int y){
  98.             l.push_back({x, y});
  99.         }
  100.        
  101.         int find_set(int v, int *parent)
  102.         {
  103.             // base condition
  104.             if(parent[v]==-1)
  105.                 return v;
  106.                
  107.             return parent[v] = find_set(parent[v], parent);
  108.         }
  109.        
  110.         void union_sets(int a, int b, int parent [], int *size)
  111.         {
  112.             a=find_set(a, parent);
  113.             b=find_set(b, parent);
  114.            
  115.             if(a!=b)
  116.             {
  117.                 if(size[a] < size[b])
  118.                     swap(a, b);
  119.                    
  120.                 parent[b] = a;
  121.                 size[a] += size[b];
  122.             }
  123.         }
  124.        
  125.         bool containsCycle()
  126.         {
  127.             int *parent = new int[v];
  128.             int *size = new int[v];
  129.            
  130.             for(int i=0; i<v; i++){
  131.                 parent[i]=-1;
  132.                 size[i]=1;
  133.             }
  134.            
  135.             for(auto p: l)
  136.             {
  137.                 int s1=find_set(p.first, parent);
  138.                 int s2=find_set(p.second, parent);
  139.                
  140.                 if(s1==s2) return true;
  141.                 else union_sets(p.first, p.second, parent, size);
  142.             }
  143.            
  144.             delete [] parent;
  145.             delete [] size;
  146.             return false;
  147.         }
  148. };
  149.  
  150. int main()
  151. {
  152.     ios_base::sync_with_stdio(false);
  153.     cin.tie(nullptr); cout.tie(nullptr);
  154.  
  155.     Graph g(4);
  156.     g.addEdge(0, 1);
  157.     g.addEdge(1, 2);
  158.     g.addEdge(2, 3);
  159.     g.addEdge(3, 0);
  160.    
  161.     cout<<(g.containsCycle() ? "Cycle is present" : "Cycle isn't present");
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement