/// <summary>
        /// Search path to target using the heuristic.
        /// </summary>
        public ShortestPathResult <T, W> FindShortestPath(IGraph <T> graph, T source, T destination)
        {
            if (this.@operator == null)
            {
                throw new ArgumentException("Provide an operator implementation for generic type W during initialization.");
            }

            if (!graph.IsWeightedGraph)
            {
                if ([email protected]() != typeof(int))
                {
                    throw new ArgumentException("Edges of unweighted graphs are assigned an imaginary weight of one (1)." +
                                                "Provide an appropriate IShortestPathOperators<int> operator implementation during initialization.");
                }
            }

            //regular argument checks
            if (graph?.GetVertex(source) == null || graph.GetVertex(destination) == null)
            {
                throw new ArgumentException();
            }

            //track progress for distance to each Vertex from source
            var progress = new Dictionary <T, W>();

            //trace our current path by mapping current vertex to its Parent
            var parentMap = new Dictionary <T, T>();

            //min heap to pick next closest vertex
            var minHeap = new FibonacciHeap <AStarWrap <T, W> >();
            //keep references of heap Node for decrement key operation
            var heapMapping = new Dictionary <T, AStarWrap <T, W> >();

            //add vertices to min heap and progress map
            foreach (var vertex in graph.VerticesAsEnumberable)
            {
                //init parent
                parentMap.Add(vertex.Key, default(T));

                //init to max value
                progress.Add(vertex.Key, @operator.MaxValue);

                if (vertex.Key.Equals(source))
                {
                    continue;
                }
            }

            //start from source vertex as current
            var current = new AStarWrap <T, W>(heuristic, destination)
            {
                Distance = @operator.DefaultValue,
                Vertex   = source
            };

            //insert neighbour in heap
            minHeap.Insert(current);
            heapMapping[source] = current;

            //until heap is empty
            while (minHeap.Count > 0)
            {
                //next min vertex to visit
                current = minHeap.Extract();
                heapMapping.Remove(current.Vertex);

                //no path exists, so return max value
                if (current.Distance.Equals(@operator.MaxValue))
                {
                    return(new ShortestPathResult <T, W>(null, @operator.MaxValue));
                }

                //visit neighbours of current
                foreach (var neighbour in graph.GetVertex(current.Vertex).Edges.Where(x => !x.TargetVertexKey.Equals(source)))
                {
                    //new distance to neighbour
                    var newDistance = @operator.Sum(current.Distance,
                                                    graph.GetVertex(current.Vertex).GetEdge(neighbour.TargetVertex).Weight <W>());

                    //current distance to neighbour
                    var existingDistance = progress[neighbour.TargetVertexKey];

                    //update distance if new is better
                    if (newDistance.CompareTo(existingDistance) < 0)
                    {
                        progress[neighbour.TargetVertexKey] = newDistance;

                        if (heapMapping.ContainsKey(neighbour.TargetVertexKey))
                        {
                            //decrement distance to neighbour in heap
                            var decremented = new AStarWrap <T, W>(heuristic, destination)
                            {
                                Distance = newDistance,
                                Vertex   = neighbour.TargetVertexKey
                            };

                            minHeap.UpdateKey(heapMapping[neighbour.TargetVertexKey], decremented);
                            heapMapping[neighbour.TargetVertexKey] = decremented;
                        }
                        else
                        {
                            //insert neighbour in heap
                            var discovered = new AStarWrap <T, W>(heuristic, destination)
                            {
                                Distance = newDistance,
                                Vertex   = neighbour.TargetVertexKey
                            };

                            minHeap.Insert(discovered);
                            heapMapping[neighbour.TargetVertexKey] = discovered;
                        }

                        //trace parent
                        parentMap[neighbour.TargetVertexKey] = current.Vertex;
                    }
                }
            }

            return(tracePath(graph, parentMap, source, destination));
        }
		public void FibonacciHeapUpdateKeyMinHeap()
		{
			FibonacciHeap<HeapElement> heap = new FibonacciHeap<HeapElement>((a, b) => a.Priority.CompareTo(b.Priority), true);

			FillHeap(heap, 100);
			Assert.AreEqual(100, heap.Count);

			// add new element. Give it a priority of 50
			HeapElement element = new HeapElement(50, "Value: 50");
			heap.Insert(element);
			Assert.AreEqual(101, heap.Count);

			// update key to 10, using an action lambda
			heap.UpdateKey(element, (a, b) => a.Priority = b, 10);
			Assert.AreEqual(10, element.Priority);

			// check if the heap is still correct
			HeapElement previous = heap.ExtractRoot();
			HeapElement current = heap.ExtractRoot();
			while(current != null)
			{
				Assert.IsTrue(previous.Priority <= current.Priority);
				previous = current;
				current = heap.ExtractRoot();
			}
			// heap should be empty 
			Assert.AreEqual(0, heap.Count);
		}
        /// <summary>
        /// Get shortest distance to target.
        /// </summary>
        public ShortestPathResult <T, W> FindShortestPath(WeightedDiGraph <T, W> graph, T source, T destination)
        {
            //regular argument checks
            if (graph?.FindVertex(source) == null || graph.FindVertex(destination) == null)
            {
                throw new ArgumentException();
            }

            //track progress for distance to each Vertex from source
            var progress = new Dictionary <T, W>();

            //trace our current path by mapping current vertex to its Parent
            var parentMap = new Dictionary <T, T>();

            //min heap to pick next closest vertex
            var minHeap = new FibonacciHeap <MinHeapWrap <T, W> >();
            //keep references of heap Node for decrement key operation
            var heapMapping = new Dictionary <T, MinHeapWrap <T, W> >();

            //add vertices to min heap and progress map
            foreach (var vertex in graph.Vertices)
            {
                //init parent
                parentMap.Add(vertex.Key, default(T));

                //init to max value
                progress.Add(vertex.Key, operators.MaxValue);

                if (vertex.Key.Equals(source))
                {
                    continue;
                }
            }

            //start from source vertex as current
            var current = new MinHeapWrap <T, W>()
            {
                Distance = operators.DefaultValue,
                Vertex   = source
            };

            minHeap.Insert(current);
            heapMapping.Add(current.Vertex, current);

            //until heap is empty
            while (minHeap.Count > 0)
            {
                //next min vertex to visit
                current = minHeap.Extract();
                heapMapping.Remove(current.Vertex);

                //no path exists, so return max value
                if (current.Distance.Equals(operators.MaxValue))
                {
                    return(new ShortestPathResult <T, W>(null, operators.MaxValue));
                }

                //visit neighbours of current
                foreach (var neighbour in graph.Vertices[current.Vertex].OutEdges.Where(x => !x.Key.Value.Equals(source)))
                {
                    //new distance to neighbour
                    var newDistance = operators.Sum(current.Distance,
                                                    graph.Vertices[current.Vertex].OutEdges[neighbour.Key]);

                    //current distance to neighbour
                    var existingDistance = progress[neighbour.Key.Value];

                    //update distance if new is better
                    if (newDistance.CompareTo(existingDistance) < 0)
                    {
                        progress[neighbour.Key.Value] = newDistance;

                        if (!heapMapping.ContainsKey(neighbour.Key.Value))
                        {
                            var wrap = new MinHeapWrap <T, W>()
                            {
                                Distance = newDistance, Vertex = neighbour.Key.Value
                            };
                            minHeap.Insert(wrap);
                            heapMapping.Add(neighbour.Key.Value, wrap);
                        }
                        else
                        {
                            //decrement distance to neighbour in heap
                            var decremented = new MinHeapWrap <T, W>()
                            {
                                Distance = newDistance, Vertex = neighbour.Key.Value
                            };
                            minHeap.UpdateKey(heapMapping[neighbour.Key.Value], decremented);
                            heapMapping[neighbour.Key.Value] = decremented;
                        }

                        //trace parent
                        parentMap[neighbour.Key.Value] = current.Vertex;
                    }
                }
            }

            return(tracePath(graph, parentMap, source, destination));
        }