Document 9
Document 9
1. Introduction to Algorithms
Example:
3. Performance Analysis
1. Time Complexity: How the runtime of an algorithm scales with the size of the input.
2. Space Complexity: How much extra memory the algorithm uses.
Time Complexity
Time complexity measures how the runtime of an algorithm grows relative to the size of its
input n. It is expressed as a function T(n), which describes the number of basic operations
or steps performed.
1. Predict Performance: To estimate how an algorithm will perform for large inputs.
2. Algorithm Comparison: To compare multiple algorithms and choose the most
efficient one.
1. Best Case: The minimum time an algorithm takes (e.g., searching in the first
position of a list).
2. Worst Case: The maximum time an algorithm takes (e.g., searching for a missing
element).
3. Average Case: The expected time considering all possible inputs.
Types of analysis :
1. Experimental/ apostrium/ relative analysis
• Represents the tight bound, meaning the algorithm grows at the same rate in both
the best and worst cases.
Example: If T(n) = 3n^2 + 5n + 10, and all inputs grow at the same rate:
• Theta: Θ(n^2).
4. Little-o Notation (oo)
• Represents a non-tight upper bound, meaning the algorithm grows slower than the
bound.
Recurrences
A recurrence relation is an equation that defines a sequence or function T(n) in terms of its
value(s) for smaller input size(s). Recurrences often arise when analyzing recursive
algorithms, as they describe the runtime or behavior of the algorithm.
Solving Recurrences
1. Data Structures
2. Algorithms
3. Machine Learning
4. Cryptography
5. Network Protocols
A Disjoint Set (or Union-Find data structure) is a fundamental data structure used to
manage a collection of non-overlapping sets. It is commonly used in graph algorithms like
Kruskal’s Minimum Spanning Tree (MST) and connected component analysis.
1. Set Representation:
a. Each set is represented by a unique representative or leader (often the root
of a tree).
Operations
1. MakeSet(x)
2. Find(x)
Path Compression:
• As the algorithm traverses up the tree to find the root, it sets the parent of each
node directly to the root.
Time Complexity: O(α(n)), where α(n) is the inverse Ackermann function (extremely
slow-growing, nearly constant in practical scenarios).
3. Union(x, y)
Union by Rank:
• Attach the smaller tree under the larger tree based on rank.
• If ranks are equal, arbitrarily choose one root and increment its rank.
1. MakeSet: O(1)
2. Find: O(α(n))
3. Union: O(α(n))
• Overall Complexity: For m operations on n elements, the total time complexity is
O(mα(n)).
Applications
1. Dynamic Connectivity:
a. Determining if two elements are in the same set.
2. Clustering:
a. Grouping elements into disjoint subsets.
3. Network Analysis:
a. Tracking connected devices in a network.
4. Game Theory:
a. Simulating merges in dynamic systems.
UNIT 2
General Method:
1. Binary Search:
Example:
Steps:
Result:
Example:
• Array: [38,27,43,3,9,82,10]
Steps:
1. Divide: Split the array into halves until single elements remain:
a. [38,27,43,3,9,82,10] → [38, 27, 43], [3, 9, 82, 10].
2. Conquer: Sort each half recursively:
a. [38, 27, 43] → [27, 38, 43],
b. [3, 9, 82, 10] → [3, 9, 10, 82].
3. Combine: Merge sorted halves:
a. [27, 38, 43] and [3, 9, 10, 82] → [3, 9, 10, 27, 38, 43, 82].
Result:
3. Quick Sort:
Example:
Steps:
Greedy Method
General Method: The Greedy Method is an algorithmic approach where decisions are
made step-by-step, choosing the best option at each stage to achieve the global optimum.
This method works well for optimization problems that exhibit the greedy-choice property
(a locally optimal choice leads to a globally optimal solution) and optimal substructure
(optimal solutions to sub-problems contribute to the overall optimal solution).
1. Steps:
a. Selection: Choose the best option available based on a specific criterion.
b. Feasibility: Check if the choice is feasible within constraints.
c. Solution Building: Add the choice to the solution and repeat.
Applications:
Problem:
You have n jobs, each with a deadline and profit. Each job takes one unit of time, and only
one job can be scheduled at a time. The goal is to schedule jobs to maximize total profit
while meeting their deadlines.
Example:
• Jobs: J1,J2,J3,J4
• Deadlines: 2,1,2,1
• Profits: 100,19,27,25
Steps:
Result:
Problem:
You are given n items, each with a weight and value. You must maximize the total value of
items in a knapsack with capacity W, allowing fractions of items.
Example:
• Items: I1,I2,I3
• Weights: 10,20,30
• Values: 60,100,120
• Knapsack Capacity: W=50.
Steps:
Result:
Problem:
Connect all vertices of a graph with the minimum total edge weight.
Steps:
Result:
Problem:
Find the shortest path from a source vertex to all others in a graph with positive edge
weights.
Steps:
Result: