Top Banner
CS 1114: Graphs and Blobs Prof. Graeme Bailey http://cs1114.cs.cornell.edu (notes modified from Noah Snavely, Spring 2009)
73

CS 1114: Graphs and Blobs Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)

Dec 21, 2015

Download

Documents

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: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

CS 1114: Graphs and Blobs

Prof. Graeme Bailey

http://cs1114.cs.cornell.edu

(notes modified from Noah Snavely, Spring 2009)

Page 2: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

2

Some major graph problems

Graph colouring– Ensuring that radio stations don’t clash

Graph connectivity– How fragile is the internet?

Graph cycles– Helping FedEx/UPS/DHL plan a route

Planarity testing– Connecting computer chips on a motherboard

Graph isomorphism– Is a chemical structure already known?

Page 3: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

3

Given a graph and a set of colours {1,…,k}, assign each vertex a colour

Adjacent vertices have different colours

Graph colouring problem

V1

V2

V3

V4

V5

V1

V3

V4

V5

V2

Page 4: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

4

Radio frequencies via colouring How can we assign frequencies to a set of radio

stations so that there are no clashes? Make a graph where each station is a vertex

– Put an edge between two stations that clash• I.e., if their signal areas overlap

– Any colouring is a non-clashing assignment of frequencies• Can you prove this? What about vice-versa?

C1

C2

C3

C4

C5

Page 5: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Images as graphs

5

Page 6: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Images as graphs

6

Page 7: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Images as graphs

7

Page 8: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

8

Graphs and paths

Can you get from vertex V to vertex W?– Is there a route from one city to another?

More precisely, is there a sequence of vertices {V, V1, V2, … , Vk, W} such that every adjacent pair has an edge between them? (Sometimes we care about directed edges.)– This is called a path– A cycle (or loop) is a path from V to V– A path is simple if no vertex appears twice

• though sometimes we define simple loops

Page 9: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

European rail links (simplified)

9

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

• Can we get from London to Prague on the train?• How about London to Stockholm?

Oslo Stockholm

Page 10: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

10

Graph connectivity For any pair of nodes, is there a path

between them?– Basic idea of the Internet: you can get from any

computer to any other computer– This pair of nodes is called connected– A graph is connected if all nodes are connected

Related question: remove an arbitrary node (or edge), how connected is the graph?– Is the Internet intact if any 1 computer fails?– Or any 1 edge between computers?

Page 11: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

11“Eastern Telegraph Co. and its General Connections” (1901)

Page 12: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

12

Page 13: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

13

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

Sometimes we look for Hamiltonian or Eulerian paths

Page 14: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Hamiltonian & Eulerian cycles

14

V5

V1

V2

V4 V3

V10

V6

V7

V9 V8

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

visits vertices visits edges

Page 15: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

15

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 16: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

16

Is this graph planar?

V1

V2

V3

V4

V5

V6

Can you prove it?

Page 17: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

17

Four-colour theorem Any planar graph can be coloured using no

more than 4 colours

Page 18: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

“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?)

18

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 19: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Graph of Flickr images ……

19

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

Images are matched using visual features

Page 20: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Image graph of the Pantheon

20

Page 21: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

21

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 22: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Blobs are components!

22

Page 23: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

23

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 24: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

24

For each vertex we visit, we colour its neighbours and remember that we need to visit them at some point (e.g., put them in a todo list):

– While there are any uncoloured vertices, select one, adding it to the (empty) todo list and colouring it uniquely

• While the todo list is not empty remove a vertex V from the todo list to visit add the uncoloured neighbors of this V to the todo list and colour

them with the same colour

– Repeat until all vertices are coloured

This is also called graph traversal

Finding components (blobs)

Page 25: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

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: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

26

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 27: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

27

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 28: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

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: CTodo List: [ ]

Page 29: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

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: CTodo List: [ B, F, D ]

Page 30: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

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: BTodo List: [ F, D ]

Page 31: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

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: BTodo List: [ F, D, E ]

Page 32: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

32

Stacks and Queues One way to implement the todo list is as a stack

– LIFO: Last In First Out– The newest task is the one you’ll do next– 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– The oldest task is the one you’ll do next– Think of a line of (well-mannered) people

• First come, first served

Page 33: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Stacks

Two operations:

Push: add something to the top of the stack

Pop: remove the thing on top of the stack

33

Page 34: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Queue

Two operations: Enqueue: add something to the end of the

queue Dequeue: remove something from the

front of the queue

34

Page 35: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Graph traversal

Suppose you’re in a maze

What strategy can you use to find the exit?

35

Page 36: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Graph traversal

36

Paris

Berlin

London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Oslo Stockholm

Page 37: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

37

Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Current node: LondonTodo list: [ ]

Graph traversal (stack)

Oslo Stockholm

Page 38: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

38

Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (stack)

Current node: LondonTodo list: [ Paris ]

Oslo Stockholm

Page 39: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

39

2Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (stack)

Current node: ParisTodo list: [ ]

Oslo Stockholm

Page 40: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

40

2Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (stack)

Current node: ParisTodo list: [ Frankfurt, Berlin, Rome ]

Oslo Stockholm

Page 41: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

41

2Paris

Berlin

1London

3Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (stack)

Current node: RomeTodo list: [ Frankfurt, Berlin ]

Oslo Stockholm

Page 42: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

42

2Paris

Berlin

1London

3Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (stack)

Current node: RomeTodo list: [ Frankfurt, Berlin, Naples ]

Oslo Stockholm

Page 43: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

43

2Paris

Berlin

1London

3Rome

Frankfurt

Vienna Prague

4Naples

Warsaw

Hamburg

Graph traversal (stack)

Current node: NaplesTodo list: [ Frankfurt, Berlin ]

Oslo Stockholm

Page 44: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

44

2Paris

5Berlin

1London

3Rome

Frankfurt

Vienna Prague

4Naples

Warsaw

Hamburg

Graph traversal (stack)

Current node: BerlinTodo list: [ Frankfurt ]

Oslo Stockholm

Page 45: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

45

2Paris

5Berlin

1London

3Rome

Frankfurt

Vienna Prague

4Naples

Warsaw

Hamburg

Graph traversal (stack)

Current node: BerlinTodo list: [ Frankfurt, Hamburg, Vienna ]

Oslo Stockholm

Page 46: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

46

2Paris

5Berlin

1London

3Rome

Frankfurt

6Vienna Prague

4Naples

Warsaw

Hamburg

Graph traversal (stack)

Current node: ViennaTodo list: [ Frankfurt, Hamburg ]

Oslo Stockholm

Page 47: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

47

2Paris

5Berlin

1London

3Rome

Frankfurt

6Vienna Prague

4Naples

Warsaw

Hamburg

Graph traversal (stack)

Current node: ViennaTodo list: [ Frankfurt, Hamburg, Prague, Warsaw ]

Oslo Stockholm

Page 48: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

48

2Paris

5Berlin

1London

3Rome

Frankfurt

6Vienna Prague

4Naples

Warsaw

Hamburg

Graph traversal (stack)

Current node: ViennaTodo list: [ Frankfurt, Hamburg, Prague, Warsaw ]

Oslo Stockholm

Page 49: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

49

2Paris

5Berlin

1London

3Rome

Frankfurt

6Vienna Prague

4Naples 7

Warsaw

Hamburg

Graph traversal (stack)

Current node: Warsaw Todo list: [ Frankfurt, Hamburg, Prague ]

Oslo Stockholm

Page 50: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

50

2Paris

5Berlin

1London

3Rome

Frankfurt

6Vienna

8

Prague

4Naples 7

Warsaw

Hamburg

Graph traversal (stack)

Current node: PragueTodo list: [ Frankfurt, Hamburg ]

Oslo Stockholm

Page 51: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

51

2Paris

5Berlin

1London

3Rome

Frankfurt

6Vienna

8

Prague

4Naples 7

Warsaw

9 Hamburg

Graph traversal (stack)

Current node: HamburgTodo list: [ Frankfurt ]

Oslo Stockholm

Page 52: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

52

2Paris

5Berlin

1London

3Rome

10Frankfurt

6Vienna

8

Prague

4Naples 7

Warsaw

9 Hamburg

Graph traversal (stack)

Current node: FrankfurtTodo list: [ ]

Oslo Stockholm

Page 53: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Depth-first search (DFS)

Call the starting node the root We traverse paths all the way until we get

to a dead-end, then backtrack (until we find an unexplored path)

53

2

5

1

3

10

6 8

47

9

Page 54: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Another strategy

54

1. Explore all the cities that are one hop away from the root

2. Explore all cities that are two hops away from the root

3. Explore all cities that are three hops away from the root

This corresponds to using a queue

Page 55: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

55

Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Current node: LondonTodo list: [ ]

Graph traversal (queue)

Oslo Stockholm

Page 56: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

56

Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: LondonTodo list: [ Paris ]

Oslo Stockholm

Page 57: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

57

2Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: ParisTodo list: [ ]

Oslo Stockholm

Page 58: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

58

2Paris

Berlin

1London

Rome

Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: ParisTodo list: [ Frankfurt, Berlin, Rome ]

Oslo Stockholm

Page 59: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

59

2Paris

Berlin

1London

Rome

3Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: FrankfurtTodo list: [ Berlin, Rome ]

Oslo Stockholm

Page 60: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

60

2Paris

Berlin

1London

Rome

3Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: FrankfurtTodo list: [ Berlin, Rome, Hamburg ]

Oslo Stockholm

Page 61: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

61

2Paris

4Berlin

1London

Rome

3Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: BerlinTodo list: [ Rome, Hamburg ]

Oslo Stockholm

Page 62: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

62

2Paris

4Berlin

1London

Rome

3Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: BerlinTodo list: [ Rome, Hamburg, Vienna ]

Oslo Stockholm

Page 63: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

63

2Paris

4Berlin

1London

5Rome

3Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: RomeTodo list: [ Hamburg, Vienna ]

Oslo Stockholm

Page 64: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

64

2Paris

4Berlin

1London

5Rome

3Frankfurt

Vienna Prague

NaplesWarsaw

Hamburg

Graph traversal (queue)

Current node: RomeTodo list: [ Hamburg, Vienna, Naples ]

Oslo Stockholm

Page 65: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

65

2Paris

4Berlin

1London

5Rome

3Frankfurt

Vienna Prague

NaplesWarsaw

6 Hamburg

Graph traversal (queue)

Current node: HamburgTodo list: [ Vienna, Naples ]

Oslo Stockholm

Page 66: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

66

2Paris

4Berlin

1London

5Rome

3Frankfurt

7Vienna Prague

NaplesWarsaw

6 Hamburg

Graph traversal (queue)

Current node: ViennaTodo list: [ Naples ]

Oslo Stockholm

Page 67: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

67

2Paris

4Berlin

1London

5Rome

3Frankfurt

7Vienna Prague

NaplesWarsaw

6 Hamburg

Graph traversal (queue)

Current node: ViennaTodo list: [ Naples, Prague, Warsaw ]

Oslo Stockholm

Page 68: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

68

2Paris

4Berlin

1London

5Rome

3Frankfurt

7Vienna Prague

8Naples

Warsaw

6 Hamburg

Graph traversal (queue)

Current node: NaplesTodo list: [ Prague, Warsaw ]

Oslo Stockholm

Page 69: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

69

2Paris

4Berlin

1London

5Rome

3Frankfurt

7Vienna

9

Prague

8Naples

Warsaw

6 Hamburg

Graph traversal (queue)

Current node: PragueTodo list: [ Warsaw ]

Oslo Stockholm

Page 70: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

70

2Paris

4Berlin

1London

5Rome

3Frankfurt

7Vienna

9

Prague

8Naples 10

Warsaw

6 Hamburg

Graph traversal (queue)

Current node: WarsawTodo list: [ ]

Oslo Stockholm

Page 71: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

Breadth-first search (BFS)

We visit all the vertices at the same level (same distance to the root) before moving on to the next level

71

2

4

1

5

3

7 9

810

6

Page 72: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

BFS vs. DFS

72

2

5

1

3

10

6 8

47

92

4

1

5

3

7 9

810

6

Breadth-first (queue) Depth-first (stack)

Page 73: CS 1114: Graphs and Blobs Prof. Graeme Bailey  (notes modified from Noah Snavely, Spring 2009)

BFS vs. DFS

73

(tree = graph with no cycles)