Problem description
Merge multiple sets into a set without intersection.
Specify a string set in the format of {aaa bbb ccc}, {bbb ddd}, {eee fff}, {ggg }, {DDD hhh} requires that the set whose intersection is not empty be merged, and that there is no intersection between the set after the merge is completed. For example, the preceding example should output {aaa bbb ccc ddd hhh }, {eee fff}, {ggg }.
(1) Describe your solution to this problem;
(2) provide the main processing procedures, algorithms, and complexity of algorithms.
(3) describe possible improvements.
Solution
The hash method is used. The key is a string, and the value is a linked list, storing the set number.
Analysis
1. Assume that each set is numbered 0, 1, 2, 3...
2. Create a hash_map. The key is a string, the value is a linked list, and the linked list node is the number of the set where the string is located. Traverse all sets and insert strings and corresponding set numbers into hash_map.
3. Create an int array with the length equal to the number of sets, indicating the merging relationship between sets. For example, if the element value of subscript 5 is 3, the set of subscript 5 is merged into the set of subscript 3. At the beginning, all values are initialized to-1, indicating that the collection is not merged. During the collection merging process, if the corresponding element of the Set in the array is-1, it is changed to the minimum set number in the linked list. If it is not-1, it is not modified, continue to read the next string.
Traverse the hash_map generated in step 2. For the Chain List in each value, first find the minimum set number (some sets have been merged, and you need to find the merged set number Along the merged relationship array ), then, all the numbered sets in the linked list are merged into the set with the smallest number (by changing the joined array ).
4. Now, the set with equal values in the merge relationship array is the final set. Its elements come from all the sets that direct or indirectly point to it.
Process
0: {aaa bbb ccc}
1: {bbb ddd}
2: {eee fff}
3: {ggg}
4: {DDD hhh}
The generated hash_map and the merged relationship array after processing each value are
AAA: 0 [0,-1,-1,-1,-1]
Bbb: 0 [0,-1,-1,-1,-1]
CCC: 0 [0,-1,-1,-1,-1]
Bbb: 0, 1 [0, 0,-1,-1,-1]
DDD: 1 [0, 0,-1,-1,-1]
EEE: 2 [0, 0, 2,-1,-1]
Fff: 2 [0, 0, 2,-1,-1]
Ggg: 3 [0, 0, 2, 3,-1]
DDD: 1, 4 [0, 0, 2, 3, 0]
Hhh: 4 [0, 0, 2, 3, 0]
Therefore, after merging, there are three sets, where the first, fourth, and fourth sets are merged,
The second and second sets are not merged.
I am the dividing line of tiantiao
Reference: http://www.cnblogs.com/ttltry-air/archive/2012/08/14/2638437.html
Http://blog.csdn.net/yahohi/article/details/7927233