Download Insertion Sort - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!
CSE115/ENGR160 Discrete Mathematics 02/28/
Insertion sort
- Start with 2nd^ term
- Larger than 1 st^ term, insert after 1 st^ term
- Smaller than 1 st^ term, insert before 1 st^ term
- At this moment, first 2 terms in the list are in correct positions
- For 3rd^ term
- Compare with all the elements in the list
- Find the first element in the list that is not less than this element
- For j-th term
- Compare with the elements in the list
- Find the first element in the list that is not less than this element
Insertion sort
procedure insertion sort ( a 1 , a 2 , …, an : real numbers with n≥2) i :=1 (left endpoint of search interval) j := 1 (right end point of search interval) for j:=2 to n begin i:= while a (^) j >a (^) i i:=i+ m:=aj for k:=0 to j-i- aj-k:= a (^) j-k- a (^) i := m end {a 1 , a 2 , …, a (^) n are sorted}
Greedy algorithm
- Many algorithms are designed to solve optimization problems
- Greedy algorithm:
- Simple and naïve
- Select the best choice at each step, instead of considering all sequences of steps
- Once find a feasible solution
- Either prove the solution is optimal or show a counterexample that the solution is non-optimal
Greedy change-making algorithm
procedure change ( c 1 , c 2 , …, cn : values of denominations of coins, where c 1 > c 2 >…> c (^) n ; n: positive integer)
for i:=1 to r
while n≥c (^) i then add a coin with value c (^) i to the change n:=n- c (^) i end
Example
- Change of 30 cents
- If we use only quarters, dimes, and pennies (no nickels)
- Using greedy algorithm:
- 6 coins: 1 quarter, 5 pennies
- Could use only 3 coins (3 dimes)
Proof (Lemma)
- Proof by contradiction
- Show that if we had more than the specified number of coins of each type, we could replace them using fewer coins that have the same value - If we had 3 dimes, could replace with 1 quarter and 1 nickel - If we had 2 nickels, could replace them with 1 dime - If we had 5 pennies, could replace them with 1 nickel - If we had 5 pennies, could replace them with 1 nickel - If we had 2 dimes and 1 nickel, could replace them with 1 quarter - Because we could have at most 2 dimes, 1 nickel, and 4 pennies, but we cannot have two dimes and a nickel, it follows 24 cents is the most we can have
Theorem
- Theorem: The greedy change-making algorithm produces change using the fewest coins possible
- Proof by contradiction
- Suppose that there is a positive integer n such that there is a way to make change for n cents using fewer coins (q’) than that of the greedy algorithm
- Let the number of quarters be q’, and the number of quarters used in the greedy algorithm be q
Proof
- As there must be the same number of quarters in the two algorithms
- The value of the dimes, nickels and pennies in these two algorithms must be the same, and their value is no more than 24 cents
- Likewise, there must be the same number of dimes,
- as the greedy algorithm used the most dimes possible
- and by Lemma 1, when change is made using the fewest coins possible, at most 1 nickel and a most 4 pennies are used, so that the most dimes possible are also used in the optimal way to make change
- Likewise, we have the same number of nickels, and finally the same number of pennies
The halting problem
- One of the most famous theorems in computer science
- There is a problem that cannot be solved using any procedure
- That is, we will show there are unsolvable problems
- The problem is the halting problem
The halting problem
- First note that we cannot simply run a program and observe what it does to determine whether it terminates when run with the given input
- If the program halts, we have our answer
- But if it is still running after any fixed length of time has elapsed, we do not know whether it will never halt or we just did not wait long enough for it to terminate
Turing’s proof
- Halting problem is unsolvable
- Proof by contradiction
- The proof presented here is not completely rigorous
- Proof: Assume there is a solution to this halting problem called H(P,I) where P is a program and I is input
- H(P,I) outputs the string “halt” as output if H determines P stops when given I
- Otherwise, H(P,I) generates the string “loops forever” as output
Turing’s proof
- Construct a simple procedure K(P) that makes use of the output H(P,P) but does the opposite of H
- If the output of H(P,P) is “loops forever”, then K(P) halts
- IF the output of H(P,P) is “halt”, then K(P) loops forever
Turing’s proof
- Suppose we provide K as input to K
- We note that if the output of H(K,K) is “loops forever”, then by the definition of K, we see K(K) halts
- Otherwise, if the output of H(K,K) is “halt”, then by the definition of K we see that K(K) loops, in violation of what H tells us
- In both cases, we have contradiction
- Thus H cannot always give the correct answers
- No procedure solves the halting problem