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

Slides:



Advertisements
Similar presentations
CS 336 March 19, 2012 Tandy Warnow.
Advertisements

Graph algorithms Prof. Noah Snavely CS1114
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Sociology and CS Philip Chan. How close are people connected? Are people closely connected, not closely connected, isolated into groups, …
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS171 Introduction to Computer Science II Graphs Strike Back.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Decision Maths Graphs Wiltshire Graphs A graph is just a diagram made up of “dots” and “lines”. These are all graphs. The dots are called “nodes” or.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Graph & BFS.
Clustering and greedy algorithms — Part 2 Prof. Noah Snavely CS1114
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Blobs and Graphs Prof. Noah Snavely CS1114
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Connected components and graph traversal Prof. Noah Snavely CS1114
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Social Media Mining Graph Essentials.
Representing and Using Graphs
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Connected Components Fun with graphs, searching, and queues.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Medians and blobs Prof. Ramin Zabih
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Unit – V Graph theory. Representation of Graphs Graph G (V, E,  ) V Set of vertices ESet of edges  Function that assigns vertices {v, w} to each edge.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Graph theory and networks. Basic definitions  A graph consists of points called vertices (or nodes) and lines called edges (or arcs). Each edge joins.
COSC 2007 Data Structures II
Graphs Upon completion you will be able to:
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Breadth-first and depth-first traversal CS1114
Graph problems Prof. Ramin Zabih
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Blobs and Graphs Prof. Noah Snavely CS1114
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CS 1114: Implementing Search
Data Structures – Stacks and Queus
Elementary Graph Algorithms
Richard Anderson Autumn 2016 Lecture 5
Decision Maths Graphs.
Prof. Ramin Zabih Graph Traversal Prof. Ramin Zabih
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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?

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

Images as graphs 5

6

7

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

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 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 “Eastern Telegraph Co. and its General Connections” (1901)

12

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

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 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  Is this graph planar? V1V2V3V4V5V6  Can you prove it?

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

“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

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

Image graph of the Pantheon 20

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

Blobs are components! 22

23 Blobs are components! A B C D EFG H A C B D F E H G

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 Coloring a component

A BCD EFG HIJ KLM Current node: A Todo List: [ ]

A BCD EFG HIJ KLM Current node: A Todo List: [ C ] Done with A, choose next from Todo List

A BCD EFG HIJ KLM Current node: C Todo List: [ ]

A BCD EFG HIJ KLM Current node: C Todo List: [ B, F, D ]

A BCD EFG HIJ KLM Current node: B Todo List: [ F, D ]

A BCD EFG HIJ KLM Current node: B Todo List: [ F, D, E ]

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

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

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

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

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

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

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

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

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

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)

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 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Current node: London Todo list: [ ] Graph traversal (queue) Oslo Stockholm

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

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

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

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

BFS vs. DFS Breadth-first (queue) Depth-first (stack)

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