c#

时间:2018-05-30 15:55:50

标签: c# performance combinations accord.net

我有三套积分。我需要每组最多一个点的所有组合。

enter image description here

从上图中结果应该是:

  • p1,p3,p6
  • p1,p3,p7
  • p1,p4,p6
  • p1,p4,p7
  • p1,p5,p6
  • p1,p5,p7
  • p2,p3,p6
  • p2,p3,p7
  • p2,p4,p6
  • p2,p4,p7
  • p2,p5,p6
  • p2,p5,p7
  • p1,p3
  • p1,p4
  • P1,P5
  • ...
  • ...
  • p4,p6
  • ...
  • P1
  • P2
  • ...

我正在使用

中的方法Combinations
  

Accord.Math

在所有集合的所有点上,之后我删除包含来自同一集合的更多点的组合。 我不想计算组合,而是计算从Set1到Set3的所有可能路径(也为Set2传递),因为组合花费了太多时间来处理大量的点。 如何更改我的代码?

  var result1 = new Dictionary<int, List<List<ClusterInPath>>>();      
  foreach (KeyValuePair<int, List<ClusterInPath>> currentClustersInImage in allClustersInVertebra)
  {
    var allPaths = currentClustersInImage.Value.ToArray().Combinations().Select(el => el.ToList()).ToList();
    var filterPaths = new List<List<ClusterInPath>>();
    foreach (List<ClusterInPath> path in allPaths)
    {
      if(PathContainsMoreClustersOnSameImage(path)) continue;
       filterPaths.Add(path);
    }       
    result1.Add(currentClustersInVertebra.Key, filterPaths);
  }

ClusterInPath类定义如下:

  public class ClusterInPath
  {
    public ClusterInPath(int imageIndex, int clusterIndex)
    {
      ImageIndex = imageIndex;
      ClusterIndex = clusterIndex;
    }

    public int ImageIndex { get; set; }
    public int ClusterIndex { get; set; }
   }

提前致谢

1 个答案:

答案 0 :(得分:0)

在每个集合中添加虚构(零,空等)元素并获得集合的笛卡尔积。在输出期间忽略或删除这些元素。

Example of computing Cartesian product with LINQ