CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem...

36
CSE 331: Review

Transcript of CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem...

CSE 331: Review

Main Steps in Algorithm DesignProblem StatementProblem Statement

AlgorithmAlgorithm

Real world problem

Problem DefinitionProblem Definition Precise mathematical def

“Implementation”“Implementation” Data Structures

AnalysisAnalysis Correctness/Run time

Stable Matching Problem

Gale-Shaply Algorithm

Stable Marriage problem

Set of men M and women W

Matching (no polygamy in M X W)

Perfect Matching (everyone gets married)

Instablity

mm ww

m’ w’

Preferences (ranking of potential spouses)

Stable matching = perfect matching+ no instablity

Gale-Shapley AlgorithmIntially all men and women are free

While there exists a free woman who can propose

Let w be such a woman and m be the best man she has not proposed to

w proposes to m

If m is free

(m,w) get engaged

Else (m,w’) are engaged

If m prefers w’ to w

w remains freeElse

(m,w) get engaged and w’ is free

Output the engaged pairs as the final output

At most n2 iterationsAt most n2 iterations

O(1) time implementation

O(1) time implementation

GS algorithm: Firefly Edition

1

1

2

2

3

3

4

4

5

5

6

6

Mal

Wash

Simon

Inara

Zoe

Kaylee

GS algo outputs a stable matching

Lemma 1: GS outputs a perfect matching S

Lemma 2: S has no instability

Proof technique de jour

Source: 4simpsons.wordpress.com

Proof by contradiction

Assume the negation of what you want to proveAssume the negation of what you want to prove

After some reasoning

After some reasoning

Two obervations

Obs 1: Once m is engaged he keeps getting engaged to “better” women

Obs 2: If w proposes to m’ first and then to m (or never proposes to m) then she prefers m’ to m

Proof of Lemma 2

By contradiction

mm ww

m’ w’

Assume there is an instability (m,w’)

m prefers w’ to w

w’ prefers m to m’

w’ last proposed to m’

w’ last proposed to m’

Contradiction by Case Analysis

Depending on whether w’ had proposed to m or not

Case 1: w’ never proposed to m

w’ prefers m’ to m

Assumed w’ prefers m to m’

Source: 4simpsons.wordpress.com

By Obs 2By Obs 2

By Obs 1By Obs 1

Case 2: w’ had proposed to m

Case 2.1: m had accepted w’ proposalm is finally engaged to w

Thus, m prefers w to w’4simpsons.wordpress.com

Case 2.2: m had rejected w’ proposal

m was engaged to w’’ (prefers w’’ to w’)

m is finally engaged to w (prefers w to w’’)

m prefers w to w’

4simpsons.wordpress.com

By Obs 1By Obs 1

By Obs 1By Obs 1

Overall structure of case analysis

Did w’ propose to m?Did w’ propose to m?

Did m accept w’ proposal?

Did m accept w’ proposal?

4simpsons.wordpress.com

4simpsons.wordpress.com4simpsons.wordpress.com

Graph Searching

BFS/DFS

O(m+n) BFS Implementation

BFS(s)

CC[s] = T and CC[w] = F for every w≠ s

Set i = 0Set L0= {s}

While Li is not empty

Li+1 = Ø

For every u in Li

For every edge (u,w)

If CC[w] = F then

CC[w] = TAdd w to Li+1

i++

ArrayArray

Linked ListLinked List

Input graph as Adjacency listInput graph as Adjacency list

Version in KT also

computes a BFS tree

Version in KT also

computes a BFS tree

An illustration

11

22 33

44 55

66

77

88

11 22 33 44 55 77 88 66

O(m+n) DFS implementation

BFS(s)

CC[s] = T and CC[w] = F for every w≠ s

Intitialize Q= {s}

While Q is not empty

Delete the front element u in Q

For every edge (u,w)

If CC[w] = F then

CC[w] = TAdd w to the back of Q

O(n)O(n)

O(1)O(1)

O(1)O(1)

Repeated nu times

Repeated nu times

