Academic Integrity Collaboration Encouraged! Groups of up to 5 per - - PowerPoint PPT Presentation

academic integrity
SMART_READER_LITE
LIVE PREVIEW

Academic Integrity Collaboration Encouraged! Groups of up to 5 per - - PowerPoint PPT Presentation

Academic Integrity Collaboration Encouraged! Groups of up to 5 per assignment (you + 4) List your collaborators (by UVA computing ID) Write-ups/code written independently DO NOT share written notes / pictures / code DO NOT


slide-1
SLIDE 1

Academic Integrity

  • Collaboration Encouraged!

– Groups of up to 5 per assignment (you + 4) – List your collaborators (by UVA computing ID)

  • Write-ups/code written independently

– DO NOT share written notes / pictures / code – DO NOT share documents (ex: Overleaf)

  • Be able to explain any solution you submit!
  • DO NOT seek published solutions online
slide-2
SLIDE 2

Late Policy

  • By default, late submissions not accepted
  • If something comes up that prevents you from submitting

quality work on time, let me know what’s going on

slide-3
SLIDE 3

Exams

  • No exams
slide-4
SLIDE 4

Regrades

  • Conducted using the submission system:

– Submit within 5 days of receiving your grade – Request a regrade if the rubric was misapplied

slide-5
SLIDE 5

Course webpage

  • www.cs.virginia.edu/~njb2b/cs4102/su20
slide-6
SLIDE 6

Extra “credit”

  • Given for extraordinary acts of engagement

– Good questions/comments – Quality discussions – Analysis of current events – References to arts and music – Extra credit projects – Slide corrections – Etc. Just ask!

  • Submit to me via email
  • Will be used for qualitative grade adjustments
slide-7
SLIDE 7

Warm up Can you cover an 8 × 8 grid with 1 square missing using “trominoes?”

Can you cover this? With these?

CS4102 Algorithms

Fall 2020

slide-8
SLIDE 8

Where does it end?

  • I have a pile of string
  • I have one end of the string in my hand
  • I need to find the other end
  • How can I do this efficiently?
slide-9
SLIDE 9

Rope End Finding

  • 1. Set aside the already obtained end
  • 2. Separate the pile of rope into 2 piles, note which

connects to the known end (call it pile A, the

  • ther pile B)
  • 3. Count the number of strands crossing the piles
  • 4. If the count is even, pile A contains the end, else

pile B does

Repeat on pile with end

slide-10
SLIDE 10

How efficient is it?

  • 𝑈 𝑜 = 𝑑𝑝𝑣𝑜𝑢(𝑜) + 𝑈

𝑜 2

  • 𝑈 𝑜 = 5 + 𝑈

𝑜 2

  • Base case: 𝑈 1 = 1
slide-11
SLIDE 11

Let’s solve the recurrence!

𝑈 𝑜 = 5 + 𝑈(𝑜 2 ) 𝑈 1 = 1 5 + 𝑈(𝑜 4 ) 5 + 𝑈(𝑜 8 ) 1

log2 𝑜 𝑈 𝑜 = 5

log2𝑜 𝑗=0

+ 1 = 5 log2 𝑜 + 1

slide-12
SLIDE 12

Algorithm Running Times

  • We can’t just measure running time with a number
  • Why shouldn’t we say: “The running time of this algorithm is 8.”?
  • Units: 8 what? Seconds?
  • What if the input is large?
  • Algorithm running times are functions

– Domain: “sizes” of the algorithm’s input – Co-Domain: “counts” of operations

  • We want to be able to say “algorithm 1 is faster than algorithm 2”
  • How can we compare functions?
slide-13
SLIDE 13

Asymptotic Notation*

  • 𝑃(𝑕 𝑜 )

– At most within constant of 𝑕 for large 𝑜 – {functions 𝑔|∃ constants 𝑑, 𝑜0 > 0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 ≤ 𝑑 ⋅ 𝑕(𝑜)}

  • Ω(𝑕 𝑜 )

