CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005
    Posts
    9

    Equivalence relation and the resulting subsets

    I wanna write a program which should give me the resulting subsets according to an Equivalence relation what I should do?
    thanks!

  2. #2
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Equivalence relation and the resulting subsets

    you should use Disjoint Set
    you should merge the pair elements which have common elemts for making the resulting subsets!

  3. #3
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Equivalence relation and the resulting subsets

    BTW an implemeted DisjiontSet includes two main functions
    1. Merge O(1)
    2. Find O(n)
    Code:
    public class DisjointSet
      {
       int[] inner=new int[12];
       public void Add(int a,int b)
       {
    	if(inner[a]==0 && inner[b]==0)
    	{
    	 if(a<b){inner[a]=a;inner[b]=a;}
    	 else
    	 {inner[a]=b;inner[b]=b;}
    	}
    	else
    	{
    	 if(inner[a]!=0 && inner[b]!=0)
    	 {
    	  int t1=findx(inner[a]);
    	  int t2=findx(inner[b]);
    	  merge(t1,t2);
    	 }
    	 else
    	 {
    	  if(inner[a]!=0 )
    	  {
    	   int t=findx(inner[a]);
    	   merge(t,b);
    	  }
    	  else
    	  {
    	   int t=findx(inner[b]);
    	   merge(t,a);
    	  }
    	 }
    	}
       }
       public string output()
       {
    	int[] innerTemp=new int[12];
    	ArrayList ar=new ArrayList();
    	
    	for(int j=inner.Length-1;j>0;j--)
    	{
    	 if(inner[j]!=0 && innerTemp[j]!=-1)
    	 {
    	  ArrayList innerar=new ArrayList();
    	  int root=this.findx(j);
    	  for(int i=inner.Length-1;i>0;i--)
    	  {
    	   if(inner[i]!=0 && innerTemp[i]!=-1 
    		&& root==findx(i))
    	   {
    		int d=i;
    		while(d!=root)
    		{
    		 
    		 int t=d;
    		 d=inner[d];
    		 innerTemp[t]=-1;
    		 innerar.Add(t);
    		}
    	   }
    	  }
    	  innerar.Add(root);
    	  innerTemp[root]=-1;
    	  ar.Add(innerar);
    	 }
    	}
    	string result="";
    	foreach(ArrayList ar1 in ar)
    	{
    	 for(int i=ar1.Count-1;i>=0;i--)
    	 {
    	  result+=ar1[i].ToString()+",";
    	 }
    	 result+=Environment.NewLine;
    	}
    	return result;
       }
       private int findx(int a)
       {
    	while(inner[a]!=a){a=inner[a];}
    	return a;
       }
       private void merge(int a,int b)
       {
    	if(a<b)
    	 inner[b]=a;
    	else
    	 inner[a]=b;
       }
      }
      private string testDisjointset()
      {
       DisjointSet dsj=new DisjointSet();
       dsj.Add(1,5);
       dsj.Add(1,6);
       dsj.Add(6,2);
       dsj.Add(7,3);
       dsj.Add(4,10);
       return dsj.output();
      }
    Last edited by mehdi62b; December 9th, 2005 at 08:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured