C#集合组相关元素

C#集合组相关元素,c#,collections,C#,Collections,我有一个点的集合,我正在尝试编写一个函数,将所有相关点分组 例如,在本例中,“相关”表示一个项目包含另一个: int[] points = {2, 3, 4, 5, 6, 7, 2, 13, 14, 15, 32, 10, 237, 22, 46, 97} getDirectRelatives(2) = {2, 2, 32, 237, 22} 这适用于返回所有直接相关的元素。但我想把间接相关的元素分组。因为3和7与2有间接关系,所以我也想要它们的所有直接关系: getAllRelatives(

我有一个点的集合,我正在尝试编写一个函数,将所有相关点分组

例如,在本例中,“相关”表示一个项目包含另一个:

int[] points = {2, 3, 4, 5, 6, 7, 2, 13, 14, 15, 32, 10, 237, 22, 46, 97}
getDirectRelatives(2) = {2, 2, 32, 237, 22}
这适用于返回所有直接相关的元素。但我想把间接相关的元素分组。因为3和7与2有间接关系,所以我也想要它们的所有直接关系:

getAllRelatives(2) = {2, 2, 32, 237, 22, 3, 13, 32, 7, 97}
有什么建议吗

更新:下面是我的实现,让它更清晰。这是可行的,但我想知道这是否是正确的方法

public void getAllRelatives()
{
int groupIndex = 1;
List<int> groupCollection = new List<int>();
bool flag = false;
int[] marked = null;
string currentOuter = null;
string currentInner = null;
List<int> current = new List<int>();
int[] points = {2, 4, 5, 6, 7, 2, 13, 14, 15, 32, 10, 237, 22, 46, 97};

//marked contains integers which identify which group an element belongs to
for (x = 0; x <= marked.Count - 1; x++) {
    marked(x) = 0;
}

//Two loops.  The first iterates over the target point, the second iterates over each sub point
//Once both loops are complete, groupCollection should contain the indexes for
//all related integers

//outerloop
for (i = 0; i <= points.Count - 1; i++) {
    current.Clear();
    currentOuter = points(i).ToString;
    current.Add(i); //used to hold matches for current loop

    //inner loop, targetpoint + 1 to end
    for (x = i + 1; x <= points.Count - 1; x++) {
        currentInner = points(x).ToString;

        if (currentInner.Contains(currentOuter)) {
            current.Add(x);
        }
    }

    //if this is the first iteration, flag as true, forces current items to marked
    if (marked(0) == 0) {
        flag = true;
    }

    //check if any current points are marked and flag if any of the elements are already in a group, add each found group to group collection
    for (x = 0; x <= current.Count - 1; x++) {
        if (!(marked(current(x)) == 0)) {
            flag = true;
            groupCollection.Add(marked(current(x)));
        }
    }

    if (flag == true) {
        groupCollection.Add(groupIndex); //all relatives end up here
    }

    for (x = 0; x <= current.Count - 1; x++) {
        marked(current(x)) = groupIndex;
    }
    groupIndex += 1;
    flag = false;


}
public void getAllRelatives()
{
int-groupIndex=1;
List groupCollection=新列表();
布尔标志=假;
int[]标记为空;
字符串currentOuter=null;
字符串currentInner=null;
列表当前=新列表();
int[]点={2,4,5,6,7,2,13,14,15,32,10,237,22,46,97};
//标记包含标识元素所属组的整数

对于(x=0;x将
int[]点
转换为
字典点标记

e、 g

然后使用
pointTokens
查找关系(无论多么任意)


这可能是解决图论问题的最佳方法。你需要找出如何创建一个适当的图表来表示你的数据,然后遍历它以获得答案。

首先找到所有包含2的数字。找到其中包含的所有其他数字,然后研究发现其他数字间接相关。这太明显了吗?你打算这样做吗继续将此应用于每个新集合?刚刚意识到您没有使用字符串,您需要使用更具逻辑性(非数学性)的像我建议的那样搜索方式。我不明白你所说的间接关联是什么意思。为什么22不包括在你的第二个数组中?为什么13、43和97包括在你的第二个数组中?@steaks:OP意味着32使3成为一个有效的搜索词,现在用3查找数字。但不确定为什么有些数字会下降。看起来应该是这里第三个2是。可能是输入错误。@C.Lang,但不是9,即使97包含9。我认为OP需要修正他的示例并明确他的规范。是的,使用图表解析大型数据集。对于小型数据集,过度使用
pointTokens.Add(237, new string[]{"2", "3", "7"})
foreach(int point in pointTokens.Keys)
{
  string[] tokens = pointTokens[point]

  if(someInt.Equals(point))
  {
    // do something
  }

  if(tokens.Contains(someInt.ToString())
  {
     // do something
  }

  // etc ...
}