简体   繁体   English

按每个数组中包含的元素对数组数组进行排序

[英]Sort an array of arrays by elements contained in each array

I need to traverse all connected components in a graph. 我需要在图表中遍历所有连接的组件。 The paths of the graph are stored in arrays like this: 图的路径存储在这样的数组中:

var paths = [
  [5,6],
  [1,2],
  [4,7,5,6],
  [6]
]

Here i see that paths[2]=[4,7,5,6] depends from paths[0]=[5,6] which in turn depends from paths[3]=[6] . 在这里,我看到paths[2]=[4,7,5,6]取决于paths[0]=[5,6] ,而paths[0]=[5,6]又取决于paths[3]=[6]

Now, i would need to recursively traverse all paths to check which one is contained in another and process the ones first that could help to solve the other, ie i need to check which arrays are contained in some of the other arrays: 现在,我需要递归遍历所有路径以检查哪一个包含在另一个路径中并首先处理那些有助于解决另一个路径的路径,即我需要检查哪些数组包含在其他一些数组中:

example: 例:

process [6]
process [5,6]
process [4,7,5,6]
process [1,2]

Due to the large amount of elements, i would prefer not to use recursion. 由于元素量很大,我宁愿不使用递归。 Is there a way to sort this list of arrays by the elements in one array contained in every other array, so that i can process them by iteration? 有没有办法按照每个其他数组中包含的一个数组中的元素对这个数组列表进行排序,以便我可以通过迭代处理它们?

EDIT: I think this could be solved by assigning a weight to each path composed as follows: sum of the nodes contained in each path multiplied by how many times this node is contained in other paths, then sort the paths by length ascending and weight descending - but this is just only my guess... 编辑:我认为这可以通过为每个路径分配一个权重来解决,如下所示:每个路径中包含的节点总和乘以该节点包含在其他路径中的次数,然后按长度上升和权重下降对路径进行排序 - 但这只是我猜...

I guess... if i have understood correctly, you are after dependency sorting. 我猜...如果我理解正确,你就是依赖排序。 Then i believe a possible way of achieving this job is as follows. 然后我相信实现这项工作的可能方式如下。 This is one way i could have come up with however there might be simpler solutions as well. 这是我可以提出的一种方式,但也可能有更简单的解决方案。

We have to form groups of consecutive items those depend on each other (such as [6] , [5,6] and [4,7,5,6] ) and then we will sort them according to their dependencies. 我们必须形成彼此依赖的连续项组(例如[6][5,6][4,7,5,6] ),然后我们将根据它们的依赖关系对它们进行排序。 I guess sorting among separate groups is not necessary since their items are not correlated (ie [1,2] can come before or after the sorted group with [6] , [5,6] and [4,7,5,6] ). 我想不需要在不同的组之间进行排序,因为它们的项目不相关(即[1,2]可以在[6][5,6][4,7,5,6]的排序组之前或之后出现)。

 var paths = [ [5,6], [1,2], [4,7,5,6], [6] ], lut = paths.reduce((table,path,i) => (path.forEach(n => table[n] = table[n] ? table[n].concat([[i,path.length]]) .sort((a,b) => a[1]-b[1]) : [[i,path.length]]), table),{}), sorted = Object.keys(lut).sort((a,b) => lut[b].length - lut[a].length) .reduce((si,k) => (lut[k].forEach(e => !si.includes(e[0]) && si.push(e[0])),si) ,[]) .map(idx => paths[idx]); console.log(sorted); 

Ok we first from a look up table like object ( lut ) and in this particular case it forms up like 好吧,我们首先从像对象( lut )这样的查找表中,在这种特殊情况下,它形成了像

{ '1': [ [ 1, 2 ] ],
  '2': [ [ 1, 2 ] ],
  '4': [ [ 2, 4 ] ],
  '5': [ [ 0, 2 ], [ 2, 4 ] ],
  '6': [ [ 3, 1 ], [ 0, 2 ], [ 2, 4 ] ],
  '7': [ [ 2, 4 ] ] 
}

So we now know that path 6 has the most dependent. 所以我们现在知道路径6具有最大的依赖性。 '6': [ [ 3, 1 ], [ 0, 2 ], [ 2, 4 ] ], means at paths[3] it is alone; '6': [ [ 3, 1 ], [ 0, 2 ], [ 2, 4 ] ],意思是paths[3]它是唯一的; at paths[0] it has a single dependent and at paths[2] it has 3 dependents. paths[0]它有一个依赖,在paths[2]它有3个依赖。 So we have sorted according o the number of dependents ( .sort((a,b) => a[1]-b[1]) ). 所以我们根据家属的数量排序( .sort((a,b) => a[1]-b[1]) )。 Once we have our table it's only getting them arranged to give us the desired index mapping. 一旦我们拥有了我们的表,它只是让它们安排给我们所需的索引映射。 The .reduce((si,k) => (lut[k].forEach(e => !si.includes(e[0]) && si.push(e[0])),si) ,[]) line is a little funny. .reduce((si,k) => (lut[k].forEach(e => !si.includes(e[0]) && si.push(e[0])),si) ,[])线有点好笑。 What it does is for each path pushing it's indice into an array "if" it is not already in the array. 它的作用是将每个路径推送到一个数组“if”,它不在数组中。 So we start with the most dependent one (6) and we push 3, 0 and 2. So when the turn of 5 comes we will not push the same indices again. 所以我们从最依赖的一个(6)开始,然后我们推动3,0和2.所以当转向5时,我们不会再次推动相同的指数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM