0% found this document useful (0 votes)
8 views

VADA

..... .

Uploaded by

savaniyakoppad6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

VADA

..... .

Uploaded by

savaniyakoppad6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Student Register Number: ….……………………………………..

Student Name:…………………………………………………….
Course:…………………...Subject : ………………………………
Class & Section: …………………………………………………..

Certificate

Thisis to certify that


smt/sri…………………………………has
satisfactorily completed the course of
assignment/seminar/presentation/case
studies prescribed by the Presidency College
(Autonomous) for the semester
…………………… degree course in the year
20 -20

MARKS
MAX OBTAINED
Signature of the Student Signature of the Staff Member

2. Apply the Quick Sort algorithm to sort the list of characters E, X, A, M, P,


L, E in alphabetical order. Also, draw the tree structure of recursive calls made
during the sorting process.

1. Introduction to the Problem Domain


* Sorting: Sorting is a fundamental operation in computer science. It involves
arranging a collection of items (numbers, characters, objects) in a specific order
(ascending, descending, alphabetical, etc.).
* Significance: Sorting algorithms have numerous applications across various
fields:
* Database Systems: Efficiently organizing data for fast retrieval and searching.
* Search Algorithms: Many search algorithms (like binary search) require sorted data.
* Data Analysis: Sorting is crucial for various data analysis techniques, such as
finding median, percentiles, and outliers.
* Graphics and Computer Vision: Sorting is used in image processing and computer
graphics algorithms

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

# Choose a pivot (here, we'll use the first


element) pivot = list[0]

# Partition the list


smaller, equal, greater = partition(list, pivot)

# Recursively sort sublists


return quick_sort(smaller) + equal + quick_sort(greater)

partition(list, pivot):

smaller = []

equal = []

greater = []

for item in list:

if item < pivot:

smaller.append(item)

elif item == pivot:

equal.append(item)

else:

greater.append(item)

return smaller, equal, greater

1. Time Complexity Analysis


* Best Case: O(n log n): Occurs when the pivot always divides the list into two roughly
equal halves at each step.
* Average Case: O(n log n): In most practical scenarios, the pivot selection leads to a
relatively balanced partitioning.
* Worst Case: O(n^2): Happens when the pivot is always the smallest or largest element
in the list, leading to highly unbalanced partitions.
2. Space Complexity Analysis
* Worst Case: O(n): In the worst case, the recursion depth can be O(n), leading to O(n)
stack space for function calls.
* Average Case: O(log n): On average, the recursion depth is O(log n), resulting in
O(log n) stack space.
Tree Structure of Recursive Calls
Note: The exact tree structure depends on the pivot selection strategy

/ \

A,M,P,L X

/ \

A,L,M P
/ \

A,L,M

/\

L M

Explanation of the Example

* Initial List: "E, X, A, M, P, L, E"


* Pivot (E): Partition: "A, L, M", "E", "P", "X"
* Recursive Calls:
* Sort "A, L, M"
* Pivot (A): Partition: "A", "L, M"
* Sort "L, M"
* Pivot (L): Partition: "L", "M"
* Base cases: "L", "M"
* Combine: "L", "M"
* Combine: "A", "L", "M"
* Sort "P", "X"
* Base case: "P"
* Base case: "X"
* Combine: "A", "L", "M", "E", "P", "X"

8. Apply Prim's Algorithm to obtain the minimum cost-spanning tree


for a given weighted graph. Explain the step-by-step process of the
algorithm and how it ensures that the spanning tree is formed with
the least possible total weight.

Introduction to the Problem Domain:


Minimum Cost Spanning Tree (MST): A spanning tree of a connected, undirected
graph is a subgraph that includes all the vertices of the original graph, and a subset of
the edges that connects all the vertices without forming any cycles. A Minimum Cost
Spanning Tree (MST) is a spanning tree where the total weight of the edges is
minimized. The concept is critical in various applications, including network design,
graph-based algorithms, and approximation algorithms for NP-hard problems like the
Traveling Salesman Problem.

The MST problem is fundamental in graph theory and has real-world applications like:

• Designing minimum-cost communication networks.

• Efficiently laying down electrical grids or water pipelines.

• Solving problems like clustering in data mining.

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:

1. Include all the vertices in the graph.

2. Have no cycles (i.e., form a tree).

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.

Key Characteristics of Prim's Algorithm:

• 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.

• Data Structures Used:

o Priority Queue (Min-Heap): To efficiently extract the smallest weight


edge.

o Visited Set: To track which nodes are part of the MST.


Step-by-Step Explanation of Prim's Algorithm:

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.

Time Complexity Analysis:

To analyze the time complexity, let's consider the operations involved:

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 If the graph is represented as an adjacency list, it requires O(V + E)


space to store all the vertices and edges.

2. Priority Queue (O(V)):

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).

Total Space Complexity:

• The total space complexity is O(V + E) for the adjacency list


representation and O(V) for the additional structures (priority queue,
visited set, and MST).

Pseudocode:
import heapq

def prim(graph, V):

mst = []

visited set()

minHeap []

actdEdges(O, graph, visited, minHeap)

while len(mst) < V - 1:

weight, u, v heapq.heappop(minHeap)

if v not in visited:

mst.append((u, v, weight))

addEdges(v, graph, visited, minHeap)

return mst

def addEdges(node, graph, visited,

minHeap):

visited.add(node)
for neighbor, weight in graph[node]:

if neighbor not in visited:

heapq. heappush (minHeap, (weight, node,


neighbor))

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.

You might also like