0% found this document useful (0 votes)
23 views28 pages

ADA 1st IA Question Bank

The document provides a comprehensive question bank on algorithm design techniques, algorithm characteristics, and various algorithm analysis methods. It covers topics such as iterative and recursive algorithms, combinatorial problems, brute force methods, and empirical vs mathematical analysis. Additionally, it includes detailed explanations of specific algorithms like Euclid's GCD algorithm and the Traveling Salesman Problem, along with their time and space complexities.

Uploaded by

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

ADA 1st IA Question Bank

The document provides a comprehensive question bank on algorithm design techniques, algorithm characteristics, and various algorithm analysis methods. It covers topics such as iterative and recursive algorithms, combinatorial problems, brute force methods, and empirical vs mathematical analysis. Additionally, it includes detailed explanations of specific algorithms like Euclid's GCD algorithm and the Traveling Salesman Problem, along with their time and space complexities.

Uploaded by

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

ADA 1st IA Question Bank - Complete Solutions

2 Marks Questions - Refined Answers

1. What is Algorithm Design Technique? Give Example


An algorithm design technique is a general strategy for solving a class of problems. It
provides a framework for developing algorithms. Example:

 Divide and Conquer – This technique divides a problem into smaller sub-problems,
solves them recursively, and combines the results. Example: Merge Sort.

2. Define an Algorithm and explain its characteristics


An algorithm is a finite sequence of well-defined steps that provide a solution for a
specific problem. Characteristics include:

 Finiteness: The algorithm must terminate after a finite number of steps.

 Definiteness: Each step must be clearly and unambiguously defined.

 Input: Zero or more inputs are externally supplied.

 Output: At least one output is produced.

 Effectiveness: All operations must be basic enough to be performed exactly and in finite
time.

3. What is Iterative and Recursive Algorithms

 Iterative Algorithm: Uses loops to repeat operations until a condition is met. Example:
Loop-based factorial.

 Recursive Algorithm: Calls itself with a subset of the original problem. Example:
Recursive factorial.

4. Explain Combinatorial problem types with example


Combinatorial problems involve finding an optimal object from a finite set of objects.
These problems often include permutations and combinations. Example: Traveling
Salesman Problem (TSP) – find the shortest route that visits each city once and returns
to the origin.

5. Explain order of growth of functions


Order of growth describes how the running time or space requirements of an algorithm
grow with input size. Expressed using Big O notation. Example:

 O(n): Linear growth


 O(n^2): Quadratic growth

6. Define Brute Force Approach with an example


Brute force is a straightforward approach that tries all possible solutions to find the
correct one. Example: Linear Search – search an element by checking every element
sequentially.

7. What is Decrease and Conquer Technique? Mention its Variations with examples
This technique solves a problem by solving a smaller version of the same problem and
extending the solution. Variations:

 Decrease by one: e.g., Insertion Sort

 Decrease by a constant factor: e.g., Binary Search

8. Explain different problem types with example

 Decision Problems: Answer is yes or no. (e.g., Is number prime?)

 Optimization Problems: Find the best solution among many. (e.g., Shortest path)

 Search Problems: Locate specific data. (e.g., Binary Search)

9. Write step count for addition of two matrices


For two n x n matrices, total operations: n × n = n² steps (each cell addition is one step).
So, step count is O(n²).

10. Write an algorithm for brute force string matching and explain with example
Algorithm:

BruteForceMatch(T, P):

n = length of text T

m = length of pattern P

for i from 0 to n - m:

j=0

while j < m and T[i+j] == P[j]:

j++

if j == m:

return i // match found

return -1 // no match
Example: Text = "abcdabc", Pattern = "abc" → Match found at index 0 and 4. Time Complexity:
O(nm)

11. Step count for multiplication of two matrices


For multiplying two n x n matrices: Each element of result matrix requires n
multiplications and (n - 1) additions → total = n³ multiplications and n²(n-1) additions →
O(n³) operations.

12. Best Case / Average Case / Worst Case Efficiencies

 Best Case: Minimum time required (e.g., Linear Search finding item at first index → O(1))

 Average Case: Expected time over all inputs (e.g., O(n/2))

 Worst Case: Maximum time taken for any input (e.g., O(n) if item is last)

13. Explain Analysis Framework


It is the structure used for analyzing the performance of algorithms. Includes:

 Input size

 Basic operations

 Number of operations

 Time and Space complexity

14. Explain Time Complexity with example. Units of measuring runtime


Time complexity measures the total number of basic operations executed. Example:

 Linear Search → O(n) Units: Measured in terms of steps/operations, not actual seconds.

