Professional Documents
Culture Documents
Data Structures: Lecture 1: Introduction
Data Structures: Lecture 1: Introduction
Lecture 1: Introduction
Algorithms
Algorithm
a well-defined computational procedure that takes some
value, or a set of values, as input and produces some
value, or a set of values, as output
Algorithm
sequence of computational steps that transform the
input into the output.
Algorithms research
design and analysis of algorithms and data structures
for computational problems
Data structures
Data Structure
a way to store and organize data to facilitate access and
modifications
Implementation
a way to realize the desired functionality
how is the data stored (array, linked list, …)
which algorithms implement the operations
The course
Design and analysis of efficient algorithms for some
basic computational problems:
Incremental algorithms
process the input elements one-by-one and maintain the
solution for the elements processed so far.
Incremental algorithms
In pseudo-code:
IncAlg (A) comment
► incremental algorithm which computes the solution of a problem
with input A = { x1,…,xn }
1. initialize: compute the solution for { x1 }
2. for i ← 2 to n assignment (Pascal :=)
3. do begin
4. compute the solution for { x1,…,xj } using the (already
computed) solution for { x1,…,xj-1 }.
5. end the book uses indentation only
An incremental sorting algorithm
InsertionSort (A)
► incremental algorithm that sorts array A[1..n] in non-decreasing
order
1. initialize: sort A[1]
2. for j ← 2 to length[A]
3. do begin
4. sort A[1..j] using the fact that A[1.. j-1] is already sorted.
5. end
An incremental sorting algorithm
InsertionSort (A)
► incremental algorithm that sorts array A[1..n] in non-decreasing
order
1. initialize: sort A[1]
2. for j ← 2 to length[A]
3. do begin
4. sort A[1..j] using the fact that A[1.. j-1] is already sorted.
5. end
key ← A[ j ] ; i ← j -1
1 j n while i > 0 and A[ i ] > key
1 3 14 17 28 6 … do A[ i+1] ← A[ i ]; i ← i -1
A[ i +1] ← key
Correctness proof via an invariant
InsertionSort (A)
1. initialize: ---
2. for j ← 2 to length[A]
3. do begin
4. key ← A[ j ] ; i ← j -1
while i > 0 and A[ i ] > key
do begin A[ i+1] ← A[ i ]; i ← i -1 end
A[ i +1] ← key
5. end
After the algorithm has terminated, Inv(n) holds. Inv(n) implies that
the output of the algorithm is correct.
Divide-and-conquer
Divide-and-conquer
break the problem into two or more subproblems, solve
the subproblems recursively, and then combine these
solutions to create a solution to the original problem.
Divide-and-conquer
D&CAlg (A)
► divide-and-conquer algorithm that computes the solution of a
problem with input A = { x1,…,xn }
1. if # elements of A is small enough (for example 1)
2. then compute Sol (the solution for A) brute-force
3. else begin
4. split A in, for example, 2 non-empty subsets A1 and A2
5. Sol1 ← D&CAlg (A1) ; Sol2 ← D&CAlg (A2 )
6. compute Sol (the solution for A) from Sol1 and Sol2
7. end
8. return Sol
Sorting by divide-and-conquer: MergeSort
MergeSort (A)
► divide-and-conquer algorithm that sorts array A[1..n]
1. if length[A] = 1
2. then compute Sol (the solution for A) brute-force
3. else begin
4. split A in 2 non-empty subsets A1 and A2
MergeSort (A)
► divide-and-conquer algorithm that sorts array A[1..n]
1. if length[A] = 1
2. then skip
3. else begin
4. n ← length[A] ; n1 ← floor(n/2); n2 ← ceil(n/2);
copy A[1.. n1] to auxiliary array A1[1.. n1]
copy A[n1+1..n] to auxiliary array A2[1.. n2]
3 14 1 28 17 8 21 7 4 35
1 3 4 7 8 14 17 21 28 35
3 14 1 28 17 8 21 7 4 35
1 3 14 17 28 4 7 8 21 35
3 14 1 28 17
3 14 1 17 28
3 14
Sorting by divide-and-conquer: MergeSort
Merging
A1 1 3 14 17 28 A2 4 7 8 21 35
A 1 3 4 7 8 14 17 21 28 35
MergeSort: correctness proof
• move all A[i] with A[i] > pivot into auxiliary array A2
InsertionSort (A)
1. for j ← 2 to length[A]
2. do begin
3. key ← A[ j ] ; i ← j -1
6. A[ i +1] ← key
7. end
Analysis of algorithms
Analyze the running time as a function of n (# of input
elements)
best case
average case
worst case
elementary operations
add, subtract, multiply, divide, load, store, copy, conditional
and unconditional branch, return …
Analysis of algorithms: example
n=10 n=100 n=1000
InsertionSort: 15 n2 + 7n – 2 1568 150698 1.5 x 107
10466 204316 3.0 x 106
MergeSort: 300 n lg n + 50 n
InsertionSort InsertionSort
6 x faster 1.35 x faster
MergeSort
5 x faster
InsertionSort: Θ (n2)
MergeSort: Θ (n log n)
Θ-notation
19 n3 + 17 n2 - 3n becomes Θ(n3)
n - ¾ n √n becomes ---
Some rules and notation
lg n denotes log2 n
1. logc (ab) =
logc a + logc b
2. logc ( ab ) =
b logc a
3. loga b =
logc b / logc a
Find the leading term
lg35n vs. √n ?
n100 vs. 2 n ?