Data Structure: O notation

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Rules for determining Big O notation of composite functions.

Composite function Big O notation c * O(f(x)) O(f(x)) c + O(f(x)) O(f(x)) g(x) * O(f(x)) O(g(x)*O(f(x))) g(x) + O(f(x)) O(g(x) + O(f(x))

O(1)

Name: Constant FindMin(x, y) { if (x < y) { return x } else { return y } }

O(c^N)

Name: Exponential Fibonacci(N) { if ((1 == N) || (2 == N)) { return 1 } return Fibonacci(N-1) + fibonacci(N-2) }

O(N)

Name: Linear LinearSearch(numbers, N, key) { for (i = 0; i < N; ++i) { if (numbers[i] == key) { return i } } return -1 // not found }

O(N log N)

Name: Log-linear MergeSort(numbers, i, k) { j = 0 if (i < k) { j = (i + k) / 2 // Find midpoint MergeSort(numbers, i, j) // Sort left part MergeSort(numbers, j + 1, k) // Sort right part Merge(numbers, i, j, k) // Merge parts } }

O (log N)

Name: Logarithmic BinarySearch(numbers, N, key) { mid = 0; low = 0; high = 0; high = N - 1; while (high >= low) { mid = (high + low) / 2 if (numbers[mid] < key) { low = mid + 1 } else if (numbers[mid] > key) { high = mid - 1 } else { return mid } } return -1 // not found }

O(N^2)

Name: Quadratic SelectionSort(int numbers, N) { for (i = 0; i < numbersSize; ++i) { indexSmallest = i for (j = i + 1; j < numbersSize; ++j) { if (numbers[j] < numbers[indexSmallest]) { indexSmallest = j } } temp = numbers[i] numbers[i] = numbers[indexSmallest] numbers[indexSmallest] = temp }

Consider

One consideration in evaluating algorithms is that the efficiency of the algorithm is most critical for large input sizes. Small inputs are likely to result in fast running times because N is small, so efficiency is less of a concern. For large N, the difference in computation time varies greatly with the rate of growth of the function f. The data assumes that a single instruction takes 1 μs to execute.

3.1.2: Big O notation: 1. Which of the following Big O notations is equivalent to O(N+9999)? 2. Which of the following Big O notations is equivalent to O(734*N)? 3. Which of the following Big O notations is equivalent to O(12*N +6*N^3 + 1000)?

Solution: 1. For O(N+9999), N is the highest order term, yielding O(N) 2. Constants in product terms are omitted. For O(734*N), the constant 734 is omitted, yielding O(N). 3. O(12*N + 6*N3 + 1000) = O(6*N3) = O(N3)

3.1.3: Big O notation for composite functions. Determine the simplified Big O notation 1. 10 * O(N^2) 2. 0 + O(N^2) 3. 3*N * O(N^2) 4. 2*N^3 + O(N^2)

Solutions: 1. 10 * O(N^2) = O(10 * N^2) = O(N^2) 2. 10 + O(N^2) = O(10 + N^2) = O(N^2) 3. 3*N * O(N^2) = O(3*N*N^2) = O(3*N^3) = O(N^3) 4. 2*N^3 + O(N^2) = O(2*N^3 + Nv2) = O(N^3)

3.1.5: Big O notation and growth rates. 1. O(5) has a _____ runtime complexity. 2. O(N log N) has a _____ runtime complexity. 3. O(N + N2) has a _____ runtime complexity. 4.A linear search has a _____ runtime complexity. 5. A selection sort has a _____ runtime complexity.

Solutions: 1. Constant 5 does not change with the input size, so the runtime of O(5) remains constant. 2. log linear The first N indicates the runtime complexity is linear, then multiplying with log N results in a log-linear runtime complexity. 3. quadratic O(N + N2) reduces to O(N2). The highest power is 2, so the runtime complexity is quadratic. 4. O(N) A linear search may need to search all N elements within a list. 5. O(N^2) The total number of operations for selection sort is proportional to N * N.

Big O notation

is a mathematical way of describing how a function (running time of an algorithm) generally behaves in relation to the input size -all functions that have the same growth rate are considered equivalent in Big O notation. Given a function that describes the running time of an algorithm, the Big O notation for that function can be determined using the following rules: -If f(x) is a sum of several terms, the highest order term (the one with the fastest growth rate) is kept and others are discarded. -If f(x) has a term that is a product of several factors, all constants (those that are not in terms of x) are omitted.


Kaugnay na mga set ng pag-aaral

PrepU Ch. 13: Blended Competencies

View Set

ITIL 4 Foundation Practice Exam 5

View Set

Structures, Locations, and Functions of the Three Types of Muscular Tissue

View Set