15. Explain Space Complexity with example


Space complexity is the amount of memory used by an algorithm. Example:

 To store an array of n elements: O(n) space.

16. Explain Big O / Big Theta / Big Omega

 Big O (O): Worst case upper bound

 Big Theta (Θ): Tight bound (average case)

 Big Omega (Ω): Best case lower bound

17. What is Mathematical Analysis of Algorithm


It uses mathematical formulas and models to predict algorithm performance without
actual execution.
18. What is Empirical Analysis of Algorithm
This involves running the algorithm on real inputs and measuring time and space usage
experimentally.

19. Differentiate between BFS and DFS

 BFS: Breadth-First Search, uses Queue, visits level-wise.

 DFS: Depth-First Search, uses Stack/Recursion, explores depth-wise.

20. Advantages and Disadvantages of Exhaustive Search

 Advantages: Simple, guarantees solution.

 Disadvantages: Not scalable, very slow for large input.

21. Advantages and Disadvantages of Brute Force Method

 Advantages: Easy to understand and implement.

 Disadvantages: Inefficient, slow for large inputs.

22. What is Decrease by a constant? Give an example


It reduces the problem size by a constant factor. Example: Binary Search reduces search
interval by half.

23. Advantages and Disadvantages of Decrease and Conquer

 Advantages: Efficient, often reduces problem complexity.

 Disadvantages: May require complex logic or recursion.

24. Different Methods of Graph Traversal

 BFS (Breadth First Search)

 DFS (Depth First Search)

ADA 1st IA Question Bank - Complete Solutions

2 Marks Questions - Refined Answers

(Already Completed)

4/5 Marks Questions - Expanded and Detailed Answers

1. Explain Basic Efficiency Classes with Example


Efficiency classes categorize algorithms based on how their resource consumption
(usually time) grows with the input size. These classes are essential for understanding
how scalable an algorithm is. These are represented by asymptotic notations like Big O.
Here are some standard classes:

 Constant Time (O(1)): The time remains constant regardless of input size. Example:
Accessing an element in an array using an index.

 Logarithmic Time (O(log n)): The number of operations grows logarithmically with input
size. Example: Binary Search, which repeatedly halves the search space.

 Linear Time (O(n)): Time increases linearly with input size. Example: Linear Search,
where each element is checked one by one.

 Linearithmic Time (O(n log n)): Common in efficient sorting algorithms like Merge Sort
and Heap Sort. It represents a mix of linear and logarithmic growth.

 Quadratic Time (O(n²)): Time increases with the square of the input size. Typical in
algorithms with nested loops like Bubble Sort and Selection Sort.

 Exponential Time (O(2^n)): Time doubles with each additional input element. Seen in
algorithms like recursive solution to the Traveling Salesman Problem. Understanding
these classes helps predict performance and choose the most suitable algorithm for
large data sets.

2. Explain Empirical Analysis of Linear Search and discuss the difference between
Mathematical and Empirical Analysis
Empirical Analysis involves running the algorithm with real input data and recording the
actual performance metrics like execution time and memory usage. To empirically
analyze linear search:

 Implement the algorithm in a programming language.

 Use test data sets of varying sizes (e.g., 1000, 10000, 100000 elements).

 Measure and record the time taken to search for an element in each case.

 Plot the results to observe the trend.

Mathematical Analysis is a theoretical approach where you count the number of


operations without actually running the algorithm. For linear search:

 In the worst case, every element is checked → O(n)

 In the best case, the first element is the target → O(1)

Differences:
 Empirical Analysis is affected by factors like hardware, compiler, and programming
language, while Mathematical Analysis is platform-independent.

 Empirical Analysis gives actual run-time values; Mathematical Analysis gives asymptotic
complexity.

 Empirical is useful for understanding real-world performance; Mathematical is helpful


for algorithm comparison in theory.

3. Define Algorithm and explain fundamentals of Algorithm problem solving


An algorithm is a finite and ordered set of instructions that performs a task or solves a
specific problem. It is precise, unambiguous, and must eventually terminate.
Fundamentals of algorithmic problem solving include:

 Understanding the problem: Clarify input/output requirements.

 Formulating a model: Translate the problem into a mathematical model.

 Designing the algorithm: Choose an approach (brute force, divide and conquer, greedy,
etc.).

 Proving correctness: Ensure the algorithm works for all valid inputs.

 Analyzing complexity: Evaluate time and space requirements.

 Implementation: Convert the algorithm to a working program.

 Testing: Run on various inputs to validate correctness and performance. This step-by-
step strategy helps systematically build robust and efficient algorithms.

4. Write Euclid's Algorithm to find GCD of two numbers and explain with example
Euclid’s algorithm is based on the principle that the GCD of two numbers a and b (a > b)
is the same as the GCD of b and a % b. Algorithm:

EuclidGCD(a, b):

while b ≠ 0:

temp = b

b=a%b

a = temp

return a

Example: Find GCD(48, 18):


 Step 1: 48 % 18 = 12 → new a = 18, b = 12

 Step 2: 18 % 12 = 6 → new a = 12, b = 6

 Step 3: 12 % 6 = 0 → b becomes 0 → return a = 6 So, GCD of 48 and 18 is 6. Time


Complexity: O(log(min(a,b))) due to the division step.

5. Explain space and time complexity and find the S(p) and T(n) of an algorithm to find
the sum of elements in an array
Time Complexity (T(n)) refers to the number of basic operations as a function of input
size. Space Complexity (S(p)) is the amount of memory used by an algorithm. Algorithm:

SumArray(A, n):

sum = 0

for i = 0 to n-1:

sum = sum + A[i]

return sum

 Time Complexity T(n): O(n) because the loop runs n times.

 Space Complexity S(p): O(1) as only a few variables (sum, i) are used regardless of input
size. The algorithm is efficient in both time and space, ideal for real-time systems and
memory-constrained environments.

ADA 1st IA Question Bank - Complete Solutions

2 Marks Questions - Refined Answers

(Already Completed)

4/5 Marks Questions - Expanded and Detailed Answers

(Questions 1 to 5 already updated with detailed explanations)

6. What is Exhaustive Search? Explain with TSP Algorithm and realize its time and space
complexity
Exhaustive Search is a brute-force approach that tries all possible solutions to find the
correct or optimal one. It is guaranteed to work but is computationally expensive,
especially for large inputs.
Traveling Salesman Problem (TSP): Given a list of cities and the distances between each
pair, the TSP asks for the shortest possible route that visits each city exactly once and
returns to the origin city.

Exhaustive Search for TSP:

 Generate all permutations of the cities.

 Calculate the total distance for each permutation.

 Return the permutation with the minimum distance.

Example: For 4 cities A, B, C, D, there are (4-1)! = 6 possible paths (excluding reverse
directions). For each, calculate the tour cost and select the minimum.

Time Complexity: O(n!) – grows factorially with the number of cities. Space Complexity:
O(n) for recursive stack or path storage.

Advantages:

 Always finds the optimal solution.

 Simple and straightforward to implement.

Disadvantages:

 Impractical for large inputs due to explosive time growth.

7. Explain Asymptotic notations, its types with example and visualization


Asymptotic notations describe the running time or space usage of an algorithm as a
function of input size, especially for large inputs. These notations help in understanding
the scalability of an algorithm.

Types:

 Big O (O): Upper bound – represents worst-case time complexity. Example: O(n^2) for
Bubble Sort.

 Big Omega (Ω): Lower bound – represents best-case time complexity. Example: Ω(n) for
Bubble Sort (when the array is already sorted).

 Big Theta (Θ): Tight bound – represents average-case time complexity. Example: Θ(n log
n) for Merge Sort.

Visualization:

 On a graph with input size (n) on the x-axis and time on the y-axis:
o Big O shows the upper limit line

o Big Ω shows the lower limit

o Big Θ lies between the two and hugs the actual performance curve

These notations are essential for comparing algorithms in a platform-independent


manner.

8. Define Algorithm and explain its characteristics


An algorithm is a precise, unambiguous, step-by-step procedure to solve a problem or
perform a computation. It must be finite, effective, and result in an output.

Characteristics of a good algorithm:

 Input: Accepts zero or more inputs.

 Output: Produces at least one output.

 Finiteness: Must terminate after a finite number of steps.

 Definiteness: Every instruction is clear and unambiguous.

 Effectiveness: All operations are basic enough to be carried out precisely.

 Correctness: Should solve the intended problem correctly.

Understanding these traits helps in writing better and more efficient algorithms.

9. General plan for Mathematical Analysis of Recursive Algorithm


The analysis of recursive algorithms involves determining a recurrence relation and
solving it.

Steps:

1. Set up a recurrence based on the algorithm structure.

2. Estimate cost of each step.

3. Solve the recurrence using:

o Substitution Method

o Recursion Tree

o Master Theorem

Example: Merge Sort has the recurrence: T(n) = 2T(n/2) + O(n) → Using Master
Theorem: T(n) = O(n log n)
This method helps determine how recursive calls grow and how they affect total time.

10. General plan for Mathematical Analysis of Non-recursive Algorithm


For non-recursive algorithms, the steps include:

11. Identify loops and nested loops.

12. Count the number of basic operations in each loop.

13. Use summation formulas where applicable.

14. Simplify the expression to get the time complexity.

Example: Bubble Sort:

 Outer loop runs n times

 Inner loop runs (n-i-1) times → ∑(n-1) to 1 = O(n²)

Hence, Bubble Sort has time complexity O(n²).

11. General plan for Empirical Analysis with example


Empirical analysis involves performance testing through implementation.

Steps:

 Implement algorithm in code.

 Prepare test data with varying input sizes.

 Use system timer or profiler to record execution time.

 Plot input size vs execution time.

Example: Test linear search on arrays of size 10, 100, 1000, etc., and observe how time
increases.

This helps identify performance trends and potential bottlenecks.

12. Compare Empirical Analysis and Mathematical Analysis


| Feature | Empirical Analysis | Mathematical Analysis |
|----------------------|-----------------------------|------------------------------| | Based on |
Experimentation | Mathematical modeling | | Accuracy | Varies with environment |
Consistent | | Platform dependency | Yes | No | | Speed of evaluation | Slower | Faster
| | Use case | Real-world performance | Theoretical efficiency |

Each approach has its strengths; combining both provides a complete performance
picture.
13. Advantages and Disadvantages of Empirical Analysis
Advantages:

 Captures real-world performance

 Accounts for hidden constants and system behavior

 Useful for practical decision-making

Disadvantages:

 Platform-dependent results

 Time-consuming to implement

 Cannot predict behavior for all inputs

14. Solve problems discussed in class related to time complexity (operation method / step
count / step per execution / Asymptotic notation / limit for comparing)
Example: Find the largest element in an array

MaxElement(A, n):

max = A[0]

for i = 1 to n-1:

if A[i] > max:

max = A[i]

return max

Operation method: 1 initialization + (n - 1) comparisons → Total operations = O(n) Step

per iteration Asymptotic notation: T(n) = O(n) Limit method: lim(n→∞) (n/n) = 1 ⇒ T(n)
count: For loop runs n-1 times Step per execution: One comparison each loop → O(1)

∈ Θ(n)

15. Write an Algorithm to perform Bubble Sort and express its complexities

BubbleSort(A, n):

for i = 0 to n-1:

for j = 0 to n-i-2:

if A[j] > A[j+1]:

swap A[j], A[j+1]


Best Case: O(n) if optimized with a flag (when the array is already sorted) Average and
Worst Case: O(n²) due to nested loops Space Complexity: O(1) as sorting is in-place
Stability: Bubble sort is stable and easy to implement, though inefficient for large
datasets

(Next: 8 Marks Questions – Will be continued in the following section)

ADA 1st IA Question Bank - Complete Solutions

2 Marks Questions - Refined Answers

(Already Completed)

4/5 Marks Questions - Expanded and Detailed Answers

(Already Completed)

8 Marks Questions - Detailed Answers

1. Write an Algorithm and realize a program to sort N elements using Insertion Sort and
also perform its time and space analysis.

Algorithm: Insertion Sort

InsertionSort(A, n):

for i = 1 to n-1:

key = A[i]

j=i-1

while j >= 0 and A[j] > key:

A[j + 1] = A[j]

j=j-1

A[j + 1] = key

Explanation:

 The algorithm treats the first element as sorted. Then it picks the next element and
inserts it into the correct position within the sorted part by shifting elements that are
greater than the key.
 This process is repeated until all elements are placed in order.

Dry Run Example: Let us sort the array [5, 2, 4, 6, 1, 3].

 i = 1, key = 2 → compare with 5, shift 5 → insert 2 before 5 → [2, 5, 4, 6, 1, 3]

 i = 2, key = 4 → compare with 5, shift 5 → compare with 2, insert after 2 → [2, 4, 5, 6, 1,


3]

 i = 3, key = 6 → no shifting needed → [2, 4, 5, 6, 1, 3]

 i = 4, key = 1 → shift 6, 5, 4, 2 → insert 1 at beginning → [1, 2, 4, 5, 6, 3]

 i = 5, key = 3 → shift 6, 5, 4 → insert 3 after 2 → [1, 2, 3, 4, 5, 6]

Final Sorted Array: [1, 2, 3, 4, 5, 6]

Time Complexity Analysis:

 Best Case: O(n) → when the array is already sorted; only comparisons are made, no
shifts.

 Worst Case: O(n²) → when the array is sorted in reverse order; maximum number of
shifts.

 Average Case: O(n²) → depends on the arrangement of elements.

Space Complexity:

 O(1) – Since insertion sort is done in-place, no extra memory is required except a few
