📖 Scenario: You are working as a network engineer. You need to connect several computers with cables. Each cable has a cost. Your goal is to connect all computers with the least total cost.This is a classic problem solved by Prim's algorithm to find the Minimum Spanning Tree (MST) of a graph.
🎯 Goal: Build a program that uses Prim's algorithm to find the Minimum Spanning Tree of a given graph represented by an adjacency matrix.You will create the graph, set up helper variables, implement Prim's algorithm, and finally print the MST edges and their costs.
📋 What You'll Learn
Create an adjacency matrix called graph representing the weighted graph
Create a variable numVertices for the number of vertices in the graph
Implement Prim's algorithm using arrays key, parent, and mstSet
Print the edges of the MST with their weights in the format: vertex1 - vertex2 : weight
💡 Why This Matters
🌍 Real World
Network design, connecting computers or cities with minimum cable cost.
💼 Career
Understanding MST algorithms is important for roles in network engineering, operations research, and software development involving graph problems.
Progress0 / 4 steps
1
Create the graph as an adjacency matrix
Create a variable called graph as a 2D array with these exact values representing the weighted graph: [[0, 2, 0, 6, 0], [2, 0, 3, 8, 5], [0, 3, 0, 0, 7], [6, 8, 0, 0, 9], [0, 5, 7, 9, 0]]
DSA Javascript
Hint
Use a 2D array to represent the graph weights between vertices. Zero means no direct edge.
2
Set up the number of vertices and helper arrays
Create a variable called numVertices and set it to the length of graph. Create three arrays: key with length numVertices filled with Infinity, parent with length numVertices filled with -1, and mstSet with length numVertices filled with false.
DSA Javascript
Hint
Use graph.length to get the number of vertices. Use new Array(numVertices).fill(value) to create arrays with default values.
3
Implement Prim's algorithm core logic
Write a function called primMST that: - Sets key[0] to 0 to start from vertex 0. - Loops numVertices times to pick the minimum key vertex not in mstSet. - Updates mstSet, key, and parent arrays accordingly. Use a helper function minKey inside primMST to find the vertex with the minimum key value not yet included in MST. Call primMST() after defining it.
DSA Javascript
Hint
Use a helper function minKey to find the vertex with the smallest key value not in MST. Update parent and key arrays when a better edge is found.
4
Print the Minimum Spanning Tree edges and weights
Use a for loop from 1 to numVertices - 1. Print each edge in the format: parent[i] - i : graph[i][parent[i]]. Use console.log for printing.
DSA Javascript
Hint
Loop from 1 to numVertices - 1 and print edges using parent[i] and graph[i][parent[i]].
Practice
(1/5)
1. What is the main goal of Prim's Algorithm in graph theory?
easy
A. To detect cycles in a graph
B. To find the shortest path between two nodes
C. To sort the nodes in topological order
D. To find the minimum spanning tree of a connected weighted graph
Solution
Step 1: Understand Prim's Algorithm purpose
Prim's Algorithm is designed to find a tree that connects all vertices with the smallest total edge weight.
Step 2: Compare with other graph algorithms
Shortest path algorithms find paths, cycle detection finds loops, and topological sort orders nodes. These are different from Prim's goal.
Final Answer:
To find the minimum spanning tree of a connected weighted graph -> Option D
Quick Check:
Prim's Algorithm = Minimum Spanning Tree [OK]
Hint: Prim's builds MST by adding cheapest edges stepwise [OK]
Common Mistakes:
Confusing Prim's with shortest path algorithms
Thinking it detects cycles
Mixing it with topological sorting
2. Which of the following is the correct way to initialize the key array in Prim's Algorithm in JavaScript for a graph with 5 vertices?
easy
A. const key = Array(5).fill(-1); key[0] = 0;
B. const key = Array(5).fill(0); key[0] = Infinity;
C. const key = new Array(5).fill(Infinity); key[0] = 0;
D. const key = new Array(5).fill(null); key[0] = 0;
Solution
Step 1: Understand key array purpose
The key array holds the minimum edge weight to connect each vertex. It starts with Infinity except the first vertex which is 0.
Step 2: Check initialization syntax
Using new Array(5).fill(Infinity) sets all to Infinity, then key[0] = 0 sets start vertex correctly.
Final Answer:
const key = new Array(5).fill(Infinity); key[0] = 0; -> Option C
Quick Check:
Key array init with Infinity except start = 0 [OK]