// Approach 1 - Using Min Heap public Dictionary <Vertex, int> ShortestPathUsingMinHeap(Graph graph, Vertex srcVertex) { // Using MinHeap instead of regular queue. BinaryMinHeap <Vertex> minHeap = new BinaryMinHeap <Vertex>(); // Stores shortest distance from root to every vertex Dictionary <Vertex, int> distanceDict = new Dictionary <Vertex, int>(); // Fill heap by initializing all vertex with infinite distance from source vertex foreach (KeyValuePair <long, Vertex> heapItem in graph.Verticies) { minHeap.Add(heapItem.Value, int.MaxValue); } // Set distance of source vertex to 0 minHeap.DecreaseWeight(srcVertex, 0); // Put it in Dictionary/Map distanceDict[srcVertex] = 0; // Iterate till heap is not empty while (!minHeap.IsEmpty()) { // Get the min value from heap node which has vertex and distance of that vertex from source vertex. BinaryMinHeap <Vertex> .Node heapNode = minHeap.DeQueueMinNode(); Vertex currentVertex = heapNode.key; // Update shortest distance of current vertex from source vertex distanceDict[currentVertex] = heapNode.Weight; // Iterate through all edges of current vertex foreach (Edge edge in currentVertex.Edges) { // Get the adjacent vertex Vertex adjacent = edge.Vertex1.Equals(currentVertex) ? edge.Vertex2 : edge.Vertex1; // If heap does not contain adjacent vertex means adjacent vertex already has shortest distance from source vertex if (!minHeap.Contains(adjacent)) { continue; } // Add distance of current vertex to edge weight to get distance of adjacent vertex from source vertex // When it goes through current vertex int newDistance = distanceDict[currentVertex] + edge.Weight; // See if this above calculated distance is less than current distance stored for adjacent vertex from source vertex if (minHeap.GetWeightByKey(adjacent) > newDistance) { minHeap.DecreaseWeight(adjacent, newDistance); } } } return(distanceDict); }
public List <Edge> PrimsMST(Graph graph) { BinaryMinHeap <Vertex> minHeap = new BinaryMinHeap <Vertex>(); // Dictionary of verticies to edge which gave minimum weight to this vertex. Dictionary <Vertex, Edge> vertexToEdgeDictionary = new Dictionary <Vertex, Edge>(); List <Edge> result = new List <Edge>(); // Insert all vertices with infinite value initially. foreach (KeyValuePair <long, Vertex> vertexPair in graph.Verticies) { minHeap.Add(vertexPair.Value, int.MaxValue); } // Start from any random vertex Vertex startVertex = graph.Verticies[0]; // For the start vertex decrease the value in heap, Dictionary to 0 minHeap.DecreaseWeight(startVertex, 0); // Iterate till heap, Dictionary has elements in it while (!minHeap.IsEmpty()) { // Extract min value vertex from heap + Dictionary Vertex current = minHeap.ExtractMin(); // Get the corresponding edge for this vertex if present and add it to final result. // This edge wont be present for first vertex. Edge spanningTreeEdge = vertexToEdgeDictionary[current]; if (spanningTreeEdge != null) { result.Add(spanningTreeEdge); } foreach (Edge edge in current.Edges) { Vertex adjacent = edge.Vertex1.Equals(current) ? edge.Vertex2 : edge.Vertex1; // Check if adjacent vertex exist in heap + map and weight attached with this vertex is greater than this edge weight if (minHeap.Contains(adjacent) && minHeap.GetWeightByKey(adjacent) > edge.Weight) { // Decrease the value of adjacent vertex to this edge weight. minHeap.DecreaseWeight(adjacent, edge.Weight); // Add vertex->edge mapping in the graph. vertexToEdgeDictionary[adjacent] = edge; } } } return(result); }