variables.

Stability: Yes. It does not change the relative order of equal elements.

Adaptiveness: Efficient when array is nearly sorted. Can be made adaptive by checking if
any shift occurred.

Use Cases:

 Small datasets

 When the list is almost sorted

 Applications that require a stable sort

Advantages:

 Simple to understand and implement

 No additional memory needed


 Performs well for nearly sorted lists

Disadvantages:

 Inefficient for large lists due to O(n²) time complexity

Program in Python:

arr = [5, 2, 4, 6, 1, 3]

print("Original array:", arr)

for i in range(1, len(arr)):

key = arr[i]

j=i-1

while j >= 0 and key < arr[j]:

arr[j + 1] = arr[j]

j -= 1

arr[j + 1] = key

print("Sorted array:", arr)

Output:

Original array: [5, 2, 4, 6, 1, 3]

Sorted array: [1, 2, 3, 4, 5, 6]

Graphical Visualization: A visual plot of time vs input size would show a linear curve for
best case and a parabolic curve for worst case.

2. Explain The Tower of Hanoi problem with an Algorithm, realize a program using
recursion? Explain time complexity using mathematical analysis.

Problem Statement: Tower of Hanoi is a classic recursive problem where you have three pegs
and n disks of different sizes. The objective is to move all the disks from the source peg to the
destination peg using an auxiliary peg, following these rules:

 Only one disk can be moved at a time.

 A disk can only be placed on top of a larger disk or an empty peg.

Algorithm:
TowerOfHanoi(n, source, auxiliary, destination):

if n == 1:

print("Move disk 1 from", source, "to", destination)

else:

TowerOfHanoi(n-1, source, destination, auxiliary)

print("Move disk", n, "from", source, "to", destination)

TowerOfHanoi(n-1, auxiliary, source, destination)

Explanation:

 The recursive idea is to move n-1 disks to the auxiliary peg, move the nth disk to the
destination peg, and finally move the n-1 disks from the auxiliary peg to the destination
peg.

Dry Run for n = 3:

Move disk 1 from A to C

Move disk 2 from A to B

Move disk 1 from C to B

Move disk 3 from A to C

Move disk 1 from B to A

Move disk 2 from B to C

Move disk 1 from A to C

Python Program:

def tower_of_hanoi(n, source, auxiliary, destination):

if n == 1:

print(f"Move disk 1 from {source} to {destination}")

return

tower_of_hanoi(n - 1, source, destination, auxiliary)

print(f"Move disk {n} from {source} to {destination}")

tower_of_hanoi(n - 1, auxiliary, source, destination)


# Example run

n=3

tower_of_hanoi(n, 'A', 'B', 'C')

Time Complexity (Mathematical Analysis): Let T(n) be the number of moves for n disks. The
recurrence relation is:

T(n) = 2T(n - 1) + 1

Solving:

 T(1) = 1

 T(2) = 3

 T(3) = 7

 ...

 T(n) = 2^n - 1

So, the time complexity is O(2^n), exponential time.

Space Complexity:

 O(n) – due to recursive call stack

Applications:

 Teaching recursion

 Understanding divide and conquer

 Simulating problem-solving under constraints

Visualization: Can be visualized using animation or diagram of peg and disk movement.

Conclusion: Tower of Hanoi demonstrates the power of recursion. Despite being simple in
concept, the number of operations grows exponentially, making it a great example to study
recursive growth and performance trade-offs.

3. Write an Algorithm to perform Matrix multiplication and


a. Realize the space complexity
b. Realize Time complexity using different methods (operation method, step per
execution method)
Problem Statement: Matrix multiplication involves computing the product of two matrices A
and B. For two matrices A of size m×n and B of size n×p, the result will be a matrix C of size m×p.

Algorithm:

MatrixMultiply(A, B, m, n, p):

for i in range(0, m):

for j in range(0, p):

C[i][j] = 0

for k in range(0, n):

C[i][j] += A[i][k] * B[k][j]

Explanation: Each element C[i][j] is computed as the dot product of the ith row of A and the jth
column of B. This requires multiplying and summing n elements.

Dry Run Example: Let A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]]

 C[0][0] = 1×5 + 2×7 = 19

 C[0][1] = 1×6 + 2×8 = 22

 C[1][0] = 3×5 + 4×7 = 43

 C[1][1] = 3×6 + 4×8 = 50 Resulting matrix C = [[19, 22], [43, 50]]

a. Space Complexity Analysis:

 Output matrix C is of size m×p → requires O(m×p) space

 No additional auxiliary arrays used → total space complexity = O(m×p)

 If m = n = p, then space complexity becomes O(n²)

