Melvik12
November 18th, 2005, 04:13 PM
I wanna write a program which should give me the resulting subsets according to an Equivalence relation what I should do?
thanks!
thanks!
|
Click to See Complete Forum and Search --> : Equivalence relation and the resulting subsets Melvik12 November 18th, 2005, 04:13 PM I wanna write a program which should give me the resulting subsets according to an Equivalence relation what I should do? thanks! mehdi62b November 18th, 2005, 04:43 PM you should use Disjoint Set (http://en.wikipedia.org/wiki/Disjoint) you should merge the pair elements which have common elemts for making the resulting subsets! mehdi62b December 9th, 2005, 07:08 AM BTW an implemeted DisjiontSet includes two main functions Merge O(1) Find O(n) 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(); } codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |