Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C - PLUS-PLUS-PRIM'S ALGORITHM - MINIMUM SPANNING TREE - CODE




Example :



#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

class Edge {

	public :
	
    string startVertex;
    string endVertex;

    int value;
};

class MinHeap {

	public :
	
    map<string, int> vertexVal;
    vector<string> verticesKeyArray;

    MinHeap(){
    }

    MinHeap(map<string, int> vertexVal){

        this->vertexVal = vertexVal;
    }
    
	vector<string> convertMapKeysToVector(map<string, int> const& inputMap) {
    
  		vector<string> verticesArray;
  
  		for (auto const& element : inputMap) {
    	
    		verticesArray.push_back(element.first);
  		}
  		return verticesArray;
	}

    void heapify(vector<string> &verticesArray,int root, int length) {

        int left = (2*root)+1;
        int right = (2*root)+2;
        int smallest = root;

        if (left < length && right <= length && vertexVal[verticesArray[right]] < vertexVal[verticesArray[left]]) {

            smallest = right;
        }
        else if (left <= length){

            smallest = left;
        }

        if (vertexVal[verticesArray[root]] > vertexVal[verticesArray[smallest]]) {

            string temp = verticesArray[root];
            verticesArray.at(root) = verticesArray[smallest];
            verticesArray.at(smallest) = temp;

            heapify(verticesArray, smallest, length);
        }
    }

    void buildHeap() {
        
        vector<string> verticesArray = convertMapKeysToVector(vertexVal);

        int len = verticesArray.size()-1;

        for (int parent = (len-1)/ 2; parent >= 0; parent--)
            heapify(verticesArray, parent, len);

        verticesKeyArray = verticesArray;

    }

    void updateHeap(string vertex, int length) {

        vertexVal[vertex] = length;

        vector<string> verticesArray = convertMapKeysToVector(vertexVal);

        int len = verticesArray.size()-1;

        for (int parent = (len-1)/ 2; parent >= 0; parent--)
            heapify(verticesArray, parent, len);

        verticesKeyArray = verticesArray;
    }

    bool containsVertex(string vertex){

		// Check if the key is present in the Map or not.
        if (vertexVal.find(vertex) != vertexVal.end())
            return true;
        else
            return false;
    }

    string deleteMin() {

        string temp = verticesKeyArray[0];

        int len = verticesKeyArray.size()-1;

        verticesKeyArray.at(0) = verticesKeyArray[len];

        vertexVal.erase(temp);

        verticesKeyArray.resize(len);

        if (len>0)
            heapify(verticesKeyArray, 0, len-1);

        return temp;
    }

    int getWeight(string vertex){

        return vertexVal[vertex];
    }

    bool empty() {

        if (verticesKeyArray.size()>0)
            return false;
        else
            return true;
    }
};

class PrimMST {

	public :
	
	vector<string> vertices;
    vector<vector<Edge>> adjcList;
    map<string,int> vertexVal;

    // Stores the Minimum spanning Tree
    vector<Edge> result;
    
    int indexOf(string val, vector<string> v) {
   
  		auto match = find(v.begin(), v.end(), val); 
 
  		if(match != v.end())  
    		return match - v.begin(); 
  		else  
    		return -1; 
	}

    void primMST(){

        // Vertex to Edge Map
        map<string, Edge> vertexToEdge;

        // Assign all the initial values as infinity for all the Vertices.
        for(string v : vertices) {
            vertexVal[v] = INT_MAX;
        }

        MinHeap minHeap(vertexVal);

        // Call buildHeap() to create the MinHeap
        minHeap.buildHeap();

        // Replace the value of start vertex to 0.
        minHeap.updateHeap("a",0);

        // Continue until the Min-Heap is not empty.
        while(!minHeap.empty()){
            // Extract minimum value vertex from Map in Heap
            string currentVertex = minHeap.deleteMin();

            // Need to get the edge for the vertex and add it to the Minimum Spanning Tree..
            // Just note, the edge for the source vertex will not be added.
            Edge spanningTreeEdge;
            
            Edge *spanningTreeEdgePtr = &spanningTreeEdge;

            if (vertexToEdge.find(currentVertex) != vertexToEdge.end()) {

                spanningTreeEdge = vertexToEdge[currentVertex];
            }
            else
                spanningTreeEdgePtr = NULL;
            
            if(spanningTreeEdgePtr != NULL) {
                result.push_back(spanningTreeEdge);
            }

            // Get all the adjacent vertices and iterate through them.
            for(Edge edge : getEdges(currentVertex)){
            
                string adjacentVertex = edge.endVertex;
                
                // We check if adjacent vertex exist in 'Map in Heap' and length of the edge is with this vertex
                // is greater than this edge length.
                if(minHeap.containsVertex(adjacentVertex) && minHeap.getWeight(adjacentVertex) > edge.value){
                
                    // Replace the edge length with this edge weight.
                    minHeap.updateHeap(adjacentVertex, edge.value);
                    
                    vertexToEdge[adjacentVertex] = edge;
                }
            }
        }
    }

    vector<Edge> getEdges(string vertex){

        vector<Edge> edgeList;

        int i = indexOf(vertex, vertices);

        for (Edge iter : adjcList[i]) {

            edgeList.push_back((Edge) iter);
        }

        return edgeList;
    }

    void constructAdjacencyList(string vertex1, string vertex2, int edgeVal) {

        Edge edge;

        edge.startVertex = vertex1;
        edge.endVertex = vertex2;
        edge.value = edgeVal;
        
        adjcList.push_back({});
        adjcList[indexOf(vertex1, vertices)].push_back(edge);
    }

    void insertVertex(string vertex) {

        vertices.push_back(vertex);
    }

    void printEdgeList() {
        
        for (Edge edge : result) {

            cout << "The Edge between " << edge.startVertex << " and " << edge.endVertex << " is " << edge.value << endl;
        }
    }
};

int main() {

    PrimMST primMST;

    // Insert Vertices
    primMST.insertVertex("a");
    primMST.insertVertex("b");
    primMST.insertVertex("c");
    primMST.insertVertex("d");
    primMST.insertVertex("e");

    // Create Adjacency List with Edges.
    primMST.constructAdjacencyList("a", "b",1);
    primMST.constructAdjacencyList("a", "c",5);
    primMST.constructAdjacencyList("a", "d",4);

    primMST.constructAdjacencyList("b", "a",1);
    primMST.constructAdjacencyList("b" ,"e",8);

    primMST.constructAdjacencyList("c", "a",5);
    primMST.constructAdjacencyList("c", "d",12);
    primMST.constructAdjacencyList("c", "e",9);

    primMST.constructAdjacencyList("d", "a", 4);
    primMST.constructAdjacencyList("d", "c", 12);
    primMST.constructAdjacencyList("d", "e", 3);

    primMST.constructAdjacencyList("e", "b", 8);
    primMST.constructAdjacencyList("e", "c", 9);
    primMST.constructAdjacencyList("e", "d", 3);

    primMST.primMST();

    primMST.printEdgeList();
}


Output :



  The Edge between a and b is 1
  The Edge between a and d is 4
  The Edge between d and e is 3
  The Edge between a and c is 5

Note : To understand, Prim's Algorithm for Minimum Spanning Tree. Prior knowledge of Heap and Adjacency List data structure is needed. But if you don't know, no worries. We will explain them in brief.

Let us recapitulate the 3 step process for Prim's Algorithm :

  1. First step is, we select any vertex and start from it(We have selected the vertex a in this case).

  2. Then, we try finding the adjacent Edges of that Vertex(In this case, we try finding the adjacent edges of vertex a).

    This is where, we will be using Adjacency List for finding the Adjacent Edges.

  3. Next, we will take all the Adjacent edges from above and check, which edge has the smallest length/weight.

    Again, this is where, we will be using Min-Heap for finding the Adjacent Edges.

Code explanation for Prim's Algorithm - Minimum Spanning Tree


Let us take the below graph, to understand the above code.

java_Collections

The first task is to construct the Graph. So, we need to store the Vertices first.


vector<string> vertices;

And also we know, we have to represent the edges as Adjacency List.


vector<vector<Edge>>  adjcList;

Eventually, in the main(...) method. We have initialised the vertices,


primMST.insertVertex("a");
primMST.insertVertex("b");
primMST.insertVertex("c");
primMST.insertVertex("d");
primMST.insertVertex("e");

And, created the Adjacency List with Edges.


// Create Adjacency List with Edges.
primMST.constructAdjacencyList("a", "b", 1);
primMST.constructAdjacencyList("a", "c", 5);
primMST.constructAdjacencyList("a", "d", 4);

primMST.constructAdjacencyList("b", "a", 1);
primMST.constructAdjacencyList("b" ,"e", 8);

primMST.constructAdjacencyList("c", "a", 5);
primMST.constructAdjacencyList("c", "d", 12);
primMST.constructAdjacencyList("c", "e", 9);

primMST.constructAdjacencyList("d", "a", 4);
primMST.constructAdjacencyList("d", "c", 12);
primMST.constructAdjacencyList("d", "e", 3);

primMST.constructAdjacencyList("e", "b", 8);
primMST.constructAdjacencyList("e", "c", 9);
primMST.constructAdjacencyList("e", "d", 3);

Let us understand creation process of Adjacency List. If you already know it, you can skip.



Click Here - For the creation process of Adjacency List.


Explanation of void constructAdjacencyList(string vertex1, string vertex2, int edgeVal)


void constructAdjacencyList(string vertex1, string vertex2, int edgeVal) {

	Edge edge;

	edge.startVertex = vertex1;
	edge.endVertex = vertex2;
	edge.value = edgeVal;

	adjcList.push_back({});
	adjcList[indexOf(vertex1, vertices)].push_back(edge);
}

Let us take the example of the Edge (a,b) with length 1 and understand the above code.

java_Collections

primMST.constructAdjacencyList("a", "b", 1);

Now, if we look at the method definition,


void constructAdjacencyList(string vertex1, string vertex2, int edgeVal)

string vertex1 is initialised with a.


string vertex2 is initialised with b.


int edgeVal is initialised with 1.


The next thing we do is, create an Edge object.


Edge edge;

And initialise the Edge edge object with the start vertex, end vertex and the length of the Edge.


edge.startVertex = vertex1;
edge.endVertex = vertex2;
edge.value = edgeVal;

And the Edge object looks somewhat like,

java_Collections

Now, since we have the Edge (a,b) initialised. The next task would be, to add this Edge to the AdjacencyList.


And as we have seen, there is a 2D List to store the Adjacency List.


vector<vector<Edge>> adjcList;

Now, we will be creating the first row of List,


adjcList[indexOf(vertex1, vertices)].push_back(edge);

And then we will try finding out, for which vertex we are going to insert the Edge.


adjcList[indexOf(vertex1, vertices)].push_back(edge);

In simple words, indexOf(vertex1, vertices) will tell us about the position of the start vertex.

java_Collections

From the method,


int indexOf(string val, vector<string> v) {

	auto match = find(v.begin(), v.end(), val);

	if(match != v.end())
		return match - v.begin();
	else
		return -1;
}

In this case, the start vertex is a. And indexOf(vertex1, vertices) will tell us about its position, i.e. 0.


So, if we substitute the line,


adjcList[indexOf(vertex1, vertices)].push_back(edge);

With the value of indexOf(vertex1, vertices),


adjcList[0].push_back(edge);

The edge would be added to the 0th location of the 2d List.

java_Collections

This is how a 2D List looks like. Just remember, the empty blocks are not yet created, but will be created eventually.


For now, only the first row is created with the first column where the edge

java_Collections

is inserted.


Next, when we try to insert the second adjacent edge of a i.e. a,c.


primMST.constructAdjacencyList("a", "c", 5);

An edge object will be created


edge.startVertex = vertex1;
edge.endVertex = vertex2;
edge.value = edgeVal;

And the Edge object looks somewhat like,

java_Collections

Once again the start vertex is a. And indexOf(vertex1, vertices) will tell us about its position, i.e. 0.


So, if we substitute the line,


adjcList[indexOf(vertex1, vertices)].push_back(edge);

With the value of vertices.IndexOf(vertex1),


adjcList[0].push_back(edge);

The edge would be added to the 1st row of the 2d List, just next to the first edge.

java_Collections

Similarly, we insert the third adjacent edge of a i.e. a,d.


primMST.constructAdjacencyList("a", "d", 4);

An edge object will be created


edge.startVertex = vertex1;
edge.endVertex = vertex2;
edge.value = edgeVal;

And the Edge object looks somewhat like,

java_Collections

Once again the start vertex is a. And vertices.IndexOf(vertex1) will tell us about its position, i.e. 0.


So, if we substitute the line,


adjcList[indexOf(vertex1, vertices)].push_back(edge);

With the value of indexOf(vertex1, vertices),


adjcList[0].push_back(edge);

The edge would be added to the 1st row of the 2d List, just next to the first edge.

java_Collections

Similarly, we insert the adjacent edge of b i.e. b,a.


primMST.constructAdjacencyList("b", "a", 1);

An edge object will be created


edge.startVertex = vertex1;
edge.endVertex = vertex2;
edge.value = edgeVal;

And the Edge object looks somewhat like,

java_Collections

Now, the start vertex is b. And indexOf(vertex1, vertices) will tell us about its position, i.e. 1.


So, if we substitute the line,


adjcList[indexOf(vertex1, vertices)].push_back(edge);

With the value of indexOf(vertex1, vertices),


adjcList[1].push_back(edge);

And this time, the edge would be added to the 2nd row of the 2d List.

java_Collections

And eventually the AdjacencyList with Edges would be created with the 2D List.


Once we are done creating the AdjacencyList with Edges. The next task would be to call the actual method for Prim's Algorithm,


primMST.primMST();

Before we understand primMST() method in detail. Let us understand the Min-Heap Data Structure.


If you already know Min-Heap Data Structure, you can skip the below part.



Click Here - For Min-Heap Data Structure Recap.


There are four important method in the MinHeap class that are quite important.


They are :

  1. string deleteMin()

  2. void updateHeap(string vertex, int length)

  3. void buildHeap()

  4. void heapify(vector<string> &verticesArray,int root, int length)

Now, if we look at the primMST() method, we could see that initially the MinHeap object is created using a parameterised constructor.


MinHeap minHeap(vertexVal);

And if we look at the Constructor definition,


MinHeap(map<string, int> vertexVal){

	this->vertexVal = vertexVal;
}

The map map<string, int> vertexVal is getting initialised here.


this.vertexVal = vertexVal;

Where, this->vertexVal is the class attribute,


map<string, int> vertexVal

Next, we initialise the Dictionary with the vertices as key and assign the value as INT_MAX. This INT_MAX is so large that it could be compared with infinity.


// Assign all the initial values as infinity for all the Vertices.
for(string v : vertices) {
	vertexVal[v] = INT_MAX;
}
java_Collections


So, the Dictionary vertexVal is loaded with values.


Next, we call the buildHeap() method to create the MinHeap, with the values of the Dictionary vertexVal.


minHeap.buildHeap();

Explanation of void buildHeap() method :


void buildHeap() {

	vector<string> verticesArray = convertMapKeysToVector(vertexVal);

	int len = verticesArray.size()-1;

	for (int parent = (len-1)/ 2; parent >= 0; parent--)
		heapify(verticesArray, parent, len);

	verticesKeyArray = verticesArray;

}

Just remember, Map is not a good Data Structure for Heap. Whereas array is an excellent Data Structure for Heap.


So, we take the keys from the Map and create an Array out of it.


vector<string> verticesArray = convertMapKeysToVector(vertexVal);

And the logic is defined in vector<string> convertMapKeysToVector(map<string, int> const& inputMap) method,


vector<string> convertMapKeysToVector(map<string, int> const& inputMap) {

	vector<string> verticesArray;

	for (auto const& element : inputMap) {

		verticesArray.push_back(element.first);
	}
	return verticesArray;
}

Then, we take the length of the Array.


int len = verticesArray.size()-1;

Then, we divide the Array into two parts and run a for loop. Then call the Heapify method.


for (int parent = (len-1)/ 2; parent >= 0; parent--)
	heapify(verticesArray, parent, len);

So, what does the heapify(..) method do?


Let us see.


Explanation of void heapify(vector<string> &verticesArray,int root, int length) method :


void heapify(vector<string> &verticesArray,int root, int length) {

	int left = (2*root)+1;
	int right = (2*root)+2;
	int smallest = root;

	if (left < length && right <= length && vertexVal[verticesArray[right]] < vertexVal[verticesArray[left]]) {

		smallest = right;
	}
	else if (left <= length){

		smallest = left;
	}

	if (vertexVal[verticesArray[root]] > vertexVal[verticesArray[smallest]]) {

		string temp = verticesArray[root];
		verticesArray.at(root) = verticesArray[smallest];
		verticesArray.at(smallest) = temp;

		heapify(verticesArray, smallest, length);
	}
}

Since, all the values of the Dictionary vertexVal are infinity now. So, it doesn't matter how the values will be inserted in the Min-Heap.

java_Collections

For the sake of understanding, let us understand the functionality of heapify(...) method.


So, we have passed the verticesArray, root element and the length of the Array to heapify(...) method.


Then, we have calculated the left, right and root element of the Heap.


int left = (2*root)+1;
int right = (2*root)+2;
int smallest = root;

Since, this is a Min-Heap. The smallest element would be at the root of the Heap.


So, we try initialising the smallest element with its right value.


And the below if condition checks, if the left and right element is less than the length of the Array.


if (left < length && right <= length && vertexVal[verticesArray[right]] < vertexVal[verticesArray[left]]) {

	smallest = right;
}
else if (left <= length){

	smallest = left;
}

And, if the element on the right side of the Dictionary vertexVal is less than the element on the right side.


vertexVal[verticesArray[root]] > vertexVal[verticesArray[smallest]]

Then element on the right side of the Dictionary is the smallest element.


smallest = right;

And in the else part, we can assume that the element on the left side of the Dictionary has the smallest element.


smallest = left;

Now, since we got the smallest element. We need to check if the actual root element is greater than the smallest element or not.


if (vertexVal[verticesArray[root]] > vertexVal[verticesArray[smallest]]) {

	string temp = verticesArray[root];
	verticesArray.at(root) = verticesArray[smallest];
	verticesArray.at(smallest) = temp;

	heapify(verticesArray, smallest, length);
}

And this is where, we swap the contents of the root element and the smallest element. Assuming the element in the root is greater than the smallest element.


Which shouldn't be. As the root element should always be the smallest element.


Then a recursive call is made to heapify(...) method.


heapify(verticesArray, smallest, length);

And the recursion continues until all the elements are arranged in MinHeap.


In the next line, we would update the value of source element a to 0.


// Replace the value of start vertex to 0.
minHeap.updateHeap("a",0);

Now, if we see the updateHeap(...) method,


Explanation of void updateHeap(string vertex, int length) method :


void updateHeap(string vertex, int length) {

	vertexVal[vertex] = length;

	vector<string> verticesArray = convertMapKeysToVector(vertexVal);

	int len = verticesArray.size()-1;

	for (int parent = (len-1)/ 2; parent >= 0; parent--)
		heapify(verticesArray, parent, len);

	verticesKeyArray = verticesArray;
}

The first thing we do is, replace the value in the Dictionary vertexVal.


vertexVal[vertex] = length;

Then we follow the same process we followed above. i.e. Extract the keys from the Dictionary. Then convert the HashSet into an Array.


vector<string> verticesArray = convertMapKeysToVector(vertexVal);

Then we call the heapify(...) method. Because we have updated a new value and in that case the Heap has to be rearranged to form a Min-Heap.


Now, that we have understood Min-Heap. Let us understand the details about the actual method that calculates the Minimum Spanning Tree using Prim's Algorithm.


Explanation of void primMST() method :


