Desgin Analysis And Algorithms Full notes
Desgin Analysis And Algorithms Full notes
Introduction to Algorithms
Characteristics of an Algorithm
1. Input
2. Output
3. Definiteness
o Example: “Mix well” is not definite, but “Stir for 2 minutes” is definite.
4. Finiteness
o Example: A recipe with infinite steps would mean you never finish cooking!
5. Effectiveness
o Example: If a recipe says “add magical powder,” it’s not effective because it’s
impossible!
Analysis of Algorithms
Once an algorithm is written, it's important to know how efficient it is. We want to know:
This is done through asymptotic analysis — a fancy way of saying “studying how an
algorithm behaves as the input size increases.”
Asymptotic analysis is used to measure the efficiency of an algorithm in terms of how the
running time or space requirement increases as the input size increases.
We use symbols like O (Big-O), Ω (Big-Omega), and Θ (Theta) to describe how the
algorithm behaves.
It helps us to predict how fast the algorithm will work as the problem size increases.
Asymptotic analysis helps us figure out which "speed" the algorithm is using!
An algorithm can behave differently depending on the type of input it gets. This leads to
three cases:
The minimum time taken by an algorithm when the input is in the best possible
state.
Example:
o If you're searching for an item in a sorted list and it happens to be the first
one, that's the best case.
👉 Example:
Linear search (finding an element in an array): Best case happens when the element
is at the beginning.
Best-case time complexity for linear search:
Ω(1)\Omega(1)
The maximum time an algorithm can take under the worst possible input.
Example:
o If you are searching for an item and it happens to be the last one (or not in
the list at all).
Notation: OO (Big-O)
👉 Example:
Linear search: Worst case happens when the element is at the last position or
missing from the list.
O(n)O(n)
The expected time an algorithm takes under typical conditions (random input).
Example:
o If you're searching for an element in a list and it’s equally likely to be at any
position.
👉 Example:
Θ(n)\Theta(n)
Linear Time O(n) Time increases directly with input size. Linear search
Travelling salesperson
Factorial Time O(n!) Time grows very fast with input size.
problem
Summary
Worst Case Slowest possible time Finding the toy at the bottom
🚀 Key Takeaways
1. Time Complexity – How fast the algorithm runs as the input size increases.
2. Space Complexity – How much memory the algorithm needs to execute as the input
size increases.
Understanding the performance helps us decide which algorithm to use based on the
problem size and available resources.
1. Time Complexity
Time complexity measures how the execution time of an algorithm increases as the size of
the input grows.
👉 Example:
Linear Time O(n) Linear search Time increases directly with input size
O(log n) grows
Exponential Time Solving a chessboard Time doubles with each additional input
O(2^n) problem element
2. Space Complexity
Space complexity measures how much memory an algorithm needs to execute. It includes:
Auxiliary space – Extra memory used during execution (e.g., for variables and
recursion stack).
👉 Example:
An algorithm with a large data structure (e.g., a matrix) will have high space
complexity.
Linear Space O(n) Storing an array Memory grows directly with input size
Quadratic Space O(n^2) Storing a matrix Memory grows with the square of input size
In most cases, improving the time efficiency of an algorithm increases memory usage — and
vice versa. This is known as the time-space trade-off.
✅ Example 1:
Bubble Sort – Takes less memory (in-place sorting) but more time → High time, low
space.
Merge Sort – Takes extra memory (for merging) but works faster → Low time, high
space.
✅ Example 2:
Trade-off Principle:
Recursive algorithms often have time complexities that are defined using recurrence
relations — equations that describe how the algorithm's running time depends on the size
of the input.
where:
This method involves guessing the solution and then proving it by mathematical induction.
✅ Steps:
Recurrence Relation:
T(n)=2T(n/2)+nT(n) = 2T(n/2) + n
1. Guess:
T(n)=cnlognT(n) = cn \log n
This method involves drawing a tree to represent the recursive calls and calculating the total
work done at each level.
✅ Steps:
✅ Example:
Recurrence Relation:
T(n)=2T(n/2)+nT(n) = 2T(n/2) + n
Recursion Tree:
Level 0: n
✅ Total Work:
✅ Example:
Recurrence Relation:
T(n)=2T(n/2)+nT(n) = 2T(n/2) + n
logba=log22=1\log_b a = \log_2 2 = 1
✅ Final Answer:
Substitution Guess the solution and prove by Small and simple recurrence
Method induction relations
🚀 Key Takeaways
✅ Performance measurement tells us how fast and memory-efficient an algorithm is.
✅ Time-space trade-off means you can trade memory for speed or vice versa.
✅ Recurrence relations define the running time of recursive algorithms.
✅ Master’s theorem provides a quick way to solve many recurrence relations.
✅ Efficient algorithms are essential for solving large-scale problems! 😎
MODULE 2:
Certainly! Let's break down each algorithmic strategy in detail, along with examples of their
application to classic problems, and then discuss their time complexity.
Definition:
The brute-force method is the simplest algorithmic technique where every possible solution
is generated and checked until the correct solution is found. This technique does not involve
any sophisticated optimization, and it usually guarantees an optimal solution, though it can
be computationally expensive. The approach is very straightforward and often used when no
better algorithms are available.
In the Bin Packing problem, we are given a collection of items of various sizes and a set of
bins, each with a fixed capacity. The goal is to minimize the number of bins used to pack all
the items.
Brute-Force Approach:
Try every possible way of packing items into bins and then check how many bins are used.
The solution with the minimum number of bins is selected.
Time Complexity:
The time complexity of brute-force depends on the number of permutations. For the
Bin Packing problem, it can be O(2^n), where nn is the number of items, because we
need to check all possible subsets and their placements.
Definition:
A greedy algorithm follows the problem-solving heuristic of making the locally optimal
choice at each step with the hope of finding the global optimum. It works in a step-by-step
manner, always choosing the best available option, without considering the consequences of
that choice in the long term.
In the Fractional Knapsack Problem, you can take fractions of items. The goal is to maximize
the total value of items placed into a knapsack with a weight limit.
Greedy Approach:
3. Take the item with the highest ratio and add as much of it as possible into the
knapsack until the capacity is full.
4. Repeat this until all items are processed or the knapsack is full.
Algorithm:
o If the current item fits in the remaining knapsack space, add it entirely.
o If it does not fit, add the fractional part of the item that fits.
Time Complexity:
Sorting the items takes O(n log n), where nn is the number of items.
Selecting items takes O(n), so the total time complexity is O(n log n).
Definition:
Dynamic programming is an optimization technique that solves problems by breaking them
down into simpler subproblems and storing the solutions to those subproblems to avoid
redundant computations. It is used when the problem has overlapping subproblems and
optimal substructure.
DP Approach:
2. Transition:
o If the ithi^{th} item is not included, the value is the same as dp[i−1][w]dp[i-1]
[w].
3. The final answer is found at dp[n][W]dp[n][W], where nn is the number of items and
WW is the capacity.
Time Complexity:
Filling the DP table takes O(nW)O(nW), where nn is the number of items and WW is
the knapsack's weight capacity.
Definition:
Branch and Bound is an algorithm design paradigm for solving optimization problems. It
divides the problem into subproblems (branching) and calculates bounds on the solution of
these subproblems to prune those that cannot possibly lead to an optimal solution
(bounding).
In the Traveling Salesman Problem, the goal is to find the shortest route that visits all cities
exactly once and returns to the starting point.
B&B Approach:
1. Start with an initial solution (e.g., a path visiting the first city and extending the
route).
2. Branch by considering all possible paths and calculate the lower bound of the
distance that can be traveled.
3. Prune any branches where the lower bound exceeds the current best solution.
The worst-case time complexity can be O(n!) because the algorithm explores all
permutations of cities in the worst case, but pruning can reduce this dramatically in
practice.
Definition:
Backtracking is a method for solving problems incrementally by trying out different solutions
and abandoning ("backtracking") those that do not lead to a valid or optimal solution. It is
often used for constraint satisfaction problems.
In the N-Queens problem, the goal is to place N queens on an N×N chessboard such that no
two queens threaten each other.
Backtracking Approach:
1. Start with an empty board and place the first queen in the first row.
2. Move to the next row and try placing a queen in every column, checking whether it is
safe (i.e., no two queens are in the same column, row, or diagonal).
4. Repeat until all queens are placed correctly or all possibilities are exhausted.
Time Complexity:
The worst-case time complexity is O(n!) because, in the worst case, we have to
explore all possible placements for the queens.
Definition:
Heuristics are techniques designed to find a good enough solution for a problem when an
exact solution is impractical due to time or computational limitations. They are often
problem-specific and do not guarantee optimal solutions but work efficiently in practice.
Characteristics of Heuristics:
Fast: Designed to find solutions quickly, even if they are not optimal.
Approximate: They provide good solutions but may not guarantee the best.
Application Domains:
Routing Problems: Heuristics like the nearest neighbor algorithm in TSP can provide
a quick, though non-optimal, solution.
Conclusion
The time complexities vary depending on the specific problem and algorithm used, but most
of these techniques provide a trade-off between solution quality and computational
efficiency.
MODULE 3:
1. Depth First Search (DFS) (10 Marks)
Definition: Depth First Search (DFS) is a graph traversal algorithm that starts at a selected
node (usually the root or an arbitrary node) and explores as far as possible along each
branch before backtracking.
Working:
1. Start at the source vertex.
2. Explore each unvisited neighboring node recursively (or using a stack in an iterative
version).
3. Once all neighbors of a vertex are visited, backtrack to the previous vertex and
continue.
4. The algorithm continues until all vertices in the connected component are visited.
DFS Algorithm:
2. For each unvisited neighbor of the current node, recursively perform DFS.
Time Complexity:
If the graph has VV vertices and EE edges, the time complexity is O(V + E).
Definition: Breadth First Search (BFS) is a graph traversal algorithm that starts at a source
node and explores all the neighbor nodes at the present depth level before moving on to
nodes at the next depth level.
Working:
2. Visit all neighboring vertices (nodes that are one step away from the source).
3. Enqueue all the visited neighbors and proceed with the next level of vertices.
BFS Algorithm:
o Dequeue a node, and for each unvisited neighboring node, enqueue it and
mark it as visited.
Time Complexity:
Definition: Shortest path algorithms are used to find the shortest path between nodes in a
graph. They are widely used in applications like routing, network design, and geographical
mapping.
1. Initialize the distance to the source node as 0 and to all other nodes as infinity.
3. For each unvisited neighbor, calculate its tentative distance. If it's smaller than the
known distance, update it.
4. Once all neighbors of the current node are processed, mark the current node as
visited and move to the unvisited node with the smallest tentative distance.
Using a priority queue, the time complexity is O((V + E) log V), where VV is the
number of vertices and EE is the number of edges.
Definition: The transitive closure of a graph is a reachability matrix that shows whether
there is a path between any pair of vertices in a directed graph. It is typically represented
using an adjacency matrix.
2. Update the matrix iteratively: For each intermediate node kk, check if a path exists
from node ii to node jj through node kk.
3. The matrix TT will eventually hold the transitive closure of the graph.
Time Complexity:
1. Start with an arbitrary node and initialize its key value to 0 and all others to infinity.
3. For every node connected to the MST, update its key value if a lower-weight edge is
found.
4. Select the node with the smallest key value that is not yet part of the MST.
Time Complexity:
Using a priority queue, the time complexity is O((V + E) log V), where VV is the
number of vertices and EE is the number of edges.
2. Pick the smallest edge and add it to the MST if it doesn't form a cycle.
Time Complexity:
Sorting the edges takes O(ElogE)O(E \log E), and the union-find operations take
O(ElogV)O(E \log V), so the total time complexity is O(E log E).
Definition: Topological sorting is the process of ordering the vertices of a directed acyclic
graph (DAG) in a linear sequence such that for every directed edge u→vu \to v, vertex uu
comes before vv in the ordering.
1. Perform DFS on the graph. For each vertex, after visiting all its neighbors, push it to a
stack.
Since DFS visits each vertex and edge once, the time complexity is O(V + E), where VV
is the number of vertices and EE is the number of edges.
Definition: A network flow algorithm is used to solve problems where there is a flow of
resources (like data or goods) through a network. The goal is to find the maximum flow from
a source node to a sink node, subject to the capacities of the edges.
2. While there exists an augmenting path (a path from source to sink with available
capacity), increase the flow along the path.
Time Complexity:
2. Update the flow and residual graph, and repeat until no augmenting path exists.
Time Complexity:
The time complexity of the Edmonds-Karp algorithm is O(V \cdot E^2), where VV is
the number of vertices and EE is the number of edges.
Conclusion:
Graph and tree algorithms form the foundation for solving various problems in computer
science. Traversal algorithms like DFS and BFS help explore graphs systematically. Shortest
path algorithms, including Dijkstra's algorithm, solve path-finding problems. Transitive
closure and MST algorithms are crucial for network analysis and optimization. Topological
sorting is vital for tasks like scheduling, and network flow algorithms are fundamental for
resource allocation problems. Each of these algorithms has well-defined time complexities,
and their choice depends on the nature of the problem and the graph structure.
MODULE 4:
1. Tractable and Intractable Problems (10 Marks)
Definition:
Tractable Problems: These are problems for which an algorithm exists that can solve
the problem in polynomial time. These problems can be efficiently solved for large
inputs, and their solutions scale well as the problem size increases.
Intractable Problems: These are problems for which no known algorithm can solve
them in polynomial time, making it infeasible to solve them for large inputs.
Intractable problems often require exponential time to solve, which becomes
impractical as the size of the problem grows.
Sorting algorithms like Merge Sort and Quick Sort. These have time complexities of
O(n log n), making them feasible for large inputs.
The Traveling Salesman Problem (TSP), where the time complexity can grow
exponentially, particularly when trying to find the optimal solution for large numbers
of cities. The brute-force approach has a time complexity of O(n!), making it
infeasible for large nn.
Tractable: Time complexity is polynomial (e.g., O(n^2), O(n log n), etc.).
A problem is computable if there exists an algorithm (or Turing machine) that can
solve it in a finite amount of time.
A problem is non-computable if there is no algorithm that can solve it for all possible
inputs.
The Binary Search algorithm on a sorted list is computable because there exists an
algorithm that will find an element in logarithmic time, O(log n).
Definition of Classes:
1. P (Polynomial Time):
The class of problems that can be solved in polynomial time by a deterministic Turing
machine. These problems are considered "tractable" since their solutions can be
computed in reasonable time for large inputs.
Example:
o Sorting (like Merge Sort, Quick Sort): Time complexity O(n log n), which is
polynomial.
Example:
3. NP-complete:
These are problems in NP that are at least as hard as every other problem in NP. If
any NP-complete problem can be solved in polynomial time, then all NP problems
can be solved in polynomial time (i.e., P=NPP = NP).
Cook’s Theorem:
Cook's Theorem, proven by Stephen Cook in 1971, states that the Boolean satisfiability
problem (SAT) is NP-complete. This was the first such result, showing that SAT is both in NP
and NP-hard.
o Traveling Salesman Problem (TSP): Finding the shortest possible route that
visits each city exactly once.
o Knapsack Problem: Given a set of items, find the subset that fits into a
knapsack of limited capacity while maximizing the total value.
4. NP-hard:
These problems are at least as hard as the hardest problems in NP, but they are not
necessarily in NP (i.e., they might not have a solution that can be verified in
polynomial time). An NP-hard problem does not have to be a decision problem, and
it can be harder than NP-complete problems.
NP: Solutions can be verified in polynomial time, but finding the solution may take
non-polynomial time.
NP-complete: These are the hardest problems in NP, and solving any one NP-
complete problem in polynomial time would imply that all NP problems can be
solved in polynomial time (i.e., P=NPP = NP).
NP-hard: These are at least as difficult as NP-complete problems, but they may not
belong to NP (e.g., non-decision problems).
Definition: Cook’s Theorem, proved by Stephen Cook in 1971, states that the Boolean
satisfiability problem (SAT) is NP-complete. This was a groundbreaking result as it was the
first proof that a problem is both in NP and NP-hard.
Cook’s Theorem essentially shows that any problem in NP can be reduced to SAT in
polynomial time. Therefore, SAT is the "hardest" problem in NP. If SAT can be solved in
polynomial time, then every problem in NP can also be solved in polynomial time, implying
P=NPP = NP.
Cook’s Theorem Concept:
1. SAT is in NP: Given a proposed solution (an assignment of truth values to the
variables), we can verify in polynomial time whether the Boolean formula evaluates
to true.
NP-complete problems are the hardest problems in NP, and every NP problem can be
reduced to them in polynomial time. Here are some classic examples of NP-complete
problems:
o Problem: Given a set of cities and distances between each pair, find the
shortest possible route that visits every city once and returns to the starting
city.
o Reduction: The decision version of TSP (i.e., does a tour exist with a cost less
than a given threshold?) is NP-complete.
2. Knapsack Problem:
o Problem: Given a set of items, each with a weight and value, determine the
maximum value that can be placed in a knapsack of limited capacity.
Definition: Reduction techniques are used to prove that a problem belongs to a certain
complexity class, such as NP-complete. A reduction involves transforming one problem into
another problem in such a way that the solution to the transformed problem can be used to
solve the original problem.
Types of Reductions:
1. Polynomial-time Reduction:
A reduction from problem A to problem B means that if we can solve problem B in
polynomial time, we can solve problem A in polynomial time. This is the core idea
behind proving NP-completeness.
2. Many-One Reduction:
In a many-one reduction, an instance of problem A is transformed into an instance of
problem B in polynomial time, and a solution to B corresponds directly to a solution
to A.
The 3-SAT problem is an NP-complete problem that asks whether a Boolean formula in
conjunctive normal form (CNF) can be satisfied, where each clause has exactly three literals.
Using a reduction from the SAT problem (which may have clauses with more than three
literals) to 3-SAT, we can prove that 3-SAT is NP-complete.
Conclusion:
The study of tractable and intractable problems, along with computability and complexity
classes like P, NP, NP-complete, and NP-hard, plays a central role in understanding the limits
of what can be computed efficiently. Cook's theorem and the concept of reductions have
deepened our understanding of NP-completeness, showing that if any NP-complete problem
can be solved in polynomial time, all NP problems can. The importance of these results lies
in their applications to optimization problems, cryptography, and algorithm design.
MODULE 5:
1. Approximation Algorithms (10 Marks)
Key Concepts:
Approximation Ratio: The ratio of the algorithm's solution to the optimal solution. If
A(x)A(x) is the solution returned by the approximation algorithm and OPT(x)OPT(x) is
the optimal solution, the approximation ratio is defined as A(x)OPT(x)\frac{A(x)}
{OPT(x)}.
Problem: Given an undirected graph, find the smallest subset of vertices such that
every edge in the graph is incident to at least one vertex in the subset.
Time Complexity:
o The algorithm runs in O(E) time, where EE is the number of edges, since each
edge is processed at most once.
Performance Bound:
For the Vertex Cover Problem, the approximation ratio of this greedy algorithm is 2.
This means the solution is at most twice the size of the optimal solution.
Time Complexity:
1. Las Vegas Algorithms: These algorithms always produce the correct result, but their
runtime may vary. The algorithm's correctness is guaranteed, but the time taken may
depend on the random choices made.
2. Partition the array into two subarrays such that all elements less than the
pivot are on the left and all elements greater than the pivot are on the right.
Time Complexity:
The Miller-Rabin Test runs in O(k log n), where kk is the number of iterations (to
reduce the probability of error), and nn is the number being tested for primality.
Applications:
Definition: PSPACE is the class of decision problems that can be solved using a polynomial
amount of memory (space). It is a more general class than NP, focusing on memory usage
rather than time. A problem belongs to PSPACE if there is an algorithm that solves the
problem in polynomial space, even if it may require exponential time.
∃x1∀x2∃x3…\exists x_1 \forall x_2 \exists x_3 \ldots, where x1,x2,x3x_1, x_2, x_3
Problem: Given a Boolean formula with quantifiers (i.e., a formula of the form
Time Complexity:
The time complexity for PSPACE algorithms can be exponential (in terms of the
number of steps), but the space complexity is constrained to be polynomial.
Space Complexity:
PSPACE problems use a polynomial amount of space, meaning that the amount of
memory used during computation does not grow exponentially with the input size.
This contrasts with EXPTIME problems, which require exponential time and space.
PSPACE Completeness:
Conclusion:
1. Approximation Algorithms provide solutions that are close to optimal for problems
that are computationally intractable to solve exactly. They often rely on greedy or
dynamic programming strategies with a known approximation ratio.
3. PSPACE covers problems that require polynomial space, but their time complexity
may still be exponential. Problems like QBF are examples of PSPACE-complete
problems, which are the hardest problems in this class. PSPACE allows for space-
efficient computation, even if the time taken may be very large.
2. Explain time and space complexity. What is time-space trade-off? Give examples.
3. Differentiate between best case, worst case, and average case complexity with
examples.
o Substitution Method
o Recursion Tree Method
o Master’s Theorem
o T(n)=2T(n/2)+nT(n) = 2T(n/2) + n
o T(n)=3T(n/4)+nT(n) = 3T(n/4) + n
2. Describe Greedy strategy. Illustrate with a problem like activity selection or coin
change.
6. Explain Branch and Bound strategy with the example of 0/1 Knapsack or TSP.
7. Illustrate how different strategies can be used to solve the same problem (e.g., TSP
using Greedy, DP, and Branch and Bound).
9. Explain Bin Packing Problem and the approximation strategies to solve it.
1. Explain BFS and DFS traversal algorithms. Write their applications and compare
them.
1. What is the meaning of tractable and intractable problems? Explain with examples.
6. List and describe standard NP-complete problems (e.g., SAT, Clique, Vertex Cover).
1. What are Approximation Algorithms? Explain with the example of Vertex Cover or
TSP.
3. What are Randomized Algorithms? Differentiate between Las Vegas and Monte
Carlo algorithms.
5. What is the Miller-Rabin Primality Test? Explain its working and error probability.
7. Explain the Quantified Boolean Formula (QBF) problem and its significance in
PSPACE.