Home > Back-end >  To divide the river warrior factions - equivalence class partition problems -- -- -- -- -- (and chec
To divide the river warrior factions - equivalence class partition problems -- -- -- -- -- (and chec

Time:09-29

, river lake top warrior divided into factions - equivalence class partition problems
-- -- -- -- -- (and check set application)
1. Problem description
If a relationship has a reflexive, symmetry and transitivity, then according to the relationship of equivalence relation, reflexivity is any things can always and you have such a relationship; For example: the same blood type, the relationship between themselves and their relationship is always the same blood type; Symmetry is that if A and B have such things, the relationship between B things and also A thing to have this relationship; Such as: the same blood type, obviously is to satisfy the conditions; Transitivity is said that if there is the relationship between A and B things C B A thing and also have this kind of relationship, then A and C have the relations, such as: the same blood type, relationship between A and B type is the same, the same blood type B and C, is the same blood group A and C, so the same blood type relationship are equivalent, if the same blood type as an equivalence class, divided into groups of different blood groups became the equivalence class partition problem,
Legend jianghu is littered with various warrior, idle all day, carrying a sword walking up and down, run into people who are not the same and branches, a fight, but they are very handout spirit, not to play their friends, and they believe in "friends of friends are my friends," as long as it is to through a friend together, no matter how many turn, are considered to be one of their own, on the river's lake was formed a united, a through the relationship between the two friends together, so in the same religious people can rest assured to die, we can in each of the factions elects a more respectable person, as head of the factions, this person is their eldest brother,
For example: there are ten warrior, number 1 to 10, article 11 friends: (6, 1), (5, 4), (4, 9), (7, 6), (10, 5), (3, 2), (9, 10), (8, 3), (7, 2), (2, 1), (7, 8), (6, 1) said the no. 6 of them with the number 1 person will be, the rest are similar, can draw,2,3,6,7,8 (1) of the six men belong to the same factions,,5,9,10 (4) the four personal belonging to the same branches, the river's lake has two branches,
Our task is to find the river branches of warrior of the division, apparently a united may be regarded as an equivalent class, thus divided into factions problem becomes equivalence class division,
2. Design requirements
Design input: input equivalence relation to (i.e., friends),
Output design: find the river there are many branches, and branches output all warrior,
3. The design thought
Can take advantage of this problem and check set implementation, check and set is A kind of abstract data types, it is A dynamic collection of several disjoint set S={A, B,... }, supports the following operations:
? MakeSet (x) : construct a collection, it contains only one element x;
? Union (x, y) : the x in the collection and y is a collection of merged into a collection;
? Find (x) : Find out about element x and collection,

(1) data structure designIn this problem, can be each branches (equivalence class) described as a tree, the tree every non-root node points to the parent node (boss) directly, with the root element (head) as a united logo (equivalence class), the river's lake were classified as a forest, jianghu now has two branches, for example, divided into hierarchical relationships as shown in figure 1, the forest available storage, said parents representation

Figure 1 runescape stoic.it hierarchy diagram
To the final will be effected according to the output of gangs warrior, a chain can be set for each gang, each strand is a fore and aft pointer, thus making high efficiency when merging two list,
(2) algorithm design
Assumptions on a total of n warrior, initial, everyone is an independent factions (equivalence class), available MakeSet (x),
One day, LanBing walk on the road, met Yang ning again, two people a word doesn't want to start, but before start they need to check your own boss, step by step first found that the boss is not a person, so today's doomed not to peace, finished after the decision to recognize a friend, each to join a friend relationship (x, y), check the x and y belong to which factions (equivalence class), can use the Find (x) and Find (y), namely the x and y, respectively, found the root node of the tree (branches),
Do if every time the query optimization, make whole branches of tree layer will remain at a relatively low level, the retrieval efficiency will be higher, for example, the blue c and lang directly do head direct reports, as shown in figure 2,
If x and y belong to the same (equivalence class), namely, their boss (roots) is the same, (x, y) without having to deal with the relationship; Otherwise, the x's branches (equivalence class) and the y's branches (equivalence class) merged into one united (equivalence class), available Union (x, y), the x x in the tree and the tree into a tree,

Figure 2 query processing sketch
One day, LanBing walk on the road, met Yang ning again, two people a word doesn't want to start, but before start they need to check your own boss, step by step first found that the boss is not a person, so today's doomed not to peace, play later decided to recognize a friend, which leads to two branches merged, so who be boss? A scheme is the original two eldest brother who when united boss can be combined, and a solution is to press jianghu rules, big gang eldest brother become a new gang eldest brother, the merged as shown in figure 3 and figure 4, the second solution is helpful to improve the retrieval speed,

Figure 3 uncompressed path small branches merged into sects schematic

Figure 4 compressed path small branches merged into sects schematic
When merging two gangs, but will be two tables end to end, and the corresponding chain gangs

CodePudding user response:

Matrix operations,,,,,,,,,,
  • Related