void primMST(){

	// Vertex to Edge Map
	map<string, Edge> vertexToEdge;

	// Assign all the initial values as infinity for all the Vertices.
	for(string v : vertices) {
		vertexVal[v] = INT_MAX;
	}

	MinHeap minHeap(vertexVal);

	// Call buildHeap() to create the MinHeap
	minHeap.buildHeap();

	// Replace the value of start vertex to 0.
	minHeap.updateHeap("a",0);

	// Continue until the Min-Heap is not empty.
	while(!minHeap.empty()){
		// Extract minimum value vertex from Map in Heap
		string currentVertex = minHeap.deleteMin();

		// Need to get the edge for the vertex and add it to the Minimum Spanning Tree..
		// Just note, the edge for the source vertex will not be added.
		Edge spanningTreeEdge;

		Edge *spanningTreeEdgePtr = &spanningTreeEdge;

		if (vertexToEdge.find(currentVertex) != vertexToEdge.end()) {

			spanningTreeEdge = vertexToEdge[currentVertex];
		}
		else
			spanningTreeEdgePtr = NULL;

		if(spanningTreeEdgePtr != NULL) {
			result.push_back(spanningTreeEdge);
		}

		// Get all the adjacent vertices and iterate through them.
		for(Edge edge : getEdges(currentVertex)){

			string adjacentVertex = edge.endVertex;

			// We check if adjacent vertex exist in 'Map in Heap' and length of the edge is with this vertex
			// is greater than this edge length.
			if(minHeap.containsVertex(adjacentVertex) && minHeap.getWeight(adjacentVertex) > edge.value){

				// Replace the edge length with this edge weight.
				minHeap.updateHeap(adjacentVertex, edge.value);

				vertexToEdge[adjacentVertex] = edge;
			}
		}
	}
}

Three things to remember :

  1. map<string,int> vertexVal;

    The Map with key as vertex and value as the Edge length. We put this in the Heap. Also we will call map<string,int> vertexVal as Map in the Heap.

  2. map<string, Edge> vertexToEdge;

    The Map with key as vertex and value as the actual Edge.

  3. vector<Edge> result;

    The List that stores the final result. i.e. All the edges to be included in the Minimum Spanning Tree.

