csci 2824, discrete structures fall 2017 tony...

Post on 17-May-2018

220 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSCI 2824, Discrete StructuresFall 2017Tony Wong

Lecture 1: Introduction andBinary Arithmetic

1

What does “Discrete Structures” mean?

2

What does “Discrete Structures” mean?It’s the computer sciency way of saying… “discrete math”

3

What does “Discrete Structures” mean?It’s the computer sciency way of saying… “discrete math”

Okay… then what is “discrete math”?

4

What does “Discrete Structures” mean?It’s the computer sciency way of saying… “discrete math”

Okay… then what is “discrete math”?

Well, there’s continuous math- like derivatives and integrals

5

What does “Discrete Structures” mean?It’s the computer sciency way of saying… “discrete math”

Okay… then what is “discrete math”?

Well, there’s continuous math- like derivatives and integrals

and then there’s discrete math- like counting, sorting, enumeration

6

Graph theory: Bridges of KonigsbergThe city of Konigsberg has two islands formed by a river with seven bridges connecting the islands and the mainland.

Is there a path that traverses each bridge exactly once?

7

Logic“Two doors” riddle:

8

Logic“Two doors” riddle:

- Two doors, guarded by two creatures.

- One door goes where you want to, but the other leads to certain death.

- One always lies, and one always tells the truth.

- How can you ask only one of them only one question to discover which door is which?

9

Logic

“100 prisoners and a lightbulb” riddle:- There are 100 prisoners in separate cells.- There is a room they can visit one at a time.- There is a light in the room that they can turn

on or off.- Any of the prisoners can claim that all of them

have visited the room at least once.- If they’re right, all the prisoners are set free.- If they’re wrong, they’re all executed.

10

Logic

“100 prisoners and a lightbulb” riddle:- There are 100 prisoners in separate cells.- There is a room they can visit one at a time.- There is a light in the room that they can turn

on or off.- Any of the prisoners can claim that all of them

have visited the room at least once.- If they’re right, all the prisoners are set free.- If they’re wrong, they’re all executed.

- How can the prisoners conspire just once at the beginning, and then develop a strategy to be set free?

11

Monty Hall problem: “Let’s Make a Deal”- There are three doors.- One has a nice prize behind it…

Discrete Probability

12

Monty Hall problem: “Let’s Make a Deal”- There are three doors.- One has a nice prize behind it… - … and the other two have goats.- You get to pick a door and will be awarded

the prize behind it.- Then the host reveals a goat behind one of the other two doors.- You now have the option to stick with your original door or switch.

- Should you stick with your original door or switch? Or does it not matter?

Discrete Probability

13

This is a good example of what makes discrete structures/math unique:

That we’re dealing with discrete events: - the cash is behind door 1, 2 or 3.

As opposed to continuous events: - the cash is hiding at one of the real numbers between 0.5 and 3.5. (you’d have pretty much 0 probability of winning that game, depending on the rounding…)

Discrete Probability

14

This is a good example of what makes discrete structures/math unique:

That we’re dealing with discrete events: - the cash is behind door 1, 2 or 3.

As opposed to continuous events: - the cash is hiding at one of the real numbers between 0.5 and 3.5. (you’d have pretty much 0 probability of winning that game, depending on the rounding…)

Real applications: (just some vague examples)

● Complexity of algorithms (100 prisoners and a light bulb)● Anything related to data science

Discrete Probability

15

RecursionTower of Hanoi

Move the stack of discs from one pegto another without placing a larger disc on top of a smaller one.

16

RecursionTower of Hanoi

Move the stack of discs from one pegto another without placing a larger disc on top of a smaller one.

Real applications:● Recursions often yield more readable code, as well as elegant

solutions● Recursions are often frowned upon because of stack memory issues● Still, recursions can shed some light on how a nice “for” loops can

work17

Number Theory

Puzzles: Show that

● all four-digit palindromic numbers are divisible by 11

18

Number Theory

Puzzles: Show that

● all four-digit palindromic numbers are divisible by 11

● if you repeat a three-digit number twice to obtain a six-digit number, the result will be divisible by 7, 11, and 13. Furthermore, if you divide the six-digit number by 7, 11, and 13 you'll get back the original three-digit number.

19

Number Theory

Puzzles: Show that

● all four-digit palindromic numbers are divisible by 11

● if you repeat a three-digit number twice to obtain a six-digit number, the result will be divisible by 7, 11, and 13. Furthermore, if you divide the six-digit number by 7, 11, and 13 you'll get back the original three-digit number.

● if you choose a digit from 1-9 and repeat it three times to form a three-digit number, the result divided by the sum of the three digits will always be 37 20

Number Theory

Real applications:

● Public key encryption/RSA security

● Psuedorandom number generation super important, it turns out

● Hash functions

● Showing off to your parents/friends (depending on how cool your friends are)

21

Changing gear: Course LogisticsWhat you need…

● Mathematical curiosity…

… or at least not math-averse.

● Do some programming○ Expect most homework assignments

to include some Python component

22

Changing gear: Course LogisticsWhat you need…

● Mathematical curiosity…

… or at least not math-averse.

● Do some programming○ Expect most homework assignments

to include some Python component

● Some form of the book

○ Old/international versions/PDF are okay, butmake sure you have matched the appropriate sections

23

Changing gear: Course LogisticsStructure:

● Weekly homework assignments (40%)○ Half online via Moodle, half written

● Participation (10%)○ Tutorial problems

● Midterm exam (20%)

● Final exam (30%)

24

Changing gear: Course LogisticsStructure:

● Weekly homework assignments (40%)○ Half online via Moodle, half written

● Participation (10%)○ Tutorial problems

● Midterm exam (20%)

● Final exam (30%)

25

Moodle enrollment keys for our classes:

csci2824-002 (9a) → key is csci2824_F17_002csci2824-003 (11a) → key is csci2824_F17_003

Changing gear: Course LogisticsStructure:

● Weekly homework assignments (40%)○ Half online via Moodle, half written

● Participation (10%)○ Tutorial problems

● Midterm exam (20%)

● Final exam (30%)

26

Must have combined exam average > 50% to get a C- or better in the class.

Changing gear: Course LogisticsStructure:

● Weekly homework assignments (40%)○ Half online via Moodle, half written

● Participation (10%)○ Tutorial problems

● Midterm exam (20%)

● Final exam (30%)

● Keep track of things via the course webpage (Piazza)

● Read the syllabus for more details!

○ Especially regarding due date and collaboration policies

○ Let us know about special needs as soon as possible27

Must have combined exam average > 50% to get a C- or better in the class.

Communicating with PiazzaWe will use Piazza to manage all communications:

https://piazza.com/colorado/fall2017/csci2824

● Announcements and course materials (notes, supplemental material, the syllabus and other logistical notes) will be posted here

28

Communicating with PiazzaWe will use Piazza to manage all communications:

https://piazza.com/colorado/fall2017/csci2824

● Announcements and course materials (notes, supplemental material, the syllabus and other logistical notes) will be posted here

● You (yes, you!) can ask questions in an open forum

○ and responses can be posted by other students or instructors/CAs/graders

● Discuss assigned work (but of course do not post answers/vital code)

● Read the other questions before posting your own!

● If you have a question specific to one instructor, be sure to use the appropriate salutation (e.g., “Hey Chris, …” or “Hey Tony, …”)

Do not send us email! Send us message via Piazza instead. 29

Expectations● Come to class prepared, at least skim the material beforehand

● Be willing to contribute

○ To classroom discussion

○ As well as on Piazza (students helping one another is one of the reasons why we use it!)

● Be on time. If you are late, do not be disruptive

● If you use a laptop, sit in the back so you do not distract others (unless you have a documented special need)

○ More on this in a moment…

○ Handwritten notes and notes on a tablet are okay anywhere.

30

Expectations

Let’s talk laptops.

“Results showed that students who used laptops in class spent considerable time multitasking and that the laptop use posed a significant distraction to both users and fellow students. Most importantly, the level of laptop use was negatively related to several measures of student learning, including self-reported understanding of course material and overall course performance.”http://www.sciencedirect.com/science/article/pii/S0360131506001436Also: http://journals.sagepub.com/doi/pdf/10.1177/0956797616677314And: http://www.sciencedirect.com/science/article/pii/S0272775716303454 (... and others…) 31

