Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 1114: Graphs and Blobs Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)"— Presentation transcript:

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

2 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?

3 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

4 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

5 Images as graphs 5

6 6

7 7

8 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, V 1, V 2, …, V k, 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

9 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

10 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?

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

12 12

13 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

14 Hamiltonian & Eulerian cycles 14 V5 V1 V2 V4V3 V10 V6 V7 V9V8  Is it easier to tell if a graph has a Hamiltonian cycle or an Eulerian cycle? visits vertices visits edges

15 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

16 16  Is this graph planar? V1V2V3V4V5V6  Can you prove it?

17 17 Four-colour theorem  Any planar graph can be coloured using no more than 4 colours

18 “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

19 Graph of Flickr images …… 19 Flickr images of the Pantheon, Rome (built 126 AD) Images are matched using visual features

20 Image graph of the Pantheon 20

21 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 V4 V1 V3 V2

22 Blobs are components! 22

23 23 Blobs are components! A0000000B0 00000000C0 0000000000 0000000000 0000D00000 000EFG0000 000H000000 0000000000 0000000000 0000000000 A C B D F E H G

24 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)

25 25 Coloring a component 1000000010 0000000010 0000000000 0000000000 0000100000 0001110000 0001110000 0001110000 0001110000 0000000000

26 26 1000000010 0000000010 0000000000 0000000000 0000A00000 000BCD0000 000EFG0000 000HIJ0000 000KLM0000 0000000000 Current node: A Todo List: [ ]

27 27 1000000010 0000000010 0000000000 0000000000 0000A00000 000BCD0000 000EFG0000 000HIJ0000 000KLM0000 0000000000 Current node: A Todo List: [ C ] Done with A, choose next from Todo List

28 28 1000000010 0000000010 0000000000 0000000000 0000A00000 000BCD0000 000EFG0000 000HIJ0000 000KLM0000 0000000000 Current node: C Todo List: [ ]

29 29 1000000010 0000000010 0000000000 0000000000 0000A00000 000BCD0000 000EFG0000 000HIJ0000 000KLM0000 0000000000 Current node: C Todo List: [ B, F, D ]

30 30 1000000010 0000000010 0000000000 0000000000 0000A00000 000BCD0000 000EFG0000 000HIJ0000 000KLM0000 0000000000 Current node: B Todo List: [ F, D ]

31 31 1000000010 0000000010 0000000000 0000000000 0000A00000 000BCD0000 000EFG0000 000HIJ0000 000KLM0000 0000000000 Current node: B Todo List: [ F, D, E ]

32 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

33 Stacks  Two operations:  Push: add something to the top of the stack  Pop: remove the thing on top of the stack 33

34 Queue  Two operations:  Enqueue: add something to the end of the queue  Dequeue: remove something from the front of the queue 34

35 Graph traversal  Suppose you’re in a maze  What strategy can you use to find the exit? 35

36 Graph traversal 36 Paris Berlin London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Oslo Stockholm

37 37 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Current node: London Todo list: [ ] Graph traversal (stack) Oslo Stockholm

38 38 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: London Todo list: [ Paris ] Oslo Stockholm

39 39 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Paris Todo list: [ ] Oslo Stockholm

40 40 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Paris Todo list: [ Frankfurt, Berlin, Rome ] Oslo Stockholm

41 41 2 Paris Berlin 1 London 3 Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Rome Todo list: [ Frankfurt, Berlin ] Oslo Stockholm

42 42 2 Paris Berlin 1 London 3 Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Rome Todo list: [ Frankfurt, Berlin, Naples ] Oslo Stockholm

43 43 2 Paris Berlin 1 London 3 Rome Frankfurt Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Naples Todo list: [ Frankfurt, Berlin ] Oslo Stockholm

44 44 2 Paris 5 Berlin 1 London 3 Rome Frankfurt Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Berlin Todo list: [ Frankfurt ] Oslo Stockholm

45 45 2 Paris 5 Berlin 1 London 3 Rome Frankfurt Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Berlin Todo list: [ Frankfurt, Hamburg, Vienna ] Oslo Stockholm

46 46 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Vienna Todo list: [ Frankfurt, Hamburg ] Oslo Stockholm

47 47 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Vienna Todo list: [ Frankfurt, Hamburg, Prague, Warsaw ] Oslo Stockholm

48 48 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Vienna Todo list: [ Frankfurt, Hamburg, Prague, Warsaw ] Oslo Stockholm

49 49 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples 7 Warsaw Hamburg Graph traversal (stack) Current node: Warsaw Todo list: [ Frankfurt, Hamburg, Prague ] Oslo Stockholm

50 50 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna 8 Prague 4 Naples 7 Warsaw Hamburg Graph traversal (stack) Current node: Prague Todo list: [ Frankfurt, Hamburg ] Oslo Stockholm

51 51 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna 8 Prague 4 Naples 7 Warsaw 9 Hamburg Graph traversal (stack) Current node: Hamburg Todo list: [ Frankfurt ] Oslo Stockholm

52 52 2 Paris 5 Berlin 1 London 3 Rome 10 Frankfurt 6 Vienna 8 Prague 4 Naples 7 Warsaw 9 Hamburg Graph traversal (stack) Current node: Frankfurt Todo list: [ ] Oslo Stockholm

53 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 4 7 9

54 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

55 55 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Current node: London Todo list: [ ] Graph traversal (queue) Oslo Stockholm

56 56 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: London Todo list: [ Paris ] Oslo Stockholm

57 57 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Paris Todo list: [ ] Oslo Stockholm

58 58 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Paris Todo list: [ Frankfurt, Berlin, Rome ] Oslo Stockholm

59 59 2 Paris Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Frankfurt Todo list: [ Berlin, Rome ] Oslo Stockholm

60 60 2 Paris Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Frankfurt Todo list: [ Berlin, Rome, Hamburg ] Oslo Stockholm

61 61 2 Paris 4 Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Berlin Todo list: [ Rome, Hamburg ] Oslo Stockholm

62 62 2 Paris 4 Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Berlin Todo list: [ Rome, Hamburg, Vienna ] Oslo Stockholm

63 63 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Rome Todo list: [ Hamburg, Vienna ] Oslo Stockholm

64 64 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Rome Todo list: [ Hamburg, Vienna, Naples ] Oslo Stockholm

65 65 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt Vienna Prague Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Hamburg Todo list: [ Vienna, Naples ] Oslo Stockholm

66 66 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna Prague Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Vienna Todo list: [ Naples ] Oslo Stockholm

67 67 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna Prague Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Vienna Todo list: [ Naples, Prague, Warsaw ] Oslo Stockholm

68 68 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna Prague 8 Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Naples Todo list: [ Prague, Warsaw ] Oslo Stockholm

69 69 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna 9 Prague 8 Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Prague Todo list: [ Warsaw ] Oslo Stockholm

70 70 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna 9 Prague 8 Naples 10 Warsaw 6 Hamburg Graph traversal (queue) Current node: Warsaw Todo list: [ ] Oslo Stockholm

71 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 8 10 6

72 BFS vs. DFS 72 2 5 1 3 10 6 8 4 7 9 2 4 1 5 3 7 9 8 6 Breadth-first (queue) Depth-first (stack)

73 BFS vs. DFS 73 (tree = graph with no cycles)


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

Similar presentations


Ads by Google