Now, let us look at the steps involved :

  1. So, firstly we initialise the Map vertexVal. Which we are going to place in the Heap.

    Also, we will call the Map vertexVal as Map in the Heap.

    map<string,int> vertexVal;

  2. Then we assign the Map vertexToEdge, where we will be storing the vertex as key and Edge as value.

    map<string, Edge> vertexToEdge;

  3. The next task is to assign the value infinity to all the vertices in the Dictionary vertexToEdge.

    for(string v : vertices) {
    	vertexVal[v] = INT_MAX;
    }

    ruby


    We can see the value Int32.MaxValue initialised to all the vertices. It can be said as infinity as it has a very large value that can be compared with infinity.

  4. Then we initialise MinHeap by passing the Dictionary as a parameter to it constructor.

    MinHeap minHeap(vertexVal);

  5. Next, we call the buildHeap(...) method of the Heap. That will create the Heap for us, by placing the elements in the right order.

    minHeap.buildHeap();


    Just remember, in a Min-Heap the root element is always smaller than its left and right child.

  6. Then we replace the value of the start element to 0 by calling the updateHeap(...) method.

    minHeap.updateHeap("a",0);

    ruby

  7. Then we come to the while loop, that continues until the Min-Heap is empty.

    // Continue until the Min-Heap is not empty.
    while(!minHeap.empty()){
    	// Extract minimum value vertex from Map in Heap
    	string currentVertex = minHeap.deleteMin();
    	// Need to get the edge for the vertex and add it to the Minimum Spanning Tree..
    	// Just note, the edge for the source vertex will not be added.
    	Edge spanningTreeEdge;
    
    	Edge *spanningTreeEdgePtr = &spanningTreeEdge;
    	if (vertexToEdge.find(currentVertex) != vertexToEdge.end()) {
    		spanningTreeEdge = vertexToEdge[currentVertex];
    	}
    	else
    		spanningTreeEdgePtr = NULL;
    
    	if(spanningTreeEdgePtr != NULL) {
    		result.push_back(spanningTreeEdge);
    	}
    	// Get all the adjacent vertices and iterate through them.
    	for(Edge edge : getEdges(currentVertex)){
    
    		string adjacentVertex = edge.endVertex;
    
    		// We check if adjacent vertex exist in Map in Heap and length of the edge is with this vertex
    		// is greater than this edge length.
    		if(minHeap.containsVertex(adjacentVertex) && minHeap.getWeight(adjacentVertex) > edge.value){
    
    			// Replace the edge length with this edge weight.
    			minHeap.updateHeap(adjacentVertex, edge.value);
    
    			vertexToEdge[adjacentVertex] = edge;
    		}
    	}
    }

  8. Inside the while loop, the first thing we do is, get the smallest element from the Heap. That is the root element.

    Also we delete the root element from the Heap at the same time.

    And the deleteMin() method of the Heap does the work for us.

    string currentVertex = minHeap.deleteMin();


    Since, the vertex a has the smallest value i.e. 0. It is deleted from vertexVal. i.e. Heap in the Dictionary.
    ruby

  9. Then we go to the Map vertexToEdge which has the key as vertex and the value as actual Edge. And try getting the Edge for that corresponding vertex (i.e. a).

    But in this case, the Map vertexToEdge has not yet being initialised.

    Why is it so?

    Because a is the starting vertex and we won't be adding the starting vertex to the Minimum Spanning Tree.

    So, the Edge spanningTreeEdge gets null and is not added to the Mimimim Spanning Tree.

    	Edge spanningTreeEdge;
    
    	Edge *spanningTreeEdgePtr = &spanningTreeEdge;
    	if (vertexToEdge.find(currentVertex) != vertexToEdge.end()) {
    		spanningTreeEdge = vertexToEdge[currentVertex];
    	}
    	else
    		spanningTreeEdgePtr = NULL;
    
    	if(spanningTreeEdgePtr != NULL) {
    		result.push_back(spanningTreeEdge);
    	}

  10. The next task we have done is getting the adjacent vertices and iterate through and get the corresponding Edges for the vertices.

    And the getEdges() method helps us getting the Adjacent Edges of the vertex a.
    ruby


    	// Get all the adjacent vertices and iterate through them.
    	for(Edge edge : getEdges(currentVertex)){
    
    		string adjacentVertex = edge.endVertex;
    
    		// We check if adjacent vertex exist in Map in Heap and length of the edge is with this vertex
    		// is greater than this edge length.
    		if(minHeap.containsVertex(adjacentVertex) && minHeap.getWeight(adjacentVertex) > edge.value){
    
    			// Replace the edge length with this edge weight.
    			minHeap.updateHeap(adjacentVertex, edge.value);
    
    			vertexToEdge[adjacentVertex] = edge;
    		}
    	}

  11. Once we have the first Adjacent Edge i.e. a,b,
    ruby


    We would only take the end vertex from it. And edge object already has the endVertex.

    string adjacentVertex = edge.endVertex;

  12. Now, that we got the adjacentVertex i.e. b. The next thing we check is, if this adjacentVertex i.e. b, exist in Dictionary in Heap and also check, if the value of the edge length associated with this vertex i.e. The value associated with vertex b is greater than this edge length i.e. The length of edge a,b. Which is 1.
    ruby


    So, we find that vertex b is present in vertexVal i.e. Dictionary in heap.

    And the value of b in vertexVal i.e. Dictionary in heap is ∞. Which is greater that the value of the edge i.e. 1.

    So, the if condition is satisfied and we get into the if block.

    if(minHeap.containsVertex(adjacentVertex) && minHeap.getWeight(adjacentVertex) > edge.value){
    
    	// Replace the edge length with this edge weight.
    	minHeap.updateHeap(adjacentVertex, edge.value);
    
    	vertexToEdge[adjacentVertex] = edge;
    }

  13. Now, since we are in the blocks of if statement, we had updated the vertex with the new value of the Edge i.e. 1.

    minHeap.updateHeap(adjacentVertex, edge.value);


    Similarly, we put the actual edge i.e. a,b to the value of vertex b in the Dictionary vertexToEdge that stores vertex as key and the Edge associated to it as value.

    vertexToEdge[adjacentVertex] = edge;

    ruby

The similar process gets repeated and we get the Minimum Spanning Tree using Prim's Algorithm.


Note : Have you noticed the link between the Dictionary vertexToEdge and vertexVal(i.e. Dictionary in Heap)? The Edge length is stored in the Dictionary vertexVal(i.e. Dictionary in Heap) and the actual Edge associated to it is stored in the Dictionary vertexToEdge. Say vertex b has the Edge length 1 that is stored in the Dictionary vertexVal(i.e. Dictionary in Heap) and the actual Edge a,b is stored in the Dictionary vertexToEdge.

Time Complexity of Prim's Algorithm for Minimum Spanning Tree


The time complexity of Prim's Algorithm for Minimum Spanning Tree is : O(E logV)


Where E is the Edge


And V is the Vertex


The time complexity O(E logV) is only when we will be using the above combination of MIn-Heap and Adjacency List.