Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii
Designing a program means… 1. Specify what you exactly want to do. What kind of input and output? What is the Precondition and Post condition? 2. Design the Algorithm Pseudocode What kind of Data structures are needed? 3. Translate the program into a programming language C++ JAVA, etc… 4. Test and Debug Does it actually work?
Algorithmic Design Techniques Divide and Conquer Decomposing tasks into smaller subtasks Dynamic Programming Break down problems into stages Keep track of what’s going on at each stage as states Determine the best solution by taking into consideration the states of the previous stage Greedy Always choosing the best local solution at the time to eventually get to the final solution.
Example - Traversing a Weighted Graph
Time and Space Complexity Efficiency of program is determined by its speed and amount of memory space it takes. Faster and smaller is better but…
Big-O Notation Order of an algorithm Rough approximation of number of operations a program will execute to determine its speed # of elements (n) Logarithmic O(log n) Linear O(n) Quadratic O(n ) ,000 1,0003 1,000,000 10, ,000,000 FastestMiddleSlowest 2
Worst, Average, Best Case Scenario Worst Case Maximum Number of Operations Average Case Average Number of Operations Best Case Fewest Number of Operations Probability of occurrence? Unless the best-case behavior occurs with high probability, it is generally not used The worst case occurs often
Example 1 void search(const int a[], int first, int size, int target, bool& found, int& location) { int middle; if(size == 0) found = false; else { middle = first + size/2; if (target == a[middle]) { location = middle; found = true; } else if (target < a[middle]) search(a, first, size/2, target, found, location); else search(a, middle+1, (size-1)/2, target, found, location); }
Example 2 Void Sort(int A[], int N) { bool sorted = false’ for(int Pass = 1; (Pass < N) && !sorted; ++Pass) { Sorted = true; for(int Index = 0; Index < N-Pass; ++Index) { int NextIndex = Index + 1; if(A[Index] > A[NextIndex]) { swap(A[Index], A[NextIndex]); Sorted = false; }
References Main Michael & Savitch Walter. Data Structures and Other Objects using C++. Addison-Wesley Walls & Millors. Data Abstraction and Problem Solving with C++. 2 nd ed. Addison-Wesley ynamic.html