Algorithm 如何将集合中的结构化数据项合并到组中?

Algorithm 如何将集合中的结构化数据项合并到组中?,algorithm,grouping,data-manipulation,data-processing,Algorithm,Grouping,Data Manipulation,Data Processing,条件如下: 1. Loop through all items in the given set - generate strings for each item in an array # After this loop, the set [ { p1: "A", p2: 1 }, { p1: "A", p2: 2 }, { p1: "B", p2: 2 } ] # will have generate something like: items = ['A1','A2','B2'

条件如下:

1. Loop through all items in the given set
      - generate strings for each item in an array
# After this loop, the set [ { p1: "A", p2: 1 }, { p1: "A", p2: 2 }, { p1: "B", p2: 2 } ]
# will have generate something like: items = ['A1','A2','B2']

2. Sort the array. #this is important to keep the order when reducing dimensions

3. Enqueue(put) all elements in 'Queue'

4. Dequeue(get) set of elements from 'Queue' such that last-1 index of string is same. # effectively we will get all A's then B's

5. If the count of elements in this set matches max elements for this case. We have identified a reduction. So, take 1 element in the set remove last index. Enqueue(put) it in the 'Queue' again

6. Else, add all elements to the 'Group' list.

7. Repeat steps 4-6 till the queue is empty.

8. 'Group' list will have the results as per requirement.
由于我有一组项,这些项是同一类的实例,该类包含的属性中每个项都有有限的可能值

例如,该类有2个属性(p1,p2),每个属性都有2个值(A,B/1,2),只有4种实例:{p1:“A”,p2:1},{p1:“A”,p2:2},{p1:“B”,p2:1},{p1:“B”,p2:2}

要按字符串显示项目,项目{p1:“A”,p2:1}将变为“项目A1”,而{p1:“B”,p2:2}将变为“项目B2”

因此,具有唯一项的集合有16种可能性,从空集([])到通用集([{p1:“A”,p2:1},{p1:“A”,p2:2},{p1:“B”,p2:1},{p1:“B”,p2:2}])

在显示集合时,如果集合包含的项在一个属性中具有所有可能的值,而在其他属性中具有相同的值,则该属性将被隐藏

例如,集合[{p1:“A”,p2:1},{p1:“A”,p2:2}]包含p2中所有可能的值,并且p1中的值相同。它将显示为“itemA”,p2被隐藏

反之亦然,[{p1:“A”,p2:2},{p1:“B”,p2:2}]显示“项目2”

通用集[{p1:“A”,p2:1},{p1:“A”,p2:2},{p1:“B”,p2:1},{p1:“B”,p2:2}]只是“项”

对于不可分组的项,set[{p1:“A”,p2:1},{p1:“A”,p2:2},{p1:“B”,p2:2}]显示可配置的“itemA&B2”或“item2&A1”

问题来了:

1. Loop through all items in the given set
      - generate strings for each item in an array
# After this loop, the set [ { p1: "A", p2: 1 }, { p1: "A", p2: 2 }, { p1: "B", p2: 2 } ]
# will have generate something like: items = ['A1','A2','B2']

2. Sort the array. #this is important to keep the order when reducing dimensions

3. Enqueue(put) all elements in 'Queue'

4. Dequeue(get) set of elements from 'Queue' such that last-1 index of string is same. # effectively we will get all A's then B's

5. If the count of elements in this set matches max elements for this case. We have identified a reduction. So, take 1 element in the set remove last index. Enqueue(put) it in the 'Queue' again

6. Else, add all elements to the 'Group' list.

7. Repeat steps 4-6 till the queue is empty.

8. 'Group' list will have the results as per requirement.
什么是一个好的算法来实现这些具有更多属性和值的规则,这些属性和值可能看起来像{p1:1 | 2,p2:1 | 2 | 3,p4:1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}

目前,我只考虑将每个值与其他固定属性循环,生成子集,并显示每个子集。但它似乎循环了很多次,而且会有很多子集。不过,我必须作出优先次序,以决定使用哪些可能的子集

这对我来说是一个复杂的问题,尽管它似乎是一个基本问题


如果有人对这个问题感兴趣,那就好了。感谢您的阅读。

按照以下步骤进行操作如何

groups = getAllCombinations()
# getAllCombinations() implementation could recursively generate all combinations. 
# For example, for class has 2 properties(p1,p2), 
# we will have: groups = [ A1, A2, B1, B2 ]

# Loop the below for all class properties [p1, p2]
# Loop through the list of property values[A, B]
for p in properties: 
    combinations = getPropertyCombinations(p) 
    # This should return all combinations by keeping the given property constant.
    # For 'A' it will return 'A1', 'A2'; For 'B' it will return 'B1', 'B2'
    if all combinations in groups:
        #remove all combinations and insert the property
        for item in combinations:
            strItem = str(item)  
            # class item { p1: "A", p2: 1 } will have 'A1' returning str(item)
            groups.remove(strItem)
        groups.add(p)

#Your resulting groups will have resulting group by as per requirement.

希望有帮助

遵循以下步骤如何

groups = getAllCombinations()
# getAllCombinations() implementation could recursively generate all combinations. 
# For example, for class has 2 properties(p1,p2), 
# we will have: groups = [ A1, A2, B1, B2 ]

