/
Integrating Different Ideas Together Integrating Different Ideas Together

Integrating Different Ideas Together - PowerPoint Presentation

marina-yarberry
marina-yarberry . @marina-yarberry
Follow
375 views
Uploaded On 2017-05-06

Integrating Different Ideas Together - PPT Presentation

Reading Materials Ch 36 of SG Contents Incrementing a Binary Counter How many subsets Representation Printing All Subsets Problem Exponential Time Algorithms Four seemingly unrelated problems ID: 545273

bit subsets time set subsets bit set time subset algorithm size incrementing problem power vector represent counter binary flipped cost analysis case

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Integrating Different Ideas Together" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Integrating Different Ideas Together

Reading Materials:

Ch

3.6

of

[SG]

Contents

:

Incrementing a Binary Counter

How many subsets, Representation

Printing All Subsets Problem

Exponential Time AlgorithmsSlide2

Four seemingly unrelated problems

P1: You

are given a bit-array

A = (A[n−1], A[n−2], ... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. P2: Suppose you are given a set S = {s1, s2, ..., sn} of n objects, and a subset X of S. How can we use the bit-vector A to represent the subset X? P3: Using P1 and P2 above, or otherwise, give a simple algorithm to generate and print all subsets of S. P4: What is the running time of your algorithm? [Note: Recall that there are 2n subsets altogether. (A proof is provided byP1 and P2 above.)] Slide3

n-bit Binary Counter

x

A[4]

A[3]A[2]A[1]A[0]Cost0000000

1

0

0

0

0

1

1

2

0

0

0

1

0

3

3

0

0

0

1

1

4

4

0

0

1

0

0

7

5

0

0

1

0

1

8

6

0

0

1

1

0

10

7

0

0

1

1

1

11

8

0

1

0

0

0

15

9

0

1

0

0

1

16

10

0

1

0

1

0

18

11

0

1

0

1

1

19Slide4

Incrementing a Binary Counter

n

-bit Binary Counter: A[0..n1] x = A[n−1]⋅2(n−1) + . . . + A[1]⋅21 + A[0]⋅20INCREMENT(A)1. i  02. while i < length[A] and A[i] = 13. do A[i]  0 ⊳ reset a bit4. i  i + 15. if i < length[A]6. then A[i]  1 ⊳ set a bitSlide5

P1: You

are given a bit-array

A = (A[n−1], A[n−2], ... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. Note: For UIT2201, only need to understand the first 2 slides on the problem P1 (incrementing a binary counter).(The other slides are included for info only.)Four seemingly unrelated problemsSlide6

Worst-case analysis

Consider a sequence of

n

insertions. The worst-case time to execute one insertion is Q(k). Therefore, the worst-case time for n insertions is n · Q(k) = Q(n k).WRONG! In fact, the worst-case cost for n insertions is only Q(n) ≪ Q(n k).Let’s see why.Slide7

Tighter analysis

x

A[4]

A[3]A[2]A[1]A[0]Cost00000

0

0

1

0

0

0

0

1

1

2

0

0

0

1

0

3

30001144001007500101860011010700111118010001590100116100101018110101119

A[0] flipped every op nA[1] flipped every 2 ops n/2A[2] flipped every 4 ops n/22A[3] flipped every 8 ops n/23 … … … … … A[i] flipped every 2i ops n/2i

Total cost of

n

operationsSlide8

Tighter analysis (continued)

Cost of

n

increments.Thus, the average cost of each increment operation is Q(n)/n = Q(1).Slide9

How many subsets & how to represent them?Slide10

Four seemingly unrelated problems

P1: You

are given a bit-array

A = (A[n−1], A[n−2], ... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. P2: Suppose you are given a set S = {s1, s2, ..., sn} of n objects, and a subset X of S. How can we use the bit-vector A to represent the subset X? Note: For UIT2201, only need to understand bit-representation method for problem P2.(The other slides are included for info only.)Slide11

PS: (Power-Set Example) (1)

Problem:

Given a set S with

n elements. How many subsets of S are there?Stage 1: Understanding the Problem PQ: What is the unknown? [# subsets of S] PQ: What is the data? [A set S with n elements] PQ: What is the condition? [Subsets of S. Need to count all of them.] PQ: Is it sufficient? [Yes? Can count one-by-one, but tedious]Slide12

PS: (Power-Set Example) (2)

Stage 2: Devising a Plan

PQ: Have you seen the problem before?

PQ: Can you try to work out some small instances? PQ: Can you see any pattern? Stage 3: Carrying out the Plan. PQ: Can you prove the result?n S P(S)#0 ϕ ϕ11 {x1} ϕ, {x1}22 {x1, x2} ϕ, {x1}, {x2}, {x1, x2}43 {x1, x2, x3} ϕ, {x1}, {x2}, {x1, x2} {x3}, {x1, x3},{x2, x3}, {x1, x2, x3}8Slide13

PS: (Power-Set Example) (3)

Another Approach?

PQ: Can you solve it differently?

PQ: Introduce suitable notations? Let An be the # of subsets of a S with n element. Now, consider the set S’ = S U {x} with n+1 elements. Divide subsets of S’ into (P1) those containing element x, and (P2) those that do not contain element x. Those in P2 are exactly all the subsets of S (and we have An of them). For every subset T of S in P2, there is a corresponding subset that contains x, namely T U {x} in P1. Thus, there is a 1-1 correspondence betw subsets in P1 and those in P2. Therefore, An+1 = 2  An for all n ≥ 0. In addition, we know A0 = 1.Slide14

PS: (Power-Set Example) (4)

Yet Another Approach?

PQ: Draw a figure?

c'cc'ccc'cc'bb'bb'aa'

Where are the subset?Slide15

PS: (Power-Set Example) (5)

Yet Another Method?

PQ: Look at the unknown. [Subsets! They come in different sizes]

IDEA: Let’s count those of the same size!How many of size 0? 1How many of size 1? nHow many of size 2? n(n-1)/2 = nC2 . . . How many of size k? nCk . . . How many of size n–1? nHow many of size n? 1 Total # of subsets is Slide16

PS: (Power-Set Example) (6)

Yet another method?

PQ: How can the subsets be represented?

Let the vector (b1, b2, …, bn) represent the subset T where bk=1, if the element xk is in the set T, and 0 otherwise. PQ: How many such bit-strings are there? Subset Bit-Representation ϕ (0,0,…,0,0) {x1} (1,0,…,0,0) {x2} (0,1,…,0,0) {x1, x2} (1,1,…,0,0) {x1, x2, ..., xn} (1,1,…,1,1)Slide17

Four seemingly unrelated problems

P1: You

are given a bit-array

A = (A[n−1], A[n−2], ... A[1] A[0]), where each A[k] is 0 or 1. Given an algorithm for incrementing A by 1. P2: Suppose you are given a set S = {s1, s2, ..., sn} of n objects, and a subset X of S. How can we use the bit-vector A to represent the subset X? P3: Using P1 and P2 above, or otherwise, give a simple algorithm to generate and print all subsets of S. P4: What is the running time of your algorithm? [Note: Recall that there are 2n subsets altogether. (A proof is provided byP1 and P2 above.)] Slide18

Solution to P3 and P4

Outline of algorithm for P3:

Represent subsets using bit vector

A1. Start with A = (0, 0, . . . , 0, 0)2. Repeat these steps 3. Increment the bit vector A (use algorithm from P1) Print out the subset corresponding to A5. until A = (1, 1, . . . , 1, 1)Analysis (P4):Steps 3,4 done 2n time; (there are 2n subsets!) Slide19

Solution to P3 and P4

Analysis (P4):

Steps 3,4 done 2

n time; (there are 2n subsets!) Step 3 takes time at most O(n) Step 4 takes time at most O(n)Total time: 2n * O(n) = O(n 2n)This is an exponential time algorithm!Lower Bound: Since there 2n subsets; Just printing them will take at least 2n time! Slide20

One

Final Question ?Slide21

Exponential Time Problems…

Intel designs CPU chips for most of

todays

’ computers (IBM-compatible, Macs, Linux, etc)If Intel wants to thoroughly verify their multiplication circuit is correct (i.e. produces the correct product for all input combinations), what does it need to do? Check that for every a, b compute and verify the product (a * b)Do you think that Intel has actually done that for their 32-bit and 64-bit processors?Slide22

Thank

you!Slide23