Big O and Algorithms Part 2 Discrete Structures

Big O and Algorithms (Part 2) Discrete Structures (CS 173) Derek Hoiem, University of Illinois 03/28/13 1

Feedback: common comments • More practice questions – See prior terms: there a few semesters’ worth • Write bigger, neater – I’ll try! • Hand out solutions to discussion sections – Not available but see all the prior term practice questions • Less overlap between lecture and reading – Balancing act but can try to vary more • Make homework due later – Done! • • Make lectures/discussion sections shorter or longer Explain general induction approach more – Today • Explain how to do proofs in more detail earlier – Good idea • Lectures/material is boring – Sorry! • Best class ever – Good! • Lots of comments about discussion sections – Will pass along • Change slides slower – Note that slides are also posted before lecture (almost always) and notes after 2

Last class • 3

This class • Midterm review: induction form and strategies • Analysis of algorithm complexity – For loops – While loops – Recursion 4

Induction • When to use it: You need to prove that something is true for all (an infinite set) of numbers, graphs, trees, etc. • The induction variable is always an integer (size of graph, number of puzzle pieces, etc. ) • Key step is figuring out how the larger problem (number) can be formulated in terms of the smaller problem (number) 5

Induction: checklist • 6

Form of an induction proof • fun even more fun 1 disclaimer: this proof isn’t entirely rational 7

Choosing the induction variable • 8

Selecting the base case • 9

Induction strategy • 10

Exam review • http: //courses. engr. illinois. edu/cs 173/sp 2013/Blecture/Exams/index. html • http: //courses. engr. illinois. edu/cs 173/sp 2013/Blecture/previous-terms. html • Discussion section 11

Algorithms • Practice analyzing runtime based on pseudocode 13

Key concept: instructions in the loops dominate 14

procedure bubble. Sort(A : list of sortable items) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure Key concept: if the number of iterations of the loop is uncertain, determine the worst case Note: can also do best-case or worst-case analysis http: //www. youtube. com/watch? v=ly. ZQPj. UT 5 B 4 15

Key concept: runtime can be in terms of multiple input parameters 16

Key concept: n/2 recursion 17

Comparison of sorting algorithms http: //www. sorting-algorithms. com/random-initial-order Key concept: Be aware of best, worse, and average case 18

http: //introcs. princeton. edu/java/23 recursion/hanoi/ 19

Have a good weekend 20
- Slides: 19