예제 #1
0
        public static IReadOnlyDictionary <GraphNode <T>, GraphNode <T> > RunDijkstraFrom(UndirectedGraph <T> graph, T src)
        {
            if (!graph.Nodes.TryGetValue(new GraphNode <T>(src), out var source))
            {
                return(null);
            }

            var predecessors = new Dictionary <GraphNode <T>, GraphNode <T> >();//keeps track of the shortest path predecessor
            //using Dijkstra's algorithm
            var priorityQ = new MinHeap <GraphNode <T> >(new GraphNode <T>(default(T)));

            foreach (var node in graph.Nodes)
            {
                node.Weight = source.Equals(node) ? 0 : int.MaxValue;
                priorityQ.Add(node);
                predecessors[source] = null;
            }
            while (priorityQ.Count != 0)
            {
                var minNode = priorityQ.GetMin();
                minNode.Color = Color.Colored;//indicating that these nodes are done
                if (!graph.Neighbors.ContainsKey(minNode))
                {
                    priorityQ.ExtractMin();
                    continue;
                }

                foreach (var neighbor in graph.Neighbors[minNode])
                {
                    graph.Nodes.TryGetValue(neighbor, out var neighborNode);

                    if (neighborNode.Color == Color.Colored)
                    {
                        continue;
                    }

                    var edge = graph.GetEdge(minNode, neighbor);
                    //if (!graph.Edges.TryGetValue(new Edge<T>(minNode, neighbor), out var edge))
                    //    throw new InvalidDataException($"failed to find edge to neighbor {minNode.Label}->{neighbor.Label}");

                    if (neighborNode.Weight > minNode.Weight + edge.Weight)
                    {
                        neighborNode.Weight        = minNode.Weight + edge.Weight;
                        predecessors[neighborNode] = minNode;
                    }
                }

                priorityQ.ExtractMin();
            }
            return(predecessors);
        }
예제 #2
0
        public void MinHeap_Test()
        {
            var minHeap = new MinHeap <int>();

            minHeap.Insert(5);
            minHeap.Insert(3);
            minHeap.Insert(1);
            minHeap.Insert(4);
            minHeap.Insert(2);

            var result = minHeap.ExtractMin();

            Assert.That(result, Is.EqualTo(1));

            var secondResult = minHeap.ExtractMin();

            Assert.That(secondResult, Is.EqualTo(2));
        }
예제 #3
0
        //for directed Graph
        public static IReadOnlyDictionary <GraphNode <T>, GraphNode <T> > RunDijkstraFrom(DirectedGraph <T> graph, T src)
        {
            if (!graph.Nodes.TryGetValue(new GraphNode <T>(src), out var source))
            {
                return(null);
            }

            var predecessors = new Dictionary <GraphNode <T>, GraphNode <T> >();//keeps track of the shortest path predecessor
            //using Dijkstra's algorithm
            var priorityQ = new MinHeap <GraphNode <T> >(new GraphNode <T>(default(T)));

            foreach (var node in graph.Nodes)
            {
                node.Weight = source.Equals(node) ? 0 : int.MaxValue;
                priorityQ.Add(node);
                predecessors[source] = null;
            }
            while (priorityQ.Count != 0)
            {
                var minNode = priorityQ.GetMin();
                if (!graph.Neighbors.ContainsKey(minNode))
                {
                    priorityQ.ExtractMin();
                    continue;
                }

                foreach (var neighbor in graph.Neighbors[minNode])
                {
                    graph.Nodes.TryGetValue(neighbor, out var neighborNode);

                    var edges = graph.GetEdges(minNode, neighbor);
                    foreach (var edge in edges)
                    {
                        if (neighborNode.Weight > minNode.Weight + edge.Weight)
                        {
                            neighborNode.Weight        = minNode.Weight + edge.Weight;
                            predecessors[neighborNode] = minNode;
                        }
                    }
                }
                priorityQ.ExtractMin();
            }
            return(predecessors);
        }
예제 #4
0
        /// <summary>
        /// Runtime O(n * log(n))
        /// </summary>
        /// <returns>The kth smallest.</returns>
        /// <param name="arr">Arr.</param>
        /// <param name="k">K.</param>
        public int FindKthSmallest(int[] arr, int k)
        {
            // push into a heap
            var minHeap = new MinHeap <int>();

            for (var i = 0; i < arr.Length; i++)
            {
                minHeap.Insert(arr[i]);
            }

            // what is insert run time for a heap - log(n)
            var min = int.MaxValue;

            for (int i = 0; i < k; i++)
            {
                min = minHeap.ExtractMin();
            }

            return(min);
        }