Docsity

Algorithms and Programming Languages - Lecture Slides | CS 1030, Study notes of Computer Science

Toggle sidebar
bg1
2/28/2009
1
Chapter 8
Algorithms and Programming
Languages
Algorithms
the central concept underlying all computation is
that of the
algorithm
Algorithm - a step-by-step set of instructions to
accomplish a task
Algorithms
programming can be viewed as the process of
designing and implementing algorithms that a
computer can carry out
a programmer’s job is to:
create an algorithm for accomplishing a given
objective, then
translate the individual steps of the algorithm into a
programming language that the computer can
understand
Algorithms
example: programming in JavaScript
we have written programs that instruct the browser to
carry out a particular task
given the proper instructions, the browser is able to
understand and produce the desired results
Example: programming in JavaScript
we decided what functionality we wanted the web
page to have
we wrote the JavaScript code that would
implement that functionality
given the proper instructions, the browser is able
to understand and produce the desired results
Algorithms in the Real World
the use of algorithms is not
limited to the domain of
computing
e.g., recipes for baking
cookies
e.g., directions to your house
pf3
pf4
pf5

Partial preview of the text

Download Algorithms and Programming Languages - Lecture Slides | CS 1030 and more Study notes Computer Science in PDF only on Docsity!

Chapter 8

Algorithms and Programming

Languages

Algorithms

 the central concept underlying all computation is

that of the algorithm

Algorithm - a step-by-step set of instructions to

accomplish a task

Algorithms

 programming can be viewed as the process of

designing and implementing algorithms that a

computer can carry out

 a programmer’s job is to:

 create an algorithm for accomplishing a given

objective, then

 translate the individual steps of the algorithm into a

programming language that the computer can

understand

Algorithms

example: programming in JavaScript

 we have written programs that instruct the browser to

carry out a particular task

 given the proper instructions, the browser is able to

understand and produce the desired results

 Example: programming in JavaScript

 we decided what functionality we wanted the web

page to have

 we wrote the JavaScript code that would

implement that functionality

 given the proper instructions, the browser is able

to understand and produce the desired results

Algorithms in the Real World

 the use of algorithms is not

limited to the domain of

computing

 e.g., recipes for baking

cookies

 e.g., directions to your house

Algorithms in the Real World

 there are many unfamiliar tasks in life that we

could not complete without the aid of

instructions

 in order for an algorithm to be effective, it must be

stated in a manner that its intended executor can

understand

 a recipe written for a master chef will look different than a recipe written for a 12 year old

 as you have already experienced, computers are

more demanding with regard to algorithm specifics

than any human could be

Algorithms

 Specifying the solution to a problem in

precise steps may be difficult

 However, once the problem's solution is

designed and written down in the form of an

algorithm, that particular problem can be

solved by anyone capable of following the

algorithm steps

Designing & Analyzing Algorithms

 4 steps to solving problems (George Polya)

1. understand the problem

2. devise a plan

3. carry out your plan

4. examine the solution

Designing & Analyzing Algorithms

 EXAMPLE: finding the oldest person in a room full

of people

 understanding the problem

 initial condition – room full of people

 goal – identify the oldest person

 assumptions

 a person will give their real birthday

 if two people are born on the same day, they are the

same age

 if there is more than one oldest person, finding any one of

them is okay

Algorithm 1

 we will consider 2 different designs for solving this problem

 Finding the oldest person (algorithm 1)

1. line up all the people along one wall

2. ask the first person to state his or her name and birthday,

then write this information down on a piece of paper

3. for each successive person in line:

i. ask the person for his or her name and birthday ii. if the stated birthday is earlier than the birthday on the paper, cross out old information and write down the name and birthday of this person

when you reach the end of the line, the name and birthday

of the oldest person will be written on the paper

Algorithm 1

when you reach the end of the line, the name and

birthday of the oldest person will be written on the

paper

Algorithm Analysis (cont.)

 when the problem size is large, performance differences

can be dramatic

 1,000,000 people  5*1,000,000 = 5,000,000 seconds or 57 days

 1,000,000 people  5* log 2 1,000,000  = 100 seconds or 1.6 minutes

Big-Oh Notation

 to represent an algorithm’s performance in

relation to the size of the problem, computer

scientists use what is known as Big-Oh notation

 executing an O(N) algorithm requires time

proportional to the size of problem

 given an O(N) algorithm, doubling the problem size

doubles the work

 executing an O(log N) algorithm requires time

proportional to the logarithm of the problem size

 given an O(log N) algorithm, doubling the problem size

adds a constant amount of work

Big-Oh Notation

 based on our previous analysis:

 algorithm 1 is classified as O(N)

 algorithm 2 is O(log N)

Another Algorithm Example

SEARCHING: a common problem in computer science involves

storing and maintaining large amounts of data, and then

searching the data for particular values

 data storage and retrieval are key to many industry applications

 search algorithms are necessary to storing and retrieving data

efficiently

 e.g., consider searching a large payroll database for a particular

record

 if the computer selected entries at random, there is no assurance that the particular record will be found  even if the record is found, it is likely to take a large amount of time  a systematic approach assures that a given record will be found, and that it will be found more efficiently

Another Algorithm Example

 there are two commonly used algorithms for

searching a list of items

 sequential search – general purpose, but relatively

slow

 binary search – restricted use, but fast

Sequential Search

 sequential search is an algorithm that involves examining each

list item in sequential order until the desired item is found

 sequential search for finding an item in a list

  1. start at the beginning of the list
  2. for each item in the list i. examine the item - if that item is the one you are seeking, then you are done ii. if it is not the item you are seeking, then go on to the next item in the list if you reach the end of the list and have not found the item, then it was not in the list

 sequential search guarantees that you will find the item if it is

in the list

 but it is not very practical for very large databases  worst case: you may have to look at every entry in the list

Binary Search

 binary search involves continually cutting the desired search

list in half until the item is found

 the algorithm is only applicable if the list is ordered

 e.g., a list of numbers in increasing order  e.g., a list of words in alphabetical order

 binary search for finding an item in an ordered list

  1. initially, the potential range in which the item could occur is the entire list
  2. as long as items remain in the potential range and the desired item has not been found, repeatedly i. examine at the middle entry in the potential range ii. if the middle entry is the item you are looking for, then you are done iii. if the middle entry is greater than the desired item, then reduce the potential range to those entries left of the middle iv. if the middle entry is less than the desired item, then reduce the potential range to those entries right of the middle

 by repeatedly cutting the potential range in half, binary search can hone in on the value very quickly

Binary Search Example

suppose you have a sorted list of state names, and want to find Illinois

  1. start by examining the middle entry (Missouri) since Missouri comes after Illinois alphabetically, can eliminate it and all entries that appear to the right
  2. next, examine the middle of the remaining entries (Florida) since Florida comes before Illinois alphabetically, can eliminate it and all entries that appear to the left
  3. next, examine the middle of the remaining entries (Illinois) the desired entry is found

Search Analysis

 sequential search

 in the worst case, the item you are looking for is in the last spot in the list (or not in the list at all)  as a result, you will have to inspect and compare every entry in the list  the amount of work required is proportional to the list size  sequential search is an O(N) algorithm

 binary search

 in the worst case, you will have to keep halving the list until it gets down to a single entry  each time you inspect/compare an entry, you rule out roughly half the remaining entries  the amount of work required is proportional to the logarithm of the list size  binary search is an O(log N) algorithm

Search Analysis

 imagine searching a phone book of the United

States (300 million people)

 sequential search requires at most

300 million inspections/comparisons

 binary search requires at most

log2(300,000,000) = 29 inspections/comparisons