=========================================================================== CSC 373H / L0101 Lecture Summary for Week 1 Winter 2005 =========================================================================== -------------------------- Administrative information -------------------------- See Course Information Sheet. Office hours for section L0101: Tuesdays 12:00-1:30 Background (from CSC263 and its prerequisites): - Asymptotic notation (big-Oh, Omega, Theta), analysis of runtimes for iterative and recursive algorithms. [Chapter 2] - Graphs: definitions, properties, traversal algos (BFS, DFS). [Ch 3] - Data structures: queues, stacks, hashing, balanced search trees, priority queues, heaps, union-find/disjoint sets. [Ch 2] - Induction and other proof techniques, proving correctness of iterative and recursive algorithms. Motivation: - Abstraction is good. Seen in ADTs: implement once, reuse often. - Same for algorithms: certain "types" of solutions come up often; useful to identify them and know about their general properties. - Overall goal: solve problems efficiently. ----------------- Greedy Algorithms [Chapter 4] ----------------- "At each step, make the choice that seems best at the time; never change your mind." Activity Scheduling (called "interval scheduling" in text). Input: Activities A_1, A_2, ..., A_n. Each activity A_i consists of start time s(i) and finish time f(i) (s(i) < f(i)). Output: Subset of activities S such that all activities are "compatible" (no two of them overlap in time) and |S| is maximal. A. Brute force: consider each subset of activities. Correctness? Trivial. Runtime? Omega(2^n), not practical. B. Greedy by start time: sort activities s.t. s(1) <= s(2) <= ... <= s(n) S := {} // partial schedule f := 0 // last finish time of activities in S for i := 1 to n: if f <= s(i): // A_i is compatible with S S := S U {A_i} f := f(i) return S Runtime? Sorting is Theta(n log n), main loop is Theta(n). Total is Theta(n log n). Correctness? Doesn't work. Counter-example: |-----------------------------| |---| |---| |---| ... |---| C. Greedy by duration: same as above except sort by nondecreasing duration, i.e., f(1)-s(1) <= f(2)-s(2) <= ... <= f(n)-s(n) Correctness? Counter-example: |-----| |-----| |-----| |-----| ... |-----| |-----| |---| |---| |---| D. Greedy by overlap count: same as above except sort from fewest conflicts to most conflicts ("conflict" = overlap with some other activity) Correctness? Counter-example: |---| |---| |---| |---| ... |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| |---| E. Greedy by finish time: same as above except sort by nondecreasing finish time, i.e. f(1) <= f(2) <= ... <= f(n) Correctness? Next time...