segmentation and greedy algorithms

33
Segmentation and greedy algorithms Prof. Noah Snavely CS1114 http :// www.cs.cornell.edu/courses/cs1114

Upload: wei

Post on 17-Jan-2016

68 views

Category:

Documents


0 download

DESCRIPTION

Segmentation and greedy algorithms. Prof. Noah Snavely CS1114 http :// www.cs.cornell.edu/courses/cs1114. Administrivia. A5P1 due tomorrow (demo slots available) A5P2 out this weekend, due 4/19 Prelim 2 on Tuesday Quizzes available Monday Midterm course evaluations. SIFT Matching Demo. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Segmentation and greedy algorithms

Segmentation and greedy algorithms

Prof. Noah SnavelyCS1114http://www.cs.cornell.edu/courses/cs1114

Page 2: Segmentation and greedy algorithms

Administrivia

A5P1 due tomorrow (demo slots available) A5P2 out this weekend, due 4/19

Prelim 2 on Tuesday– Quizzes available Monday

Midterm course evaluations

2

Page 3: Segmentation and greedy algorithms

SIFT Matching Demo

3

Page 4: Segmentation and greedy algorithms

Last time: k-means

4

Legend- centers (means)

- clusters

Page 5: Segmentation and greedy algorithms

k-means

Idea: find the centers that minimize the sum of squared distances to the points

Objective:

5

Page 6: Segmentation and greedy algorithms

A greedy method for k-means

6

Page 7: Segmentation and greedy algorithms

A greedy method for k-means

Unfortunately, this doesn’t work that well

The answer we get could be much worse than the optimum

However, if we change our objective (e.g., k-centers, then we get an answer within 2 times the cost of the best answer

7

Page 8: Segmentation and greedy algorithms

“Hill climbing”

8

Page 9: Segmentation and greedy algorithms

Back to k-means

There’s a simple iterative algorithm for k-means– Lloyd’s algorithm

1.Start with an initial set of means– For instance, choose k points at random from

the input set

2.Assign each point to the closest mean3.Compute the means of each cluster4.Repeat 2 and 3 until nothing changes

9

Page 10: Segmentation and greedy algorithms

Lloyd’s algorithm

Demo

10

Page 11: Segmentation and greedy algorithms

Lloyd’s algorithm

Does it always terminate?– Yes, it will always converge to some solution– Might be a local minima of the objective

function

– Error decreases after every iteration– Error could be arbitrarily bad

11

Page 12: Segmentation and greedy algorithms

Questions?

12

Page 13: Segmentation and greedy algorithms

Possible algorithms

1.Greedy algorithms– Do what seems best at any given point– Example: making change

2.Iterative algorithms– Start with some answer, take a small step to

improve it, repeat until it doesn’t get better– Examples: Lloyd’s algorithm for k-means,

bubble sort, hill climbing

13

Page 14: Segmentation and greedy algorithms

Where we are so far Greedy algorithms and iterative algorithms

sometimes give the right answer (e.g., making change with U.S. currency)

Some clustering objective functions are easier to optimize than others:– k-means very hard– k-centers very hard, but we can use a

greedy algorithm to get within a factor of two of the best answer

14

Page 15: Segmentation and greedy algorithms

Back to graphs

We can also associate a weight with each edge (e.g., the distance between cities)

15

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

Naples Warsaw

Hamburg

200

400

100

400

300

200150

100

100

250

150

250

Page 16: Segmentation and greedy algorithms

Spanning trees A spanning tree of a graph is a subgraph that

(a) connects all the vertices and (b) is a tree

16Spanning trees

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

400

100

400

300

200150

100

100

250

150

250

Paris

Berlin

London

Rome

Frankfurt

ViennaPrague

Naples Warsaw

Hamburg200

400

100

300

200100

250

150250

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

Naples Warsaw

Hamburg200

400

100400

150100

250

150250

Q: How many edges are there in a spanning tree on n vertices?

Page 17: Segmentation and greedy algorithms

Graph costs

We’ll say the cost of a graph is the sum of its edge weights

17

Paris

Berlin

London

Rome

Frankfurt

ViennaPrague

Naples Warsaw

Hamburg200

400

100

300

200100

250

150250

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

Naples Warsaw

Hamburg200

400

100400

150100

250

150250

Cost = 200 + 200 + 100 + 400 + 300 + 100 + 250 + 150 + 250 = 1950

Cost = 200 + 400 + 100 + 400 + 150 + 250 + 100 + 150 + 250 = 2000

Page 18: Segmentation and greedy algorithms

Minimum spanning trees We define the minimum spanning tree (MST) of a

graph as the spanning tree with minimum cost (Suppose we want to build the minimum length of

track possible while still connecting all the cities.)

18

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

400

100

400

300

200150

100

100

250

150

250

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

100

400

200100

100

250

150

250

MST: Cost = 1750

(Eurorail needs to build 1750 mi of track at minimum)

Page 19: Segmentation and greedy algorithms

Minimum spanning trees

This is an optimization problem where the objective function is the cost of the tree

Can you think of a greedy algorithm to do this?

19

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

400

100

400

300

200150

100

100

250

150

250

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Page 20: Segmentation and greedy algorithms

Minimum spanning tree

Greedy algorithm:

20

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

400

100

400

300

200150

100

100

250

150

250

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

100

400

200100

100

250

150

250

Page 21: Segmentation and greedy algorithms

Minimum spanning tree This greedy algorithm is called Kruskal’s

algorithm

Not that simple to prove that it gives the MST How many connected components are there after adding

the kth edge?

21

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

400

100

400

300

200150

100

100

250

150

250

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

100

400

200100

100

250

150

250

Page 22: Segmentation and greedy algorithms

Kruskal’s algorithm

Start with an empty graph Sort edges by weight, in increasing order Go through each edge in order

– If adding edge creates a cycle, skip it– Otherwise, add the edge to the graph

22

Page 23: Segmentation and greedy algorithms

Back to clustering

We can define the clustering problem on graphs

23

12

9

4

57

10

11

8

48

Page 24: Segmentation and greedy algorithms

Clustering using graphs

Clustering breaking apart the graph by cutting long edges

Which edges do we break?24

12

9

4

57

10

11

8

48

Page 25: Segmentation and greedy algorithms

Spacing as a clustering metric

Another objective function for clustering:– Maximize the minimum distance between

clusters– (Called the spacing.)

25

spacing

Page 26: Segmentation and greedy algorithms

Cool fact We compute the clusters with the

maximum spacing during MST! To compute the best k clusters, just stop

MST construction k-1 edges early

26

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

400

100

400

300

200150

100

100

250

150

250

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg200

100

200100

100

250

150

250

2 clusters with max spacing (=400)

400

Page 27: Segmentation and greedy algorithms

Proof of cool fact

Suppose this wasn’t true – then someone could give us a different clustering with a bigger spacing

Let C be our MST clustering, and let D be the purportedly better one

There must be two nodes u and v in different clusters in D but in the same cluster in C

There’s a path between u and v in C, and at some point this path crosses a cluster boundary in D

27

Page 28: Segmentation and greedy algorithms

Pictorial proof

28

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

200

100

200

100

100

250

150

250

D

400

Page 29: Segmentation and greedy algorithms

Demo http://www.kovan.ceng.metu.edu.tr/~maya/kmeans/index.html

29

Page 30: Segmentation and greedy algorithms

Where we are so far Greedy algorithms work sometimes (e.g.,

with MST)

Some clustering objective functions are easier to optimize than others:– k-means very hard– k-centers very hard, but we can use a

greedy algorithm to get within a factor of two of the best answer

– maximum spacing very easy! Just do MST and stop early (this gives exact answer)

30

Page 31: Segmentation and greedy algorithms

Back to image segmentation

31

Page 32: Segmentation and greedy algorithms

Questions?

32

Page 33: Segmentation and greedy algorithms

Greedy algorithm for graph coloring?

33