/// <summary> /// Dijkstra algorithm /// </summary> /// <param name="sourceIndex">the source index of vertex</param> /// <param name="heap">the heap tha support priority queue</param> /// <returns>return the array that contains the minimal length from the source to all vertex</returns> public double[] Dijkstra(int sourceIndex, Heap <VertexNode> heap) { double[] result = new double[count]; for (int i = 0; i < count; i++) { result[i] = maxDistance; } result[sourceIndex] = 0; GraphPriorityQueue queue = new GraphPriorityQueue(count, new BinaryHeap <VertexNode>(count)); VertexNode[] vertexNodeArray = new VertexNode[count]; for (int i = 0; i < count; i++) { vertexNodeArray[i] = new VertexNode(i, maxDistance); } queue.BuildHeap(vertexNodeArray); queue.ModifyVertexNode(sourceIndex, 0); while (queue.Count != 0) { VertexNode minNode = queue.ExtractMin(); if (minNode.Length == maxDistance) { break; } for (AdjEdgeNode edgeNode = nodes[minNode.Index].FirstEdge; edgeNode != null; edgeNode = edgeNode.Next) { if (edgeNode.Length < maxDistance) { double newLength = minNode.Length + edgeNode.Length; if (newLength < result[edgeNode.Index]) { result[edgeNode.Index] = newLength; queue.ModifyVertexNode(edgeNode.Index, newLength); } } } } return(result); }
/// <summary> /// Dijkstra algorithm /// </summary> /// <param name="sourceIndex">the source index of vertex</param> /// <param name="heap">the heap tha support priority queue</param> /// <returns>return the array that contains the minimal length from the source to all vertex</returns> public double[] Dijkstra(int sourceIndex, Heap <VertexNode> heap) { double[] result = new double[count]; for (int i = 0; i < count; i++) { result[i] = maxDistance; } result[sourceIndex] = 0; GraphPriorityQueue queue = new GraphPriorityQueue(count, heap); VertexNode[] vertexNodeArray = new VertexNode[count]; for (int i = 0; i < count; i++) { vertexNodeArray[i] = new VertexNode(i, maxDistance); } queue.BuildHeap(vertexNodeArray); queue.ModifyVertexNode(sourceIndex, 0); while (queue.Count != 0) { VertexNode minNode = queue.ExtractMin(); if (minNode.Length == maxDistance) { break; } for (int i = 0; i < count; i++) { if (matrix[minNode.Index][i] < maxDistance) { double newLength = minNode.Length + matrix[minNode.Index][i]; if (newLength < result[i]) { result[i] = newLength; queue.ModifyVertexNode(i, newLength); } } } } return(result); }