About Me● Call me Tony. Or Dr. Wong if you’re more comfortable with that.

● First year teaching in CS.

● Before this, I was a postdoc at Penn State’s Earth and Environmental Systems Institute. And taught Earth Science.

● Taught throughout grad school in APPM.

● Research interests: applying statistics to climate science

● Office: ECOT 737

● Office hours: W Th 12:30-2pm / by appointment

● Email: For class: through Piazza only (this keeps things organized)

If necessary, be sure to make it clear to which instructor your Piazza message isspecific to in the salutation. 32

Enough chit chat. Let’s get to work!Binary Arithmetic● There is a nice overview of binary arithmetic available here:

http://www.cs.colorado.edu/~srirams/courses/csci2824-spr14/binaryLecture.html

33

Enough chit chat. Let’s get to work!Binary Arithmetic● There is a nice overview of binary arithmetic available here:

http://www.cs.colorado.edu/~srirams/courses/csci2824-spr14/binaryLecture.html

● We are most used to using decimal arithmetic (base-10)○ Counting on your fingers or toes?

● Why do we care?○ Computers are good are knowing whether something is “on” (1) or “off” (0)

○ Two options → base-2 system

34

Enough chit chat. Let’s get to work!Binary Arithmetic● There is a nice overview of binary arithmetic available here:

http://www.cs.colorado.edu/~srirams/courses/csci2824-spr14/binaryLecture.html

● We are most used to using decimal arithmetic (base-10)○ Counting on your fingers or toes?

● Why do we care?○ Computers are good are knowing whether something is “on” (1) or “off” (0)

○ Two options → base-2 system

● Example: holding down Ctrl (0/1)? Alt (0/1)? Del (0/1)?

⇒ Abort only if (111)35

● For example:The decimal number 12045 implies that we have:

● For another example:The binary number 11011 follows the same pattern, but with powers of 2 instead of 10.

So what is this in decimal form? 36

● For example:The decimal number 12045 implies that we have:

5 x 100 + 4 x 101

+ 0 x 102

+ 2 x 103

+ 1 x 104

= 12045

● For another example:The binary number 11011 follows the same pattern, but with powers of 2 instead of 10.

So what is this in decimal form? 37

● For another example:The binary number 11011 follows the same pattern, but with powers of 2 instead of 10.

So what is this in decimal form?

We have:

38

● For another example:The binary number 11011 follows the same pattern, but with powers of 2 instead of 10.

So what is this in decimal form?

We have:

1 x 20

+ 1 x 21

+ 0 x 22

+ 1 x 23

+ 1 x 24

= 1 + 2 + 0 + 8 + 16

= 27 39

● Some general rules:

1. We chop off leading 0s

a. Just like the decimal number 0123 is written as 123, the binary number 0011011 is written as 11011.

2. Gosh, it would be cumbersome to keep writing “the decimal number this and the binary number that”. Surely there must be an easier way!

40

● Some general rules:

1. We chop off leading 0s

a. Just like the decimal number 0123 is written as 123, the binary number 0011011 is written as 11011.

2. Gosh, it would be cumbersome to keep writing “the decimal number this and the binary number that”. Surely there must be an easier way!

a. There is! We write the base (10 for decimal, 2 for binary, e.g.) as a subscript next to the number.

b. So from the previous example,

41

● Start from right (the ones place) and move to the left.

Converting the number N from decimal to binary

42

● Start from right (the ones place) and move to the left.● If the first digit is odd, then the first bit is a 1. Otherwise, it’s a 0.

○ Reset N → (N-1) / 2 if N was odd.○ Reset N → N/2 if N was even.

Converting the number N from decimal to binary

43

● Start from right (the ones place) and move to the left.● If the first digit is odd, then the first bit is a 1. Otherwise, it’s a 0.

○ Reset N → (N-1) / 2 if N was odd.○ Reset N → N/2 if N was even.

