Discrete Mathematics CS 2610 February 24, 2009 -- part 4 Uncountable sets
Theorem: The set of real numbers is uncountable.
If a subset of a set is uncountable, then the set is uncountable. The cardinality of a subset is at least as large as the cardinality of the entire set.
It is enough to prove that there is a subset of R that is uncountable
Theorem: The open interval of real numbers [0,1) = {r ∈ R | 0 ≤ r < 1} is uncountable.
Proof by contradiction using the Cantor diagonalization argument (Cantor, 1879)
2 Uncountable Sets: R PfProof (BWOC) using di n liago ti n nalization: Suppos e R is countable (then any subset say [0,1) is also countable). So, we can list them: r1, r2, r3, … where r1 = 0.d11d12d13d14… the dij are digits 0-9 r2 = 0.d21d22d23d24… r3 = 0. d31d32d33d34… r4 = 0.d41d42d43d44… etc.
Now let r = 0.d1d2d3d4… where di = 4 if dii ≠ 4 di = 5 if dii = 4 But r is not equal to any of the items in the list ∴ it’ s missing from the list so we can’t list them after all. th r differs from ri in the i position, for all i. So, our assumption that we could lis t them all is incorrec t.
An Algorithm is a finite set of precise instructions for performing a computation or for solving a problem. Proppgerties of an algorithm: - input: input values are from a specified set - output: output values are from a specified set - definiteness: each step is precisely defined - correctness: correct output produced - finiteness: takes a finite number of steps - effectiveness: each step is finite & exact - generality: applica ble to various itinput sizes
4 Analysis of Algorithms
Analyzing an algorithm Time comppylexity Space complexity
Time complexity Running time needed by an algorithm as a function of the size of the input Denoted as T(N)
We are iitnterest tded in measuri ng how fast the time complexity increases as the input size grows Asyypmptotic Time Comp lexity of an Alg orithm
procedure procName(argument: type) variable := expression informal statement statement begin statements end {comment} if condition then statement1 [else statement2] for variable := initial value to final value statement while condition statement return expression procName(arg1,..,argn) 6 Algorithm Complexity
Worst Case Analysis
Largest number of operations to solve a problem of a specified size.
Analyypze the worst input case for each inp ut size.
Upper bound of the running time for any input.
Most widely used.
Average Case Analysis Average number of operations over all inputs of a given size Sometimes it’ s too complicated
7 Example: Max Algorithm
procedure max(a1, a2, …, an: integers)
v := a1 1 for i := 2 to n n n - 1 if ai > v then
v := ai ? 0, 1, .., n -1 return v 1
How many times is each step executed ?
Worst-Case: the input sequence is a strictly increasing sequence
8 Searching Algorithms
Searching Algorithms Problem:
Find an element x in a list a1,…an (not necessarily ordered)
Linear Search Strategy: Examine the sequence one element after another until all the elements have been examined or the current element being examined is the element x.
9 Example: Linear Search
procedure linear search (x: integer,
a1, a2, …, an: distinct integers) i := 1
while (i ≤ n ∧ x ≠ ai) i := i + 1 if i ≤ n then location := i else location := 0 return location
Worst-Case occurs when x is the last element in the sequence
Best Case occurs when x is the first element in the sequence
10 Example: Linear Search
Average Case:
x is the first element: 1 loop comparison
x is the second element 2 loop comparisons, 1 iteration of the loop
x is the third element 3 loop comparisons, 2 iterations of the loop
x is n-th element n loop comparisons, n-1 iterations of the loop
11 Binary Search
Problem: Locate an element x in a sequence of sorted elements in non-decreasing order.
Strategy: On each step , look at the middle element of the remaining list to eliminate half of it, and quickly zero in on the desired element.
12 Binary Search
procedure binary search (x:integer, a1, a2, …, an: integer)
{a1, a2, …, an are distinct integers sorted smallest to largest} i := 1 {{fstart of search ran g}ge} j := n {end of search range} while i < j begin m := ⎣(i +j)/2⎦
if x > am then i := m + 1 else j := m end
if x = ai then location := i else location := 0 return location
Suppose n=2k
13 Binary Search
Binary Search The loop is executed k times
k=log2(n)
14 Linear Search vs. Binary Search
Linear Search Time Complexity: T(n) is O(n) Binary Search Time Complexity:
T(n) is O(log2n)
15 Sorting Algorithms
Problem: Given a sequence of numbers, sort the sequence in weakly increasing order.
Sortinggg Algorithms:
Input:
A sequence of n numbers a1, a2, …, an
OtOutpu tt:
A reordering of the input sequence (a’1, a’2, …, a’n) such that a’1 ≤ a’2 ≤ … ≤ a’n
16 Bubble Sort
Smallest elements “float” up to the top (front) of the list, like bubbles in a container of liquid Largest elements sink to the bottom (end).
See the animation at:
httpjp://math.hws.edu/TMCM/java/xSortLab
17 Example: Bubble Sort
procedure bubblesort
(a1, a2, …, an: distinct integers)
for i = 1 to n-1 for j = 1 to n-i
if (aj > aj1j+1) then
swap aj and aj+1
Worst-Case: The sequence is sorted in decreasing order At step i •The loop conditi on of the inner loop is execut ed n – i + 1 times. •The body of the loop is executed n – i times
18 Algorithm- Insertion Sort
For each element: The elements on its left are already sorted Shift the element with the element on the left until it is in the correct place.
See animation http:// math .h ws.ed u/TMCM/j ava/ xS ortL ab/
19 Algorithm- Insertion Sort
procedure insertSort
(a1, a2, …, an: distinct integers)
for j=2 to n begin i=j - 1
while i > 0 and ai > ai+1
swap ai and ai1i+1 i=i-1 end Worst-Case: The sequence is in decreasing order At step j, the whhlile loop cond ition is executed j times
the body of the loop is executed j-1 times 20