Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii."— Presentation transcript:

1 Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii

2 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?

3 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.

4 Example - Traversing a Weighted Graph

5 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…

6 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 ) 101 100 2 10,000 1,0003 1,000,000 10,0004 100,000,000 FastestMiddleSlowest 2

7 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

8 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); }

9 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; }

10 References Main Michael & Savitch Walter. Data Structures and Other Objects using C++. Addison-Wesley. 1997. Walls & Millors. Data Abstraction and Problem Solving with C++. 2 nd ed. Addison-Wesley. 1998 http://mat.gsia.cmu.edu/classes/dynamic/d ynamic.html


Download ppt "Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii."

Similar presentations


Ads by Google