You are on page 1of 320

CS 300

DATA
STRUCTURES

Fall 2022
THE BORING STUFF

• Administrative Issues

• Evaluation

• Exams & Homeworks

2
THE GOOD STUFF

• Why are we here?

• What are we going to learn?

3
ADMINISTRATIVE

• Instructor: Cemal Yilmaz


• Office: MDBF G019
• Telephone: x9532
• E-mail: cyilmaz@sabanciuniv.edu
• Web: http://people.sabanciuniv.edu/~cyilmaz

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)

– For other times,


• please request an appointment by email
• indicating 2-3 suitable times for you

7
ADMINISTRATIVE

• Book: Data Structures & Algorithm


Analysis in C++
(2nd/3rd/4th E.)
• Author: Mark Allen Weiss
• Publisher: Addison Wesley

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

• All course material will be distributed via SU-


COURSE+
– Course slides and any other course material
– Homeworks
• Homeworks will only be accepted via SU-
COURSE+

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%)

• Midterm and Final Performance is not sufficient


to pass the course!

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

• Each homework will have strict guidelines for


input / output / submission
– Any homework which does NOT obey these
guidelines will not be graded.

17
HOMEWORKS

We expect that you adhere by basic academic


ethics guidelines while working on your
homeworks.

18
HOMEWORKS

• All homeworks have to be personal. Thus ....


• Plagiarism (copying/cheating) will be dealt with
extreme prejudice.

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

• Why do homeworks have such a high


percentage?

21
Frequently (Un)asked Questions

• Why do homeworks have such a high


percentage?
– You need to learn building “solutions” to
“problems”
– The only real way you learn this is by actually
struggling through a real problem.
– You need to improve your programming skills.

22
Frequently (Un)asked Questions

• How are programming homeworks graded?

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

• Do you ask programming problems in exams?

25
Frequently (Un)asked Questions

• Do you ask programming problems in exams?


– Not really.
– But you may have to write (and understand) small
segments of code (10-15 lines)

26
Frequently (Un)asked Questions

• Then, what do you ask in exams?

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

• How should we deal with homeworks?

29
Frequently (Un)asked Questions

• How should we deal with homeworks?


– Read and understand every detail and requirement
– Use your time carefully - Do not leave it to the last
day
– Make sure you understand your solution before you
start coding it – otherwise you will have big
problems
– Use office hours to discuss it with your
professor/assistants

30
WHAT IS CS 300 ABOUT?

31
WHAT IS CS 300 ABOUT?

• This course is on the organization of data so that


programs using this “ organized” data
– Run faster
– Need less memory

32
WHAT IS CS 300 ABOUT?

• This course is on the organization of data so


that programs using this “ organized” data
– Run faster
– Need less memory
• Why do we care?

33
WHAT IS CS 300 ABOUT?

• This course is on the organization of data so that


programs using this “ organized” data
– Run faster
– Need less memory
• Why do we care?
– Among programs that perform correctly the
same function, faster and smaller programs are
preferable.

34
BUT!

But Master, computers are getting faster and


faster and memory is getting cheaper and
cheaper. Wouldn’t it better if we just learn
the simple stuff and let technology deal
with it?

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!

Not so, Grasshopper!


There are problems for which the fastest computer
on earth would take time remaining until the
end of the universe* and still not find a solution.

* Obviously we are making some assumptions about physics here. 38


NOT SO!

Or require lots of memory (e.g., bytes > the number


of protons in the universe)

39
NOT SO!

So we should understand the nature of such problems


and design “good” algorithms.

40
NOT SO!

So we should understand the nature of such problems


and design “good” algorithms.

And “good” algorithms use “good” data structures.

41
FUN!

That sounds like fun, but can we learn all


that in one course?

42
FUN!

Of course not Grasshopper; in CS 300, we learn the simple


stuff to get your wings off the ground.

You will learn much more in CS 301 “Algorithms”

43
FUN OR PAIN?

Well, I heard learning all this can be


painful. They must be kidding, right?

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

• Linear Data Structures


• Trees
• Hash tables
• Heaps and Priority Queues
• Sorting
• Graphs and Graph Algorithms
• Algorithmic Paradigms

52
rek-lam-LAR

• What is Computer Science and Engineering?

53
Computer Science and Engineering?

• What is Computer Science and Engineering?

54
Computer Science and Engineering?

• What is Computer Science and Engineering?


• What skills does one need?

55
Computer Science and Engineering?

• What is Computer Science and Engineering?


• What skills does one need?
• What will you learn here?

56
OVERVIEW

• What is Computer Science and Engineering?


• What skills does one need?
• What will you learn here?
• What will you NOT learn here?

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

