Original post Address http://www.cnblogs.com/ttltry-air/archive/2012/08/14/2638437.html
1, problem description
Merges multiple collections into a collection without intersections.
Given a collection of strings, formatted as: {AAA BBB CCC}, {bbb ddd},{eee fff},{ggg},{ddd HHH} requires merging of collections where the intersection is not empty, requiring no overlap between the merged collection, such as the above example should output {AAA BBB CCC DDD Hhh},{eee FFF}, {GGG}.
(1) Please describe your idea of solving this problem;
(2) Please give the main processing flow, algorithm, and complexity of the algorithm
(3) Please describe the possible improvements.
2. Analysis
1. Assume that each collection is numbered 0,1,2,3 ...
2. Create a Hash_map,key string, value is a linked list, and the linked list node is the number of the collection where the string resides. Iterates through all the collections, inserting strings and corresponding collection numbers into the hash_map.
3. Create an int array with a length equal to the number of sets, representing the merge relationship between the collections. For example, an element with subscript 5 has a value of 3, which means that a collection of subscript 5 is merged into a set labeled 3. Initially, all values are initialized to-1, indicating that there is no merging between the collections. In the collection merge process, we merge all the strings into the smaller numbered collection.
Traversing the hash_map generated in the second step, for each linked list in value, first find the smallest collection number (some have already been merged and need to find the merged collection number along the merged relational array). Then all the numbered collections in the linked list are merged into the lowest numbered collection (by changing the merge relationship array).
4. The collection of values-1 in the merged relational array is now the final collection, and its elements originate from all the collections that directly or indirectly point to it.
0: {AAA BBB CCC}
1: {BBB DDD}
2: {eee FFF}
3: {GGG}
4: {DDD HHH}
The resulting hash_map, and the array of merge relationships after each value, are
aaa:0
bbb:0, 1
ccc:0
Ddd:1, 4
Eee:2
Fff:2
Ggg:3
Hhh:4
So after merging, there are three collections, and the first 0,1,4 collection is merged together,
2nd, 3 collections are not merged.
3. Concrete Realization
1:class Disjointsetproblem {2:private final int SIZE = 7; 3:private int[] Father; 4:private static list<set<string>> resultlist = new arraylist<set<string>> (); 5:6: public static void Main (string[] args) {7:string[] Str0 = {"AAA", "BBB", "CCC",}; 8:string[] str1 = {"BBB", "ddd",}; 9:string[] str2 = {"Eee", "FFF",}; 10:string[] Str3 = {"GGG",}; 11:string[] Str4 = {"ddd", "HHH",}; 12:string[] Str5 = {"xx", "yy",}; 13:string[] Str6 = {"zz", "yy",}; 14:string[][] STRs = {STR0, str1, str2, STR3, STR4, STR5, STR6}; ://change string[][] to list<set> 16:for (string[] str:strs) {://when I WR ite--"Arraylist list=arrays.aslist (Strarray)", "AddAll ()" is unsupported for such a Arraylist. 18:set<string> Set = new hashset<string> (); 19:set.addall (ArrayS.aslist (str)); 20:resultlist.add (set); :} 22:disjointsetproblem Disjointset = new Disjointsetproblem (); 23:disjointset.disjoin (STRs); 24:} 25:26:/* 27: * Get HASHMAP process: * */29:public void Disjoin (string[][] strings) {30 : if (strings = = NULL | | Strings.length < 2) 31:return; 32:initial (); 33://Get Hash_map:key As String, value is a list 34:map<string, list<integer>> map = Storeinhashmap (stri NGS); 35://And check to merge 36:union (map); PNS:} 38:39://in The Beginning,each element is in its own "group". 40:public void Initial () {41:father = new int[size]; 42:for (int i = 0; i < SIZE; i++) {43:father[i] = i; *:} 46:47:/* map<k,v>: * key:string: * value:list<integer>- In which sets the string shows up. 50: */51: Public map<string, List<integer>> Storeinhashmap (string[][] strings) {52:map<string, List< integer>> map = new hashmap<string, list<integer>> (); 53:for (int i = 0; i < SIZE; i++) {54:for (String each:strings[i]) {55:if (!map.containskey (each)) {56:list<integer> List = new arraylist<integer> (); 57:list.add (i); 58:map.put (each, list); :} else {60:map.get (each). Add (i); 61:} 62:} 63:} 64:65://Print out Map 66:system.out.println ("Set mapping The hashmap generated by the shot is: "); 67:printmap (map); 68:return map; 70:71:private void Printmap (map<string, list<integer>> Map) {$://TODO Auto-ge nerated method Stub 73:iterator<map.entry<string, list<integer>>>iter = Map.entryset ():. iterator (); 75:while (Iter.hasnext ()) {76:map.entry<string, list<integer>> Entry = Iter.next (); 77:string key = Entry.getkey (); 78:list<integer> value = Entry.getvalue (); 79:SYSTEM.OUT.PRINTLN (key + ":" + value); :} 81:system.out.println (); 82:} 83:84:/* 85: * HashMap and Collection merge operation: * */87:public void Union (map<string, LIST&L T;integer>> map) {88:iterator<map.entry<string, list<integer>>> it = Map.entrySet () 89 :. iterator (); 90:while (It.hasnext ()) {91:map.entry<string, list<integer>> Entry = It.next (); 92:list<integer> value = Entry.getvalue (); 93:unionhelp (value);//the arrays whose indexes is in the same list should is merged to one set. 94:} 95:Prints out the Father parent node information 96:system.out.println ("The parent node information after the HashMap collection is merged:"); 97:printfather (father);//system.out.println ("The Father Array is" + arrays.tostring (father)); 98:printsetlist (resultlist); //merge: Sets 100:for (int i = 0; i < SIZE; i++) {101:if (i! = Father[i]) {102 ://set: No repeating element 103:set<string> dest = Resultlist.get (Father[i]); 104:set<string> Source = Resultlist.get (i); 105:dest.addall (source); 106:} 107:} 108://clear a set which has been added. 109://When the B collection is added to the a set, empty the B collection 110:for (int i = 0; i < SIZE; i++) {111:if (i! = Father[i]) { 112:resultlist.get (i). Clear (); 113:} 115:system.out.println ("merged:" + resultlist); :} 117:118:public void Unionhelp (list<integer> List) {119:int MinfathER = getfather (list.get (0));//list[0] is the smaller. 120://The list parameter passed is already ordered 121:for (int i = 0, size = list.size (); i < size; i++) {122://FA Ther[list.get (i)] = Minfather; 123:unionhelp (list.get (0), List.get (i)); 124:} 125:} 126:127://path compression 128:public int getfather (int x) {129:while (x! = Father[x ] {130:x = father[x]; 131:} 132:return x; 133:} 134:135:private void Printfather (int[] fathernode) {136://TODO auto-generated method Stu b 137:for (int node:fathernode) 138:system.out.print (node + ""); 139:system.out.println (); :} 141:142:private void Printsetlist (list<set<string>> List) {143://TODO Auto-gener Ated method Stub 144:system.out.print ("Pre-merger:"); 145:for (int i = 0; i < SIZE; i++) {146:system.out.print (List.get (i) + ""); 147:} 148:system.out.println (); 149:} 150:151://general union in Disjoin set. But we overload it. 152:public void Unionhelp (int x, int y) {153:if (father[x]! = Father[y]) {154:int FX = Getfat Her (x); 155:int fy = getfather (y); 156://merge The arrays to the array, the has a smaller index. 157:if (FX < FY) {158:father[y] = FX; 159:} else {160:fat Her[x] = FY; 161:} 162:} 163:} 164:165:}
"Reprint" multiple collections merged into a set without intersections-implements