Introduction to Algorithms
Notes
- What is an algorithm?
- Our authors suggest "Any well defined computational procedure that takes some value, or set of values, as input and produces a value, or set of values as output."
- Implied in this definition is a finite set of steps
- And that the output is the correct.
- They discuss Computational problem
- The statement of which describes the relationship between the input and output.
- They state that this must be well defined or well specified
- For example, they discuss the sorting problem
- Input: A sequence of values <a1, a2, ..., an>, with a well defined comparison operator.
- Output: A permutation of these numbers, <a1', a2', ..., an'> such that a1' ≤ a2' ≤ ... ≤ an'.
- An instance of the problem is a specific set of data that meets the constraints
- < 8, 23, 4, 1, 9, 22, 8> is an instance of the sorting problem.
- < dog, cat, cow, rabbit, horse, zebra, ant> is also an instance of the sorting problem.
- An algorithm is said to be correct if for every possible instance, the algorithm halts with the correct output.
- An algorithm is usually considered to be efficient if it solves the problem in polynomial time. (But more about that later).
- In this class we will be concerned with
- What does an algorithm do at a high level?
- How does it do this, at a lower level?
- Is the algorithm correct?
- What is the performance of the algorithm?
- Let's do this informally with the problem of searching
- Given a sequence of values <a1, a2, ..., an>, with a well defined comparison operator, and an additional value k, is there an ai, 1 ≤ i ≤ n, such that k = ai?
- Algorithm 1: Sequential Search:
SEQUENTIAL-SEARCH(A, k)
- found = false
- i = 1
- while i < A.length and not found
- if A[i] == k
- found = true
- i = i + 1
- return found
- Note, the book uses arrays from 1 to n, not 0 to n-1
- I will undoubtedly switch between the two.
- We will discuss the algorithm notation shortly
- How does this work?
- The best way is to trace the algorithm for several different cases.
- A = < 8, 23, 4, 1, 9, 22, 8>, k = 8.
-
i found 1
falsetrue - A = < 8, 23, 4, 1, 9, 22, 8>, k = 22.
-
i found 1 false 2 3 4 5 6
falsetrue - A = < 8, 23, 4, 1, 9, 22, 8>, k = 5.
-
i found 1 false 2 3 4 5 6 7 8
- How does the algorithm work?
- It finds if the key value is present: too vague
- it sets found equal to false, and it sets i equal to 1, then while I is less than the length of the data... way too detailed, this is equivalent to writing the code in English.
- It sets a flag to false, then looking at each value in order, it checks to see if it is equal to the key, if so it returns true, otherwise it returns false.
- Is the algorithm correct:
-
The algorithm assumes the key value is not present (line 1) Starting with the first value (line 2) in the array Examine each value of the array to see if it is the key (lines 3,4, 6) If the value in the array is the key, mark the flag as true and exit the loop (lines 5 and 3) We know that this algorithm will exit when the counter exceeds the size of the data, or the item is found. - This is just one method, we will examine other methods soon.
-
- How long does the this algorithm take to run?
- If the key is in the data, it may take as little as executing each line 1 time, with a possible second time for line 3
- If the key is not in the data, line 3-6 will execute once for each element of the data, and perhaps one more time.
- So in the best case, this will execute 7 statements, in the worst it will execute 3+ 3*n, where n is the number of elements in the data set.
- This analysis has some assumptions that we will examine later.
- In general, we always want to
- Give the algorithm and state conditions on the input
- Give a high level description of the algorithm
- Discuss at a medium level of detail how the algorithm works
- Discuss at a medium level of rigor why the algorithm is correct
- Discuss the performance of the algorithm.