● Move to the next bit to the left.● Repeat procedure until N either is 0 or 1.

Converting the number N from decimal to binary

44

● Start from right (the ones place) and move to the left.● If the first digit is odd, then the first bit is a 1. Otherwise, it’s a 0.

○ Reset N → (N-1) / 2 if N was odd.○ Reset N → N/2 if N was even.

● Move to the next bit to the left.● Repeat procedure until N either is 0 or 1.

● Example: Convert 2824 from decimal to binary.

Converting the number N from decimal to binary

45

● Example: Convert 2824 from decimal to binary.○ First digit is even, so first bit is a 0.

■ Next iteration, we have 2824/2 = 1412.

Converting the number N from decimal to binary

46

● Example: Convert 2824 from decimal to binary.Converting the number N from decimal to binary

47

● Example: Convert 2824 from decimal to binary.○ First digit is even, so first bit is a 0.

■ Next iteration, we have 2824/2 = 1412.○ First digit is even, so second bit is a 0.

■ Next iteration, we have 1412/2 = 706.○ First digit is even, so third bit is 0.

■ Next iteration, we have 706/2 = 353.○ First digit is odd, so fourth bit is 1.

■ Next iteration, we have (353-1)/2 = 176.○ First digit is even, so fifth bit is 0.

■ Next iteration, we have 176/2 = 88.○ First digit is even, so sixth bit is 0. ○ … eventually, find 282410 = 1011000010002

Converting the number N from decimal to binary

48

● We can!○ First bit right of the “radix point” represents ½.○ Second bit on the right represents ¼.○ Third bit represents ⅛.○ … and so on.

Representing fractions as binary

49

● We can!○ First bit right of the “radix point” represents ½.○ Second bit on the right represents ¼.○ Third bit represents ⅛.○ … and so on.

● Example: Convert 0.75 from decimal to binary.

Representing fractions as binary

50

● We can!○ First bit right of the “radix point” represents ½.○ Second bit on the right represents ¼.○ Third bit represents ⅛.○ … and so on.

● Example: Convert 0.75 from decimal to binary.

○ 0.75 = ¾ = ½ + ¼

○ 0.7510 = 0.112

Representing fractions as binary

51

● A tougher example: Convert 0.875 from decimal to binary.

○ We need a better algorithm, like for the big numbers:

■ Multiply N by 2. Next bit is the digit in the ones place.

■ Proceed with new N as the part to the right of the decimal point.

■ Continue until you have 0 remaining.

Representing fractions as binary

52

● A tougher example: Convert 0.875 from decimal to binary.

○ We need a better algorithm, like for the big numbers:

■ Multiply N by 2. Next bit is the digit in the ones place.

■ Proceed with new N as the part to the right of the decimal point.

■ Continue until you have 0 remaining.

○ Quick check: does this work for 0.7510 = 0.112 ?

Representing fractions as binary

53

● A tougher example: Convert 0.875 from decimal to binary.

○ We need a better algorithm, like for the big numbers:

■ Multiply N by 2. Next bit is the digit in the ones place.

■ Proceed with new N as the part to the right of the decimal point.

■ Continue until you have 0 remaining.

○ 0.875 * 2 = 1.75, so first bit right of the radix point is a 1.

■ Continue with 0.75.

○ 0.75 * 2 = 1.5, so next bit is a 1.

■ Continue with 0.5.

○ 0.5 * 2 = 1.0, so next bit is a 1.

■ Continue with 0. → Exit → Left with 0.87510 = 0.1112

Representing fractions as binary

54

Representing fractions as binary● Example: Convert 123.321 from decimal to binary.

55

● Example: Convert 123.321 from decimal to binary.

○ Do the part left of the decimal first.

■ 123 → Odd, so first bit is a 1. Proceed with (123-1)/2 = 61.

Representing fractions as binary

56

● Example: Convert 123.321 from decimal to binary.

○ Do the part left of the decimal first.

■ 123 → Odd, so first bit is a 1. Proceed with (123-1)/2 = 61.

■ 61 → Odd, so next bit is a 1. Proceed with (61-1)/2 = 30.

