Finish Activity-Selector, Graph Algorithms




CS146

Chris Pollett

Apr 30, 2014

Outline

Introduction

Last Day's Substructure Results

Greed is Good (in this case)

Theorem. Consider any nonempty subproblem `S_k`, and let `a_m` be an activity in `S_k` with the earliest finish time (might be more than one). Then `a_m` is included in some maximum-size subset of mutually compatible activities of `S_k`.

Proof. Let `A_k` be a maximum-size subset of mutually compatible activities in `S_k` and let `a_j` be the activity in `A_k` with the earliest finish time. If `a_j = a_m` as then `a_m` would be in some maximal sized solution of the `S_k` subproblem. If `a_j ne a_m`, let `A_k' = (A_k - {a_j}) cup {a_m}`. The activities in `A_k'` are disjoint because the activities in `A_k` are disjoint, and `a_j` is the first activity in `A_k` to finish and `f_m le f_j`. Since `|A_k'| = |A_k|`, we conclude that `A_k'` is a solution as well. Moreover, it contains `a_m`. QED.

A Recursive Greedy Algorithm

RECURSIVE-ACTIVITY-SELECTOR(s,f,k,n)
1 m = k + 1
2 while m ≤ n and s[m] < f[k]
3     m = m + 1
4 if m ≤ n
5    return {a_m} ∪ RECURSIVE-ACTIVITY-SELECTOR(s, f, m, n)
6 else return 0.

RECURSIVE-ACTIVITY-SELECTOR in Action

Tables of inputs to algorithm
Activity-Selection computation example

Elementary Graph Algorithms

Example Graph

Representations of Graphs

Two representations for undirected graphs
Two representations for diected graphs

Breadth-First Search

Breadth First Search Algorithm

Image showing BFS algorithm

BFS Example

Example fo BFS Search

BFS Runtime