– At least within constant of 𝑕 for large 𝑜 – {functions 𝑔|∃ constants 𝑑, 𝑜0 > 0s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 ≥ 𝑑 ⋅ 𝑕(𝑜)}

  • Θ 𝑕 𝑜

– “Tightly” within constant of 𝑕 for large 𝑜 – Ω 𝑕 𝑜 ∩ 𝑃(𝑕 𝑜 )

*CLRS Chapter 3

slide-14
SLIDE 14

𝑔(𝑜) = 𝑃(𝑕 𝑜 ) 𝑔(𝑜) = Θ(𝑕 𝑜 ) 𝑔(𝑜) = Ω(𝑕 𝑜 )

slide-15
SLIDE 15

Asymptotic Notation

  • 𝑝(𝑕 𝑜 )

– Below any constant of 𝑕 for large 𝑜 – {functions 𝑔|∀ constants 𝑑, ∃𝑜0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 < 𝑑 ⋅ 𝑕(𝑜)}

  • 𝜕(𝑕 𝑜 )

– Above any constant of 𝑕 for large 𝑜 – {functions 𝑔|∀ constants 𝑑, ∃𝑜0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 > 𝑑 ⋅ 𝑕(𝑜)}

  • 𝜄 𝑕 𝑜

?

– 𝑝(𝑕 𝑜 ) ∩ 𝜕(𝑕 𝑜 ) = ∅

slide-16
SLIDE 16

Asymptotic Notation Example

  • Show: 𝑜 log 𝑜 ∈ 𝑃 𝑜2
slide-17
SLIDE 17

Asymptotic Notation Example

  • To Show: 𝑜 log 𝑜 ∈ 𝑃 𝑜2

– Technique: Find 𝑑, 𝑜0 > 0 s.t. ∀𝑜 > 𝑜0, 𝑜 log 𝑜 ≤ 𝑑 ⋅ 𝑜2 – Proof: Let 𝑑 = 1, 𝑜0 = 1. Then, 𝑜0 log 𝑜0 = 1 log 1 = 0, 𝑑 𝑜0

2 = 1 ⋅ 12 = 1,

0 ≤ 1. ∀𝑜 ≥ 1, log 𝑜 < 𝑜 ⇒ 𝑜 log 𝑜 ≤ 𝑜2 □ Direct Proof!

slide-18
SLIDE 18

Asymptotic Notation Example

  • Show: 𝑜2 ∉ 𝑃 𝑜
slide-19
SLIDE 19

Asymptotic Notation Example

  • To Show: 𝑜2 ∉ 𝑃 𝑜

– Technique: Contradiction – Proof: Assume 𝑜2 ∈ 𝑃 𝑜 . Then ∃𝑑, 𝑜0 > 0 s. t. ∀𝑜 > 𝑜0, 𝑜2 ≤ 𝑑𝑜 Let us derive constant 𝑑. For all 𝑜 > 𝑜0 > 0, we know: 𝑑𝑜 ≥ 𝑜2, 𝑑 ≥ 𝑜. Since 𝑑 is lower bounded by 𝑜, it is not a constant.

  • Contradiction. Therefore 𝑜2 ∉ 𝑃 𝑜 . □

Proof by Contradiction!

slide-20
SLIDE 20

Asymptotic Notation Example

  • 𝑝 𝑕 𝑜

= {functions 𝑔 ∶ ∀ constants 𝑑 > 0, ∃𝑜0 s.t. ∀𝑜 > 𝑜0, 𝑔 𝑜 < 𝑑 ⋅ 𝑕(𝑜)}

  • Show: 𝑜 log 𝑜 ∈ 𝑝 𝑜2

– given any 𝑑 find a 𝑜0 > 0 s.t. ∀𝑜 > 𝑜0, 𝑜 log 𝑜 < 𝑑 ⋅ 𝑜2 – Find a value of 𝑜 in terms of 𝑑:

  • 𝑜 log 𝑜 < 𝑑 ⋅ 𝑜2
  • log 𝑜 < 𝑑 ⋅ 𝑜
  • log 𝑜

𝑜

< 𝑑

