Discrete Mathematics CS 2610 February 24, 2009 -- part 5
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.hws.edu/TMCM/java/xSortLab/
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 ai+1 i=i-1 end Worst-Case: The sequence is in decreasing order At step j, the while loop condition is executed j times the body of the loop is executed j-1 times
Greedy Algorithms Can you think of a better solution ? Problem: Assign meeting to conference rooms Policy: In decreasing order of room capacity, assign a meeting to the next largest available room Room A 200 Room B 150 Room C 150 Room D 100 Room E 75 Room F 50 Meeting 1: 70 Meeting 2: 46 Meeting 3: 125 Meeting 4: 110 Meeting 5: 30 Meeting 6: 87 M 1 M 2 Can you think of a better solution ? M3 X M5 X
Greedy Algorithm Policy: : In ascending order of room capacity, assign a meeting to the smallest room that can hold the meeting. Room A 200 Room B 150 Room C 150 Room D 100 Room E 75 Room F 50 Meeting 1:70 Meeting 2:46 Meeting 3:125 Meeting 4:110 Meeting 5:30 Meeting 6:87 M 2 M 1 M5 M3 M4 M6
Order of Growth Terminology O(1) Constant O(log cn) Logarithmic (c Z+) O(logc n) Polylogarithmic (c Z+) O(n) Linear O(nc) Polynomial (c Z+) O(cn) Exponential (c Z+) O(n!) Factorial Best Worst
Complexity of Problems Tractable A problem that can be solved with a deterministic polynomial (or better) worst-case time complexity. Also denoted as P Example: Search Problem Sorting problem Find the maximum
Complexity of Problems Intractable Problems that are not tractable. Example: Traveling salesperson problem Wide use of greedy algorithms to get an approximate solution. For example under certain circumstances you can get an approximation that is at most double the optimal solution.
P vs. NP NP: Solvable problems whose solution can be checked in polynomial time. PNP The most famous unproven conjecture in computer science is that this inclusion is proper. PNP rather than P=NP
Complexity of Problems Not Solvable Proven to have no algorithm that computes it Example: Halting problem (Alan Turing) Determine whether an arbitrary given algorithm, will eventually halt for any given finite input. Corollary: The question of whether or not a program halts for a given input is unsolvable.