b. Time Complexity Analysis: Operation Method:

 Outer loop (i) runs m times

 Middle loop (j) runs p times

 Inner loop (k) runs n times

 Total operations = m × p × n → Time Complexity: O(m × n × p)

 If m = n = p, then complexity = O(n³)

Step Per Execution Method:


 Each computation of C[i][j] involves n multiplications and (n - 1) additions

 For each i and j, total steps = 2n - 1

 Number of C[i][j] = m×p

 Total steps = (2n - 1) × m × p ≈ O(m × n × p)

Program in Python:

def matrix_multiply(A, B):

m, n = len(A), len(A[0])

p = len(B[0])

C = [[0 for _ in range(p)] for _ in range(m)]

for i in range(m):

for j in range(p):

for k in range(n):

C[i][j] += A[i][k] * B[k][j]

return C

A = [[1, 2], [3, 4]]

B = [[5, 6], [7, 8]]

result = matrix_multiply(A, B)

for row in result:

print(row)

Output:

[19, 22]

[43, 50]

Conclusion: Matrix multiplication is fundamental in scientific computing, graphics, and machine


learning. Though O(n³) is standard for naive approach, more efficient algorithms like Strassen's
exist with reduced time complexity. For large matrices, these advanced methods are preferable.
4. Explain BFS and DFS with example. Write Algorithms and discuss its complexity.

Breadth-First Search (BFS):

 Explanation: BFS explores all vertices at the current depth before moving to the next
depth level, using a queue. It’s suitable for finding the shortest path in unweighted
graphs.

 Example: In a graph with vertices {A, B, C, D} and edges {A-B, A-C, B-D}, BFS starting at A
visits A, B, C, then D.

Depth-First Search (DFS):

 Explanation: DFS explores as far as possible along a branch before backtracking, using a
stack (or recursion). It’s useful for topological sorting or detecting cycles.

 Example: In the same graph, DFS starting at A might visit A, B, D, then C.

BFS Algorithm:

Algorithm BFS(G, s)

// Input: Graph G (adjacency list), source vertex s

// Output: Vertices reachable from s in BFS order

Initialize queue Q

Initialize visited array as false

Enqueue s into Q

Mark s as visited

while Q is not empty do

v ← Dequeue(Q)

Process v

for each neighbor u of v do

if u is not visited then

Mark u as visited

Enqueue u into Q
DFS Algorithm (Recursive):

Algorithm DFS(G, v, visited)

// Input: Graph G (adjacency list), vertex v, visited array

// Output: Vertices reachable from v in DFS order

Mark v as visited

Process v

for each neighbor u of v do

if u is not visited then

DFS(G, u, visited)

Complexity Analysis:

1. BFS Complexity:

o Time Complexity:

 Adjacency List: O(V + E), where V is vertices, E is edges (each vertex and
edge processed once).

 Adjacency Matrix: O(V²) (scanning each row for neighbors).

o Space Complexity: O(V) for queue and visited array.

2. DFS Complexity:

o Time Complexity:

 Adjacency List: O(V + E) (similar reasoning as BFS).

 Adjacency Matrix: O(V²).

o Space Complexity: O(V) for visited array + O(V) for recursion stack = O(V).

Summary:

 BFS: Time = O(V + E) (list) or O(V²) (matrix); Space = O(V).

 DFS: Time = O(V + E) (list) or O(V²) (matrix); Space = O(V).

5. Explain the mathematical Analysis of an Algorithm, its general plan, and find the time
efficiency for finding the duplicate element in an array.

Mathematical Analysis of an Algorithm:


 Definition: Mathematical analysis involves deriving the time or space complexity of an
algorithm by counting operations or solving recurrence relations, independent of
empirical testing.

 General Plan:

1. Identify Key Operations: Determine the dominant operation (e.g., comparisons,


assignments).

2. Count Operations: Express the number of operations as a function of input size


n.

3. Establish Recurrence (if recursive): For recursive algorithms, set up and solve
recurrence relations.

4. Sum Operations: For iterative algorithms, sum operations across loops.

5. Simplify Using Asymptotic Notations: Express complexity as O, Θ, or Ω.

Problem: Find Duplicate Element in an Array

 Assumption: Array A of size n contains integers from 1 to n-1, with exactly one duplicate.

 Algorithm (Using Sum Method):

text

Copy

Algorithm FindDuplicate(A, n)

// Input: Array A of n elements with one duplicate

// Output: The duplicate element

expectedSum ← n * (n-1) / 2 // Sum of 1 to n-1

actualSum ← 0

