Академический Документы
Профессиональный Документы
Культура Документы
and Data
Structures
(c) Marcin
Sydow
Algorithms
and Data
Structures
(c) Marcin
Sydow
Algorithms
and Data
Structures General:
(c) Marcin T.Cormen, C.Leiserson, R.Rivest et al.
Sydow
Introduction to Algorithms, MIT Press an excellent
textbook for beginners and practitioners (also available in
Polish: Wprowadzenie do Algorytmów, WNT 2000)
Algorithms
and Data
Structures
(c) Marcin
N.Wirth Algorithms + Data Structures = Programs (also
Sydow
in Polish)
Algorithms
and Data
Structures
(c) Marcin
Sydow What does algorithm mean?
Algorithm
Algorithms
and Data
Structures
(c) Marcin
Sydow What does algorithm mean?
Algorithms
and Data
Structures Pseudocode
(c) Marcin
Sydow
an abstract notation of algorithm
Algorithms
and Data Task: compute sum of numbers in an array of length len:
Structures
(it is not any particular programming language but precisely expresses the
algorithm
For conveniece, sometimes the '.' (dot) operator will be used (object
Algorithms
and Data
Structures
(c) Marcin
Sydow
Topics:
1 Algorithm Design
2 Algorithm Analysis
3 Data Structures
Algorithm Design
Algorithms
and Data
Structures
(c) Marcin
Sydow
There is a computational task to be performed on computer.
Algorithms
and Data
Structures
Algorithms
and Data Assuming the task: given the array and its length compute
Structures
the sum of numbers in this array
(c) Marcin
Sydow
output:(nal condition)
Algorithm must return:
Algorithms
and Data
Structures correct input data is the data which satises the initial
(c) Marcin condition of the specication
Sydow
Denition
1 stops and
Algorithms
and Data
Structures Usually, while checking the correctness of an algorithm it is
(c) Marcin easier to separately:
Sydow
Denition
Algorithms
and Data
Structures
sum(array, len){
sum = 0
i = 0
while(i < len)
sum += array[i]
return sum
}
Algorithms
and Data
Structures
(c) Marcin
Sydow
1 (to prove that) the algorithm always stops for correct input
data (stop property)
Algorithms
and Data
sum(array, len){
Structures sum = 0
i = 0
(c) Marcin
Sydow while(i < len){
sum += array[i]
i++
}
return sum
}
Algorithms
and Data
sum(array, len){
Structures sum = 0
i = 0
(c) Marcin
Sydow while(i < len){
sum += array[i]
i++
}
return sum
}
Algorithms
and Data
sum(array, len){
Structures sum = 0
i = 0
(c) Marcin
Sydow while(i < len){
sum += array[i]
i++
}
return sum
}
Algorithms
and Data
sum(array, len){
Structures sum = 0
i = 0
(c) Marcin
Sydow while(i < len){
sum += array[i]
i++
}
return sum
}
Algorithms
and Data
sum(array, len){
Structures sum = 0
i = 0
(c) Marcin
Sydow while(i < len){
sum += array[i]
i++
}
return sum
}
Algorithms
and Data
sum(array, len){
Structures sum = 0
i = 0
(c) Marcin
Sydow while(i < len){
sum += array[i]
i++
}
return sum
}
As the result, the algorithm will certainly stop after nite number of
iterations for any input correct data
Proving Partial Correctness - Invariants
Algorithms
and Data
Proving the stop property of an algorithm is usually easy.
Structures
Proving the remaining part of its total correctness (i.e. partial
(c) Marcin
Sydow
correctness) needs usually more work and sometimes invention,
even for quite simple algorithms.
Denition
Algorithms
and Data
Structures
What does the following algorithm do (prove your answer):
(c) Marcin
Sydow
(the names of variables are purposely obscure :) )
input: Arr - an array of integers, len > 0 - length of array
algor1(Arr, len){
i = 1
x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
An example of a typical task in algorithmics:
Algorithms
and Data
Structures
What does the following algorithm do (prove your answer):
(c) Marcin
Sydow
(the names of variables are purposely obscure :) )
input: Arr - an array of integers, len > 0 - length of array
algor1(Arr, len){
i = 1
x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Easy? OK.
An example of a typical task in algorithmics:
Algorithms
and Data
Structures
What does the following algorithm do (prove your answer):
(c) Marcin
Sydow
(the names of variables are purposely obscure :) )
input: Arr - an array of integers, len > 0 - length of array
algor1(Arr, len){
i = 1
x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Algorithms
and Data
Structures 2 steps are needed (what steps?)
(c) Marcin
Sydow
An example - proving total correctness, cont.
Algorithms
and Data
Structures 2 steps are needed (what steps?)
(c) Marcin
Sydow
1 proving the stop property of algorithm
An example - proving total correctness, cont.
Algorithms
and Data
Structures 2 steps are needed (what steps?)
(c) Marcin
Sydow
1 proving the stop property of algorithm
Algorithms
and Data
Structures 2 steps are needed (what steps?)
(c) Marcin
Sydow
1 proving the stop property of algorithm
Algorithms
and Data
Structures 2 steps are needed (what steps?)
(c) Marcin
Sydow
1 proving the stop property of algorithm
algor1(Arr, len){
i = 1
x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
An example - proving total correctness, cont.
Algorithms
and Data
Structures 2 steps are needed (what steps?)
(c) Marcin
Sydow
1 proving the stop property of algorithm
algor1(Arr, len){
i = 1
x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
It was easy.
An example - proving total correctness, cont.
Algorithms
and Data
Structures 2 steps are needed (what steps?)
(c) Marcin
Sydow
1 proving the stop property of algorithm
algor1(Arr, len){
i = 1
x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Algorithms
and Data
Structures algor1(Arr, len){
(c) Marcin
i = 1
Sydow x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Algorithms
and Data
Structures algor1(Arr, len){
(c) Marcin
i = 1
Sydow x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Algorithms
and Data
Structures algor1(Arr, len){
(c) Marcin
i = 1
Sydow x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Algorithms
and Data
Structures algor1(Arr, len){
(c) Marcin
i = 1
Sydow x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Algorithms
and Data
Structures algor1(Arr, len){
(c) Marcin
i = 1
Sydow x = Arr[0]
while(i < len)
if(Arr[i] > x){
x = Arr[i]
}
i++
return x
}
Algorithms
Algorithms
Algorithms
Algorithms
Algorithms
Algorithms
Algorithms
Algorithms
and Data
Structures 1 Organisation and Passing Rules of this course :)
(c) Marcin 2 What is specication
Sydow
(c) Marcin
Sydow