CS 40 - Lecture 20

CS is Not Just Programming
- I am not a programmer
- I am a computer scientist
- I like puzzles more than code
CS is Not Just Programming
- I am not a programmer
- I am a computer scientist
- I like puzzles more than code
- I make boring slides with no pictures
CS is Abstraction
- Set rules of a game
- Play by the rules
- Not about "trick questions"
- I like trick questions too:
- The cabin
- The bus driver
- The insane psychopath?
CS is Abstraction
- Set rules of a game
- Play by the rules
- Not about "trick questions"
- I like trick questions too:
- The cabin
- The bus driver
- The insane psychopath?
- That doesn't mean that I put them on tests.
Marines Crossing a Bridge
Four injured marines come to an old, rickety bridge, at night.
- Only two can cross at a time (the bridge is fragile).
- They need to use their lamp (they only have one).
- They can cross at various speeds, but two traveling together
must travel with slower marine.
- The bridge is wired to blow up at some time.
Marines Crossing a Bridge: Details
Four injured marines come to an old, rickety bridge, at night.
- Only two can cross at a time (the bridge is fragile).
- They need to use their lamp (they only have one).
- They can cross at various speeds, but two traveling together
must travel with slower marine.
- The bridge is wired to blow up at some time.
Lets add some details:
- Their speeds: 2, 3, 9, 11 minutes.
- The bridge will blow up in 25 minutes.
Can they make it across in time?
Use resources wisely.
Error Detecting Codes
Consider a number:
94783341609
Add one extra digit to make the digit sum "%10 == 0"
947833416096
Why? It gives good probability to find an error
947833476096 % 10 = 6
- Communication or transmission errors
- Fraud detection (depends on theives not knowing code).
- Scanning error (UPC/bar codes)
For CS, we usually just use binary.
- %2 also known as XOR (exclusive or)
- Count number of 1s. If even, XOR=0
- Count number of 1s. If odd, XOR=1
Error CORRECTING Codes
Pick one of the following 7 bit numbers:
0000000,
1101001,
0101010,
1000011,
1001100,
0100101,
1100110,
0001111,
1110000,
0011001,
1011010,
0110011,
0111100,
1010101,
0010110,
1111111
Write it down carefully. No transcription errors allowed.
For your code:
- Let r = b1 XOR b3 XOR b5 XOR b7
- Let s = b2 XOR b3 XOR b6 XOR b7
- Let t = b4 XOR b5 XOR b6 XOR b7
All (r, s, t) should be zero.
Error Correcting Codes
Flip one bit in your code. (Don't tell me which.) For your
altered code, calculate:
- Let r = b1 XOR b3 XOR b5 XOR b7
- Let s = b2 XOR b3 XOR b6 XOR b7
- Let t = b4 XOR b5 XOR b6 XOR b7
Now, at least one of (r, s, t) should be one.
Error Correcting Codes
Flip one bit in your code. (Don't tell me which.) For your
altered code, calculate:
- Let r = b1 XOR b3 XOR b5 XOR b7
- Let s = b2 XOR b3 XOR b6 XOR b7
- Let t = b4 XOR b5 XOR b6 XOR b7
Now, at least one of (r, s, t) should be one.
Calculate x = r * 1 + s * 2 + t * 4.
- It should be a number, 1 to 7.
Error Correcting Codes
Flip one bit in your code. (Don't tell me which.) For your
altered code, calculate:
- Let r = b1 XOR b3 XOR b5 XOR b7
- Let s = b2 XOR b3 XOR b6 XOR b7
- Let t = b4 XOR b5 XOR b6 XOR b7
Now, at least one of (r, s, t) should be one.
Calculate x = r * 1 + s * 2 + t * 4.
- It should be a number, 1 to 7.
- It should also be the number of the bit you flipped.
- We can correct any single bit flip in this code.
Error Correcting Codes
Here, 3 error correcting bits (positions 1, 2, 4) were added to
four original code word bits. We use 7 bits to transmit 4 bits of
information, but can correct if a bit is flipped, or detect if two
bits have flipped.
Similarly, we can use 4 error correcting bits (postions 1, 2, 4,
8), along with 11 original bits, and still correct one bit flip.
Can you guess the pattern for how we set the error correcting bits
in the word of 15 bits?
This can be extended indefinitely. We only need 10 error
correcting bits, with 1013 original code word bits, to correct for one
bit flip in a word of 1023 bits.
Codes to correct two or more errors also exist, though the need
more error correcting bits per code word bit.
Another White/Black Hat Game
- You are two team mates are each given a hat, either white or black
(randomly assigned).
- You can see your team mates' hats, but not your own.
- You must try to guess the color of your own hat.
- There is no communication between teammates.
You win if:
- At least one of the three attempts to guess (you may choose to not
guess).
- ALL who guess, guess correctly
Otherwise, you lose.
How well can your team do?
Another White/Black Hat Game
- Half of all guesses are expected to be wrong.
- There is no cheating, or getting around the above fact.
- But, who cares?
- Back to "resource usage" of the marines problem
Another White/Black Hat Game
- Half of all guesses are expected to be wrong.
- There is no cheating, or getting around the above fact.
- But, who cares?
- Back to "resource usage" of the marines problem
Strategy:
- Each player will assume that all three hats are not the same
color, and guess, or pass, accordingly.
- What are the team's odds of winning?
- Either one of three guesses, correctly, or all three guess incorrectly!
- Win 75% of time.
Another White/Black Hat Game
- What if hats are NOT assigned randomly? We still get a team
meeting to strategize before hats are assigned, but cannot communicate
once hats are distributed. Can you still beat the "hat assigner" more
than half the time?
- What if there were 7 teammates instead of 3?
- What if there were 1100 teammates? How often should your team win?
Another White/Black Hat Game
- What if hats are NOT assigned randomly? We still get a team
meeting to strategize before hats are assigned, but cannot communicate
once hats are distributed. Can you still beat the "hat assigner" more
than half the time?
- What if there were 7 teammates instead of 3?
- What if there were 1100 teammates? How often should your team win?
This is the error correcting code in disguise!
NIM (time permitting)
We saw this in our AI class, but we can play a bit in class if we
have time.
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
5 15 3 7 12 2 18
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
5 15 3 7 12 2 18
5 3 7 12 2 15 18
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
5 15 3 7 12 2 18
5 3 7 12 2 15 18
5 3 7 2 12 15 18
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
5 15 3 7 12 2 18
5 3 7 12 2 15 18
5 3 7 2 12 15 18
5 3 2 7 12 15 18
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
5 15 3 7 12 2 18
5 3 7 12 2 15 18
5 3 7 2 12 15 18
5 3 2 7 12 15 18
3 2 5 7 12 15 18
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
5 15 3 7 12 2 18
5 3 7 12 2 15 18
5 3 7 2 12 15 18
5 3 2 7 12 15 18
3 2 5 7 12 15 18
2 3 5 7 12 15 18
Efficiency and Sorting? (time permitting)
We saw a sorting algorithm in the homework on Lists. Lets consider
the "find Max value" approach on the following data:
5 15 3 18 7 12 2
5 15 3 7 12 2 18
5 3 7 12 2 15 18
5 3 7 2 12 15 18
5 3 2 7 12 15 18
3 2 5 7 12 15 18
2 3 5 7 12 15 18
2 3 5 7 12 15 18
Wow, that was painful. For each iteration, we needed to go through
almost the same length list as the previous iteration. (One shorter
each time.)
Efficiency and Sorting? (time permitting)
Consider instead a "Divide and Conquer" strategy:
5 15 3 18 7 12 2
Efficiency and Sorting? (time permitting)
Consider instead a "Divide and Conquer" strategy:
5 15 3 18 7 12 2
3 2 5 15 18 7 12
Efficiency and Sorting? (time permitting)
Consider instead a "Divide and Conquer" strategy:
5 15 3 18 7 12 2
3 2 5 15 18 7 12
2 3 5 15 18 7 12
Efficiency and Sorting? (time permitting)
Consider instead a "Divide and Conquer" strategy:
5 15 3 18 7 12 2
3 2 5 15 18 7 12
2 3 5 15 18 7 12
2 3 5 7 12 15 18
Efficiency and Sorting? (time permitting)
Consider instead a "Divide and Conquer" strategy:
5 15 3 18 7 12 2
3 2 5 15 18 7 12
2 3 5 15 18 7 12
2 3 5 7 12 15 18
2 3 5 7 12 15 18
Efficiency and Sorting? (time permitting)
Consider instead a "Divide and Conquer" strategy:
5 15 3 18 7 12 2
3 2 5 15 18 7 12
2 3 5 15 18 7 12
2 3 5 7 12 15 18
2 3 5 7 12 15 18
2 3 5 7 12 15 18
Much better! After the 1st iteration, all others are much quicker.
Reminders

- Next class, December 2: Project presentations. All must be ready to
present that day.
- December 7: Last day of classes, and any projects we don't get
to on December 2.