Lecture 01 - Algorithms, Correctness, and Efficiency
Lecture 01 - Algorithms, Correctness, and Efficiency
Lecture 01
Algorithms, Correctness, and E ciency
Lukas Holik
[email protected]
(based on slides of Giovanni Bacci, thanks!)
1
ffi
Outline
• Correctness of an algorithm
2
ffi
fi
Intended Learning Goals
KNOWLEDGE
• Mathematical reasoning on concepts such as recursion, induction,
concrete and abstract computational complexity
• Data structures, algorithm principles e.g., search trees, hash tables,
dynamic programming, divide-and-conquer
• Graphs and graph algorithms e.g., graph exploration, shortest path,
strongly connected components.
SKILLS
• Determine abstract complexity for speci c algorithms
• Perform complexity and correctness analysis for simple algorithms
• Select and apply appropriate algorithms for standard tasks
COMPETENCES
• Ability to face a non-standard programming assignment
• Develop algorithms and data structures for solving speci c tasks
• Analyse developed algorithms
3
fi
fi
Problem Solving Procedure
Covered in this course
• Analysis Real-life problem
• Speci cation
• Correctness Computational
• E ciency Problem
• Design
• Computational Thinking
• Algorithm patterns Algorithms
• Data Structures
NOT about Coding
Blueprint
• Speci c programming languages
• Computer architectures
• Software architectures
t Program Source
• Software design and developmen Code
principles
• Software testing and veri cation 4
ffi
fi
fi
fi
What are algorithms?
Input Output
Example:
• An input sequence ⟨32,5,8,9,20⟩ is a valid instance of
the sorting problem.
• Its associated output sequence is ⟨5,8,9,20,32⟩.
De nition: An instance of a problem consists of the input
(satisfying whatever constraints are imposed in the problem
statement) needed to compute a solution to the problem.
7
fi
Quiz
The element search problem:
Input: A sequence of n numbers ⟨a1, a2, …, an⟩ and a number a
Output: An index 1 ≤ i ≤ n such that a = ai if there exists such
an index, 0 otherwise.
8
Quiz
The element search problem:
Input: A sequence of n numbers ⟨a1, a2, …, an⟩ and a number a
Output: An index 1 ≤ i ≤ n such that a = ai if there exists such
an index, 0 otherwise.
• Which of the following are correct I/O pairs for the ‘element
search problem’?
A. ⟨1,2,4,20,0, − 5⟩, 2 ↦0
B. ⟨1,2,4,20,2, − 5⟩, 2 ↦2
C. ⟨1,2,4,20,2, − 5⟩, 2 ↦5
D. ⟨1,7,30,5⟩, 20 ↦0
E. ⟨⟩, 4 ↦0
Find-Element(A, a)
Sketched Idea: scan the array 1 j=0
A[1..n] element by element and 2 for i = 1 to A . length
compare the current element
A[i] with a. If the two are equal, 3 if A[i] = a
store the current index. 4 j=i
5 return j
12
fi
Finding an element
There are many alternative approaches for solving the same
problem. The following pseudocode describes one possible
alternative algorithm for solving the element search problem.
13
Quiz
• Consider the pseudocode aside. Find-Element(A, a)
What will be the value of the
1 j=0
variables i and j after execution
on the following instances? 2 for i = 1 to A . length
A. ⟨1,2,4,20,0, − 5⟩, 2 3 if A[i] = a
B. ⟨1,2,4,2,2,3⟩, 2 4 j=i
C. ⟨1,7,30,5⟩, 3 5 return j
D. ⟨⟩, 4
14
Ef ciency
15
ff
fi
ff
ffi
ffi
fi
Ef ciency Analysis
• Analysing the e ciency of algorithms deals with
predicting the resources that the algorithm requires.
16
fi
fi
ff
ffi
ff
Input Size
The best notion of input size depends on the problem being
solved
17
Running Time
• The running time of an algorithm on a particular input is
the number of primitive operations or ‘steps’ executed
Remark: as done before with na, you can use additional variables to help
you describe the exact expression for T(n).
21
Answer
Describe the running time of Find-ElementV2 on a generic instance
(A[1..n], a) as a function T(n) where n is the number of elements of A.
23
Order of growth
• We already used some abstraction to describe T(n)
1. We ignored actual cost of statements by using generic
constants ci
2. Then, we expressed the worst-case running time as
an + b for some a, b > 0 (that depend on ci’s)
• We can abstract even further by only looking at the rate of
growth (or order of growth) of T(n)
• We consider only the leading term (i.e., an) and we also
ignore its constant coe cient for large values of n
• Find-Element has worst case running time of Θ(n)
24
ffi
Quiz
Comparison of running times. For each function f(n) and time t in
the following table, determine the largest size n of an instance that can
be solved in time t, assuming the algorithms for solving the problem
takes f(n) microsecond (1 microsecond = 10−6 sec)
1 sec 1 min 1 hr
log n
n
2
n
n
2
25
Learned Today
• Formal concepts of
• algorithm
• computational problem
• problem instance
• correctness of an algorithm