VADA
VADA
Student Name:…………………………………………………….
Course:…………………...Subject : ………………………………
Class & Section: …………………………………………………..
Certificate
MARKS
MAX OBTAINED
Signature of the Student Signature of the Staff Member
2. Problem Statement
Given a list of characters: "E, X, A, M, P, L, E", sort them in alphabetical order using the
Quick Sort algorithm.
3. Algorithm Description
Quick Sort is a divide-and-conquer algorithm.
* Core Idea:
* Choose a Pivot: Select a "pivot" element from the list.
* Partition: Rearrange the list so that all elements smaller than the pivot come before it,
and all elements greater than the pivot come after it.
* Recursively Sort Sublists: Recursively apply Quick Sort to the sublists on both sides
of the pivot.
Pseudocode:
quick_sort(list):
if length(list) <= 1:
return list # Base case: list of size 0 or 1 is already sorted
partition(list, pivot):
smaller = []
equal = []
greater = []
smaller.append(item)
equal.append(item)
else:
greater.append(item)
/ \
A,M,P,L X
/ \
A,L,M P
/ \
A,L,M
/\
L M
The MST problem is fundamental in graph theory and has real-world applications like:
Prim's Algorithm is one of the most popular algorithms to find the Minimum Cost
Spanning Tree. It incrementally builds the MST by selecting the smallest edge that
connects a vertex in the tree to a vertex outside the tree.
Problem Statement:
Given a weighted, connected, undirected graph, the goal is to apply Prim's Algorithm
to compute the Minimum Cost Spanning Tree (MST). The MST is a subset of the
edges that connects all vertices of the graph without forming any cycles, and the total
weight of the edges in the MST is minimized.
Graph Representation:
• The graph consists ofV vertices (nodes) and E edges (connections between
vertices).
• Each edge has an associated weight (a value representing the cost or distance
between two nodes).
• The graph is undirected, meaning if there's an edge between vertex A and
vertex B, it can be traversed in both directions.
Objective:
Use Prim's Algorithm to find the MST of the given graph. The tree must:
3. Have the minimum total weight possible, meaning the sum of the edge weights
should be as small as possible.
Algorithm Description:
Prim's algorithm is a well-known greedy algorithm used to find the Minimum Spanning
Tree (MST) of a weighted, connected, undirected graph. A Minimum Spanning Tree is
a subset of the edges of the graph that connects all the vertices together without forming
any cycle and with the least possible total edge weight.
• Greedy Strategy: At each step, the algorithm chooses the smallest weight
edge that connects a vertex inside the MST to a vertex outside the MST.
• Iterative: The algorithm starts with an arbitrary node, and each iteration
adds the shortest possible edge to the MST.
1. Start from an arbitrary node: Choose any node as the starting point and add
it to the MST.
2. Add edges to the Min-Heap: Add all edges connected to the current
node to the priority queue (min-heap), sorted by their edge weights.
3. Select the minimum weight edge: From the priority queue, select the edge
with the smallest weight that connects a vertex inside the MST to a vertex
outside the MST.
4. Add the selected edge: Add the edge and its corresponding vertex to the MST.
5. Repeat: Continue this process until all vertices are included in the MST.
6. Termination: The algorithm terminates when the MST contains all the
vertices of the graph, and the total edge weight is minimized.
Algorithm Description:
The algorithm operates by iteratively growing the MST, starting from a single node and
repeatedly adding the minimum edge that connects the MST to a new vertex.
1. Heap Operations:
• Inserting edges into the priority queue: Each insertion into the min-
heap takes O(log E) time, where E is the number of edges.
• Extracting the minimum edge from the heap: Each extraction
operation takes O(log E) time.
• In the worst case, we insert and extract each edge exactly once.
• In the worst case, we insert and extract each edge exactly once.
1. Main Loop:
• The main loop runs until the MST contains all vertices (i.e., V - 1 edges).
• For each vertex added, we consider all adjacent edges (i.e., all
outgoing edges from a vertex).
Time Complexity Breakdown:
• Inserting and Extracting Edges: Since each edge is inserted and extracted
from the heap once, there are O(E) heap operations, and each operation takes
O(log E) time.
• Overall Complexity: The time complexity is therefore O(E log E).
Best-Case Scenario:
• In the best case, the graph is sparse, meaning there are fewer edges
compared to vertices (O(E) edges, where E << VA2).
• The algorithm still processes all edges, but the performance is generally
quicker in graphs with fewer edges, though the time complexity will still be
O(E log E).
Average-Case Scenario:
• In a typical scenario with random edge weights, the algorithm will run in O(E
log E) time, where E is the number of edges in the graph.
• The priority queue operations will dominate the time complexity, but the
actual performance depends on the graph's structure.
Worst-Case Scenario:
• In the worst case, for a complete graph where every pair of vertices is
connected by an edge (i.e., a dense graph), the number of edges will be
Q(VA2). In this case, the time complexity is O(E log V), where E;:::: V/\2.
This makes the worst-case time complexity approximately O(VA2 log V).
Space Complexity:
1. Graph Representation (O(V + E)):
o At most, the priority queue stores O(V) nodes, as the algorithm only
pushes unvisited vertices into the heap.
3. Visited Set (O(V)):
o The visited set keeps track of which vertices have been added to
the MST, requiring O(V) space.
4. MST (O(V)):
o The MST stores O(V) edges, one for each vertex except the
starting vertex (since the first node doesn't require an edge to
connect it).
Pseudocode:
import heapq
mst = []
visited set()
minHeap []
weight, u, v heapq.heappop(minHeap)
if v not in visited:
mst.append((u, v, weight))
return mst
minHeap):
visited.add(node)
for neighbor, weight in graph[node]:
Diagrams:
Now, let's see the working of prim's algorithm using an example. It will be easier
to understand the prim's algorithm using an example.
Consider the following graph as an example for which we need to find the
Minimum Spanning Tree using prim's algorithm.
Step 1: As a first step, a vertex is chosen arbitrarily to serve as the initial vertex for
the Minimum Spanning Tree. In this particular case, vertex 00 has been selected as
the starting vertex.
Step 2: The edges that connect the unfinished Minimum Spanning Tree to the
remaining vertices are represented by the edges {0,1}{0,1} and {0,7}{0,7}. Out of
these two
edges, {0,1}{0,1} has the smallest weight, and thus it is added to the Minimum
Spanning Tree. Moreover, vertex 11 is also added to the Minimum Spanning Tree.
Step 3: There exist edges that link the unfinished Minimum Spanning Tree to the
remaining vertices, and they are {0,7}{0,7}, {1,7}{1,7}, and {1,2}{1,2}. Among
these edges, the edges {0,7}{0,7} and {1,2}{1,2} have the smallest weight, with a
weight of 88. In this instance, the edge {0,7}{0,7} and vertex 77 are included in the
Minimum Spanning Tree.
Alternatively, we could have included the edge {1,2}{1,2} and vertex 22 in the
Minimum Spanning Tree.
Step 4: The edges that link the unfinished Minimum Spanning Tree to the fringe
vertices are {1,2}{1,2}, \left\{7,6\right\}, and {7,8}{7,8}. As the edge {7,6}{7,6}
has the lowest weight (11), it is added to the Minimum Spanning Tree along with
vertex 66.
Step 5: The edges that now link the Minimum Spanning Tree
are {7,8}{7,8}, {1,2}{1,2}, {6,8}{6,8}, and {6,5}{6,5}. Since the edge {6,5}{6,5} has
the smallest weight (22) among these edges, it is added to the Minimum Spanning Tree
along with vertex 55.
Step 6: Out of the present connecting edges, the edge {5,2}{5,2} has the smallest
weight. Therefore, the Minimum Spanning Tree includes this edge and vertex 22.
Step 7: Include the vertex 88 in the MST by adding the edge {2,8}{2,8} with a
weight of 22, which is the minimum weight among the connecting edges between the
incomplete MST and the other edges, namely {2,3}{2,3}, {5,3}{5,3}, and {5,4}
{5,4}.
Step 8: Note that both edges {7,8}{7,8} and {2,3}{2,3} have the same minimum
weight. However, since vertex 77 is already included in the MST, we will select the
edge {2,3}{2,3} and add it to the MST along with vertex 33.
Step 9: The vertex 44 is the only one that has yet to be added to the MST. The
edge {3,4}{3,4} has the minimum weight among the edges connecting the
incomplete MST to vertex 44.
Step 10: Here is the ultimate configuration of the MST, with the total weight of its
edges being (4+8+1+2+4+2+7+9)=37(4+8+1+2+4+2+7+9)=37.
Please note that had we chosen the edge {1,2}{1,2} in the third step, the MST would
have appeared as depicted below.
Conclusion:
Prim's Algorithm efficiently constructs the Minimum Cost Spanning Tree by
incrementally adding the smallest edge that connects a new vertex to the MST. This
approach ensures that the tree is formed with the least possible total weight, making it
a greedy algorithm suitable for dense graphs. The time complexity is manageable for
graphs with up to thousands of edges and vertices, especially with the help of priority
queues or min-heaps.