# Loop the below for all class properties [p1, p2]
# Loop through the list of property values[A, B]
for p in properties: 
    combinations = getPropertyCombinations(p) 
    # This should return all combinations by keeping the given property constant.
    # For 'A' it will return 'A1', 'A2'; For 'B' it will return 'B1', 'B2'
    if all combinations in groups:
        #remove all combinations and insert the property
        for item in combinations:
            strItem = str(item)  
            # class item { p1: "A", p2: 1 } will have 'A1' returning str(item)
            groups.remove(strItem)
        groups.add(p)

#Your resulting groups will have resulting group by as per requirement.

希望有帮助

另一种方法是使用队列数据结构来帮助您减少每个维度上的元素

算法:

1. Loop through all items in the given set
      - generate strings for each item in an array
# After this loop, the set [ { p1: "A", p2: 1 }, { p1: "A", p2: 2 }, { p1: "B", p2: 2 } ]
# will have generate something like: items = ['A1','A2','B2']

2. Sort the array. #this is important to keep the order when reducing dimensions

3. Enqueue(put) all elements in 'Queue'

4. Dequeue(get) set of elements from 'Queue' such that last-1 index of string is same. # effectively we will get all A's then B's

5. If the count of elements in this set matches max elements for this case. We have identified a reduction. So, take 1 element in the set remove last index. Enqueue(put) it in the 'Queue' again

6. Else, add all elements to the 'Group' list.

7. Repeat steps 4-6 till the queue is empty.

8. 'Group' list will have the results as per requirement.
该算法将在O(n*m)中运行。其中n是集合中元素的数量,m是属性的数量


希望有帮助

另一种方法是使用队列数据结构来帮助您减少每个维度上的元素

算法:

1. Loop through all items in the given set
      - generate strings for each item in an array
# After this loop, the set [ { p1: "A", p2: 1 }, { p1: "A", p2: 2 }, { p1: "B", p2: 2 } ]
# will have generate something like: items = ['A1','A2','B2']

2. Sort the array. #this is important to keep the order when reducing dimensions

3. Enqueue(put) all elements in 'Queue'

4. Dequeue(get) set of elements from 'Queue' such that last-1 index of string is same. # effectively we will get all A's then B's

5. If the count of elements in this set matches max elements for this case. We have identified a reduction. So, take 1 element in the set remove last index. Enqueue(put) it in the 'Queue' again

6. Else, add all elements to the 'Group' list.

7. Repeat steps 4-6 till the queue is empty.

8. 'Group' list will have the results as per requirement.
该算法将在O(n*m)中运行。其中n是集合中元素的数量,m是属性的数量

希望有帮助

感谢您的解决方案建议

我对两个属性(每个属性有两个值)的解决方案是:

/*伪javascript ish代码*/
//准备数据
属性=[p1,p2]
值={p1:[A,B],p2:[1,2]}
//团体
属性组合=[],[p2],[p1],[p1,p2]]
//组合
值\u组合=[
[A1]、[A2]、[B1]、[B2]、//[]
[A1,B1],[A2,B2],//[p2]
[A1,A2],[B1,B2],//[p1]
[A1、A2、B1、B2]//[p1、p2]
]
//组和组合可以由函数生成
函数getSetItemString(数据){
子组=[]
嵌套:
对于(i=value\u.length-1;i>=0;i--){
对于(j=0;jsubgorup=[[A1,A2]];集合=[B1]
//[B1]不匹配[B1,B2]
//子群[p2]
//[B1]不匹配[A1,B1]
//[B1]不匹配[A2,B2]
//子群[]
//[B1]不匹配[A1]
//[B1]不匹配[A2]
//[B1]匹配[B1]=>子组=[[A1,A2],[B1]];set=[]
//[]打破循环
//输出
//[[A1,A2],[B1]=>项目A和B1
其复杂度与组合矩阵的生成和给定集合的匹配项有关

但对我来说,计算复杂度的精确数字有点复杂,它似乎很大

子组顺序与值组合中的顺序相关

为了生成具有更多属性和值的组合矩阵,我最终提出了以下函数:

函数createItemCompositions(结构){//输入示例:{p1:[“A”,“B”],p2:[1,2,3],p3:[“+”,“-”]}
让props=Object.keys(结构),
prop_combs=nm(props.map(p=>1)).map(ps=>ps.reduce((r,v,p)=>r.concat(v?props[p]:[]),[]),
value\u combs=prop\u combs.map(sub=>{
让mutable=Object.entries(structure).reduce((r[p,vs])=>{
if(sub.indexOf(p)=-1){
返回Object.assign(r,{[p]:vs})
}否则{
返回r
}
}, {}),
immutable=Object.entries(structure).reduce((r[p,vs])=>{
if(sub.indexOf(p)!=-1){
返回Object.assign(r,{[p]:vs})
}否则{
返回r
}
}, {}),
mu_keys=Object.keys(可变),
im_keys=Object.keys(不可变),
mu_-combs=nm(mu_-keys.map(k=>mutable[k].length-1)).map(m=>m.reduce((r,v,i)=>Object.assign(r,{[mu_-keys[i]]:mutable[mu-keys[i]]v]}),{}),
im\u combs=nm(im\u keys.map(k=>imm