for i ← 0 to n-1 do

actualSum ← actualSum + A[i]

return actualSum - expectedSum

Time Efficiency (Mathematical Analysis):

 Step 1: Key Operation: Addition in the loop (actualSum ← actualSum + A[i]).

 Step 2: Count Operations:


o Loop: n iterations, each with 1 addition.

o Total additions in loop: n.

o Other operations: Computing expectedSum (O(1)), final subtraction (O(1)).

 Step 3: Total Operations: n + O(1) ≈ n.

 Step 4: Asymptotic Notation: Time Complexity = O(n).

 Alternative Approach (Sorting): Sorting the array (O(n log n)) and checking adjacent
elements (O(n)) yields O(n log n), but the sum method is more efficient.

Summary: Time Complexity = O(n); Space Complexity = O(1).

6. What is Asymptotic Notations, its types, and prove:

a. Given f(n) = 20n³ - 5, prove that f(n) = O(n³)


b. Given f(n) = 6*2^n + n², prove that f(n) = O(2^n)
c. Given f(n) = 1/2 n² - 3n, prove that f(n) = Θ(n²)
d. Given f(n) = 20n³ - 3, prove that f(n) = O(n³) using limit for comparing order growth

Asymptotic Notations:

 Definition: Asymptotic notations describe the growth rate of an algorithm’s running time
as input size approaches infinity.

 Types:

1. Big O (O): Upper bound, f(n) = O(g(n)) if ∃ constants c, n₀ such that f(n) ≤ c·g(n)
for all n ≥ n₀.

2. Big Omega (Ω): Lower bound, f(n) = Ω(g(n)) if ∃ constants c, n₀ such that f(n) ≥
c·g(n) for all n ≥ n₀.

3. Big Theta (Θ): Tight bound, f(n) = Θ(g(n)) if f(n) = O(g(n)) and f(n) = Ω(g(n)).

Proofs:

a. f(n) = 20n³ - 5 = O(n³):

 Need: f(n) ≤ c·n³ for some c, n ≥ n₀.

 Compute: |20n³ - 5| ≤ 20n³ + 5 ≤ 20n³ + 5n³ (for n ≥ 1, 5 ≤ 5n³) = 25n³.

 Thus, f(n) ≤ 25n³ for c = 25, n₀ = 1.

 Conclusion: f(n) = O(n³).


b. f(n) = 6*2^n + n² = O(2^n):

 Need: f(n) ≤ c·2^n for some c, n ≥ n₀.

 Compare terms: n² ≤ 2^n for large n (e.g., n ≥ 10, since 2^10 = 1024 > 100 = 10²).

 Estimate: f(n) = 62^n + n² ≤ 62^n + 2^n (for n ≥ 10) = 7*2^n.

 Thus, f(n) ≤ 7*2^n for c = 7, n₀ = 10.

 Conclusion: f(n) = O(2^n).

c. f(n) = 1/2 n² - 3n = Θ(n²):

 Upper Bound (O): f(n) = 1/2 n² - 3n ≤ 1/2 n² for n ≥ 1 (since -3n ≤ 0).

o Thus, f(n) ≤ 1/2 n², so f(n) = O(n²) with c = 1/2, n₀ = 1.

 Lower Bound (Ω): f(n) = 1/2 n² - 3n ≥ 1/4 n² for n ≥ 12 (since -3n ≥ -3n²/12 = -1/4 n²).

o Thus, f(n) ≥ 1/4 n², so f(n) = Ω(n²) with c = 1/4, n₀ = 12.

 Since f(n) = O(n²) and f(n) = Ω(n²), f(n) = Θ(n²).

 Conclusion: f(n) = Θ(n²).

d. f(n) = 20n³ - 3 = O(n³) using limit:

 Compute limit: lim (n→∞) f(n)/g(n) = lim (n→∞) (20n³ - 3)/n³ = lim (n→∞) (20 - 3/n³) =
20.

 Since the limit is a positive constant (20), f(n) = O(n³).

 Conclusion: f(n) = O(n³).

Summary: All proofs confirm the stated asymptotic bounds.

7. Write an Algorithm to perform Selection Sort, realize its program, and express its
complexities.

Algorithm for Selection Sort:

text

Copy

Algorithm SelectionSort(A, n)

// Input: Array A of n elements


// Output: Array A sorted in non-decreasing order

for i ← 0 to n-2 do

minIndex ← i

for j ← i+1 to n-1 do

if A[j] < A[minIndex] then

minIndex ← j

Swap A[i] with A[minIndex]

return A

Program (Pseudo-code realization):

text

Copy

Procedure SelectionSort(A, n)

