winter 2016 comp-250: introduction to computer...

26
Winter 2016 COMP-250: Introduction to Computer Science Lecture 3, January 19, 2016

Upload: others

Post on 24-Jan-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Winter 2016COMP-250: Introduction

to Computer ScienceLecture 3, January 19, 2016

Page 2: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Thu 10:00Thu 10:30Thu 11:00

Mon 11:30 Tue 11:30 Wed 11:30 Thu 11:30 Fri 11:30Mon 12:00 Tue 12:00 Thu 12:00Mon 12:30 Tue 12:30 Thu 12:30Mon 13:00 13:00Mon 13:30 WED 13:30Mon 14:00 WED 14:00Mon 14:30 Tue 14:30 WED 14:30 Thu 14:30Mon 15:00 Tue 15:00 Wed 15:00 Fri 15:00Mon 15:30 Tue 15:30 Wed 15:30 Fri 15:30Mon 16:00 Tue 16:00 Wed 16:00 Fri 16:00

DoYeonTR-3110

DavidB-RTR-3110

FaizyTR-3110

OmarMC-102

ClaudeST-S1/4

ClaudeST-S1/4

ClaudeMC-110N

HardikTR-3110

ChrisTR-3110

DavidBTR-3110

FaizTR-3110

MC=MCENG ST=STBIO TR=TRENG

COMP-250 Weekly Schedule

Page 3: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Analysis of Algorithms

Page 4: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Analysis of Addition

{cst

{cst

{cst{linear

Time(N) = c1 + c2⨯N

Page 5: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Analysis of Multiplication

{cst

{cst

{cst{linear{quadratic

Page 6: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

{quadraticAnalysis of Multiplication

{cst

{cst

cst{

linear{

{cst

{cst

Page 7: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Time(N) = c1 + c2⨯N + c3⨯N2

Analysis of Algorithms

Page 8: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Analysis of Algorithms

Time(N) = c1 + c2⨯N + c3⨯N2

Time(N) = c1 + c2⨯N

Multiplication

Addition

Page 9: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Analysis of Algorithms

Time(N) is O(N2)

Time(N) is O(N)

Multiplication

Addition

Page 10: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Analysis of Algorithms

Multiplication

Addition

UnaryRepresentation

Decimal/binaryRepresentation

50x x2 2x

Page 11: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Today, the best known Multiplicationalgorithm has running time

O(N⨯2log* N) barely slower than Addition… ( log* N = number of log until < 1 )

Analysis of Algorithms

Addition

50x x2 2x

MultiplicationMultiplicationMultiplicationMultiplication

Multiplication

Page 12: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Bases and Binary Representation

Page 13: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Base 8 vs Base 2

=(????)2

(2143)8

Page 14: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

=( 010 001 100 101 )2 =(10001100101)2

(2143)8

Base 8 vs Base 2

Page 15: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

(10010010101010101)2 =(10 010 010 101 010 101)2

=(2 2 2 5 2 5)8 =(222525)8

Base 2 vs Base 8

Page 16: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

(10010010101010101)2 =(65536+8192+1024+256+64+16+4+1)10

=(75093)10

Base 2 vs Base 10

Page 17: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

20=1 21=2 22=4 23=8 24=16 25=32 26=64 27=128 28=256 29=512 210=1024 211=2048 212=4096 213=8192 214=16384 215=32768 216=65536 232 = 4 294 967 296

Powers of 2 in Base 10

Page 18: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

100=1 101=1010 102=1100110 103=1111101000 ≈210 104=10011100010000

Powers of 10 in Base 2

Page 19: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

(75093)10 =(111*10011100010000 .

+101*1111101000 . +1001*1010 .

+101)2 =(10010010101010101)2

Base 10 vs Base 2

Page 20: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

to Base 2

Page 21: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

75093 /2 %2 37546 1 18773 0 9386 1 4693 0 2346 1 1173 0 586 1 293 0 146 1

/2 %2 73 0 36 1 18 0 9 0 4 1 2 0 1 0 0 1

=(10010010101010101)2

Page 22: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

why does it work ?

m = 2* (m/2) + m%2

10010010101010101 = 2*1001001010101010 + 1

m = 𝛃* (m/𝛃) + m%𝛃

Page 23: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

why does it work ?m = ∑i

n=-01 bi𝛃i

= (bn-1bn-2…b1b0)𝛃 = (bn-1bn-2…b10)𝛃 + b0 = 𝛃*(bn-1bn-2…b1)𝛃 + b0

= 𝛃*(bn-1bn-2…b1b0/𝛃) + b0 m = 𝛃* (m/𝛃) + m%𝛃

Page 24: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

to Base 2

𝛃

𝛃𝛃

𝛃

Page 25: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

75093 /5 %5 15018 3 3003 3 600 3 120 0 24 0 4 4 0 4

=(4400333)5

Page 26: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-3.pdfTR-3110 DavidB-R TR-3110 Faizy TR-3110 Omar MC-102 Claude ST-S1/4 Claude ST-S1/4

Winter 2016COMP-250: Introduction

to Computer ScienceLecture 3, January 19, 2016