Docsity

Basic asymptotic analysis, Exercises of Advanced Algorithms

Toggle sidebar
bg1
Tutorial Problems
O(1)
•O(1) describes an algorithm that will always execute in the same time (or
space) regardless of the size of the input data set.
1. Accessing Array Index (int a = ARR[5];)
O(1): Accessing an element in an array (i.e. int i = a[9])
2. Inserting a node in Linked List at the beginning
3. Pushing and Poping on Stack
4. Insertion and Removal from Queue
5. Finding out the parent or left/right child of a node in a tree stored in Array
6. Jumping to Next/Previous element in Doubly Linked List
O (log n) time
1. Binary Search
2. Finding largest/smallest number in a binary search tree
3. Calculating Fibonacci Numbers - Best Method
The basic part here is NOT using the complete data, and reducing the problem side
in every iteration
O(n) time
•O(N) describes an algorithm whose performance will grow linearly and in
direct proportion to the size of the input data set..
Traversing an array
2. Traversing a linked list
3. Linear Search
4. Deletion of a specific element in a Linked List (Not sorted)
5. Comparing two strings
6. Checking for Palindrome
7. Counting/Bucket Sort
And here too you can find a million more such examples..... In a nutshell, all Brute
Force Algorithms that requires linearity, are based on O(n) time complexity
O(nlogn) time
pf3
pf4
pf5

Partial preview of the text

Download Basic asymptotic analysis and more Exercises Advanced Algorithms in PDF only on Docsity!

Tutorial Problems

O(1)

  • O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set.
  1. Accessing Array Index (int a = ARR[5];) O(1): Accessing an element in an array (i.e. int i = a[9])
  2. Inserting a node in Linked List at the beginning
  3. Pushing and Poping on Stack
  4. Insertion and Removal from Queue
  5. Finding out the parent or left/right child of a node in a tree stored in Array
  6. Jumping to Next/Previous element in Doubly Linked List

O (log n) time

  1. Binary Search
  2. Finding largest/smallest number in a binary search tree
  3. Calculating Fibonacci Numbers - Best Method

The basic part here is NOT using the complete data, and reducing the problem side in every iteration

O(n) time

  • O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set.. Traversing an array
  1. Traversing a linked list
  2. Linear Search
  3. Deletion of a specific element in a Linked List (Not sorted)
  4. Comparing two strings
  5. Checking for Palindrome
  6. Counting/Bucket Sort

And here too you can find a million more such examples..... In a nutshell, all Brute Force Algorithms that requires linearity, are based on O(n) time complexity

O(nlogn) time

O(n log n): quick or mergesort (On average) Merge Sort Heap Sort Quick Sort Certain Divide and Conquer Algorithms based on optimizing O(n^2) algorithms

O(n^2 ) time

  • O(N^2 ) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(N^3 ), O(N^4 ) etc. 1. Bubble Sort 2. Insertion Sort 3. Selection Sort 4. Traversing a simple 2D array
  • O(2N^ ) denotes an algorithm whose growth will double with each additional element in the input data set. The execution time of an O(2 N) function will quickly become very large.

Q.No 1

For each of the following algorithms, indicate i. a natural size metric for its inputs; ii. Its basic operation; iii. Whether the basic operation count can be different for inputs of the same size: a. computing the sum of n numbers b. computing n! c. finding the largest element in a list of n numbers d. Euclid’s algorithm

Solution: The answers are as follows. a. (i) n; (ii) addition of two numbers; (iii) no

b. (i) the magnitude of n, i.e., the number of bits in its binary representation; (ii) Multiplication of two integers; (iii) No

  • Contains : This method involves looking at each item in the list in turn to see if it is equal to the given item. In the worst case (the given item is at the end of the list or is not in the list at all), this takes time proportional to the size of the list.
  • Get : This method checks for a bad position and either throws an exception or returns the value in the given position in the array. In either case it is independent of the size of the list and so it is a constant-time operation.

Q.No 3

What is the worst-case complexity of the each of the following code fragments?

  1. Two loops in a row: for (i = 0; i < N; i++) { sequence of statements } for (j = 0; j < M; j++) { sequence of statements } How would the complexity change if the second loop went to N instead of M?
  2. for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { Sequence of statements } } for (k = 0; k < N; k++) { Sequence of statements }
  3. for (i = 0; i < N; i++) { for (j = N; j > i; j--) { Sequence of statements } }

Solution:

  1. The first loop is O (N) and the second loop is O (M). Since you don't know which is bigger, you say this is O (N+M) or O(max(N,M)). In the case where the second loop goes to N instead of M the complexity is O(N).
  2. The first set of nested loops is O (N^2 ) and the second loop is O(N). This is O (max(N^2 ,N)) which is O(N^2 ).
  3. This is very similar to our earlier example of a nested loop where the number of iterations of the inner loop depends on the value of the index of the outer loop. It is still the case that the inner loop executes N times, then N-1, then N-2, etc, so the total number of times the innermost "sequence of statements" executes is O(N^2 ).
  4. The time complexity of the following C function is (assume n > 0)

a)

int recursive (mt n) { if (n == 1) return (1); else return (recursive (n-1) + recursive (n-1)); }