What is Algorithm, Data Structure - Data Structures and Algorithms | CS 361L, Study notes of Computer Science

Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Unknown 2002;

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-189
koofers-user-189 šŸ‡ŗšŸ‡ø

1

(1)

8 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 1
Jared Saia
University of New Mexico
Today’s Outline
•Administrative Info
•What is an Algorithm? Data Structure?
•Why study algorithms?
•Todo list for next class
1
Why study algorithms?
ā€œSeven years of College down the toiletā€ - John Belushi in Animal
House
•Q: Can I get a programming job without knowing something
about algorithms and data structures?
•A: Yes, but do you really want to be programming GUIs your
entire life?
2
Why study algorithms? (II)
•Almost all big companies want programmers with knowledge
of algorithms: Microsoft, Google, Oracle, IBM, Yahoo, San-
dia, Los Alamos, etc.
•In most programming job interviews, they will ask you several
questions about algorithms and/or data structures
•Your knowledge of algorithms will set you apart from the
large masses of interviewees who know only how to program
•If you want to start your own company, you should know that
many startups are successful because they’ve found better
algorithms for solving a problem (e.g. Google, Akamai, etc.)
3
pf3
pf4
pf5
pf8

Partial preview of the text

Download What is Algorithm, Data Structure - Data Structures and Algorithms | CS 361L and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 1

Jared Saia

University of New Mexico

Today’s Outline

Administrative Info

What is an Algorithm? Data Structure?

Why study algorithms?

Todo list for next class

Why study algorithms?

House ā€œSeven years of College down the toiletā€ - John Belushi in Animal

about algorithms and data structures?Q: Can I get a programming job without knowing something

entire life?A: Yes, but do you really want to be programming GUIs your

Why study algorithms? (II)

dia, Los Alamos, etc.of algorithms: Microsoft, Google, Oracle, IBM, Yahoo, San-Almost all big companies want programmers with knowledge

questions about algorithms and/or data structuresIn most programming job interviews, they will ask you several

large masses of interviewees who know only how to programYour knowledge of algorithms will set you apart from the

algorithms for solving a problem (e.g. Google, Akamai, etc.)many startups are successful because they’ve found betterIf you want to start your own company, you should know that

Why Study Algorithms? (III)

You’ll write better, faster code

You’ll learn to think more abstractly and mathematically

It’s the most challenging and interesting area of CS!

A Real Job Interview Question

dept web page for the full compilation of questions):2002 (thanks to Maksim Noy, see the career center link from the The following is a question commonly asked in job interviews in

You are given an array with integers between 1 and 1

All integers between 1 and 1

(^) 000 are in the array at least

once, and one of those integers is in the array twice

Can you do it while iterating through the array only once?Q: Can you determine which integer is in the array twice?

Solution

Ideas on how to solve this problem??

What if we allowed

multiple iterations?

Naive Algorithm

Create a new array of ints between 1 and 1

(^) 000, which

all entries to 0we’ll use to count the occurences of each number. Initialize

update its count in the new arrayGo through the input array and each time a number is seen,

twice.Go through the count array and see which number occurs

Return this number

Take Away

Designing good algorithms matters!

Not always this easy to improve an algorithm

However, with some thought and work, you can

almost al-

ways

get a better algorithm than the naive approach

How to analyze an algorithm?

about:There are several resource bounds we could be concerned

time, space, communication bandwidth, logic gates,

etc.

However, we are usually most concerned about time

guages and machine typesRecall that algorithms are independent of programming lan-

Q: So how do we measure resource bounds of algorithms

Random-access machine model

We will use RAM model of computation in this class

All instructions operate in serial

etc.) take unit timeAll basic operations (e.g. add, multiply, compare, read, store,

unit spaceAll ā€œatomicā€ data (chars, ints, doubles, pointers, etc.) take

Worst Case Analysis

We’ll

generally

be

pessimistic

when

we

evaluate

resource

bounds

possible input sequenceWe’ll evaluate the run time of the algorithm on the worst

Amazingly,

in most cases,

we’ll still be able to get pretty

good bounds

Justification:

The ā€œaverage caseā€ is often about as bad as

the worst case.

Example Analysis

redundant element in an arrayConsider the problem discussed last tuesday about finding a

are 1 toLet’s consider the more general problem, where the numbers

n

instead of 1 to 1

Algorithm 1

Create a new ā€œcountā€ array of ints of size

n , which we’ll use

to 0to count the occurences of each number. Initialize all entries

update its count in the ā€œcountā€ arrayGo through the input array and each time a number is seen,

already been counted once, return this numberAs soon as a number is seen in the input array which has

Algorithm 2

letIterate through the input array, summing up all the numbers,

S

be this sum

Let

x

=

S

n (^) + 1)

n/

Return

x

Example Analysis: Time

Worst case:

Algorithm 1 does 5

n

operations (

n

inits to 0

in ā€œcountā€ array,

n

reads of input array,

n

reads of ā€œcountā€

array (to see if value is 1),

n

increments, and

n

stores into

count array)

Worst case:

Algorithm 2 does 2

(^) n

(^) + 4 operations (

n

reads

of input array,

n

additions to value

S

4 computations to

determine

x

given

S

More Examples

E.g.

n , 10

n

āˆ’

(^) 2000, and

n

  • 2 are all

O

n )

n

  • log

(^) n

,

n (^) āˆ’

n

are

O

n )

n 2

+^

(^) n

(^) + log

(^) n

, 10

n 2 +^

(^) n

(^) āˆ’

n

are

O

n 2 )

n (^) log

(^) n

n

is

O

n (^) log

(^) n

)

(^) log

2 n^

is

O

(log

2 n^ )

n √

n (^) +

(^) n

(^) log

(^) n

n

is

O

n √

n )

50

and 4 are

O

More Examples

Algorithm 1 and 2 both take time

O

n )

Algorithm 1 uses

O

n ) extra space

But, Algorithm 2 uses

O

(1) extra space

Formal Defn of Big-O

A function

f (^) ( n )

is

O

g ( n ))

if there exist positive constants

c

and

n 0

such that

f (^) ( n ) ≤

(^) cg

n )

for all

n

≄

n 0

Example

Let’s show that

f (^) ( n ) = 10

n (^) + 100 is

O

g ( n )) where

g ( n ) =

n

We need to give constants

c

and

n 0

such that

f (^) ( n )

≤

cg

( n )

for all

n

≄

n 0

In other words, we need constants

c

and

n 0

such that 10

n (^) +

cn

for all

n

≄

n 0

Example

We can solve for appropriate constants:

n

  • 100

cn

/n

c

So if

n >

1, then

c

should be greater than 110.

In other words, for all

n >

n (^) + 100

n

So 10

n (^) + 100 is

O

n )

Questions

Express the following in

O

notation

n 3 / 1000

n 2

āˆ’

(^100)

n (^) + 3

log

(^) n

(^) log

2 n^ (^) + 100

āˆ‘

in

(^) i

Todo

Work on pretest, due next Tuesday!

Visit the class web page: www.cs.unm.edu/

saia/361/

Sign up for the class mailing list (cs361)

Read Chapter 1 in textbook