+ All Categories
Home > Documents > Connected components and graph traversal Prof. Noah Snavely CS1114 .

Connected components and graph traversal Prof. Noah Snavely CS1114 .

Date post: 22-Dec-2015
Category:
View: 227 times
Download: 1 times
Share this document with a friend
Popular Tags:
36
Connected components and graph traversal Prof. Noah Snavely CS1114 http://cs1114.cs.cornell.edu
Transcript
Page 1: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Connected components and graph traversal

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

Page 2: Connected components and graph traversal Prof. Noah Snavely CS1114 .

2

Administrivia

Assignment 2 is out– First part due tomorrow by 5pm– Second part due next Friday by 5pm

First prelim will be in two weeks– Thursday, February 26, in class

Page 3: Connected components and graph traversal Prof. Noah Snavely CS1114 .

3

What is a graph?

Loosely speaking, a set of things that are paired up in some way

Precisely, a set of vertices V and edges E– Vertices sometimes called nodes– An edge (or link) connects a pair of vertices

V1

V2

V3

V4

V5 V = { V1, V2, V3, V4, V5 }

E = { (V1,V3), (V2,V5), (V3,V4) }

Page 4: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Images as graphs

4

Page 5: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Images as graphs

5

Page 6: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Images as graphs

6

Page 7: Connected components and graph traversal Prof. Noah Snavely CS1114 .

More graph problems

7

Page 8: Connected components and graph traversal Prof. Noah Snavely CS1114 .

8

Hamiltonian & Eulerian cycles

Two questions that are useful for problems such as mailman delivery routes

Hamiltonian cycle:– A cycle that visits each vertex exactly once

(except the start and end)

Eulerian cycle:– A cycle that uses each edge exactly once

Page 9: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Hamiltonian & Eulerian cycles

9

V5

V1

V2

V4 V3

V10

V6

V7

V9 V8

Is it easier to tell if a graph has a Hamiltonian or Eulerian cycle?

Page 10: Connected components and graph traversal Prof. Noah Snavely CS1114 .

10

Planarity testing

A graph is planar if you can draw it without the edges crossing– It’s OK to move the edges or vertices around,

as long as edges connect the same vertices

V1

V2

V3

V4

V5

Page 11: Connected components and graph traversal Prof. Noah Snavely CS1114 .

11

Is this graph planar?

V1

V2

V3

V4

V5

V6

Can you prove it?

Page 12: Connected components and graph traversal Prof. Noah Snavely CS1114 .

12

Four-color theorem Any planar graph can be colored using no

more than 4 colors

Page 13: Connected components and graph traversal Prof. Noah Snavely CS1114 .

“Small world” phenomenon(Six degrees of separation)

How close together are nodes in a graph (e.g., what’s the average number of hops connecting pairs of nodes?)

13

Milgram’s small world experiment:• Send postcard to random person A in

Omaha; task is to get it to a random person B in Boston

• If A knows B, send directly• Otherwise, A sends to someone A knows

who is most likely to know B• People are separated by 5.5 links on

average

Page 14: Connected components and graph traversal Prof. Noah Snavely CS1114 .

14

Friend wheel

Page 15: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Another graph

15

Page 16: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Graph of Flickr images

16

Flickr images of the Pantheon, Rome (built 126 AD)

Images are matched using visual features

Page 17: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Image graph of the Pantheon

17

Page 18: Connected components and graph traversal Prof. Noah Snavely CS1114 .

18

Connected components

Even if all nodes are not connected, there will be subsets that are all connected– Connected components

– Component 1: { V1, V3, V5 }– Component 2: { V2, V4 }

V5

V4V1

V3

V2

Page 19: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Blobs are components!

19

Page 20: Connected components and graph traversal Prof. Noah Snavely CS1114 .

20

Blobs are components!

A 0 0 0 0 0 0 0 B 0

0 0 0 0 0 0 0 0 C 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 D 0 0 0 0 0

0 0 0 E F G 0 0 0 0

0 0 0 H 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

A C

B

D

F E

HG

Page 21: Connected components and graph traversal Prof. Noah Snavely CS1114 .

21

Finding blobs

1. Pick a 1 to start with, where you don’t know which blob it is in– When there aren’t any, you’re done

2. Give it a new blob color3. Assign the same blob color to each pixel

that is part of the same blob

Page 22: Connected components and graph traversal Prof. Noah Snavely CS1114 .

22

Finding components

1. Pick a 1 to start with, where you don’t know which component it is in– When there aren’t any, you’re done

2. Give it a new component color3. Assign the same component color to each

pixel that is part of the same component– Basic strategy: color any neighboring 1’s, have

them color their neighbors, and so on

Page 23: Connected components and graph traversal Prof. Noah Snavely CS1114 .

23

Coloring a component

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Page 24: Connected components and graph traversal Prof. Noah Snavely CS1114 .

24

Coloring a component

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Page 25: Connected components and graph traversal Prof. Noah Snavely CS1114 .

25

Coloring a component

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Page 26: Connected components and graph traversal Prof. Noah Snavely CS1114 .

26

Coloring a component

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 1 1 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Page 27: Connected components and graph traversal Prof. Noah Snavely CS1114 .

27

Strategy

For each vertex we visit, we color its neighbors and remember that we need to visit them at some point– Need to keep track of the vertices we still need

to visit in a todo list– After we visit a vertex, we’ll pick one of the

vertices in the todo list to visit next

Page 28: Connected components and graph traversal Prof. Noah Snavely CS1114 .

28

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 A 0 0 0 0 0

0 0 0 B C D 0 0 0 0

0 0 0 E F G 0 0 0 0

0 0 0 H I J 0 0 0 0

0 0 0 K L M 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Current node: ATodo List: [ ]

Page 29: Connected components and graph traversal Prof. Noah Snavely CS1114 .

29

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 A 0 0 0 0 0

0 0 0 B C D 0 0 0 0

0 0 0 E F G 0 0 0 0

0 0 0 H I J 0 0 0 0

0 0 0 K L M 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Current node: ATodo List: [ C ] Done with A, choose

next from Todo List

Page 30: Connected components and graph traversal Prof. Noah Snavely CS1114 .

30

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 A 0 0 0 0 0

0 0 0 B C D 0 0 0 0

0 0 0 E F G 0 0 0 0

0 0 0 H I J 0 0 0 0

0 0 0 K L M 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Current node: CTodo List: [ ]

Page 31: Connected components and graph traversal Prof. Noah Snavely CS1114 .

31

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 A 0 0 0 0 0

0 0 0 B C D 0 0 0 0

0 0 0 E F G 0 0 0 0

0 0 0 H I J 0 0 0 0

0 0 0 K L M 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Current node: CTodo List: [ B, F, D ]

Page 32: Connected components and graph traversal Prof. Noah Snavely CS1114 .

32

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 A 0 0 0 0 0

0 0 0 B C D 0 0 0 0

0 0 0 E F G 0 0 0 0

0 0 0 H I J 0 0 0 0

0 0 0 K L M 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Current node: BTodo List: [ F, D ]

Page 33: Connected components and graph traversal Prof. Noah Snavely CS1114 .

33

1 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 A 0 0 0 0 0

0 0 0 B C D 0 0 0 0

0 0 0 E F G 0 0 0 0

0 0 0 H I J 0 0 0 0

0 0 0 K L M 0 0 0 0

0 0 0 0 0 0 0 0 0 0

Current node: BTodo List: [ F, D, E ]

Page 34: Connected components and graph traversal Prof. Noah Snavely CS1114 .

35

Graph traversal

Select an uncolored vertex, add it to the todo list

While the todo list is not empty– Remove a vertex V from the todo list– Add the uncolored neighbors of V to the todo

list and color them

Page 35: Connected components and graph traversal Prof. Noah Snavely CS1114 .

36

Stacks and Queues

One way to implement the todo list is as a stack– LIFO: Last In First Out– Think of a pile of trays in a cafeteria

• Trays at the bottom can stay there a while…

The alternative is a queue– FIFO: First In First Out– Think of a line of (well-mannered) people– First come, first served

Page 36: Connected components and graph traversal Prof. Noah Snavely CS1114 .

Next time

More on stacks and queues

37


Recommended