Download Pseudo Code - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!
CSE115/ENGR160 Discrete Mathematics 03/03/
3.1 Algorithms
- When presented a problem, e.g., given a sequence of integers, find the larges one
- Construct a model that translates the problem into a mathematical context - Discrete structures in such models include sets, sequences, functions, graphs, relations, etc.
- A method is needed that will solve the problem (using a sequence of steps)
- Algorithm : a sequence of steps
Example
- Perform the following steps
- Set up temporary maximum equal to the first integer in the sequence
- Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this
- Repeat the previous step if there are more integers in the sequence
- Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.
Pseudo code
- Provide an intermediate step between English and real implementation using a particular programming language procedure max ( a 1 , a 2 , …, an : integers) max := a 1 for i:= 2 to n if max < ai then max := ai { max is the largest element}
Searching algorithms
- Locate an element x in a list of distinct elements, a 1 , a 2 , …, a (^) n , or determine it is not in the list
- Solution is the location of the term in the list that equals x, and is 0 if x is not in the list
Linear Search
procedure linear search ( x:integer, a 1 , a 2 , …, a (^) n : distinct integers) i := 1 while (i≤ n and x≠ai ) i:=i+ if i < n then location := n else location:= { location is the index of the term equal to x, or is 0 if x is not found} (^) Docsity.com 8
Binary search
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
- Then compare 19 and the largest term in the first list, and determine to use the list
- Continue
12 13 15 16 18 19 20 22 18 19 20 22 19 (down to one term)
Binary search
procedure binary search ( x:integer, a 1 , a 2 , …, a (^) n : increasing integers) i :=1 (left endpoint of search interval) j := 1 (right end point of search interval) while ( i < j ) begin m:=⌞(i+j)/2⌟ if x>a (^) m then i:=m+ else j:=m end if x=a (^) i then location := i else location:= { location is the index of the term equal to x, or is 0 if x is not found}
Bubble sort
procedure bubble sort ( a 1 , a 2 , …, a (^) n : real numbers with n≥2)
for i:=1 to n-
for j:=1 to n-i if a (^) j >a (^) j+1 then interchange a (^) j and a (^) j+ { a 1 , a 2 , …, a (^) n is in increasing order }
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)