Thursday, 27 December 2012

Loop in Graph(Union-Find algorithm)


This code create a graph and then ideitifies is there is any loop in the graph.

#include<iostream.h>

class Edge{
public:
int s;
int d;
};

class Graph{
public:
int v;
int e;
Edge* edge;
};



Graph* createGraph(int v,int e)
{
Graph *g= new Graph;
g->v = v;
g->e=e;
g->edge = new Edge[e];
return g;
}

int findParent(int *p,int i)
{
if(p[i]==-1)
return i;
return findParent(p,p[i]);
}

void unionSet(int *p,int s,int d)
{
int x = findParent(p,s);
int y = findParent(p,d);

p[x]=y;
}

int isCycle(Graph *g)
{
int *p=new int[g->v];
memset(p,-1,sizeof(int)*g->v);

for(int i=0;i<g->e;i++)
{
int x = findParent(p,g->edge[i].s);
int y = findParent(p,g->edge[i].d);

if(x==y)
return 1;
unionSet(p,x,y);
}
return 0;
}

// Driver program to test above functions
int main()
{
    /* Let us create following graph
         0
        |  
        |    
        1-----2 */

//three vertices (0,1,2) and two edges (0-1,1-2)    
Graph* graph = createGraph(3, 2);

    // add edge 0-1
    graph->edge[0].s = 0;
    graph->edge[0].d = 1;

    // add edge 1-2
    graph->edge[1].s = 1;
    graph->edge[1].d = 2;

    // add edge 0-2
    //graph->edge[2].s = 0;
    //graph->edge[2].d = 2;

    if (isCycle(graph))
        printf( "Graph contains cycle" );
    else
        printf( "Graph doesn't contain cycle" );

    return 0;
}

Self assignment check in assignment operator

I found this code some site/blog.Found it very useful.

In C++, assignment operator should be overloaded with self assignment check.
For example, consider the following class Array and overloaded assignment operator function without self assignment check.
// A sample class
class Array {
 private:
   int *ptr;
   int size;
 public:
   Array& operator = (const Array &rhs);
   // constructors and other functions of class........
};
// Overloaded assignment operator for class Array (without self
// assignment check)
Array& Array::operator = (const Array &rhs)
{
  // Deallocate old memory
  delete [] ptr;
  // allocate new space
  ptr = new int [rhs.size];
  // copy values
  size = rhs.size;
  for(int i = 0; i < size; i++)
    ptr[i] = rhs.ptr[i];
  return *this;
}
If we have an object say a1 of type Array and if we have a line like a1 = a1 somewhere, the program results in unpredictable behavior because there is no self assignment check in the above code. To avoid the above issue, self assignment check must be there while overloading assignment operator. For example, following code does self assignment check.
// Overloaded assignment operator for class Array (with self
// assignment check)
Array& Array::operator = (const Array &rhs)
{
  /* SELF ASSIGNMENT CHECK */
  if(this != &rhs)
  {
    // Deallocate old memory
    delete [] ptr;
    // allocate new space
    ptr = new int [rhs.size];
    // copy values
    size = rhs.size;
    for(int i = 0; i < size; i++)
      ptr[i] = rhs.ptr[i];   
  
  return *this;
}

AWS Data Pipeline Services

https://www.youtube.com/watch?v=tykcCf-Zz1M