0% found this document useful (0 votes)
86 views4 pages

Algorithm Answers 10 Marks

The document discusses various algorithmic concepts including asymptotic analysis, P and NP classes, pattern matching methods like KMP, backtracking techniques, and sorting algorithms like quick sort. It also covers greedy algorithms, dynamic programming applications such as the 0/1 knapsack problem, and methods for analyzing recurrences. Additionally, it differentiates between greedy and dynamic programming approaches, providing examples and explanations for each concept.

Uploaded by

yashpandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topics covered

  • 0/1 knapsack,
  • Algorithm applications,
  • Best case,
  • NP class,
  • N-Queen problem,
  • NP complete,
  • Algorithm behavior,
  • Computational complexity,
  • Asymptotic analysis,
  • Data structures
0% found this document useful (0 votes)
86 views4 pages

Algorithm Answers 10 Marks

The document discusses various algorithmic concepts including asymptotic analysis, P and NP classes, pattern matching methods like KMP, backtracking techniques, and sorting algorithms like quick sort. It also covers greedy algorithms, dynamic programming applications such as the 0/1 knapsack problem, and methods for analyzing recurrences. Additionally, it differentiates between greedy and dynamic programming approaches, providing examples and explanations for each concept.

Uploaded by

yashpandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topics covered

  • 0/1 knapsack,
  • Algorithm applications,
  • Best case,
  • NP class,
  • N-Queen problem,
  • NP complete,
  • Algorithm behavior,
  • Computational complexity,
  • Asymptotic analysis,
  • Data structures

Q1) What is Asymptotic analysis and define big O, big Omega and Theta notation?

Asymptotic analysis is used to describe the behavior of algorithms as input size approaches infinity.

It helps compare algorithm efficiency regardless of machine or implementation. Big O (O) describes

the upper bound or worst-case scenario, ensuring an algorithm doesn't perform worse than O(f(n)).

Big Omega (Omega) represents the lower bound or best-case scenario. Theta (Theta) describes the

tight bound, where the algorithm runs in both O(f(n)) and Omega(f(n)). Example: For linear search,

time complexity is O(n), Omega(1), and Theta(n).

Q2) Define P class, NP class, NP hard, NP complete.

P class: Problems that can be solved in polynomial time. NP class: Problems for which a solution

can be verified in polynomial time. NP-Hard: As hard as any problem in NP; not necessarily in NP.

NP-Complete: Problems that are both in NP and NP-Hard. If any NP-Complete problem can be

solved in polynomial time, then P = NP.

Q3) What is Knuth Morris Pratt Method of pattern matching? Give examples.

KMP algorithm avoids redundant comparisons by preprocessing the pattern. It builds a prefix table

(LPS array) to shift pattern intelligently. It runs in O(n + m) time where n is text length, m is pattern

length. Example: Pattern = 'ABABC', Text = 'ABABABC'. KMP skips repeated checks using LPS

values.

Q4) Explain Backtracking with n-p queen problem.

Backtracking is a recursive problem-solving method where solutions are built step-by-step. For the

N-Queen problem, it places one queen per row and checks for safe positions. If no safe position is

found, it backtracks. The algorithm ensures no two queens threaten each other on a NxN

chessboard.

Q5) Explain Naive string machine algorithm with examples.

The Naive algorithm compares the pattern with all substrings of the text. It checks for a match

starting at every character in the text, making it O(mn) in time, where m is pattern length and n is

text length. Example: Searching 'AB' in 'AABAB' compares starting at positions 0 to 3.

Q6) Explain binary search algorithm.


Binary search finds the target element in a sorted array by repeatedly dividing the search range in

half. Start with mid = (low + high)/2. If key == mid, return index. If key < mid, search left half; else

right half. Time complexity is O(log n). Only works on sorted data.

Q7) Explain Best case, Average case, and worst case.

Best case: Minimum operations (e.g., first element match in linear search = O(1)). Average case:

Expected time over all inputs (linear search = O(n/2)). Worst case: Maximum operations (e.g., key

not found in linear search = O(n)). Used for guaranteed performance.

Q8) What is greedy algorithm?

A greedy algorithm builds a solution step by step by choosing the best available option at each

stage. It does not reconsider choices once made. Used in problems like Kruskals algorithm, Prims

algorithm, and fractional knapsack. Greedy is fast but may not give optimal solution for all problems.

Q9) Explain quick sort algorithm with examples.

Quick sort is a divide-and-conquer algorithm that picks a pivot, partitions the array around it, and

recursively sorts the subarrays. Average time complexity: O(n log n); Worst-case: O(n2) when pivot

is poorly chosen. Example: For array [3, 7, 2, 5], pivot=3: [2], [3], [7, 5] sort left and right recursively.

Q10) Explain multistage graph with example.

A multistage graph is a directed graph where nodes are grouped into stages and edges move from

one stage to the next. Its used to find shortest paths in staged problems. Example: Stages represent

time steps; edges represent allowed transitions with cost. Dynamic programming is used to find

minimum cost from source to destination.

Q11) What is greedy algorithm?

A greedy algorithm chooses the best possible option at each step. It aims for a global optimum

through local choices. Used in problems like activity selection, job sequencing, and Dijkstras

shortest path.

Q12) Write an abstract algorithm for greedy design method.

1. Initialize an empty solution set.

2. While a feasible option exists:


a. Choose the best candidate using a greedy criterion.

b. Add to solution if it maintains feasibility.

3. Return the final solution.

Q13) Explain 0/1 knapsack problem using dynamic programming.

Given weights and values of items, choose items to maximize value under a weight limit. Each item

can be selected once or not at all. DP Table: dp[i][w] = max value using first i items and capacity w.

Recurrence: dp[i][w] = max(dp[i-1][w], value[i] + dp[i-1][w-weight[i]]). Time: O(nW).

Q14) Write an algorithm to find the minimum and maximum value using divide and conquer and also

derive its complexity.

Algorithm:

1. Divide array into halves.

2. Find min and max in each half recursively.

3. Compare results to find overall min and max.

Time Complexity: T(n) = 2T(n/2) + 2 = O(n). More efficient than linear scan due to fewer

comparisons.

Q15) Explain recursion and various method to solve recurrences.

Recursion is a method where a function calls itself to solve subproblems. To analyze recursive

algorithms, solve recurrence relations. Methods: Substitution (guess and prove), Recursion Tree

(visualize calls), Master Theorem (applies to divide-and-conquer forms).

Q16) Explain Job Sequencing with deadline with an example

Each job has a deadline and profit. Goal: schedule jobs to maximize profit before deadlines.

Approach: Sort jobs by profit, use greedy method to place each job in the latest available slot before

its deadline. Example: Jobs [(100, 2), (19, 1), (27, 2)] select highest profit jobs fitting within time

slots.

Q17) Explain 15-puzzle problem using branch and bound strategy.

15-puzzle has a 4x4 board with tiles 1-15 and one empty space. Goal: arrange tiles in order. Branch

and Bound explores all possible moves but prunes paths that exceed current best solution. Uses
heuristic (e.g., number of misplaced tiles) to guide the search efficiently.

Q18) Differentiate between the following:

i) greedy knapsack and 0/1 knapsack.

ii) divide and conquer approach and dynamic programming.

i) Greedy knapsack selects items based on value/weight ratio, works only for fractional. 0/1

knapsack uses DP to choose or reject whole items.

ii) Divide & conquer solves problems independently (e.g., merge sort), while DP stores solutions to

overlapping subproblems (e.g., Fibonacci).

You might also like