Docsity

Computational Complexity: An Overview - Understanding Algorithm Efficiency - Prof. Edward , Study notes of Computer Architecture and Organization

Partial preview of the text

Download Computational Complexity: An Overview - Understanding Algorithm Efficiency - Prof. Edward and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Computational Complexity:

An Overview

Ed Bosworth Computer Science Department Columbus State University Columbus, GA [email protected]

BACKGROUND ASSUMED

Some programming experience, maybe in Visual Basic or C++ Have seen some algorithms for searching and sorting Algebra, including logarithms and exponents. Factorial function: N! = N  (N – 1) … N – 1) … ) …  2  1) …

Problems, Strategies, and Algorithms

Problem Sort a list by comparison, using a defined  operator Traveling salesman Strategy Divide and conquer Algorithm Bubble Sort, Quick Sort, Merge Sort, etc. Breadth first search, depth first search, etc. Programs Implementation of an algorithm in a specific language

Intractable Problems

Characteristic: Try every solution and see if one works. Classic problem: Tiling a floor with irregularly shaped tiles Like a jigsaw puzzle if you do it “upside-down”, except you know that the puzzle can be completed. Heuristics: For jigsaw puzzles you use a number of clues, such as colors to aid in putting the puzzle together. Heuristics do not guarantee an answer, but often produce one.

Problems, Strategies, and Algorithms

Sample Problems Sort a list by comparison, using a defined ≥ operator. Traveling Salesman – route through each city exactly once. Matrix addition and matrix multiplication Sample Strategies Brute Force Divide and Conquer Dynamic Programming Greedy Sample Algorithms Bubble Sort, Quick Sort, Merge Sort, etc. Breadth-First Search (N – 1) … BFS), Depth-First Search (N – 1) … DFS)

Time Complexity of Algorithms

Question: How to make the program run faster? Answers: Buy a bigger computer (N – 1) … Your boss has lots of money). Implement in a more efficient computer language. Devise a more efficient algorithm. We focus on developing a more efficient algorithm. Efficiency is measured in time complexity, which is different from total run time. Run time is dependent on the computer. This allows us to assess the algorithm independently of the computer and programming language implementation.

Bounds on Functions

The “Big-O” and “Big-’ notation for positive functions; f(N – 1) … n) > 0. f(N – 1) … n) is O(N – 1) … g(N – 1) … n)) if there exist positive integers K and N such that for all n  N, f(N – 1) … n)  Kg(N – 1) … n). f(N – 1) … n) is (N – 1) … g(N – 1) … n)) if there exist positive integers K and N such that for all n  N, f(N – 1) … n)  Kg(N – 1) … n). Typical functions for g(N – 1) … n) logarithmic: ln(N – 1) … n), log1) … 0(N – 1) … n), etc. polynomial: n 2 , n 3 , etc. nlog(N – 1) … n) is “polynomial” exponential: 2 n , e n , n!, etc.

CLAIM: The Number 1) … 00! Is “Astronomical”

Examples of Function Bounds

log1) … 0(N – 1) … n) is O(N – 1) … n) because for n > 1) … , log1) … 0(N – 1) … n)  n. log1) … 0(N – 1) … n) and ln(N – 1) … n) are the same order because log1) … 0(N – 1) … n) = log1) … 0(N – 1) … e)  ln(N – 1) … n) n 2

  • nlog(N – 1) … n) is O(N – 1) … n 2 ) because For n > 1) … , n 2
  • nlog(N – 1) … n)  2 n 2 , or Lim (^) n   (N – 1) … n 2
  • nlog(N – 1) … n)) / n 2 = 1) … , if you like calculus. Big issue: polynomial vs. exponential

Time Complexity of an Algorithm

An algorithm A is said to be O(N – 1) … f(N – 1) … n)) if the input size is a positive integer n the number of basic steps is T(N – 1) … n) T(N – 1) … n) is O(N – 1) … f(N – 1) … n)) Bubble sort is O(N – 1) … n 2 ) Quick sort is O(N – 1) … nlog(N – 1) … n)), also O(N – 1) … n 2 ) Both are “polynomial”

Time Complexity: Algorithms vs. Problems

Lower Bound: Problem X is (N – 1) … f(N – 1) … n))

Let problem X be solved by algorithms A1) … , A 2 , …., An. The time complexity of X is not greater than the minimum of the time complexities of A1) … , A 2 , …., An. The lower bound for a problem must be established theoretically, not just by comparing every algorithm known to solve it.

Sorting by comparisons only has been proven to be (N – 1) … nlog(N – 1) … n)).

Example: Lower Bound for Matrix

Addition and Multiplication

Consider three square matrices (N – 1) … N by N) A, B, C where C is either the sum or product of matrices A and B The basic structure for setting values for array C must be for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) c[i][j] = something It takes n 2 steps to fill the n by n array, so both the addition and multiplication problems are (N – 1) … n 2 ).

Consider Matrix Multiplication

The standard algorithm for C = A * B can be expressed as for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) // The following step is executed n 2 times { c[i][j] = 0; for (int k = 0; k < n; k++) // This step is executed n 3 times. c[i][j] = c[i][j] + a[i][k] * b[k][j]; } The number of steps is n 3

  • n 2 , which is O(N – 1) … n 3 ). But the problem is (N – 1) … n 2 ), so matrix multiplication theoretically is an open problem. Two known multiplication algorithms are O(N – 1) … n

) and O(N – 1) … n

)

Example: Towers of Hanoi

Published by the French mathematician Edouard Lucas in 1) … 883. Question: How many steps to move N disks? RULES: Move all disks from peg 1) … to peg 2 Move one disk at a time Do not place a larger disk on top of a smaller disk.