public void FindPath(T source, Func <SortedDictionary <WeightedUndirectedGraphNode <T, int>, int>, bool> resultHandler)
        {
            WeightedUndirectedGraphNode <T, int> sourceNode = Nodes[source];
            SortedDictionary <WeightedUndirectedGraphNode <T, int>, int> distances = new SortedDictionary <WeightedUndirectedGraphNode <T, int>, int>();

            distances[sourceNode] = m_ZeroWeightValue;
            foreach (var node in Nodes)
            {
                distances[node.Value] = m_InfinityWeightValue;
            }

            while (distances.Count > 0)
            {
                WeightedUndirectedGraphNode <T, int> currentVertex = Nodes[distances.Min().Key.Data];
                distances.Remove(currentVertex);

                foreach (var neighbor in currentVertex.GetNeighbors())
                {
                    int alternativeDist = distances[currentVertex] + neighbor.Value.ConnectionWeight;
                    if (alternativeDist < distances[neighbor.Value.ConnectionNeighbor])
                    {
                        distances[neighbor.Value.ConnectionNeighbor] = alternativeDist;
                    }
                }
            }
        }
예제 #2
0
 private void InternalDfs(Dictionary <T, WeightedUndirectedGraphNode <T, int> > nodes, WeightedUndirectedGraphNode <T, int> node, Func <WeightedUndirectedGraphNode <T, int>, bool> inspectFunc)
 {
     if (inspectFunc(node))
     {
         return;
     }
     foreach (var neighbor in node.GetNeighbors())
     {
         if (!neighbor.Value.ConnectionNeighbor.IsVisited)
         {
             InternalDfs(nodes, neighbor.Value.ConnectionNeighbor, inspectFunc);
         }
     }
 }
예제 #3
0
        /// <summary>
        /// Generic BFS function. Can be used to solve any problem that requires BFS, just plug in the inspectionFunc.
        /// </summary>
        /// <param name="inspectFunc">Must return true if the search is completed and no more nodes need to be checked</param>
        /// <param name="countComponents">Bool that when enabled counts the number of components in a graph.</param>
        public void Bfs(Func <WeightedUndirectedGraphNode <T, int>, bool> inspectFunc, bool countComponents)
        {
            if (Nodes.Count == 0)
            {
                return;
            }

            Queue <WeightedUndirectedGraphNode <T, int> > nodeQueue = new Queue <WeightedUndirectedGraphNode <T, int> >();

            foreach (var undirectedGraphNode in Nodes)
            {
                if (!undirectedGraphNode.Value.IsVisited)
                {
                    if (countComponents)
                    {
                        NumberOfComponents++;
                    }
                    nodeQueue.Enqueue(undirectedGraphNode.Value);
                    undirectedGraphNode.Value.IsVisited = true;
                    while (nodeQueue.Count != 0)
                    {
                        WeightedUndirectedGraphNode <T, int> currentNode = nodeQueue.Dequeue();

                        if (inspectFunc != null && inspectFunc(currentNode))
                        {
                            return;
                        }

                        foreach (var neighbor in currentNode.GetNeighbors())
                        {
                            if (!neighbor.Value.ConnectionNeighbor.IsVisited)
                            {
                                neighbor.Value.ConnectionNeighbor.IsVisited = true;
                                nodeQueue.Enqueue(neighbor.Value.ConnectionNeighbor);
                            }
                        }
                    }
                }
            }
        }