
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Prim’s MST for Adjacency List Representation
It is similar to the previous algorithm. Here the only difference is, the Graph G(V, E) is represented by an adjacency list.
Time complexity adjacency list representation is O(E log V).
Input and Output
Input: The cost matrix:Output: Edge: A--B And Cost: 1 Edge: B--E And Cost: 2 Edge: A--C And Cost: 3 Edge: A--D And Cost: 4 Edge: E--F And Cost: 2 Edge: F--G And Cost: 3 Total Cost: 15
Algorithm
prims(g: Graph, start)
Input − The graph g and the seed vertex named ‘start’
Output − The Tree after adding edges.
Begin create two set B, N add the start node in B set. for all vertices u in graph g do add u in the set N done while B ≠ N do min := ∞ for all vertices u in graph g do if u is in the set B then for all vertices v which are adjacent with u do if v is in (N – B) then if min > cost of uv edge then min := cost of uv edge parent := u node := v done done insert node in the B set add the edge starting from parent to node in the tree done return the tree End
Example
#include<iostream> #include<list> #include<set> using namespace std; typedef struct nodes { int dest; int cost; }node; class Graph { int n; list<node> *adjList; private: void showList(int src, list<node> lt) { list<node> :: iterator i; node tempNode; for(i = lt.begin(); i != lt.end(); i++) { tempNode = *i; cout << "(" << src << ")---("<<tempNode.dest << "|"<<tempNode.cost<<") "; } cout << endl; } public: Graph() { n = 0; } Graph(int nodeCount) { n = nodeCount; adjList = new list<node>[n]; } void addEdge(int source, int dest, int cost) { node newNode; newNode.dest = dest; newNode.cost = cost; adjList[source].push_back(newNode); } void displayEdges() { for(int i = 0; i<n; i++) { list<node> tempList = adjList[i]; showList(i, tempList); } } friend Graph primsMST(Graph g, int start); }; set<int> difference(set<int> first, set<int> second) { set<int> :: iterator it; set<int> res; for(it = first.begin(); it != first.end(); it++) { if(second.find(*it) == second.end()) res.insert(*it); //add those item which are not in the second list } return res; //the set (first-second) } Graph primsMST(Graph g, int start) { int n = g.n; set<int> B, N, diff; Graph tree(n); //make tree with same node as graph B.insert(start); //insert start node in the B set for(int u = 0; u<n; u++) { N.insert(u); //add all vertices in the N set } while(B != N) { int min = 9999; //set as infinity int v, par; diff = difference(N, B); //find the set N - B for(int u = 0; u < n; u++) { if(B.find(u) != B.end()) { list<node>::iterator it; for(it = g.adjList[u].begin(); it != g.adjList[u].end(); it++) { if(diff.find(it->dest) != diff.end()) { if(min > it->cost) { min = it->cost; //update cost par = u; v = it->dest; } } } } } B.insert(v); tree.addEdge(par, v, min); tree.addEdge(v, par, min); } return tree; } main() { Graph g(7), tree(7); g.addEdge(0, 1, 1); g.addEdge(0, 2, 3); g.addEdge(0, 3, 4); g.addEdge(0, 5, 5); g.addEdge(1, 0, 1); g.addEdge(1, 3, 7); g.addEdge(1, 4, 2); g.addEdge(2, 0, 3); g.addEdge(2, 4, 8); g.addEdge(3, 0, 4); g.addEdge(3, 1, 7); g.addEdge(4, 1, 2); g.addEdge(4, 2, 8); g.addEdge(4, 5, 2); g.addEdge(4, 6, 4); g.addEdge(5, 0, 5); g.addEdge(5, 4, 2); g.addEdge(5, 6, 3); g.addEdge(6, 4, 4); g.addEdge(6, 5, 3); tree = primsMST(g, 0); tree.displayEdges(); }
Output
Edge: A--B And Cost: 1 Edge: B--E And Cost: 2 Edge: A--C And Cost: 3 Edge: A--D And Cost: 4 Edge: E--F And Cost: 2 Edge: F--G And Cost: 3 Total Cost: 15
- Related Articles
- Dijkstra’s Algorithm for Adjacency List Representation
- Program to find out an MST using Prim's algorithm in Python
- Prim’s Algorithm (Simple Implementation for Adjacency Matrix Representation) in C++
- Prim's algorithm in Javascript
- C++ Program to Implement Adjacency List
- C++ Program to Represent Graph Using Adjacency List
- Linked List representation in Javascript
- Convert a string representation of list into list in Python
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Minimum spanning tree (MST) in Javascript
- Kruskal’s (Minimum Spanning Tree) MST Algorithm
- Prim’s (Minimum Spanning Tree) MST Algorithm
- Adjacency lists in Data Structures
- Adjacency Matrices and their properties
- C++ Program to Implement Adjacency Matrix

Advertisements