160 likes | 176 Views
EECE.3220 Data Structures. Instructor: Dr . Michael Geiger Spring 2017 Lecture 8: Algorithmic complexity examples. Lecture outline. Announcements/reminders Program 1 due today All programs to be submitted via Dropbox E-mail Dr. Geiger for access to shared Dropbox folder
E N D
EECE.3220Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 8: Algorithmic complexity examples
Lecture outline • Announcements/reminders • Program 1 due today • All programs to be submitted via Dropbox • E-mail Dr. Geiger for access to shared Dropbox folder • HW 1 to be posted; due Friday, 2/10 • Problem set dealing with algorithmic complexity • Program 2 to be posted; due date TBD • Today’s lecture • Review • cin.ignore(), cin.getline() • Algorithmic complexity • Algorithmic complexity examples Data Structures: Lecture 8
Review: Characters and input • Input (cin) streams • Use cin to read values into variables • E.g., cin >> x; • Skips whitespace characters • Input value must be compatible with type of x • Reading n characters: cin.get(buffer, n); • Reading 1 character: cin.get(ch); • Reading an entire line (at most m characters): cin.getline(buffer, m) • May need cin.ignore(x) to skip characters Data Structures: Lecture 8
Review: Algorithmic complexity • Typically try to approximate worst-case computing time • Measure time as T(n), function of n • Count number of times each step in algorithm executes • Use big O notation—O(f(n))—to express order of magnitude • Choose slowest growing function that provides upper bound on execution time • Look at largest exponent in T(n) term • Ignore constants, multipliers Data Structures: Lecture 8
Example: Calculating the Mean Task # times executed • Initialize the sum to 0 1 • Initialize index i to 0 1 • While i < n do following n+1 • a) Add x[i] to sum n • b) Increment i by 1 n • Return mean = sum/n 1 Total 3n + 4 = O(n) Data Structures: Lecture 8
Examples • For each function, find worst case execution time & order of magnitude • Part (a) has a typo in the handout (highlighted in red) • First line of part (b) isn’t numbered in handout—don’t ignore it (a) int F(int n) { inti, res; 1 if (n < 2) 2 return 1; 3 else { 4 res = 1; 5 for (i=2; i<=n; i++) 6 res *= i; 7 return res; } } (b) unsigned F(unsigned n){ 1unsigned res = 0; 2for (i=0; i<n+1; i++) 3for(j=0; j<n+1; j++) 4 res = res + j; 5return res; } Data Structures: Lecture 8
Example solution—part (a) int F(int n) { inti, res; 1 if(n < 2) 1 2return1; 1 3else { 1 4 res = 1; 1 5for(i=2; i<=n; i++) n 6 res *= i; n-1 7 returnres; 1 } } • Condition evaluated in both cases • If case—1 other statement (return) • T(n) = 2 = O(1) • Else case—execute statements in red • T(n) = 3 + n = O(n) Worst case Data Structures: Lecture 8
Example solution—part (b) unsigned F(unsigned n){ 1unsignedres = 0; 1 2 for (i=0; i<n+1; i++)n+2 3 for (j=0; j<n+1; j++) (n+1)*(n+2) 4 res = res + j; (n+1)*(n+1) 5 return res; 1 } • In nested loop, inner loop goes through all iterations for every outer loop iteration • T(n) = 2n2 + 6n + 7 = O(n2) Data Structures: Lecture 8
Worst case analysis examples • Following slides present pseudocode and analysis for three common operations on arrays • Linear search • Binary search • Selection sort • Pseudocode: describes steps of algorithm in code-like fashion, but doesn’t correspond to any actual language Data Structures: Lecture 8
Worst case analysis: linear search • Search entire array of n values for item; found = true and loc = item position if successful; otherwise, found = false • Set found = false • Set loc = 0 • While loc< n and not found, do following: • If item == a[loc] then • Set found = true • Else Increment loc by 1 Data Structures: Lecture 8
Worst case analysis: linear search (2) • Worst case: item is not in list • Algorithm will go through all elements in array • In all cases • Lines 1 & 2 execute once • In worst case • Line 3 executes n+1 times • Lines 4 & 6 execute n times • Therefore, T(n) = 3n + 3 = O(n) Data Structures: Lecture 8
Worst case analysis: binary search • Searching ordered array much more efficient • Search array of nascending values for item; found = true and loc = item position if successful; otherwise, found = false • Set found = false • Set first = 0 • Set last = n - 1 • While first ≤ last and not found, do following: • Calculate loc = (first + last) / 2 • If item < a[loc] then • Set last = loc – 1 // Search first half • Else if item > a[loc] then • Set first = loc + 1 // Search second half • Else Set found = true // Item found Data Structures: Lecture 8
Worst case analysis: binary search (2) • Algorithm splits list into smaller sublist to be searched • Loop control statement again limiting factor • Each time, sublist size ≤ ½ previous sublist size • Total number of loop iterations: 1 + (# iterations to produce sublist of size 1) • If k = # iterations to produce sublist of size 1, n / 2k < 2 n < 2k* 2 n < 2k+1 log2n < k + 1 • Therefore, in worst case (item is larger than everything in list) line 4 executed 2 + log2n times • T(n) = O(log2n) Data Structures: Lecture 8
Worst case analysis: selection sort • Algorithm to sort array of n elements into ascending order • On ith pass, first find smallest element in sublist x[i] … x[n-1], then place that value in position i • For i = 0 to n – 2 do the following • Set smallPos = i • Set smallest = x[smallPos] • For j = i+1 to n-1 do the following • If x[j] < smallest then • SetsmallPos = j • Set smallest = x[smallPos] • Set x[smallPos] = x[i] • Set x[i] = smallest Data Structures: Lecture 8
Worst case analysis: selection sort (2) • Outer loop condition (1) executed n times • Statements inside outer loop but not in inner loop (2, 3, 8, 9) executed n – 1 times • Inner loop • If i = 0, (4) executed n times, (5,6,7) n – 1 times • If i = 1, (4) executed n-1 times, (5,6,7) n – 2 times • … i = n-2, (4) executed 2 times, (5,6,7) 1 time • In total • (4) executed n(n+1)/2 – 1 times • (5,6,7) executed n(n+1)/2 – 2 times • Therefore • T(n) = n + 4(n-1) + n(n+1)/2 – 1 + 3(n(n-1)/2) = 2n2 + 4n – 5 = O(n2) Data Structures: Lecture 8
Final notes • Next time • Abstract data types • Reminders: • Program 1 due today • All programs to be submitted via Dropbox • E-mail Dr. Geiger for access to shared Dropbox folder • HW 1 to be posted; due Friday, 2/10 • Program 2 to be posted; due date TBD Data Structures: Lecture 8