• What is Computer Science and Engineering?

59
The Burning Question

• What is Computer Science and Engineering?

60
The Burning Question

• What is Computer Science and Engineering?


• What is Science?
– Science is the study and understanding of the
possible (and beyond)
– Science is mainly analytic, that is,
• it tries to analyze, understand and describe nature
• and also the unnatural, (e.g., Herbert Simon, The
Sciences of the Artificial))
• Observe, hypothesize, predict, evaluate, and refine

61
The Burning Question

• What is Computer Science and Engineering?


• What is Engineering?
– Engineering is the study of the feasible, that is,
what is possible within time, space and money
constraints
– Engineering aims to synthesize the artificial, but,
one should have very good analytical skills, also!

62
What is Computer Science?

• Computer Science is the study of how


information is created, processed and
communicated

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?

• Content vs. Representation

71
What is information?

• Content vs. Representation


• Content is “music”, an arrangement of notes

72
What is information?

• Content vs. Representation


• Content is “ music” , an arrangement of notes
• Representation
– Musical scores
– Air waves – “ Sound”
– Sequence of digital representation of the “ sound”
– Wav file, mp3, AAC

73
What is Computer Engineering?

• An engineering discipline for building


hardware and software systems that
– store
– transmit
– process
information

74
What do Computer Engineers do?
• They build systems that process information.

• They do research into how to build systems:


– that are new (incorporating new ideas and technology),
– that solve an important problem hitherto unsolved
satisfactorily,
– that are “ cheap” (so that they are affordable)

75
Building Systems

• Computer Engineers build information


processing systems by bringing together
– hardware
– software
so that the system
– works (i.e. has (almost) no bugs )
– is fast, maintainable, robust, and affordable.

76
Interdisciplinary Cooperation

Electronics Engineering
Computer
(Microelectronics,
Science
Telecommunications)

INFORMATION PROCESSING AND


TRANSMISSION TECHNOLOGIES

INTELLIGENT
PRODUCTS AND SERVICES
OF THE FUTURE
77
Some important & interesting problems

• Printing payroll checks,


• Recognizing human speech, or signature
• Driving a car,
• Making investment decisions,
• Playing chess,
• Translating speech from one language to another.
• Summarizing evening TV news for you
• Controlling your house
• Providing secure e-commerce transactions
• Blasting a photorealistic T-Rex
78
Research

• Build, experiment with, and evaluate prototype


systems that incorporate new
– ideas,
– technologies,
– approaches
to solve old and new problems.

79
Research
Segmentation (potential boundaries)

Normalization
Recognition (Neural Networks)

Transcribed text

önceden evlendiği eşinden


Does this chip
work correctly?
Water Simulation
Facial Animation What is real ?

Texture Mapping Cloth Simulation 82


Technology

• 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

• Better user interfaces (recognize emotions,


motions and gestures, read lips, etc.)
• Virtual Reality
• Entertainment technology
– Special Effects
– Computer Games:
• Computer Graphics
• Artificial Intelligence

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

• Understanding the problem, requirements,


specifications
• Decomposing problems into manageable parts
(Always!)
• Good understanding of data organization
• Knowing how to select good algorithms

90
Skills of a good computer engineer

• Choosing a good implementation environment


(language, platform)
• Synthesis
– programing in the large --software engineering and
– programing in the small (hacking()),
• Division of labor between HW and SW

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!

• You will NOT have courses on specific


languages, or systems
• You will NOT learn how to use specific
packages like Visual Basic, MS Access or
AutoCAD, or things like that
• You will NOT learn how to assemble or
network PC’s

94
What you will NOT learn!

• You can learn about these on your own by


reading manuals (provided you know the
fundamentals)
– For example, any new programming language can
be learned in a week (mastery takes longer!)
– See Peter Norvig’s excellent article
• Teach Yourself Programming in Ten Years

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

• An expert is someone who knows more and


more

100
Specialization

• An expert is someone who knows more and


more
• about less and less, until

101
Specialization

• An expert is someone who knows more and


more
• about less and less, until
• s/he knows everything about nothing

102
Specialization

• An expert is someone who knows more and


more
• about less and less, until
• s/he knows everything about nothing ()
Expert
Depth
Ph. D.

M. Sc.

B. Sc.
General Practitioner
Breadth 103
A Lil’ bit of History

• Where are we coming from?

104
First Generation (1951-1959)

105
First Generation (1951-1959)

• 1950’s hard drive

106
Punched Cards

• Punched Card Input? What Is that?

107
Punched Cards

• Punched Card Input? What Is that?

108
Punched Cards

• Input with punch cards

109
A personal history of computer technology

• Input with punch cards

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 - ?)

• Large scale integrated circuits


• The phrase “ personal computer” (PC) was
coined
• Apple, Tandy/Radio Shack, Atari, Commodore,
Sun, …

113
Very Large Scale Integrated Circuit
Technology
• Xbox One SoC, 2013
– 5 trillion transistors
– 853 MHz
– 768 cores

• Sparc M7, 2014


9mm
– More than 10 trillion transistors 12mm

– 3.x GHz: More than 600 times that


of 8086
– 32 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

• Let’s get down to business.

119
COMPUTERS- A BRIEF REVIEW

• What’s in a computer?

120
COMPUTERS

Pentium (PCs, MacIntosh)

Central Processing Unit PowerPC (XBox360)


(CPU)
Sparc (SUN Workstations)

The CPU executes primitive instructions

Programs such as C++ programs are translated


into programs using these primitive instructions. .
121
COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

Memory stores instructions and data

122
COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

BUS

123
COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

Instructions and Data

BUS

124
COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

Instructions and Data

BUS

(Slow!: 5 – 50 nanoseconds (10-9 seconds))

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

……Bunch of other hardware…..

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

USB, PCMCIA,Firewire, etc


Hard Disk

I/O Devices 130


COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

BUS

Hard Disk Monitor

I/O Devices 131


COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

BUS

Hard Disk Monitor Keyboard


& Mouse

I/O Devices 132


COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

BUS

Hard Disk Monitor Keyboard Ethernet


& Mouse Interface

Internet

I/O Devices 133


COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

BUS

Hard Disk Monitor Keyboard Ethernet Modem Wireless


& Mouse Interface Card

Internet

I/O Devices 134


COMPUTERS

Random Access
Central Processing Unit Memory
(CPU) (RAM)

BUS

Hard Disk Monitor Keyboard Ethernet Modem Wireless Printer


& Mouse Interface Card

Internet

I/O Devices 135


COMPUTERS
• Important Parameters

Random Access
Central Processing Unit Memory
(CPU) (RAM)

• CPU Speed (Hz, Mhz(106 Hz), Ghz (109Hz ),


etc.)
• Memory Capacity (Megabytes – Gigabytes...)
136
How Much Memory Is That?

• 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

• What would YOU store if you had a petabyte


disk in your laptop?

143
Petabyte Memory

• What would YOU store if you had a petabyte


disk in your laptop?
– Everything your eyes see
– Everything you hear/say

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!

Source: How Much Data is That?

146
PERFORMANCE

• CPU Speed is not a good indicator of


performance:

147
PERFORMANCE

• CPU Speed is not a good indicator of


performance:
– Other things like
• bus speed,
• Memory hierarchy (caches) and speed,
• program behavior,
• disk speed, etc.,
determine overall performance.

148
PERFORMANCE
• Some Important non-technical laws
– Software (usually by one company) grows to
suck up the CPU speed

– CPU speed is never enough

– Memory capacity is never enough

– Internet capacity is never enough


149
PERFORMANCE

• These are corollaries to more general law of


nature that says
– Work expands to consume the extra resources!

150
MEMORY REPRESENTATION

• We will refer to memory cells as bytes.


– Each byte has 8 bits

151
MEMORY REPRESENTATION

• We will refer to memory cells as bytes.


– Each byte has 8 bits
• The CPU provides direct support to represent
certain data structures
– Characters
– Integers
– Real Numbers
– Addresses

152
MEMORY REPRESENTATION

• Each byte in a memory has an address.

0
4
8
12
16
18
….
536,870,908
153
MEMORY REPRESENTATION

• Each byte in a memory has an address.

• The basic functions of the memory are


– Given an address, store some content (represented
with 8 bits) at the memory location at that address
(Write)
– Given an address, get the content located at that
address (Read/Retrieve)

154
MEMORY REPRESENTATION

• With 8 bits we can only represent 256 distinct


units of content (28=256)

155
MEMORY REPRESENTATION

• With 8 bits we can only represent 256 distinct


units of content (28=256)

• So we use more than one byte to represent other


useful data items.

156
INTEGERS

• We represent integers using either


– 16 bits (2 bytes) (short integers)
– 32 bits (4 bytes) (integers, long integers)

157
INTEGERS

• We represent integers using either


– 16 bits (2 bytes) (short integers)
– 32 bits (4 bytes) (integers, long integers)
– 64 bits (8 bytes)
– On most CPUs
• Short integers start at even addresses.
• Integers start at addresses which are divisible by 4

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

• Further, we represent integers using a system


known as 2’s complement notation

159
2’s COMPLEMENT NOTATION

• With n bits (n = 16 or 32)


– 0 is represented with n 0 bits
– Positive numbers in the range 1 – (2n-1 -1) are
represented by n bits
• 0XXXXX...XXX (X = 0 or 1)
– Negative numbers in the range (2n-1 – (-1)) are
represented by n bits
• 1XXXXX...XXX (X= 0 or 1)
– So the first bit (or zeroth bit) represents the sign of
the number
160
2’s COMPLEMENT NOTATION
• Let’s assume n = 4
• Here are the representations for 0 and positive
numbers.
– 0 (0000)
– 1 (0001)
– 2 (0010)
– 3 (0011)
– 4 (0100)
– 5 (0101)
– 6 (0110)
– 7 (0111)
161
NEGATIVE NUMBERS

• To get the representation for negative numbers,


take the representation of the positive number
and complement each bit (0 → 1, 1 → 0)
• Then ADD 1, ignoring any final carry outs

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

• If you add two “ positive” numbers and get a


“ negative” number, or
• If you add two “ negative” numbers and get a
“ positive” number
• Then you have an overflow, the result can not
be represented in the system.

167
OVERFLOW

• What is 5 + 7?
0101
0111
----
1100

168
OVERFLOW

• What is 5 + 7?
0101
0111
----
1100

• We have an overflow, the result, 12 can not be


represented!
169
OVERFLOW

• What is -5+ -7?


1011
1001
----
10100

• We have an overflow, the result, -12 can not be


represented!
170
OVERFLOW

• What is -2+ -4?


1110
1100
----
1010

• We DO NOT have an overflow, the result, -6


can be represented!
171
INTEGERS

• With 2 bytes (16 bits) we can represent integers


in the range
– -32768 (-215) to +32767 (215 -1)

• With 4 bytes (32 bits) we can represent integers


in the range
– -2,147,483,648 (-231) to
– +2,147,483,647 (231-1)
172
INTEGERS

• With 2 bytes (16 bits) we can represent integers


in the range
– -32768 (-215) to +32767 (215 -1)
• With 4 bytes (32 bits) we can represent integers
in the range
– -2,147,483,648 (-231) to +2,147,483,647 (231-1)
• For most practical purposes these ranges are
sufficient.

173
UNSIGNED INTEGERS

• If we know we will never be using negative


values, we could employ unsigned integers of
16 or 32 bits
– 16 bit unsigned integers would give us a range 0 –
65,535 (216-1)
– 32 bit unsigned integers would give us a range 0 –
4,294,967,295 (232-1)

174
FLOATING POINT NUMBERS

• Floating point numbers are meant to represent


the real numbers.

• They are represented with three components


– A mantissa or significand (m)
– An exponent (e)
– A sign (s)

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

• In Pentium processors, a normal floating point


number is represented with 32 bits in the
following format
– 1 bit sign
– 8 bit exponent
– 23 bit mantissa (actually 24 bits  7 decimal digits
precision)
– Exponent is represented in a “ biased” form for
some technical reasons (i.e., [-126, 127])

177
FLOATING POINT NUMBERS

• In Pentium processors a double floating point


number is represented with 64 bits in the
following format
– 1 bit sign
– 8 bit exponent
– 55 bit mantissa (actually 56 bits  17 decimal digits
precision)

178
FLOATING POINT NUMBERS

• In Pentium processors a double floating point


number is represented with 64 bits in the
following format
– 1 bit sign
– 8 bit exponent
– 55 bit mantissa (actually 56 bits  17 decimal digits
precision)
• There is also an extended floating point format
that uses 80 bits (10 bytes)
179
FLOATING POINT NUMBERS

180
FLOATING POINT NUMBERS

(1 + ¼ + 1/8 + 1/64 + 1/1024) * 27 =


1.3916015625 * 128 = 178.125

181
FLOATING POINT NUMBERS

(1 + ¼ + 1/8 + 1/64 + 1/1024) * 27 =


1.3916015625 * 128 = 178.125
= 127 (bias) + 7 (exp)
01111111+ 111

182
FLOATING POINT NUMBERS

• For more details see


• http://en.wikipedia.org/wiki/IEEE_floating-poin
t_standard#Exponent_biasing

183
CHARACTERS AND STRINGS

• A byte holds a “ character”


• A sequence of characters terminated with a byte
whose “ value” is 0, is interpreted as a string of
characters.

• Unicode characters are composed of two-bytes


to accomodate all languages of the world.

184
ADDRESSES

• Occasionally we will interpret the contents of a


4-byte field in the memory as the address of
another data item.

185
ADDRESSES

• Occasionally we will interpret the contents of a


4-byte field in the memory as the address of
another data item.
• Data items with such content are known as
addresses.

186
ADDRESSES

• Suppose the 4 bytes starting at the memory


location 8 contain (in binary)
• (00000000,00000000,00000000,00010000) = 16.

187
ADDRESSES

• Suppose the 4 bytes starting at the memory


location 8 contain
(00000000,00000000,00000000,00010000) = 16.
• Suppose memory location 16 contains
(01111111) = byte with value 127.

188
ADDRESSES

• Suppose the 4 bytes starting at the memory


location 8 contain
(00000000,00000000,00000000,00010000) = 16.
• Suppose memory location 16 contains
(01111111) = byte with value 127.
• The content of the location whose address is in
8 is 127.

189
ADDRESSES

• We will get back to addresses when we study


pointers later !

190
COMPUTER SYSTEMS

• The Onion Skin Analogy

Hardware

191
COMPUTER SYSTEMS

• The Onion Skin Analogy

Operating System (XP, Linux, MacOS, etc.)

Hardware

192
COMPUTER SYSTEMS

• The Onion Skin Analogy

Application Support
(Window Managers, Graphics, User Interfaces, Development Env.)

Operating System (XP, Linux, MAcOS, etc)

Hardware

193
COMPUTER SYSTEMS

• The Onion Skin Analogy

Applications (Word, Firefox, iTunes)

Application Support
(Window Managers, Graphics, User Interfaces, Development Env.)

Operating System (XP, Linux, etc)

Hardware

194
COMPUTER SYSTEMS

• The Onion Skin Analogy


• Data Structures are used at all levels

Applications (Word, Firefox, iTunes)

Application Support
(Window Managers, Graphics, User Interfaces, Development Env.)

Operating System (XP, Linux, MacOS, etc)

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

• So developing a computer system is a VERY,


VERY COMPLEX affair.

197
COMPUTER SYSTEMS

• Managing this complexity is THE MAJOR


PROBLEM in building computer systems.
– So, such (software) systems are also built in again a
hierarchical fashion like the onion-skin analogy.
– Rather similar to building something with a Lego
kit.

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

• In this course we will learn


– How to design the smallest pieces?
• Their interfaces
• Their functionality
– They should function correctly, and
– as efficiently as possible

• Functionality AND interfaces AND efficiency


are THE MOST IMPORTANT ASPECTS

201
ENOUGH WITH COMPUTERS

• Let’s get a little bit abstract

202
ALGORITHM ANALYSIS

203
ALGORITHM ANALYSIS

Given an algorithm for a problem, how


much resource does it use
on an input data of size N?

204
ALGORITHM ANALYSIS

Given an algorithm for a problem, how


much resource does it use
on an input data of size N?

205
ALGORITHM ANALYSIS

Given an algorithm for a problem, how


much resource does it use
on an input data of size N?

206
ALGORITHM ANALYSIS

Given an algorithm for a problem, how


much resource does it use
on an input data of size N?

207
ALGORITHM ANALYSIS

• How much time does Quicksort take to sort N


integers?
– Problem: Sorting integers
– Algorithm: Quicksort
– Resource: Running time

208
ALGORITHM ANALYSIS

• How much time does the “ high-school”


algorithm take to multiply two matrices of N
rows by N columns?
– Problem: Multiplying matrices
– Algorithm: “ High-school” algorithm
– Resource: Running time

209
ALGORITHM ANALYSIS

• How much time does the “ high-school”


algorithm take to multiply two matrices of N
rows by N columns?
– Problem: Multiplying matrices
– Algorithm: “ High-school” algorithm
– Resource: Running time

• Note that there is an implicit reference to other


algorithms for matrix multiplication!
210
ALGORITHM ANALYSIS

• There are other “ resources” that we are


interested in
– Memory (reusable)
– Network traffic
– Disk access

211
ALGORITHM ANALYSIS

• There are other “ resources” that we are


interested in
– Memory (reusable)
– Network traffic
– Disk access
• But, we will mostly be concerned with running
time!

212
ALGORITHM

• An algorithm is a computational procedure for


solving a problem.
– Problem: Sorting
– Algorithms: Quicksort, Bubble Sort, Heap Sort ...

213
ALGORITHM

• An algorithm is a computational procedure for


solving a problem.
– Problem: Sorting
– Algorithms: Quicksort, Bubble Sort, Heap Sort ...

– Problem: Matrix Multiplication


– Algorithms: “ high-school” , Strassen’s Alg.

214
ALGORITHM - PROGRAM

• An algorithm is a computational procedure for


solving a problem.

• A program is
– an implementation of an algorithm
– using a specific programming language
– to execute on specific platforms

215
THE INPUT

• We parameterize the input by measuring the


“ size” of the input data.

216
THE INPUT

• We parameterize the input by measuring the


“ size” of the input data.
– Sorting N integers (each of fixed size- 32 bits)

217
THE INPUT

• We parameterize the input by measuring the


“ size” of the input data.
– Sorting N integers (each of fixed size- 32 bits)
– Matrix Multiplication 2 N2 real numbers (each of
fixed size – 64 bits) assuming N by N matrices

218
THE INPUT

• We parameterize the input by measuring the


“ size” of the input data.
– Sorting N integers (each of fixed size- 32 bits)
– Matrix Multiplication 2 N2 real numbers (each of
fixed size – 64 bits) assuming N by N matrices
• We measure the size of the representation of the
input.

219
COMPUTATIONAL COMPLEXITY

• The “ time” an algorithm takes or the


number of steps an algorithm executes

is a measure of the algorithm’s


computational complexity.

220
COMPUTATIONAL COMPLEXITY

• Try to do the following problems in your head!


(Remember: the brain is the finest computing
device ever known!)

221
COMPUTATIONAL COMPLEXITY

• Is Tekirdağ a city in Turkey?

222
COMPUTATIONAL COMPLEXITY

• Is Tekirdağ a city in Turkey?

• Or more abstractly, is x  S ?
– S is a set of objects and |S| = N

223
COMPUTATIONAL COMPLEXITY

• Is Tekirdağ a city in Turkey?

• Or more abstractly, is x  S ? (|S| = N)

• This looks like a very “ simple” problem,


anyone (not sleeping) should be able to answer
it immediately

224
COMPUTATIONAL COMPLEXITY

• List the cities in Turkey in reverse alphabetical


order

225
COMPUTATIONAL COMPLEXITY

• List the cities in Turkey in reverse alphabetical


order

• Somehow you can NOT do this immediately

• It is “ harder” than the previous problem.

226
COMPUTATIONAL COMPLEXITY

• List the cities in Turkey in reverse alphabetical


order

• Somehow you can NOT do this immediately, it


is “ harder” than the previous problem.

• But after a “ short” time you probably can do it.

227
COMPUTATIONAL COMPLEXITY

• Find the shortest tour of cities in Turkey, such


that
– starting with İstanbul,
– you visit each city exactly once and
– return back to İstanbul.

• e.g., İstanbul, Kocaeli, Bolu, ?? ...... Tekirdağ,


İstanbul.

228
COMPUTATIONAL COMPLEXITY

229
COMPUTATIONAL COMPLEXITY

230
COMPUTATIONAL COMPLEXITY

231
COMPUTATIONAL COMPLEXITY

232
COMPUTATIONAL COMPLEXITY

233
COMPUTATIONAL COMPLEXITY

?
234
COMPUTATIONAL COMPLEXITY

• Find the shortest tour of cities in Turkey, such


that starting with İstanbul, you visit each city
exactly once and return back to İstanbul.

235
COMPUTATIONAL COMPLEXITY

• Find the shortest tour of cities in Turkey, such


that starting with İstanbul, you visit each city
exactly once and return back to İstanbul.

• This seems to be a very “ complex” problem.

236
COMPUTATIONAL COMPLEXITY

• Find the shortest tour of cities in Turkey, such


that starting with İstanbul, you visit each city
exactly once and return back to İstanbul.

• This seems to be a very “ complex” problem.


– There are (n-1)! possible tours to consider!*

* You can fly from city to city if you want


237
COMPUTATIONAL COMPLEXITY

• Let’s put n! into perspective.


– log n!  n log n
• So for 81 cities we get
– log10 81 !  81 log10 81  154
– so 81! has  154 digits  10153
– This is a large, really large number.

238
COMPUTATIONAL COMPLEXITY

• 81! has  154 digits  10153


• This is a large, really large number.
– Assuming we can check 109 (1 billion) tours every
second using a fast computer
– We need 10144 seconds
 10139 days
 2.5 * 10137 years
 2.5 * 10135 centuries
– For reference: Big Bang is estimated to be about 1.5
*107 centuries ago.
239
COMPUTATIONAL COMPLEXITY

• A much better approach for this problem takes


time proportional to n22n.

• For n = 81 this number  6500 * 1027  1029


• So assuming we can do 109 “operations” a
second, we need about
– 1020 seconds  1015 days 
– 2.5 * 1012 years  2.5 * 1010 centuries
• 10125 times better but still meaningless
240
COMPUTATIONAL COMPLEXITY

• A much better approach for this problem takes


time proportional to n22n.

• 10126 times better but still meaningless


– So a little bit of thinking beats a lot of hardware!
• There are many other tricks people employ for
this problem

241
COMPUTATIONAL COMPLEXITY

• There are many other tricks people employ for


this problem
– In May 2004, the traveling salesman problem of
visiting all 24,978 cities in Sweden was solved: a
tour of length 855,597 TSPLIB units (approximately
72,500 kilometers) was found and it was proven that
no shorter tour exists. This is currently the largest
solved TSP instance, surpassing the previous record
of 15,112 cities through Germany set in April 2001.

242
How Large is Really Large?

243
CLASSES OF PROBLEMS

• There are some fundamental problems that we


use computers to solve.

244
CLASSES OF PROBLEMS

• There are some fundamental problems that we


use computers to solve.
– Searching (Functionality)
• Given a “ description” of what we want,
• Find, from a set of “ things” those, that match that
description

245
CLASSES OF PROBLEMS

• There are some fundamental problems that we


use computers to solve.
– Searching
• Given a “ description” of what we want,
• Find, from a set of “ things” those, that match that
description
• AND DO IT FAST!

246
CLASSES OF PROBLEMS

• There are some fundamental problems that we


use computers to solve.
– Searching
• Given a “ description” of what we want,
• Find, from a set of “ things” those, that match that
description
• AND DO IT FAST!
– In fact most of what we do (in life) is searching!

247
SEARCH

• What is the name of the student with ID=5555?

248
SEARCH

• Find me all “ interesting” documents containing


the words “ Turkish Politics” ?

249
SEARCH

• What Turkish words are “ close” to the


misspelled word “ iktpata” ?

250
SEARCH

• What Turkish words are “ close” to the


misspelled word “ iktpata” ?
• ispata
• itaata
• kapata
• kitapta

251
SEARCH

• Whose signatures are these?

252
SEARCH

• Which letter is this?

253
SEARCH

• Who is this woman and who is she hugging?

254
SEARCH

• Is this a dangerous animal and should I escape


from it?

255
SEARCH

• Whose speech is this and what is he saying?

256
SEARCH

• Whose speech is this and what is he saying?

Yangını söndürmeye uğraştık

257
CLASS OF PROBLEMS

• There are some fundamental problems that we


use computers to solve.
– Searching
• Given a “ description” of what we want,
• Find, from a set of “ things” those, that match that
description
– Sorting
• Arrange items in a set into some kind of an order
– Name, number, size, color

258
WHAT DO WE MEASURE?

• Time (milliseconds, seconds, weeks)

259
WHAT DO WE MEASURE?

• Time (milliseconds, seconds, weeks)

• 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?

• Time (milliseconds, seconds, weeks)

• Hardware dependent
– Same program will take different times on different
machines.

• We have to write the program to evaluate it!

262
WHAT DO WE MEASURE?

• The program is written in some kind of a


programming language.

263
WHAT DO WE MEASURE?

• The program is written in some kind of a


programming language.
• Count the (worst case) number of statements
executed, when it runs on a input of size N.

264
WHAT DO WE MEASURE?

• The program is written in some kind of a


programming language.
• Count the (worst case) number of statements
executed, when it runs on a input of size N.
– We assume all statements take the same amount of
time.

265
WHAT DO WE MEASURE?

• The program is written in some kind of a


programming language.
• Count the (worst case) number of statements
executed, when it runs on a input of size N.
– We assume all statements take the same amount of
time.

– Too detailed and tedious to do by hand.

266
EXAMPLE

• Matrix Multiplication

267
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}
• To simplify matters I will assume for loops do not take any
time.
268
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}

N(
269
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}

N (N (
270
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}

N(N(1+
271
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}

N(N(1+N(
272
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}

N ( N ( 1 + N ( 1)))
273
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}

N ( N ( 1 + N ( 1))) = N3 + N2 steps
274
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


for (j=1; j<= N; j++) {
C[i][j]=0.0;
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}

This statement takes more time than the others.


275
WHAT DO WE MEASURE?

• Count “ important” arithmetic or comparison


operations.
– Concentrate on the “ hot spots” of the program
where the action is taking place!

276
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++)


Hot Spot
for (j=1; j<= N; j++) {
C[i][j]= 0.0
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}
A total of about N3 floating point additions and
multiplications take place here.
277
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

for (i=1; i <= N; i++) Hot Spot


for (j=1; j<= N; j++) {
C[i][j]= 0.0
for (k = 1; k<= N; k++)
C[i][j] = C[i][j]+ A[i][k]*B[k][j];
}
A total of about N3 floating point additions and
multiplications take place here . Everything else takes
place about N2 times.
278
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

• For “ large” N, the N3 component dominates


– N = 100, N3 + N2 = 1000000+10000
= 1010000  1000000.
– N = 1000, N3 + N2 = 109 + 106  109.

279
EXAMPLE

• Matrix Multiplication Cnxn = Anxn * Bnxn

• For “ large” N, the N3 component dominates


– N = 100, N3 + N2 = 1000000+10000
= 1010000  1000000.
– N = 1000, N3 + N2 = 109 + 106  109
• So perhaps we should not worry about these
“ other” operations and concentrate only on the
main action!
280
BACK TO ELEMENTARY SCHOOL

• How to multiply two numbers?

281
BACK TO ELEMENTARY SCHOOL

• How to multiply two numbers?

• How to multiply two complex numbers


(a+bi)*(c+di)?

282
BACK TO ELEMENTARY SCHOOL

• How to multiply two numbers?

• How to multiply two complex numbers


(a+bi)*(c+di) = (ac-bd) + (ad+bc)i

283
BACK TO ELEMENTARY SCHOOL

• How to multiply two numbers?

• How to multiply two complex numbers


(a+bi)*(c+di) = (ac-bd) + (ad+bc)i

• Input a,b,c,d and output ac-bd, ad+bc

284
BACK TO ELEMENTARY SCHOOL

• How to multiply two numbers?

• How to multiply two complex numbers


(a+bi)*(c+di) = (ac-bd) + (ad+bc)

• Input a,b,c,d and output ac-bd, ad+bc


• If real multiplication costs 1 TL and real addition
costs 0.10 TL what is the cheapest way to obtain
the result?
285
Multiplication

• Input a,b,c,d and output ac-bd, ad+bc


• If real multiplication costs 1 TL and real
addition costs 0.10 TL what is the cheapest way
to obtain the result?

• 4*1 + 2 * .10 = 4.20 TL

286
Multiplication

• Input a,b,c,d and output ac-bd, ad+bc


• If real multiplication costs 1 TL and real
addition costs 0.10 TL what is the cheapest way
to obtain the result?

• 4*1 + 2 * .10 = 4.20 TL


• Can we do any better?

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

• Can we do any better ? We will elaborate on


this later.

290
MULTIPLICATION

• Can we do any better ? We will elaborate on


this later.

• Now let us look at a simpler problem.

291
ADDITION

• How to add two N-bit numbers?

* * * * * * 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

• Determining the growth rate of the


resource curve as a function of the
problem size is one of the
fundamental ideas in computer
science.

302
ADDITION

• Is there an algorithm to add two N-bit numbers


whose time grows sub-linearly in N?

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

• Suppose there is a mystery algorithm that


does not examine each bit.

306
ADDITION

• Suppose there is a mystery algorithm that


does not examine each bit.

• Give this algorithm a pair of numbers.

• There must be some unexamined position i in


one of the numbers.

307
ADDITION

• If the algorithm is not correct on these numbers,


we found a bug.

308
ADDITION

• If the algorithm is not correct on these numbers,


we found a bug.

• If the algorithm is correct,


– flip the bit at position i, and give the algorithm the
new pair.
– It will give the same answer as the previous answer
and it will be wrong.

309
ADDITION

• So any algorithm for addition must use time that


is at least linear in the size of the numbers.

310
ADDITION

• So any algorithm for addition must use time that


is at least linear in the size of the numbers.

• So elementary school algorithm is as good as


can be.

311
ADDITION

• So any algorithm for addition must use time that


is at least linear in the size of the numbers.

• So elementary school algorithm is as good as


can be.

• One can however do parallel operations on bits


to add in sublinear time!

312
ADDITION

• So any algorithm for addition must use time that


is at least linear in the size of the numbers.

• So elementary school algorithm is as good as


can be.

• But what about multiplication?

313
MULTIPLICATION

*******
X *******
*******
*******
*******
*******
*******
*******
*******
************** 314
MULTIPLICATION




N
*******
X *******
*******
*******
*******

*******  N 2

*******
******* 
*******
************** 315
MULTIPLICATION

• It looks like the total time is bounded by c•N2

316
MULTIPLICATION

• It looks like the total time is bounded by c•N2


40
38
36
34
32
30
28
26
24
time

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

• Note that we have only shown that the grade


school multiplication algorithm uses more time
than the grade school addition algorithm.

319
MULTIPLICATION
• Note that we have only shown that the grade
school multiplication algorithm uses more time
than the grade school addition algorithm.

• To show that multiplication is inherently a


harder problem than addition, we have to show
that NO multiplication algorithm runs in linear
time (which is a VERY HARD thing to prove!)

320

You might also like