Professional Documents
Culture Documents
DATA
STRUCTURES
Fall 2022
THE BORING STUFF
• Administrative Issues
• Evaluation
2
THE GOOD STUFF
3
ADMINISTRATIVE
4
ADMINISTRATIVE
• Lecture Hours
– Mondays 08:40 - 09:30 (FENS G077)
– Fridays 14:40 - 16:30 (FMAN 1099)
5
ADMINISTRATIVE
• Recitation Hours
– A1-A3 : Tuesdays 16:40 - 18:30
–B : Tuesdays 14:40 - 16:30
– Recitations will be held to
• Cover additional lectures and materials
• Review course material
• Discuss homeworks
• Solve data structures-related interview questions
• Exams
– No recitation this week!
6
ADMINISTRATIVE
• Office Hours
– Tuesdays 08:40-10:30 (physical or online)
7
ADMINISTRATIVE
8
ADMINISTRATIVE
• TAs
– Engincan Varan (evaran@sabanciuniv.edu)
– Elif Ecem Şamlıoğlu ( esamlioglu@sabanciuniv.edu)
– Berkay Demireller (bdemireller@sabanciuniv.edu)
• LAs
– Muhammed Orhun Gale (morhun@sabanciuniv.edu)
– Mert Dursun (mertdursun@sabanciuniv.edu)
– Oktay Çelik (celikoktay@sabanciuniv.edu)
9
ADMINISTRATIVE
10
EVALUATION
• 2 Midterms (32%)
11
EVALUATION
• 2 Midterms (32%)
• Homeworks (28%)
12
EVALUATION
• 2 Midterms (32%)
• Homeworks (28%)
• Final (30%)
13
EVALUATION
• 2 Midterms (32%)
• Homeworks (28%)
• Final (30%)
• Weekly quizzes (10%)
14
EVALUATION
• 2 Midterms (32%)
• Homeworks (28%)
• Final (30%)
• Weekly quizzes (10%)
15
EXAMS
• You need to come to exams with a picture
identification (SU Picture ID, etc.).
• Otherwise, you will NOT permitted to take
the exam. NO EXCEPTIONS.
16
HOMEWORKS
17
HOMEWORKS
18
HOMEWORKS
19
MAKE-UP EXAMS
• If you miss an exam, no makeup exams will be granted unless
you have a documented emergency situation and notify the
instructor within 48 hours after the exam date
• The instructor reserves the right as to when and how a
makeup exam will be granted
20
Frequently (Un)asked Questions
21
Frequently (Un)asked Questions
22
Frequently (Un)asked Questions
23
Frequently (Un)asked Questions
• How are programming homeworks graded?
– Useless effort does not get credit.
– Every program will have to pass a series of tests of
functionality and documentation!
– All the assignments are due 11:55pm on their due dates
– 10% penalty will be incurred for each day of overtime!
– Submissions that are late by more than 3 days will not
get any credit
24
Frequently (Un)asked Questions
25
Frequently (Un)asked Questions
26
Frequently (Un)asked Questions
27
Frequently (Un)asked Questions
• Then, what do you ask in exams?
– Mostly questions that check
• if you have understood the basic theoretical and abstract
aspects of the course, and can apply them.
– In addition to programming skills, problem solving
skills, you need to have a very good understanding
the details vs the general picture.
– Exam questions will require intelligence ()
28
Frequently (Un)asked Questions
29
Frequently (Un)asked Questions
30
WHAT IS CS 300 ABOUT?
31
WHAT IS CS 300 ABOUT?
32
WHAT IS CS 300 ABOUT?
33
WHAT IS CS 300 ABOUT?
34
BUT!
35
ADVANCES IN TECHNOLOGY
• Moore’s Law
– The speed of processors double about every 18-24
months
– The capacity of memory chips double about every
18-24 months
– The prices of semiconductor chips go down about
the same rate
• BUT the wall is close
– New technologies (optical, molecular, quantum
computing)
36
Tunneling effect
37
NOT SO!
39
NOT SO!
40
NOT SO!
41
FUN!
42
FUN!
43
FUN OR PAIN?
44
DON’T WORRY!
WE WILL HAVE FUN
• You will learn to
– Attack underspecified problems
45
DON’T WORRY!
WE WILL HAVE FUN
• You will learn to
– Attack underspecified problems
• What is an “underspecified problem”?
46
DON’T WORRY!
WE WILL HAVE FUN
• You will learn to
– Attack underspecified problems
• What is an “underspecified problem”?
– Fill in any blanks
47
DON’T WORRY!
WE WILL HAVE FUN
• You will learn to
– Attack underspecified problems
• What is an “underspecified problem”?
– Fill in any blanks
– Make decisions
48
DON’T WORRY!
WE WILL HAVE FUN
• You will learn to
– Attack underspecified problems
• What is an “underspecified problem”?
– Fill in any blanks
– Make decisions
– Use techniques you learned
49
CS 300 OVERVIEW
• Week 1
– Deer Talk
• Overview of CS,
• History of Computers etc
– Overview of Computers and Data Representation
50
CS 300 OVERVIEW
• Week 2
– Introduction to Algorithm Analysis
• Problems, Algorithms
• Modeling Runtimes
• O(f(n)) and (f(n)) notation
• Simple Recurrences and their solutions
• Worst-case and average case analyses
• Then off to data structures
51
CS 300 OVERVIEW
52
rek-lam-LAR
53
Computer Science and Engineering?
54
Computer Science and Engineering?
55
Computer Science and Engineering?
56
OVERVIEW
57
OVERVIEW
• What is Computer Science and Engineering?
• What skills does one need?
• What will you learn here?
• What will you NOT learn here?
• What will you be when you grow up?
58
The Burning Question
59
The Burning Question
60
The Burning Question
61
The Burning Question
62
What is Computer Science?
63
What is Information?
• Hard to say!
64
What is Information?
• Hard to say!
– You know when you have it (and when you don't
have it), but
65
What is Information?
• Hard to say!
– You know when you have it (and when you don't
have it), but
– You can't touch it!
66
What is Information?
• Hard to say!
– You know when you have it (and when you don't
have it), but
– You can't touch it!
– It takes energy, time, money to produce it, but yet it
is very abstract.
67
What is Information?
• Hard to say!
– You know when you have it (and when you don't
have it), but
– You can't touch it!
– It takes energy, time, money to produce it, but yet it
is very abstract.
– You can store it for later use (if you don't, you lose
it permanently)
68
What is Information?
• Hard to say!
– You know when you have it (and when you don't
have it), but
– You can't touch it!
– It takes energy, time, money to produce it, but yet
it is very abstract.
– You can store it for later use (if you don't, you lose
it permanently)
– You can duplicate it, you can sell it or you can buy
it.
69
Wht s nfrmtn?
• Hrd t sy!
– Y knw whn y hv t (nd whn y dn't hv t), bt
– Y cn't tch t!
– t tks nrgy, tm, mny t prdc t, bt yt t s vry bstrct.
– Y cn str t fr ltr s (f y dn't, y ls t prmnntly)
– Y cn dplct t, y cn sll t r y cn by t.
70
What is information?
71
What is information?
72
What is information?
73
What is Computer Engineering?
74
What do Computer Engineers do?
• They build systems that process information.
75
Building Systems
76
Interdisciplinary Cooperation
Electronics Engineering
Computer
(Microelectronics,
Science
Telecommunications)
INTELLIGENT
PRODUCTS AND SERVICES
OF THE FUTURE
77
Some important & interesting problems
79
Research
Segmentation (potential boundaries)
Normalization
Recognition (Neural Networks)
Transcribed text
• Hardware Technologies
• Software Technologies
• Theoretical Results
• New Approaches
83
Technology
• Speech Recognition
• Computer Vision
• Understanding Natural Language
• Cryptography
• Internet
• Electronic Publishing
84
Technology
• Electronic Commerce
• Ubiquitous Computing (Computers everywhere
but invisible)
• Convergence of mobile phone, PDA, internet
access all in one device, wearable computers
(your jacket is your computer!)
85
Technology
86
Skills of a good computer engineer
• Good, open mind
– things are changing faster that you think
– Rate of change of change is increasing
• Accelerating change
• Common sense
– engineering, like politics, is the art of compromises
• Ability to analyze
– crucial!
• Ability to communicate
– written, verbal, bi-lingual (at least!)
87
Skills of a good computer engineer
• Ability to abstract
– understand the detail, but
always in relation to the Ab s tr a c tio n :
whole, Ab s tr a c t a wa y th thee in e s s e ntia l
fe a tu r e s o f a p r o b le m o r s o lu tio n
– know how to forget or
avoid the details when
necessary)
• Ability to synthesize =
(remember, this is
engineering, you HAVE
TO SYNTHESIZE !)
88
Skills of a good computer engineer
• Ability to learn and generalize
– from successes and failures
• Ability to do trade-offs
– you do not need a 3 Ghz Pentium 4 if all you need to
do is word processing!
• Intel thinks otherwise ()
• Ability to justify hard decisions
• Very good understanding of basic techniques of
the field.
89
Skills of a good computer engineer
90
Skills of a good computer engineer
91
Skills of a good computer engineer
• Debugging
– this usually ends up being 50% of the whole job,
and gives the field a bad name!
• Evaluation
– of functionality, maintainability, speed, memory
req., manufacturability, price, esthetics
92
What will you learn in the CS
program ?
• Software Engineering
• Basics of Computer Hardware
• Basics of the Theory of Computation
• Theory and Practice behind Programming Languages
• Operating Systems
• Database Systems
• Algorithm Design and Analysis
• Computer Networks and Distributed Systems
• Artificial Intelligence
• Computer Graphics
• System security
93
What you will NOT learn!
94
What you will NOT learn!
95
What will you be when you grow up?
• Job opportunities
– Software Companies (Google, Apple, Microsoft, etc.)
– IT Departments
• Banks (Web development, security, speech processing, databases,e.g.,
Akbank and Garanti Teknoloji)
• Manufacturing (Databases, process automation, simulation, vision,
e.g., Vestel, Arçelik)
• Media (Graphics, Web development, text processing, information
retrieval)
• Telecom (Networking, security)
• General (System Management)
– Startup companies
– Visual Communications: Computer Games, Special Effects
96
What will you be when you grow up?
• Graduate Study
– Master of Science (M. Sc.) (2 years – Advanced
Courses + Research+Thesis)
– Doctor of Philosophy (Ph. D.) (4-5 years – more
advanced courses + original research+Thesis)
97
What will you be when you grow up?
• Job Opportunities
– Faculty Members at Universities
– Researchers at advanced research labs
98
Specialization
Depth
Ph. D.
Expert
M. Sc.
B. Sc.
General Practitioner
Breadth
99
Specialization
100
Specialization
101
Specialization
102
Specialization
M. Sc.
B. Sc.
General Practitioner
Breadth 103
A Lil’ bit of History
104
First Generation (1951-1959)
105
First Generation (1951-1959)
106
Punched Cards
107
Punched Cards
108
Punched Cards
109
A personal history of computer technology
110
Second Generation (1959-1965)
• 1960's:
– 100 KHz Machines (IBM 1620),
– 20-30 KB Memory,
– Punched Card Input,
– Teletypes, Line Printers,
– 10-20 MB Disks
– (Consume kilowatts, needs AC cooling)
(ARPANET starts about here also)
– Magnetic disks
111
Third Generation (1965 - 1971)
• Integrated circuits
• Keyboard and screen
• Transistors for memory construction
112
Forth Generation (1971 - ?)
113
Very Large Scale Integrated Circuit
Technology
• Xbox One SoC, 2013
– 5 trillion transistors
– 853 MHz
– 768 cores
114
Very Large Scale Integrated Circuit
Technology
• Moore’s “ Law”
– Transistors double
every 18-24
months
– Memory capacity
doubles every 18-
24 months
– The prices of
semiconductor
chips go down
about the same
rate.
115
Moore’s Law
116
Other trends
117
Where we are heading?
Single CPU
Multiple Users
1 CPU per
Person
Many CPUs
per Person
118
OK Enough Deer Talk
119
COMPUTERS- A BRIEF REVIEW
• What’s in a computer?
120
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
122
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
123
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
124
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
125
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
On-board Cache
BUS
126
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
On-chip Cache
On-board Cache
BUS
127
COMPUTERS
Motherboard
Random Access
Central Processing Unit Memory
(CPU) (RAM)
On-chip Cache
On-board Cache
BUS
128
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
On-chip Cache
On-board Cache
BUS
Fast: < 1 ns
Important Fact. Light travels 30 cm in one nanosecond
129
COMPUTERS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
Internet
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
Internet
Random Access
Central Processing Unit Memory
(CPU) (RAM)
BUS
Internet
Random Access
Central Processing Unit Memory
(CPU) (RAM)
• Bytes (8 bits)
– 0.1 bytes: A single yes/no decision (actually 0.125
bytes, but I rounded)
– 1 byte: One character
– 10 bytes: One word (a word of language, not a
computer word)
– 100 bytes: Telegram; two punched computer
(Hollerith) cards
137
How Much Memory Is That?
• Kilobyte
1,024 bytes; 210; approx. 1,000 or 103
– 1 Kilobyte: Joke; (very) short story
– 2 Kilobytes: Typewritten page
– 10 Kilobytes: Page out of an encyclopedia
– 50 Kilobytes: Image of a document page, compressed
– 100 Kilobytes: Photograph, low-resolution
– 200 Kilobytes: Two boxes (4000) punched computer
(Hollerith) cards
– 500 Kilobytes: Five boxes, one case (10,000 of punched
computer (Hollerith) cards
138
How Much Memory Is That?
• Megabyte
1,048,576 bytes; 220; approx 1,000,000 or 106
– 1 Megabyte: Small novel; 3-1/2 inch diskette
– 2 Megabytes: Photograph, high resolution
– 5 Megabytes: Complete works of Shakespeare; 30 seconds of
broadcast-quality video
– 10 Megabytes: Minute of high-fidelity sound; digital chest
X-ray; Box of 3-1/2 inch diskettes
– 20 Megabytes: Two boxes of 3-1/2 inch diskettes
– 50 Megabytes: Digital mammogram
– 100 Megabytes: Meter of books on a shelf; two encyclopedia
volumes
– 700 Megabytes: CD-ROM
139
How Much Memory Is That?
• Gigabyte
1,073,741,824 bytes; 230; approx
1,000,000,000 or 109
– 1 Gigabyte: Paper in the bed of a pickup; symphony
in high-fidelity sound; broadcast quality movie
– 2 Gigabytes: 20 meters of books on a shelf
– 20 Gigabytes: Audio collection of the works of
Beethoven; VHS tape used to store digital data
– 50 Gigabytes: Library floor of books on shelves
140
How Much Memory Is That?
• Terabyte
1,099,511,627,776 or 240;
approx. 1,000,000,000,000 or 1012
– 1 Terabyte: All the X-ray films in a large
technological hospital; 50,000 trees made into paper
and printed;
– 2 Terabytes: Academic research library
– 10 Terabytes: Printed collection of the U. S. Library
of Congress
– 50 Terabytes: Contents of a large mass storage
system (Google uses many many of these)
141
How Much Memory Is That?
• Petabyte
1,125,899,906,842,624 bytes or 250
approx. 1,000,000,000,000,000 or 1015
– 2 Petabytes: All U. S. academic research libraries
– 20 Petabytes: 1995 production ALL hard-disk
drives
– 200 Petabytes: All printed material;
142
Petabyte Memory
143
Petabyte Memory
144
How Much Memory Is That?
• Exabyte
1,152,921,504,606,846,976 bytes or 260
approx. 1,000,000,000,000,000,000 or 10 18
– 5 Exabytes: All words ever spoken by human
beings.
145
How Much Memory Is That?
• Exabyte
1,152,921,504,606,846,976 bytes or 260
approx. 1,000,000,000,000,000,000 or 10 18
– 5 Exabytes: All words ever spoken by human
beings.
– Not very clear you would want that in your laptop!
146
PERFORMANCE
147
PERFORMANCE
148
PERFORMANCE
• Some Important non-technical laws
– Software (usually by one company) grows to
suck up the CPU speed
150
MEMORY REPRESENTATION
151
MEMORY REPRESENTATION
152
MEMORY REPRESENTATION
0
4
8
12
16
18
….
536,870,908
153
MEMORY REPRESENTATION
154
MEMORY REPRESENTATION
155
MEMORY REPRESENTATION
156
INTEGERS
157
INTEGERS
158
INTEGERS
• We represent integers using either
– 16 bits (2 bytes) (short integers)
– 32 bits (4 bytes) (integers, long integers)
– 64 bits (8 bytes)
– Short integers start at even addresses.
– Integers start at addresses which are divisible by 4
159
2’s COMPLEMENT NOTATION
162
NEGATIVE NUMBERS
• Negative of 0 = (0000)
– Complement 0000 → 1111
– Add 1 to 1111 → 10000, ignore the carry, we
get back 0000, which is what we should expect.
• Negative of 2 = (0010)
– Complement 0010 → 1101
– Add 1 to 1101 → 1110
– Note that sign bit of 1110 is 1
163
2’s COMPLEMENT NOTATION
• Let’s assume n = 4
• Here are the representations for 0 and positive numbers
AND negative numbers
– 0 (0000)
– 1 (0001) -1 (1111)
– 2 (0010) -2 (1110)
– 3 (0011) -3 (1101)
– 4 (0100) -4 (1100)
– 5 (0101) -5 (1011)
– 6 (0110) -6 (1010)
– 7 (0111) -7 (1001)
164
2’s COMPLEMENT NOTATION
• Let’s assume n = 4
• Here are the representations for 0 and positive numbers
AND negative numbers
– 0 (0000)
– 1 (0001) -1 (1111) -8 (1000) but there is no 8
– 2 (0010) -2 (1110)
– 3 (0011) -3 (1101)
– 4 (0100) -4 (1100)
– 5 (0101) -5 (1011)
– 6 (0110) -6 (1010)
– 7 (0111) -7 (1001)
165
2’s COMPLEMENT NOTATION
• What is 2+ (-2) ?
0010
1110
----
Ignore the final carry out
10000
166
OVERFLOW
167
OVERFLOW
• What is 5 + 7?
0101
0111
----
1100
168
OVERFLOW
• What is 5 + 7?
0101
0111
----
1100
173
UNSIGNED INTEGERS
174
FLOATING POINT NUMBERS
175
FLOATING POINT NUMBERS
• Floating point numbers are meant to represent
the real numbers.
• They are represented with three components
– A mantissa (m)
– An exponent (e)
– A sign (s)
• The mathematical value of a floating point
number is
– +/- m 2 e
176
FLOATING POINT NUMBERS
177
FLOATING POINT NUMBERS
178
FLOATING POINT NUMBERS
180
FLOATING POINT NUMBERS
181
FLOATING POINT NUMBERS
182
FLOATING POINT NUMBERS
183
CHARACTERS AND STRINGS
184
ADDRESSES
185
ADDRESSES
186
ADDRESSES
187
ADDRESSES
188
ADDRESSES
189
ADDRESSES
190
COMPUTER SYSTEMS
Hardware
191
COMPUTER SYSTEMS
Hardware
192
COMPUTER SYSTEMS
Application Support
(Window Managers, Graphics, User Interfaces, Development Env.)
Hardware
193
COMPUTER SYSTEMS
Application Support
(Window Managers, Graphics, User Interfaces, Development Env.)
Hardware
194
COMPUTER SYSTEMS
Application Support
(Window Managers, Graphics, User Interfaces, Development Env.)
Hardware
195
COMPUTER SYSTEMS
• Structural Complexity
– Hardware – Several trillion transistors
– Software – 100’s of millions of lines of code
• Windows XP: 45 millions of lines of source code
• Premium-class automobile: 100 millions of lines of code
196
COMPUTER SYSTEMS
• Structural Complexity
– Hardware – Several trillion transistors
– Software – 100’s of millions of lines of code
• Windows XP: 45 millions of lines of source code
• Premium-class automobile: 100 millions of lines of code
197
COMPUTER SYSTEMS
198
COMPUTER SYSTEMS
• We have small pieces
(objects, libraries) that
have
– certain functionality
– and
– INTERFACE
• how they “ fit” with others
199
COMPUTER SYSTEMS
• We have small pieces
that have
– certain functionality
– and
– INTERFACE
• how they “ fit” with others
• We put them together to
achieve a goal
– The interfaces have to fit
– The final functionality is
what we want
200
COMPUTER SYSTEMS
201
ENOUGH WITH COMPUTERS
202
ALGORITHM ANALYSIS
203
ALGORITHM ANALYSIS
204
ALGORITHM ANALYSIS
205
ALGORITHM ANALYSIS
206
ALGORITHM ANALYSIS
207
ALGORITHM ANALYSIS
208
ALGORITHM ANALYSIS
209
ALGORITHM ANALYSIS
211
ALGORITHM ANALYSIS
212
ALGORITHM
213
ALGORITHM
214
ALGORITHM - PROGRAM
• A program is
– an implementation of an algorithm
– using a specific programming language
– to execute on specific platforms
215
THE INPUT
216
THE INPUT
217
THE INPUT
218
THE INPUT
219
COMPUTATIONAL COMPLEXITY
220
COMPUTATIONAL COMPLEXITY
221
COMPUTATIONAL COMPLEXITY
222
COMPUTATIONAL COMPLEXITY
• Or more abstractly, is x S ?
– S is a set of objects and |S| = N
223
COMPUTATIONAL COMPLEXITY
224
COMPUTATIONAL COMPLEXITY
225
COMPUTATIONAL COMPLEXITY
226
COMPUTATIONAL COMPLEXITY
227
COMPUTATIONAL COMPLEXITY
228
COMPUTATIONAL COMPLEXITY
229
COMPUTATIONAL COMPLEXITY
230
COMPUTATIONAL COMPLEXITY
231
COMPUTATIONAL COMPLEXITY
232
COMPUTATIONAL COMPLEXITY
233
COMPUTATIONAL COMPLEXITY
?
234
COMPUTATIONAL COMPLEXITY
235
COMPUTATIONAL COMPLEXITY
236
COMPUTATIONAL COMPLEXITY
238
COMPUTATIONAL COMPLEXITY
241
COMPUTATIONAL COMPLEXITY
243
CLASSES OF PROBLEMS
244
CLASSES OF PROBLEMS
245
CLASSES OF PROBLEMS
246
CLASSES OF PROBLEMS
247
SEARCH
248
SEARCH
249
SEARCH
250
SEARCH
251
SEARCH
252
SEARCH
253
SEARCH
254
SEARCH
255
SEARCH
256
SEARCH
257
CLASS OF PROBLEMS
258
WHAT DO WE MEASURE?
259
WHAT DO WE MEASURE?
• Hardware dependent
– Same program will take different times on different
machines.
260
WHAT DO WE MEASURE?
• Time (milliseconds, seconds, weeks)
• Hardware dependent
– Same program will take different times on different
machines.
• If a program runs in 10 minutes on a 3.8Ghz
Pentium i7 but another program (for the same
problem) runs in 20 minutes on a 400 Mhz
SparcStation, which one is the better program?
261
WHAT DO WE MEASURE?
• Hardware dependent
– Same program will take different times on different
machines.
262
WHAT DO WE MEASURE?
263
WHAT DO WE MEASURE?
264
WHAT DO WE MEASURE?
265
WHAT DO WE MEASURE?
266
EXAMPLE
• Matrix Multiplication
267
EXAMPLE
N(
269
EXAMPLE
N (N (
270
EXAMPLE
N(N(1+
271
EXAMPLE
N(N(1+N(
272
EXAMPLE
N ( N ( 1 + N ( 1)))
273
EXAMPLE
N ( N ( 1 + N ( 1))) = N3 + N2 steps
274
EXAMPLE
276
EXAMPLE
279
EXAMPLE
281
BACK TO ELEMENTARY SCHOOL
282
BACK TO ELEMENTARY SCHOOL
283
BACK TO ELEMENTARY SCHOOL
284
BACK TO ELEMENTARY SCHOOL
286
Multiplication
287
Multiplication
• Gauss’ Hack
– X1 = a + b
– X2 = c + d
– X3 = X1*X2 = ac+ad+bc+bd
– X4 = a*c
– X5 = b*d
– X6 = X4 – X5 = ac-bd
– X7 = X3 – X4 – X5 = ad+bc
288
Multiplication
• Gauss’ Hack
– X1 = a + b
3 Multiplications and
– X2 = c + d 5 additions
– X3 = X1*X2 = ac+ad+bc+bd
– X4 = a*c
– X5 = b*d
– X6 = X4 – X5 = ac-bd
– X7 = X3 – X4 – X5 = ad+bc
• Total 3.50 TL, a 17% improvement!
289
MULTIPLICATION
290
MULTIPLICATION
291
ADDITION
* * * * * * 1012(5)
* * * * * * 11112(15)
+ +
-------------- ---------
? ? ? ? ? ? ? 101002(20)
292
ADDITION
*
* * * * * *
* * * * * *
+
--------------
*
293
ADDITION
* *
* * * * * *
* * * * * *
+
--------------
* *
294
ADDITION
* * *
* * * * * *
* * * * * *
+
--------------
* * *
295
ADDITION
• .....
* * * * * *
* * * * * *
* * * * * *
+
--------------
* * * * * * *
296
ADDITION
• ..... • For all practical
* *
* * * * *
* * * * * purposes
* * * * * * – adding two bits and
+ – generating a carry and
-------------- a sum
* * * * * * * can be done in
constant amount of
time, call it c
297
ADDITION
• ..... • For all practical
* *
* * * * *
* * * * * purposes
* * * * * * – adding two bits and
+ – generating a carry and
-------------- a sum
* * * * * * * can be done in
constant amount of
time, call it c
• Adding N bits takes c•N
units of time.
298
ADDITION
• ..... • For all practical purposes
* *
* * * * * *
* * * * adding two bits and
generating a carry and a
* * * * * *
sum can be done in
+ constant amount of time,
-------------- call it c.
* * * * * * * • Adding N bits takes c•N
units of time.
• For different people
(computers) c changes.
299
ADDITION
24
• The fact that we get a
22
20 line is invariant under
18 changes in
16 implementations (fast
14
Fast machine, slow machine)
time
12
Slow
10
8
6
4
2
0
# of bits in numbers
300
ADDITION
24
• The fact that we get a
22
20 line is invariant under
18 changes in
16 implementations (fast
14
Fast machine, slow machine)
time
12
Slow
10 • Grade school Addition is
8
a
6
4 Linear Time Algorithm
2
0
# of bits in numbers
301
IMPORTANT IDEA
302
ADDITION
303
ADDITION
• Is there an algorithm to Linear function
add two N-bit numbers f ( n) an b
whose time grows sub-
linearly in N?
Sublinear Functions
• A sub-linear function
grows slower than a
linear function. f ( n) a n
f ( n) a log n
304
ADDITION
• Is there an algorithm to 24
add two N-bit numbers 22
20
whose time grows sub- 18
linearly in N? 16
14 Fast
time
12 Slow
10 Sublinear
8
6
4
2
0
# of bits in numbers
305
ADDITION
306
ADDITION
307
ADDITION
308
ADDITION
309
ADDITION
310
ADDITION
311
ADDITION
312
ADDITION
313
MULTIPLICATION
*******
X *******
*******
*******
*******
*******
*******
*******
*******
************** 314
MULTIPLICATION
N
*******
X *******
*******
*******
*******
******* N 2
*******
*******
*******
************** 315
MULTIPLICATION
316
MULTIPLICATION
22 Addition
20
18 Mult.
16
14
12
10
8
6
4
2
0
# of bits in numbers 317
MULTIPLICATION
40 • No matter how
38
36
34
dramatic the difference
32
30
in the constants, the
28
26
quadratic curve will
24
22 Addition
“ eventually” dominate
time
20
18 Mult. the linear curve.
16
14
12
10
8
6
4
2
0
# of bits in numbers
318
MULTIPLICATION
319
MULTIPLICATION
• Note that we have only shown that the grade
school multiplication algorithm uses more time
than the grade school addition algorithm.
320