for i = 0 to n-2

minIndex = i

for j = i+1 to n-1

if A[j] < A[minIndex]

minIndex = j

Swap(A[i], A[minIndex])

End

Complexity Analysis:

 Time Complexity:

o Outer loop: n-1 iterations.

o Inner loop: For i=0, n-1 comparisons; i=1, n-2 comparisons; ..., i=n-2, 1
comparison.

o Total comparisons: (n-1) + (n-2) + ... + 1 = n(n-1)/2 = O(n²).

o Swaps: At most n-1 (one per outer loop iteration) = O(n).


o Total: O(n²) (dominated by comparisons).

o Best/Average/Worst Case: O(n²) (inner loop always runs fully).

 Space Complexity:

o Only uses minIndex and loop variables: O(1) auxiliary space.

Summary: Time Complexity = O(n²); Space Complexity = O(1).

8. Explain the mathematical Analysis of an Algorithm, its general plan, and find the time
efficiency for matrix multiplication.

Mathematical Analysis of an Algorithm: (Repeated from Q5 for completeness)

 Definition: Derives complexity by counting operations or solving recurrences.

 General Plan:

1. Identify key operations.

2. Count operations as a function of input size.

3. Set up and solve recurrences (if recursive).

4. Sum operations across loops (if iterative).

5. Express in asymptotic notation.

Matrix Multiplication Algorithm: (From Q32)

text

Copy

Algorithm MatrixMultiplication(A, B, n)

Initialize C[n][n] with zeros

for i ← 0 to n-1 do

for j ← 0 to n-1 do

for k ← 0 to n-1 do

C[i][j] ← C[i][j] + A[i][k] * B[k][j]

return C

Time Efficiency (Mathematical Analysis):


 Key Operations: Multiplication and addition in C[i][j] ← C[i][j] + A[i][k] * B[k][j].

 Count Operations:

o Inner loop (k): n iterations, each with 1 multiplication + 1 addition = 2n


operations.

o Middle loop (j): n iterations × 2n = 2n² operations.

o Outer loop (i): n iterations × 2n² = 2n³ operations.

o Initialization of C: n² assignments = O(n²).

 Total Operations: 2n³ + O(n²) ≈ O(n³).

 Asymptotic Notation: Time Complexity = O(n³).

Summary: Time Complexity = O(n³); Space Complexity = O(n²).

9. Explain the mathematical Analysis of an Algorithm, its general plan, and find the time
efficiency to find the largest element in an Array.

Mathematical Analysis of an Algorithm: (As above)

 General Plan: Identify operations, count them, sum across iterations, express
asymptotically.

Algorithm to Find Largest Element:

text

Copy

Algorithm FindMax(A, n)

// Input: Array A of n elements

// Output: Largest element

max ← A[0]

for i ← 1 to n-1 do

if A[i] > max then

max ← A[i]

return max

Time Efficiency (Mathematical Analysis):


 Key Operation: Comparison (A[i] > max).

 Count Operations:

o Loop: n-1 iterations, each with 1 comparison.

o Total comparisons: n-1.

o Other operations: 1 initialization, up to n-1 assignments (when max updates) =


O(n).

 Total Operations: n-1 comparisons + O(n) assignments = O(n).

 Asymptotic Notation: Time Complexity = O(n).

Summary: Time Complexity = O(n); Space Complexity = O(1).

10. Explain the mathematical Analysis of an Algorithm, its general plan, and find the time
efficiency for finding Factorial of a given number.

Mathematical Analysis of an Algorithm: (As above)

 General Plan: Identify operations, count or solve recurrences, express asymptotically.

Algorithm for Factorial (Recursive):

text

Copy

Algorithm Factorial(n)

// Input: Non-negative integer n

// Output: n!

if n = 0 then

return 1

return n * Factorial(n-1)

Time Efficiency (Mathematical Analysis):

 Key Operation: Multiplication in n * Factorial(n-1).

 Recurrence Relation:

o T(n) = T(n-1) + 1 (1 multiplication + recursive call).


o T(0) = 1 (base case).

 Solve Recurrence:

o T(n) = T(n-1) + 1 = T(n-2) + 1 + 1 = ... = T(0) + n = 1 + n.

 Total Operations: n multiplications = O(n).

 Alternative (Iterative):

text

Copy

Algorithm FactorialIterative(n)

result ← 1

for i ← 1 to n do

result ← result * i

return result

 Loop: n iterations, each with 1 multiplication.

 Total: O(n).

Summary: Time Complexity = O(n); Space Complexity = O(n) (recursive, due to call stack) or O(1)
(iterative).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy