




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Unknown 2002;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Jared Saia
University of New Mexico
Administrative Info
What is an Algorithm? Data Structure?
Why study algorithms?
Todo list for next class
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
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
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!
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?
Ideas on how to solve this problem??
What if we allowed
multiple iterations?
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
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
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
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
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.
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
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
letIterate through the input array, summing up all the numbers,
be this sum
Let
x
=
n (^) + 1)
n/
Return
x
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
4 computations to
determine
x
given
E.g.
n , 10
n
ā
(^) 2000, and
n
n )
n
(^) n
,
n (^) ā
n
are
n )
n 2
+^
(^) n
(^) + log
(^) n
, 10
n 2 +^
(^) n
(^) ā
n
are
n 2 )
n (^) log
(^) n
n
is
n (^) log
(^) n
)
(^) log
2 n^
is
(log
2 n^ )
n ā
n (^) +
(^) n
(^) log
(^) n
n
is
n ā
n )
50
and 4 are
Algorithm 1 and 2 both take time
n )
Algorithm 1 uses
n ) extra space
But, Algorithm 2 uses
(1) extra space
A function
f (^) ( n )
is
g ( n ))
if there exist positive constants
c
and
n 0
such that
f (^) ( n ) ā¤
(^) cg
n )
for all
n
ā„
n 0
Letās show that
f (^) ( n ) = 10
n (^) + 100 is
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
We can solve for appropriate constants:
n
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
n )
Express the following in
notation
n 3 / 1000
n 2
ā
(^100)
n (^) + 3
log
(^) n
(^) log
2 n^ (^) + 100
ā
(^) i
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