CSCI 6212 Design and Analysis of Algorithms Dr. Juman Byun The George Washington University • • Please drop this course if you have not taken the following prerequisite. Sometimes enthusiasm alone is not enough. CSci 1311: Discrete Structures I (3) CSci 1112: Algorithms and Data Structures (3) Official Course Description • http://www.cs.gwu.edu/academics/courses/graduate/csci6212 • Design and analysis of algorithms. Turing machines; NPComplete theory. Algorithmic techniques: divide-and-conquer, greedy, dynamic programming, graph traversal, backtracking, and branch-and-bound. Applications include sorting and searching, graph algorithms, and optimization. Prerequisite: CSci 1311, 1112. (Fall and spring) • Textbook: Introduction to Algorithms, Cormen, Leiserson, Rivest, and Stein. Basic Principles of Algorithm Design and Analysis Algorithm Design How to Start the Design ? • Understand the problem. What are we trying to achieve ? • Get a rough idea how to solve the problem using your intuition and imagination Example: Sorting an Array of integers 5 2 4 6 1 3 Example: Sorting an Array of integers 2 5 4 6 1 3 Example: Sorting an Array of integers 4 2 5 6 1 3 Example: Sorting an Array of integers 4 2 5 6 1 3 Example: Sorting an Array of integers 6 4 2 5 1 3 Example: Sorting an Array of integers 6 2 4 5 1 3 Example: Sorting an Array of integers 1 6 2 4 5 3 Example: Sorting an Array of integers 1 2 4 5 6 3 Example: Sorting an Array of integers 3 1 2 4 5 6 Example: Sorting an Array of integers 3 2 4 5 1 6 Example: Sorting an Array of integers 3 2 4 5 1 6 Example: Sorting an Array of integers 3 2 4 5 1 6 Example: Sorting an Array of integers 3 2 4 5 1 6 Example: Sorting an Array of integers 3 1 2 4 5 6 Example: Sorting an Array of integers 3 1 2 4 5 6 Example: Sorting an Array of integers 1 2 4 5 3 6 Example: Sorting an Array of integers 1 2 4 5 3 6 Example: Sorting an Array of integers 1 2 4 5 3 6 Example: Sorting an Array of integers 1 2 3 4 5 6 Accept Concise Notation • The previous colorful animation was pretty but it took me a some time to create it. • In order for a human being to perceive the entire problem, you need to compress the problem or commit it into the long-term storage of your brain. • Algorithmic notation is an excellent way of compressing a problem so that it is easier to remember and handle. Accept Concise Notation 5 2 4 6 1 3 Accept Concise Notation 5 2 4 6 1 3 Disambiguate it 5 , 2 , 4 , 6 , 1, 3 Abstract it A = { 5 , 2 , 4 , 6 , 1, 3 } Denote it Insertion Sort (A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i=j-1 5 while i > 0 and A[i] > key 6 A[i +1] = A[i] 7 i=i-1 8 A[i + 1] = key Pascal Notation Insertion Sort (A) 1 for j = 2 to A.length 2 begin 3 key = A[j] 4 // Insert A[j] into the sorted sequence A[1..j-1] 5 i=j-1 6 while i > 0 and A[i] > key 7 begin 8 A[i +1] = A[i] 9 i=i-1 10 end 11 A[i + 1] = key 12 end C Notation Insertion Sort (A) 1 for j = 2 to A.length 2{ 3 key = A[j] 4 // Insert A[j] into the sorted sequence A[1..j-1] 5 i=j-1 6 while i > 0 and A[i] > key 7 { 8 A[i +1] = A[i] 9 i=i-1 10 } 11 A[i + 1] = key 12 } Algorithm Analysis Example: Running (Execution) Time Analysis Insertion Sort (A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i=j-1 5 while i > 0 and A[i] > key 6 A[i +1] = A[i] 7 i=i-1 8 A[i + 1] = key How many times does each statement execute ? Insertion Sort (A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i=j-1 5 while i > 0 and A[i] > key 6 A[i +1] = A[i] 7 i=i-1 8 A[i + 1] = key Factors to Consider • Input Size • Running Time • • Worst-Case • Best-Case • Average Rate of Growth Assumptions • Technologies • Memory Model: RAM (Random Access Machine/Memory) • Processor: Single Processor