– For a given 𝑑, select any value of 𝑜0 such that

log 𝑜 𝑜

< 𝑑

slide-21
SLIDE 21

Trominoes Puzzle Solution

What about larger boards? 2𝑜 2𝑜

slide-22
SLIDE 22

Trominoes Puzzle Solution

Divide the board into quadrants

slide-23
SLIDE 23

Trominoes Puzzle Solution

Place a tromino to occupy the three quadrants without the missing piece

slide-24
SLIDE 24

Trominoes Puzzle Solution

Each quadrant is now a smaller subproblem

slide-25
SLIDE 25

Trominoes Puzzle Solution

Solve Recursively

slide-26
SLIDE 26

Divide and Conquer

Our first algorithmic technique!

slide-27
SLIDE 27

Trominoes Puzzle Solution

slide-28
SLIDE 28

Divide and Conquer*

  • Divide:

– Break the problem into multiple subproblems, each smaller instances of the original

  • Conquer:

– If the suproblems are “large”:

  • Solve each subproblem recursively

– If the subproblems are “small”:

  • Solve them directly (base case)
  • Combine:

– Merge together solutions to subproblems

*CLRS Chapter 4

When is this a good strategy?

slide-29
SLIDE 29

Analyzing Divide and Conquer

  • 1. Break into smaller subproblems
  • 2. Use recurrence relation to express recursive running time
  • 3. Use asymptotic notation to simplify
  • Divide: 𝐸(𝑜) time,
  • Conquer: recurse on small problems, size 𝑡
  • Combine: C(𝑜) time
  • Recurrence:

– 𝑈 𝑜 = 𝐸 𝑜 + 𝑈(𝑡) + 𝐷(𝑜)

slide-30
SLIDE 30

Recurrence Solving Techniques

Tree Guess/Check “Cookbook”

30

?

slide-31
SLIDE 31

Merge Sort

  • Divide:

– Break 𝑜-element list into two lists of 𝑜 2 elements

  • Conquer:

– If 𝑜 > 1:

  • Sort each sublist recursively

– If 𝑜 = 1:

  • List is already sorted (base case)
  • Combine:

– Merge together sorted sublists into one sorted list

31

slide-32
SLIDE 32

Merge

  • Combine: Merge sorted sublists into one sorted list
  • We have:

– 2 sorted lists (𝑀1, 𝑀2) – 1 output list (𝑀𝑝𝑣𝑢)

While (𝑀1 and 𝑀2 not empty): If 𝑀1 0 ≤ 𝑀2[0]: 𝑀𝑝𝑣𝑢.append(𝑀1.pop()) Else: 𝑀𝑝𝑣𝑢.append(𝑀2.pop()) 𝑀𝑝𝑣𝑢.append(𝑀1) 𝑀𝑝𝑣𝑢.append(𝑀2)

32

𝑃(𝑜)

slide-33
SLIDE 33

Analyzing Merge Sort

  • 1. Break into smaller subproblems
  • 2. Use recurrence relation to express recursive running time
  • 3. Use asymptotic notation to simplify
  • Divide: 0 comparisons
  • Conquer: recurse on 2 small subproblems, size 𝑜

2

  • Combine: 𝑜 comparisons
  • Recurrence:

– 𝑈 𝑜 = 2 𝑈

𝑜 2 + 𝑜

33

slide-34
SLIDE 34

Recurrence Solving Techniques

Tree Guess/Check “Cookbook”

34

?

slide-35
SLIDE 35

Tree method

35

𝑜 total / level log2 𝑜 levels

  • f recursion

𝑜

𝑈 𝑜 = 2𝑈(𝑜 2 ) + 𝑜

𝑈 𝑜 = 𝑜

log2 𝑜 𝑗=1

= 𝑜 log2 𝑜

𝑜 2 𝑜 2 𝑜 4 𝑜 4 𝑜 4 𝑜 4

… … … …

1 1 1

1 1 1

𝑜 𝑜 2 𝑜 2 𝑜 4 𝑜 4 𝑜 4 𝑜 4 1 1 1 1 1 1