Beruflich Dokumente
Kultur Dokumente
i
i
i
i
| |
i A famous quote: Program = Algorithm + Data
Structure.
i All of you have programmed; thus have already been
exposed to algorithms and data structure.
i Perhaps you didn't see them as separate entities;
i Perhaps you saw data structures as simple
programming constructs (provided by STL--
STL--
standard template library).
i However, data structures are quite distinct from
algorithms, and very important in their own right.
| |
!"|
i The main focus of this course is to introduce you to a
systematic study of algorithms and data structure.
i The two guiding principles of the course are:
abstraction and formal analysis.
i Abstraction: We focus on topics that are broadly
applicable to a variety of problems.
i Analysis: We want a formal way to compare two
objects (data structures or algorithms).
i In particular, we will worry about "always correct"-
correct"-
ness, and worst-
worst-case bounds on time and memory
(space).
| |
# $|
%||
i ÷oundations of Algorithm Analysis and Data
Structures.
i Analysis:
ü How to predict an algorithm·s performance
ü How well an algorithm scales up
ü How to compare different algorithms for a problem
i Data Structures
ü How to efficiently store, access, manage data
ü Data structures effect algorithm·s performance
| |
i &
i
|'&
( ))* )
( * )* )
* )* )
* )* $
))* ( )
* ( )* )
* )* )
* )* +
| |
&
i onstructions of Euclid
i Newton's root finding
i ÷ast ÷ourier Transform
i ompression (Huffman, Lempel-
Lempel-Ziv, GI÷, MPEG)
i DES, RSA encryption
i Simplex algorithm for linear programming
i Shortest Path Algorithms (Dijkstra, Bellman-
Bellman-÷ord)
i Error correcting codes (Ds, DVDs)
i TP congestion control, IP routing
i Pattern matching (Genomics)
i Search Engines
| |
, '
i Two algorithms for computing the ÷actorial
i Which one is better?
i j j j
j
}
| |
Role of Algorithms in Modern World
| |
A Real-
Real-World Problem
i ommunication in the Internet
i Message (email, ftp) broken down into IP packets.
i Sender/receiver identified by IP address.
i The packets are routed through the Internet by special
computers called Routers.
i Each packet is stamped with its destination address,
but not the route.
i Because the Internet topology and network load is
constantly changing, routers must discover routes
dynamically.
i What should the Routing Table look like?
| |
IP Prefixes and Routing
i Each router is really a switch: it receives packets at
several input ports, and appropriately sends them out to
output ports.
i Thus, for each packet, the router needs to transfer the
packet to that output port that gets it closer to its
destination.
i Should each router keep a table: IP address x Output
Port?
i How big is this table?
i When a link or router fails, how much information would
need to be modified?
i A router typically forwards several million packets/sec!
| |
Data Structures
i The IP packet forwarding is a Data Structure problem!
i Efficiency, scalability is very important.
| |
Algorithm 1 for Max Subsequence Sum
j j
jj
j
j j
j
j j
à è ` `
j j
` èè
èè
j
à è à è
jj
j
à è
j j
jj
j
j j
j j
j
j
jj
j
| |
Algorithm 3
i This algorithm uses divide-
divide-and-
and-conquer paradigm.
i Suppose we split the input sequence at midpoint.
i The max subsequence is entirely in the left half,
half,
entirely in the right half,
half, or it straddles the
midpoint..
midpoint
i Example:
left half | right half
4 -3 5 -2 | -1 2 6 -2
i Max in left is 6 (A1 through A3); max in right is
8 (A6 through A7). But straddling max is 11 (A1
thru A7).
| |
Algorithm 3 (cont.)
i Example:
left half | right half
4 -3 5 -2 | -1 2 6 - 2
i Max subsequences in each half found by recursion.
i How do we find the straddling max subsequence?
i Key Observation:
Observation:
ü Left half of the straddling sequence is the max
subsequence ending with -2.
ü Right half is the max subsequence beginning with -1.
T(1) = 1
T(n) = 2T(n/2) + O(n)
| |
Simplifications
i Keep just one term!
ü the fastest growing term (dominates the runtime)
i No constant coefficients are kept
ü onstant coefficients affected by machines, languages, etc.
ü Example
Examplem
m
'(
ip n = 1,000, then T(n) = 10,001,040,800
i error is 0.01% i we drop all but the n3 term
ü In an assembly line the slowest worker determines the
throughput rate
| |
Simplification
i Drop the constant coefficient
ü Does not effect the relative order
| |
Simplification
i The faster growing term (such as 2n)
eventually will outgrow the slower growing
terms (e.g., 1000 n) no matter what their
coefficients!
Rá Rá R á á á
Rá Rá á á ' '
Rá Rá á
'
Rá á á
á 'á
| |
` ` ` ` `' ` '`
'
' '
'
' ' '
'
' ' ' ' '
§§§§
§§§§§
§§§§
§§§§
§§§§
§§§§ §§§
§§§§
§§
§§§§
§
§§§§
§
| |
Another View
i More resources (time and/or processing power) translate into
large problems solved if complexity is low
.0
= +
=
+
=
>2/ >2? +
>22 >2 >22 >2
>222 > >2 >2
@A >? ?@ /7A
/ >2 AA A7A
A >2 >/ >7/
| |
Asympotics
m è
| |
aveats
i ÷ollow the spirit, not the letter
ü a 100n algorithm is more expensive than n2
algorithm when n < 100
i àther considerations:
ü a program used only a few times
ü a program run on small data sets
ü ease of coding, porting, maintenance
ü memory requirements
| |
Asymptotic Notations
i Big--à, ´bounded above byµ: T(n) = à(f(n))
Big
ü ÷or some c and N, T(n) @ c f(n) whenever n > N.
| |
By Pictures
i Big
Big--àh
(most
commonly used)
ü bounded above
i Big
Big--àmega °
ü bounded below
i Big
Big--Theta
ü exactly
i Small
Small--o
°
ü not as expensive as ...
| |
Example
m `è ` } '` '
è
è
` `
'
` `
` `
| |
,
`è á
w è
` w` è
`
w` ' è
` '
w` è
`
w` è
`
w ` è
` w ` ` è ` è
`
w ` è
| |
Summary (Why à(n)?)
im
#
i Too complicated
i à
ü a single term with constant coefficient
dropped
i Much simpler, extra terms and
coefficients do not matter asymptotically
i àther criteria hard to quantify
| |
Runtime Analysis
i Useful rules
ü simple statements (read, write, assign)
ià(1) (constant)
ü simple operations (+ - * / == > >= < <=
ià(1)
| |
Runtime Analysis (cont.)
if (cond) then O(1)
body1 T1(n)
else
body2 T2(n)
endif
| |
Runtime Analysis (cont.)
i Method calls
ü A calls B
ü B calls
ü etc.
iA sequence of operations when call
sequences are flattened
T(n) = max(TA(n), TB(n), TC(n))
| |
Example
for (i=1; i<n; i++)
if A(i) > maxVal then
maxVal= A(i);
maxPos= i;
| |
Example
for (i=1; i<n-
i<n-1; i++)
for (j=n; j>= i+1; j--
j--))
if (A(j-
(A(j-1) > A(j)) then
temp = A(j-
A(j-1);
A(j--1) = A(j);
A(j
A(j) = tmp;
endif
endfor
endfor
| |
i ==
' 7
i ( ''
( '' ))'' =& + ' '
==( ' ' 77 &
(3 , 3 & &( 7
& (3 , 3 ( = & &
==( 7
i ,( ' & ,
' 7
i
)) &= ,( 3 =
3 D( 3 &E
3 3 = & '7
| |
|
i , | E- F ( &
G,3 7
i
& '- |'' ' H=
& H7
i = - ' I
&= 3 ' 3
3 ' E ' =( 7
i | - 3
''
| |
( J
i ) 3 & ' (
)
= 5= ( 3 & & , 7
E ' (3 3 77 '3
D7 = (
= ' & ( 5= +- >)) 7 >22
= ' & ( 5= +- >
5=4 ?K =( 7 KL =( E4 7 =
'7
i ' ' ' D
( J3
& 5= = 3 D 5=
= (7
i $
=( .(
= 3 07 ')
3 07 ')
' =& AM ANAM )
AM ANAM )>
7 3 & . 07 '(
)) (-
' ( O3 E(..O00
6 E(.O07 ., 3 & 0
| |
|
i ' ' '3
3 7 F 3
E &7 F =( 3
& 3 3 3 ' 3 '
. 07
i ( - 23 >3 A 7
i $ ' ' - 4 , 4
=)))
= )= 7P7
i ( | - ' =(
7
i F E =( | HH
(- ' ( O3 E( ' = 6
E(.O0 E( = *
E(.O07
| |
|(
i |'
|'))5 ( |
ü ."
3 & =3
) 0 . 0 & 3
= 3
D , ' =3 =7
ü |( I ( , =
4 ( (
3 ( " %
7
| |
i '3 E ' '
, .07
i 3 & & E))= 3 & E
3 & & E
& ' (-
ü = ' E 3
ü
7
i ) ( ' 773
F 7
i ( ( ( ( 3
& = ' & '7
i | & & + = ' 3 & &
+ ' ) +7
i 3 & E( ' = @2))A222 3
3 & E( ' = @2
& = '7
| |
#
|
i # ' '
= =5
= =7
i $(
$( ))' = = =(
7
i
' (
( '
=
| |
, ' #
i (-
1 4 1 ' &
1 4 1 ' & ))
'7 .&G 1 '4 4 (0
i -
1 4 1 E7
i - .Q ' 0
1 4 1 'GDG))
1 4 1 'GDG
i -
1 4 1 D4
| |
|
i .MA0
,3 |
i E | ) = "
E | )
i
ü
)) .0 7
ü $)
$) . 0 7
i JE
ü ' ' 7
ü I , . 03 &
I , . 03 &))
.MA04
i $
ü , '
, ' )))
)D7
ü .0 1 AM 8 1 8 7
| |
=
=
i $ |D
i
i !
i $+
i (
=
i | $
| |
i 5( $,3 |
i |
3 F |
3
i |
| )) $3 |
R
S 3 LES 3
i D3 E E3
i 3
i (
7
| |