Data Structures and
Data Structures and
Algorithms
Algorithms
Luciano Bononi
Computer Science Engineering
University of Bologna
[email protected]http!!""".cs.unibo.it!#bononi!
Slide credits these slides have been translated from slides created by $oreno $ar%olla
Data Structures and Algorithms 2
Copyright & '())* Luciano Bononi and $oreno $ar%olla*
Universit+ di Bologna* ,taly
This work is licensed under the Creative Commons Attribution-ShareAlike License. To
view a copy of this license, visit http:creativecommons.or!licensesby-sa".# or send a
letter to Creative Commons, $%" &oward Street, $th 'loor, San 'rancisco, California,
(%)#$, *SA.
Data Structures and Algorithms 3
Course information
Luciano Bononi
[email protected]
http://www.cs.unibo.it/~bononi/
Lessons
$onday -.((.)/.((
0riday -.((.)/.((
Some variations scheduled 1see detailed calendar2
3o tal4 "ith me
Al"ays drop me an email before to define a date!hour.
$y office $ura Anteo 5amboni 6* office 3(7
Data Structures and Algorithms 4
8eneral information
Data Structures and Algorithms 5
Course "ebsite
http!!""".cs.unibo.it!#bononi!
9 Courses 9 Data Structures and Algorithms A.A. '())!'()'
:ill find
General information
Lesson slides
e;ercises
Lin4s and recommended readings
E;am preparation material
Also chec4 <SS and ne"s on the "ebsite
http!!""".unibo.it!Sito:ebDocente!default.htm=upn>luciano.bononi?@(unibo.it
http!!""".unibo.it!Sito:ebDocente!default.htm=upn>luciano.bononi?@(unibo.itA3abControl)>3abAvvisi
3oday , "ill collect your names for a mailing list
Data Structures and Algorithms 6
<ecommended readings
Alfred B. Aho* Ceffrey D. Ullman* Cohn E. Dopcroft* Data Structures and
Algorithms* Addison :esley* )-7/.
3homas D. Cormen* Charles E. Leiserson* <onald L. <ivest* Clifford
Stein* ,ntroduction to Algorithms* $c8ra".Dill* '(().
Donald E. Enuth* 3he Art of Computer Frogramming* Bolumes )./*
Addison.:esley Frofessional* )--7.
S.B. Eishor* Data Structures* Edition /* Das 8anu Fra4ashan* Gagpur*
'((7.
0urther information* boo4s* and material "ill be provided as a :eb
reference.
Data Structures and Algorithms 7
E;am
:ritten e;am
Hral e;am
Dates "ill be agreed by using the mailing list.
Data Structures and Algorithms 8
Algorithms and Data Structures
Data Structures and Algorithms 9
:hat is an algorithm=
A algorithm is a procedure to resolve a
problem by means of a finite seIuence
of basic atomic steps.
3he procedure must be defined in a
not ambiguous and accurate "ay to be
e;ecuted automatically
3he name comes from a Fersian
mathematician Abu CaJfar $uhammad
ibn $usa Eh"ari%mi
Author of the first reference
algebraic te;t
A $oon crater is dedicated to him
Data Structures and Algorithms 10
Algorithm vs Frogram
A algorithm describes 1at high level2 a computation
procedure "hich "hen e;ecuted produces a result.
A program is the implementation of a algorithm by means
of a programming language
A program can be e;ecuted on a computer 1creating a process
under e;ecution2K an algorithm cannot be e;ecuted as is in a
natural form.
Data Structures and Algorithms 11
Algorithms are every"hereL
,nternet. :eb search* pac4et routing* distributed file sharing.
Biology. Duman genome proMect* protein folding.
Computers. Circuit layout* file system* compilers.
Computer graphics. $ovies* video games* virtual reality.
Security. Cell phones* e.commerce* voting machines.
$ultimedia. CD player* DBD* $F/* CF8* DivN* DD3B.
3ransportation. Airline cre" scheduling* map routing.
Fhysics. G.body simulation* particle collision simulation.
...
Data Structures and Algorithms 12
:hy "eJre studying algorithms=
Data Structures and Algorithms 13
:hy :EJre studying algorithms=
e.g. A protein /D structure is determined by interactions of
aminoacids.
Some health issues generated by "rong folding* to be studied.
0olding@Dome
http!!en."i4ipedia.org!"i4i!FroteinOfolding
Data Structures and Algorithms 14
Algorithms again=
Dide rendering surfaces* gaming* physical simulation* etc.
Data Structures and Algorithms 15
:hy to care about algorithms=
Algorithms provide advantages
An efficient algorithm is often the difference bet"een being or
being not able to solve a problem "ith the given resources
$any algorithms "e "ill see "ere invented by studentsL
Algorithms are fun. .2... yes they are. Go* seriously.
Data Structures and Algorithms 16
:here do "e start from=
3here are some classical algs to resolve common
problems
Hrdering* searching* visit of graphs...
Do" could "e evaluate the efficiency of an algorithm=
Do" to derive or invent ne" algorithms that better e;ploit
the resources tradeoffs 1and the opportune data
structures2=
Data Structures and Algorithms 17
:armup 0ibonacci numbers
3he 0ibonacci seIuence
'
)
, '
+
, ... '
n
, ... is defined as
Leonardo 0ibonacci
1Fisa* ))6(PFisa* )'Q(2
http!!it."i4ipedia.org!"i4i!LeonardoO0ibonacci
F
1
=1
F
2
=1
F
n
=F
n1
F
n2
, n2
Data Structures and Algorithms 18
Closed form
8ood ne"s a close form e;ists for '
n
"here
Bad ne"s to evaluate this formula errors are introduced
due to need to compute floating point aritmetics
F
n
=
1
=
1
5
2
1.618
=
1
5
2
0.618
Data Structures and Algorithms 19
3he trivial 0ibonacci algorithm
LetJs define an algorithm to compute 0n based on a trivial
recursive function
:e "ill use pseudo.code description of algorithms. 3he
translation in programming languages is Iuite straight.
for"ard.
algorithm Fibonacci2(int n) int
if ( n==1 || n==2 ) then
return 1;
else
return Fibonacci2(n-1)+Fibonacci2(n-2);
endif
Data Structures and Algorithms 20
<ecursion tree
01@2
01/2 01'2
01'2 01)2
01Q2
01/2
01'2 01)2
01@2>/
01/2>' 01'2>)
01'2>) 01)2>)
01R2>7
01@2>/
01/2>' 01'2>)
01'2>) 01)2>)
01Q2>Q
01/2>'
01'2>) 01)2>)
01@2
01/2 01'2
01'2 01)2
01Q2
01/2
01'2 01)2
01@2>/
01/2>' 01'2>)
01'2>) 01)2>)
01Q2>Q
01/2>'
01'2>) 01)2>)
0162>)/
Data Structures and Algorithms 21
So far so good... but...
3ime needed to compute '
n
gro"s too much as a function
of n
DJohL
Data Structures and Algorithms 22
Do" to estimate the e;ecution time=
,n seconds=
S "ill depend on the computer e;ecuting the program
Gumber of machine language instructions e;ecuted per
second=
Dard to estimate from pseudo.code* and also still depends on
the computer e;ecuting the program
:e estimate the e;ecution time by calculating the number
of basic operations e;ecuted in the pseudo.code.
Data Structures and Algorithms 23
:here is the efficiency problem=
,ntermediate values are often re.calculated again and
again...
01@2
01/2 01'2
01'2 01)2
01Q2
01/2
01'2 01)2
01@2
01/2 01'2
01'2 01)2
01R2
01@2
01/2 01'2
01'2 01)2
01Q2
01/2
01'2 01)2
01@2
01/2 01'2
01'2 01)2
01Q2
01/2
01'2 01)2
01@2
01/2 01'2
01'2 01)2
01Q2
01/2
01'2 01)2
0162
Data Structures and Algorithms 24
Estimation of e;ecution time
let T,n- be the time needed to compuet the n.th 0ibonacci
number.
:e estimate T,n- as the number of nodes of the recursion
tree of '
n
Question ho" to obtain the recursive e;pression of 31n2 as the
number of recursive nodes in the tree for calculating '
n
Data Structures and Algorithms 25
Estimation of e;ecution time
:e can demonstrate 1by induction2 that
T,n- . +'
n
/ )
Question demonstrate that.
By remembering the close form for '
n
"e conclude that
T,n- gro"s e;ponentially
:e can calculate a lo"er bound for 31n2
See ne;t page
Data Structures and Algorithms 26
Estimation of e;ecution time
let 31n2 be the number of nodes of the recursive tree
for calculating '
n
T,)- . T,+- . )K
T,n- . T,n-)- 0 T,n-+- 0 ) ,se n1+-
,t is similar to the recurrence that defines '
n
algorithm Fibonacci2(int n) int
if ( n==1 || n==2 ) then
return 1;
else
return Fibonacci2(n-1)+Fibonacci2(n-2);
endif
Data Structures and Algorithms 27
Lo"er bound of the e;ecution time
T n = T n)T n')
'3n')
@3n@')
73nR'
'
')
...
'
k
T n'4
i =(
k )
'
i
...
'
n/ '
'
n/'
)
')
'
n/ '
:e e;ploit the fact that
31n2 is monotone
increasing
<ecursion ends
"hen 4>n!'
Data Structures and Algorithms 28
Can "e do it better=
LetJs use a vector of si%e n to compute and store the
values of '
)
, '
+
, ... '
n
algorithm Fibonacci3(int n) int
let Fib[1..n] be an array of n ints
Fib[1] := 1;
Fib[2] := 1;
for i:=3 to n do
Fib[i] := Fib[i-1] + Fib[i-2];
endfor
return Fib[n];
Data Structures and Algorithms 29
Do" much does it cost=
LetJs estimate the cost of 0ibonacci/ by counting the
number of pseudocode operations e;ecuted
algorithm Fibonacci3(int n) int
let Fib[1..n] be an array of n integers
Fib[1] := 1; ..................... 1 time
Fib[2] := 1; ..................... 1 time
for i:=3 to n do .................. (n-1) times
Fib[i] := Fib[i-1] + Fib[i-2]; (n-2) times
endfor
return Fib[n]; .................. 1 time
!otal............ 2n
. 3ime is proportional to n
. Space is proportional to n
Data Structures and Algorithms 30
Can "e do it even better=
$emory usage of 0ibonacci/ is proportional to n. Can "e
use less memory=
Tes* because to calculate '
n
"e simply need '
n-)
e '
n-+
algorithm Fibonacci"(int n) int
if ( n==1 || n==2 ) then
return 1;
else
F#n$1 := 1;
F#n$2 := 1;
for i:=3 to n do
F#n := F#n$1 + F#n$2;
F#n$2 := F#n$1;
F#n$1 := F#n;
endfor
return F#n;
endif
Data Structures and Algorithms 31
Do" much does it cost=
letJs count the number of operations e;ecuted
algorithm Fibonacci"(int n) int
if ( n==1 || n==2 ) then
return 1;
else
F#n$1 := 1; .............. 1 time
F#n$2 := 1; .............. 1 time
for i:=3 to n do ........... (n-1) times
F#n := F#n$1 + F#n$2; . (n-2) times
F#n$2 := F#n$1; ........ (n-2) times
F#n$1 := F#n; ........... (n-2) times
endfor
return F#n; ............... 1 time
endif
!otal......... 4n-4
. 3ime is proportional to n
. Space 1memory2 is constantL
Data Structures and Algorithms 32
3hatJs all fol4sL Hr not=
LetJs consider the matri; A
3heorem for any n2+* "e have
1demonstrable by induction2
A=
1 1
1 0
A
n1
=
1 1
1 0
n1
=
F
n
F
n1
F
n1
F
n2
Data Structures and Algorithms 33
,deaL Algorithm 0ibonacciR
:e e;ploit the previous theorem to define algorithm
0ibonacciR as follo"s
algorithm Fibonacci%(int n) : int
& = &at'o(( )* n-1 );
ret+rn &[1][1];
A=
1 1
1 0
$U)VU)V is the first
item of the ro"
Data Structures and Algorithms 34
Tes but...Algorithm $atFo"=
3o compute the 4.th po"er of a matri; A* "e e;ploit the
fact that* for even E* A
k
. ,A
k+
-
+
algorithm &at'o((&atri, )* int -) &atri,
if ( -==. ) then
else
if ( - is e/en ) then
t$0 := &at'o(()*-2)
& := t$0 121 t$0;
else
t$0 := &at'o(()*(--1)2);
& := t$0 121 t$0 121 );
endif
endif
return &;
M=
1 0
0 1
operator JWJ computes the
product of matrices
Data Structures and Algorithms 35
3o sum up
0ibonacci'
0ibonacci/
0ibonacci@
0ibonaccR
3,+
n+
-
4,n-
4,n-
4,lo! n-
4,n-
4,n-
4,)-
4,lo! n-
Algorithm Time Memory
E;ponential time
Logaritmic time
Data Structures and Algorithms 36
Lessons learned=
0or a given problem* "e started from a inefficient
algorithm 1e;ponential cost2 to reach a very efficient
algorithm 1logaritmic cost2.
3he choice of the good algorithm ma4es the difference
bet"een being able to solve a problem or GH3.
Data Structures and Algorithms 37
:armup e;ercise
8iven an array AU)..n.)V containing a permutation of all
values ) . n 1e;tremes included2 but oneK values in A can
be in any order
Eg A > U)* /* @* QV is a permutation of )..Q "ithout the value '
Eg A > U6* )* /* Q* @* 'V is a permutation of )..6 "ithout the value
R
LetJs "rite an algorithm "hich ta4es AU)..n.)V* and returns
the value in the interval )..n "hich is not in A.