■ 30 → Even, so next bit is 0. Proceed with 30/2 = 15.

■ 15 → Odd, so next bit is a 1. Proceed with (15-1)/2 = 7.

■ 7 → Odd, so next bit is a 1. Proceed with (7-1)/2 = 3.

■ 3 → Odd, so next bit is a 1. Proceed with (3-1)/2 = 1.

■ 1 is a 1, so last bit is a 1.

■ Put it all together: 12310 = 11110112

Representing fractions as binary

57

● Example: Convert 123.321 from decimal to binary.

○ Do the part right of the decimal now.■ 0.321 * 2 = 0.642 → first bit is a 0

Representing fractions as binary

58

● Example: Convert 123.321 from decimal to binary.

○ Do the part right of the decimal now.■ 0.321 * 2 = 0.642 → first bit is a 0■ 0.642 * 2 = 1.284 → next bit is a 1■ 0.284 * 2 = 0.568 → next bit is a 0■ 0.568 * 2 = 1.136 → next bit is a 1■ 0.136 * 2 = 0.272 → next bit is a 0

■ 0.272 * 2 = 0.544 → next bit is a 0

■ 0.544 * 2 = 1.088 → next bit is a 1

■ … eventually find 0.32110 = 0.010100100012

■ … so 123.32110 = 1111011.010100100012

Representing fractions as binary

59

● Example: Convert 0.1 from decimal to binary.Representing fractions as binary

60

● Example: Convert 0.1 from decimal to binary.

○ 0.1 * 2 = 0.2, so first bit is a 0

○ 0.2 * 2 = 0.4, so next bit is a 0

○ 0.4 * 2 = 0.8, so next bit is a 0

○ 0.8 * 2 = 1.6, so next bit is a 1

○ 0.6 * 2 = 1.2, so next bit is a 1

○ 0.2 * … NOW HOLD ON A SECOND!

■ Restarts the pattern from here.

Representing fractions as binary

61

● Example: Convert 0.1 from decimal to binary.

○ 0.1 * 2 = 0.2, so first bit is a 0

○ 0.2 * 2 = 0.4, so next bit is a 0

○ 0.4 * 2 = 0.8, so next bit is a 0

○ 0.8 * 2 = 1.6, so next bit is a 1

○ 0.6 * 2 = 1.2, so next bit is a 1

○ 0.2 * … NOW HOLD ON A SECOND!

■ Restarts the pattern from here.

■ So 0.110 = 0.00011001100110011...2

Representing fractions as binary

62

● Example: Convert 0.1 from decimal to binary.

○ So 0.110 = 0.00011001100110011...2○ So computers would need to store an infinite number of bits in order

to store 0.110 exactly.

○ … obviously, computers can’t do that.

○ They truncate at a certain point, leading to some error.

Representing fractions as binary

63

● Example: Convert 0.1 from decimal to binary.

○ So 0.110 = 0.00011001100110011...2○ So computers would need to store an infinite number of bits in order

to store 0.110 exactly.

○ … obviously, computers can’t do that.

○ They truncate at a certain point, leading to some error.

○ How bad can this error be?

Representing fractions as binary

64

● How bad can this error be?

● Consider the function

f(a,b) = 333.75b6 + a2(11a2b2 - b6 - 121b4 - 2) + 5.5b8 + a/2b

where a=77617 and b=33096.

Representing fractions as binary

65

● How bad can this error be?

● Consider the function

f(a,b) = 333.75b6 + a2(11a2b2 - b6 - 121b4 - 2) + 5.5b8 + a/2b

where a=77617 and b=33096.

● If you run this on a 64-bit machine, you’ll findf(a,b) = -44450695952321879337122922496.000000, or maybef(a,b) = -1.180592e+21

(depending on the way the program you code in truncates)● But the true answer is more like

f(a,b) = -0.82739605...

Representing fractions as binary

66

Introduction and Binary Arithmetic

67

● Recap:

○ Today, we…

■ Confirmed that we are in the right classroom at the right time

■ Learned what the goals for this class are

■ Learned how to represent decimal numbers as binary, and vice versa

● Next time:

○ We talk logic!

top related