O(nu)O(nu)

Repeated at most once for each

vertex u

Repeated at most once for each

vertex u

Σu O(nu) = O(Σu nu) =

O(m)

Σu O(nu) = O(Σu nu) =

O(m) O(1)O(1)

A DFS run using an explicit stack

11

22 33

44 55

66

77

88

11

22

44

55

66

33

88

77

33

55

33

77

Topological Ordering

Run of TopOrd algorithm

Greedy Algorithms

Interval Scheduling: Maximum Number of Intervals

Schedule by Finish Time

End of Semester blues

Monday Tuesday Wednesday Thursday Friday

ProjectProject

331 HW331 HWExam studyExam study

Party!Party!

Write up a term paperWrite up a term paper

Can only do one thing at any day: what is the maximum number of tasks that you can do?Can only do one thing at any day: what is the maximum number of tasks that you can do?

Schedule by Finish Time

Set A to be the empty set

While R is not empty

Choose i in R with the earliest finish time

Add i to A

Remove all requests that conflict with i from R

Return A*=A

O(n log n) time sort intervals such that f(i) ≤ f(i+1)O(n log n) time sort intervals such that f(i) ≤ f(i+1)

O(n) time build array s[1..n] s.t. s[i] = start time for iO(n) time build array s[1..n] s.t. s[i] = start time for i

Do the removal on

the fly

Do the removal on

the fly

The final algorithm

Monday Tuesday Wednesday Thursday Friday

ProjectProject

331 HW331 HWExam studyExam study

Party!Party!

Write up a term paperWrite up a term paper

Order tasks by their END timeOrder tasks by their END time

Proof of correctness uses“greedy stays ahead”

Interval Scheduling: Maximum Intervals

Schedule by Finish Time

Scheduling to minimize lateness

Monday Tuesday Wednesday Thursday Friday

ProjectProject

331 HW331 HW

Exam studyExam study

Party!Party!

Write up a term paperWrite up a term paper

All the tasks have to be scheduledGOAL: minimize maximum latenessAll the tasks have to be scheduled

GOAL: minimize maximum lateness

The Greedy Algorithm

(Assume jobs sorted by deadline: d1≤ d2≤ ….. ≤ dn)

f=s

For every i in 1..n do

Schedule job i from s(i)=f to f(i)=f+ti

f=f+ti

Proof of Correctness uses“Exchange argument”

Proved the following

Any two schedules with 0 idle time and 0 inversions have the same max lateness

Greedy schedule has 0 idle time and 0 inversions

There is an optimal schedule with 0 idle time and 0 inversions

Shortest Path in a Graph: non-negative edge weights

Dijkstra’s Algorithm

Shortest Path problem

Input: Directed graph G=(V,E)

Edge lengths, le for e in E

“start” vertex s in V

Output: All shortest paths from s to all nodes in V

100

155

s

u

w

5

s

u

155

s

u

w

Dijkstra’s shortest path algorithm

Input: Directed G=(V,E), le ≥ 0, s in V

R = {s}, d(s) =0

While there is a x not in R with (u,x) in E, u in R

d’(w) = min e=(u,w) in E, u in R d(u)+le

Pick w that minimizes d’(w) Add w to Rd(w) = d’(w)

ss

ww

uu

zz

xx

yy

1

2

4

3

3

1

2

1

2

d(s) = 0

1

4

2 ss

uu

d(u) = 1

4

2

ww

d(w) = 2

5

xx

d(x) = 2

3

4

yy

d(y) = 3

zz

d(z) = 4

Shortest paths

Shortest paths

Dijkstra’s shortest path algorithm (formal)

Input: Directed G=(V,E), le ≥ 0, s in V

S = {s}, d(s) =0

While there is a v not in S with (u,v) in E, u in S

Pick w that minimizes d’(w) Add w to Sd(w) = d’(w)

At most n iterationsAt most n iterations

O(m) timeO(m) time

O(mn) time bound is trivial

O(m log n) time implementation is possible

Proved that d’(v) is best when v is added