Top Banner
CSE 331: Review
36

CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Jan 04, 2016

Download

Documents

Vincent Martin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

CSE 331: Review

Page 2: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 3: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Stable Matching Problem

Gale-Shaply Algorithm

Page 4: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 5: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 6: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

GS algorithm: Firefly Edition

1

1

2

2

3

3

4

4

5

5

6

6

Mal

Wash

Simon

Inara

Zoe

Kaylee

Page 7: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

GS algo outputs a stable matching

Lemma 1: GS outputs a perfect matching S

Lemma 2: S has no instability

Page 8: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 9: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 10: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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’

Page 11: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 12: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 13: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 14: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Graph Searching

BFS/DFS

Page 15: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 16: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

An illustration

11

22 33

44 55

66

77

88

11 22 33 44 55 77 88 66

Page 17: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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)

Page 18: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 19: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Topological Ordering

Page 20: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Run of TopOrd algorithm

Page 21: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Greedy Algorithms

Page 22: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Interval Scheduling: Maximum Number of Intervals

Schedule by Finish Time

Page 23: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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?

Page 24: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 25: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 26: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Proof of correctness uses“greedy stays ahead”

Page 27: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Interval Scheduling: Maximum Intervals

Schedule by Finish Time

Page 28: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 29: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 30: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Proof of Correctness uses“Exchange argument”

Page 31: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 32: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Shortest Path in a Graph: non-negative edge weights

Dijkstra’s Algorithm

Page 33: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 34: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 35: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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

Page 36: CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

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