Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查图中所有节点的距离是否在<=相互之间的信任_C#_Algorithm_Graph - Fatal编程技术网

C# 检查图中所有节点的距离是否在<=相互之间的信任

C# 检查图中所有节点的距离是否在<=相互之间的信任,c#,algorithm,graph,C#,Algorithm,Graph,在给定的图中,我需要检查图中的所有节点的距离是否为0) { 访问的HashSet=新HashSet(); 队列1=新队列(); 队列2=新队列(); queue1.排队(节点); 添加(节点); int currentDistance=0; while(queue1.Count>0&¤tDistance

在给定的图中,我需要检查图中的所有节点的距离是否为0) { 访问的HashSet=新HashSet(); 队列1=新队列(); 队列2=新队列(); queue1.排队(节点); 添加(节点); int currentDistance=0; while(queue1.Count>0&¤tDistance您可以从图形中创建一个矩阵,然后在该矩阵中找到较低的值,当您尝试在节点之间找到较短的路径或对图形应用某些算法等时,它也很有用


首先,您的算法实际上是V*(V+E)

我不确定你在练习中是否能做得更好。你肯定可以改进你的代码


有计算所有对最短路径的算法,例如Floyd Warshall。对于您的情况,最快的一个(无向未加权图)称为Seidel算法。

查找较低的值是什么意思?抱歉,我没有注意到您试图查找k距离,而不是最低值。你可以在矩阵中找到你的距离,然后得到坐标,这将是你试图找到的一个点和方式。我认为你假设有一个距离矩阵,但事实并非如此。计算这样一个矩阵不是免费的,OP也不是在寻找一个与其他节点有k距离的节点。OP想要一个答案是/否,不管所有节点之间的距离是否不超过k。我说的是距离矩阵。是的,你是对的,计算不是免费的,但是在最坏的情况下,矩阵的计算会更快,渐近复杂度为O(n^2-n)和有向图O((n^2-n)/2),你也可以优化它(这取决于具体任务)。关于第二条评论,OP想知道什么,这只取决于您确定的任务(由您决定如何处理数组),我只是假设处理速度会更快。
// node defenition
public class GraphNode
{
   public GraphNode(int data, List<GraphNode> neighbours)
   {
        Data = data;
       Neighbours = neighbours;
   }
}

// Loop on every Node, and find all k-distance neighbours
public bool IfAllGraphNodesInKdistance1(List<GraphNode> nodes, int k)
{
    for(int i=1; i< nodes.Count; i++)
    {
         if(FindKdistanceNeighboursInGraph(nodes[i], k).Count != nodes.Count)
                return false;
        }
        return true;
    }
}


// Find k-distance neighbours of a Node
public HashSet<GraphNode> FindKdistanceNeighboursInGraph(GraphNode node, int distance )
{

    HashSet<GraphNode> resultHash = new HashSet<GraphNode>();

    if (node != null && distance > 0)
    {
        HashSet<GraphNode> visited = new HashSet<GraphNode>();
        Queue<GraphNode> queue1 = new Queue<GraphNode>();
        Queue<GraphNode> queue2 = new Queue<GraphNode>();
        queue1.Enqueue(node);
        visited.Add(node);
        int currentDistance = 0;
        while (queue1.Count > 0 && currentDistance < distance)
        {
            GraphNode current = queue1.Dequeue();
            foreach (GraphNode graphNode in current.Neighbours)
            {
                if (!visited.Contains(graphNode))
                {
                    queue2.Enqueue(graphNode);
                    visited.Add(graphNode);
                    resultHash.Add(graphNode);
                }
            }
            if (queue1.Count == 0)
            {
                queue1 = queue2;
                queue2 = new Queue<GraphNode>();
                currentDistance++;
            }
        }
    }
    resultHash.Add(node); // if it will include current
    return resultHash;
}