GRAPHS Lecture 19 CS2110 – Fall 2013. Time to do A4, Recursion Histogram: max: 28.45 [00:02): 17 av: 5.2 [02:04): 102 median: 4.5 [04:06): 134 [06:08):

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
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.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.
Connected Components, Directed Graphs, Topological Sort COMP171.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
Chapter 2 Graph Algorithms.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
Great Theoretical Ideas in Computer Science for Some.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
COMPSCI 102 Introduction to Discrete Mathematics.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Graphs. Introduction Graphs are a collection of vertices and edges Graphs are a collection of vertices and edges The solid circles are the vertices A,
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Graphs - I CS 2110, Spring 2017 Leonhard Euler. 1736
Graphs Lecture 19 CS2110 – Spring 2013.
GRAPHS Lecture 16 CS2110 Fall 2017.
Graph Search Lecture 17 CS 2110 Fall 2017.
Depth-First Search.
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs Lecture 18 CS2110 – Fall 2009.
Graphs Chapter 13.
Chapter 11 Graphs.
GRAPHS Lecture 17 CS2110.
GRAPHS Lecture 17 CS2110 Spring 2018.
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

GRAPHS Lecture 19 CS2110 – Fall 2013

Time to do A4, Recursion Histogram: max: [00:02): 17 av: 5.2 [02:04): 102 median: 4.5 [04:06): 134 [06:08): 50 [08:10): 29 [10:12): 8 [12:14): 6 [14:16): 2 [16:18): 1 [18:30): 5 2 If you took more than 6-7 hours for this assignment, you may have been wasting your time. A certain amount of “floundering”, just trying things, is good. But after a point, it just wastes time. Seek help if after an hour on one of the recursion problems you are stuck.

These are not Graphs 3...not the kind we mean, anyway

These are Graphs 4 K5K5 K 3,3 =

Applications of Graphs 5  Communication networks  The internet is a huge graph  Routing and shortest path problems  Commodity distribution (flow)  Traffic control  Resource allocation  Geometric modeling ...

Graph Definitions 6  A directed graph (or digraph) is a pair (V, E) where  V is a set  E is a set of ordered pairs (u,v) where u,v ∈  V Sometimes require u  v (i.e. no self-loops)  An element of V is called a vertex (pl. vertices) or node  An element of E is called an edge or arc  |V| is the size of V, often denoted by n  |E| is size of E, often denoted by m

Example Directed Graph (Digraph) 7 V = { a,b,c,d,e,f } E = { (a,b), (a,c), (a,e), (b,c), (b,d), (b,e), (c,d), (c,f), (d,e), (d,f), (e,f) } |V| = 6, |E| = 11 b a c d e f

Example Undirected Graph 8 An undirected graph is just like a directed graph, except the edges are unordered pairs (sets) {u,v} Example: V = { a,b,c,d,e,f } E = { {a,b}, {a,c}, {a,e}, {b,c}, {b,d}, {b,e}, {c,d}, {c,f}, {d,e}, {d,f }, {e,f } } b a c d e f

Some Graph Terminology 9  u is the source, v is the sink of (u,v)  u, v, b, c are the endpoints of (u,v) and (b, c)  u, v are adjacent nodes. b, c are adjacent nodes  outdegree of u in directed graph: number of edges for which u is source  indegree of v in directed graph: number of edges for which v is sink  degree of vertex w in undirected graph: number of edges of which w is an endpoint uv bc uv w outdegree of u: 4 indegree of v: 3 degree of w: 2

More Graph Terminology 10  path: sequence of adjacent vertexes  length of path: number of edges  simple path: no vertex is repeated simple path of length 2: (b, c, d) simple path of length 0: (b) not a simple path: (b, c, e, b, c, d) b c d e

More Graph Terminology 11  cycle: path that ends at its beginning  simple cycle: only repeated vertex is its beginning/end  acyclic graph: graph with no cycles  dag: directed acyclic graph b c d e cycles: (b, c, e, b) (b, c, e, b, c, e, b) simple cycle: (c, e, b, c) graph shown is not a dag Question: is (d) a cycle? No. A cycle must have at least one edge

Is this a dag? 12  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears b a c d e f

Is this a dag? 13  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears b a c d e f

Is this a dag? 14  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears b c d e f

Is this a dag? 15  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears c d e f

Is this a dag? 16  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears d e f

Is this a dag? 17  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears e f

Is this a dag? 18  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears f

Topological Sort 19  We just computed a topological sort of the dag This is a numbering of the vertices such that all edges go from lower- to higher-numbered vertices  Useful in job scheduling with precedence constraints

Coloring of an undirected graph: an assignment of a color to each node such that no two adjacent vertices get the same color How many colors are needed to color this graph? Graph Coloring 20 b a c d e f

A coloring of an undirected graph: an assignment of a color to each node such that no two adjacent vertices get the same color How many colors are needed to color this graph? Graph Coloring 21 b a c d e f 3

An Application of Coloring 22  Vertices are jobs  Edge (u,v) is present if jobs u and v each require access to the same shared resource, so they cannot execute simultaneously  Colors are time slots to schedule the jobs  Minimum number of colors needed to color the graph = minimum number of time slots required b a c d e f

Planarity 23 A graph is planar if it can be embedded in the plane with no edges crossing Is this graph planar? b a c d e f

Planarity 24 A graph is planar if it can be embedded in the plane with no edges crossing Is this graph planar? b a c d e f b a c d e f YES

Detecting Planarity 25 Kuratowski's Theorem A graph is planar if and only if it does not contain a copy of K 5 or K 3,3 (possibly with other nodes along the edges shown) K 3,3 K5K5

Detecting Planarity 26 Early 1970’s John Hopcroft spent time at Stanford, talked to grad student Bob Tarjan (now at Princeton). Together, they developed a linear-time algorithm to test a graph for planarity. Significant achievement. Won Turing Award

The Four-Color Theorem 27 Every planar graph is 4-colorable (Appel & Haken, 1976) Interesting history. “Proved” in about 1876 and published, but ten years later, a mistake was found. It took 90 more years for a proof to be found. Countries are nodes; edge between them if they have a common boundary. You need 5 colors to color a map —water has to be blue!

The Four-Color Theorem 28 Every planar graph is 4-colorable (Appel & Haken, 1976) Proof rests on a lot of computation! A program checks thousands of “configurations”, and if none are colorable, theorem holds. Program written in assembly language. Recursive, contorted, to make it efficient. Gries found an error in it but a “safe kind”: it might say a configuration was colorable when it wasn’t.

Bipartite Graphs 29 A directed or undirected graph is bipartite if the vertices can be partitioned into two sets such that all edges go between the two sets

The following are equivalent  G is bipartite  G is 2-colorable  G has no cycles of odd length Bipartite Graphs 30

Traveling Salesperson 31 Find a path of minimum distance that visits every city Amsterdam Rome Boston Atlanta London Paris Copenhagen Munich Ithaca New York Washington

Representations of Graphs Adjacency List Adjacency Matrix

Adjacency Matrix or Adjacency List? 33 n: number of vertices m: number of edges d(u): outdegree of u Adjacency Matrix Uses space O(n 2 ) Can iterate over all edges in time O(n 2 ) Can answer “Is there an edge from u to v?” in O(1) time Better for dense graphs (lots of edges)  Adjacency List  Uses space O(m+n)  Can iterate over all edges in time O(m+n)  Can answer “Is there an edge from u to v?” in O(d(u)) time  Better for sparse graphs (fewer edges)

Graph Algorithms 34 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum spanning trees –Prim's algorithm –Kruskal's algorithm

Depth-First Search 35 Follow edges depth-first starting from an arbitrary vertex r, using a stack to remember where you came from When you encounter a vertex previously visited, or there are no outgoing edges, retreat and try another path Eventually visit all vertices reachable from r If there are still unvisited vertices, repeat O(m) time Difficult to understand! Let’s write a recursive procedure

Depth-First Search 36 boolean[] visited; node u is visited means: visited[u] is true To visit u means to: set visited[u] to true Node u is REACHABLE from node v if there is a path (u, …, v) in which all nodes of the path are unvisited Suppose all nodes are unvisited. The nodes that are REACHABLE from node 1 are 1, 0, 2, 3, 5 The nodes that are REACHABLE from 4 are 4, 5, 6.

Depth-First Search 37 boolean[] visited; To “visit” a node u: set visited[u] to true. Node u is REACHABLE from node v if there is a path (u, …, v) in which all nodes of the path are unvisited Suppose 2 is already visited, others unvisited. The nodes that are REACHABLE from node 1 are 1, 0, 5 The nodes that are REACHABLE from 4 are 4, 5, 6.

Depth-First Search 38 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 The nodes that are REACHABLE from node 1 are 1, 0, 2, 3, visited[u]= true;

Depth-First Search 39 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 The nodes to be visited are 0, 2, 3, visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); Have to do dfs on all unvisited neighbors of u

Depth-First Search 40 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 The nodes to be visited are 0, 2, 3, visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); Suppose the for each loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2, 3, 5

Depth-First Search 41 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); Example: There may be a different way (other than array visited) to know whether a node has been visited That’s all there is to the basic dfs. You may have to change it to fit a particular situation. Example: Instead of using recursion, use a loop and maintain the stack yourself.

Depth-First Search 42 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); That’s all there is to the basic dfs. You may have to change it to fit a particular situation. Example: In Bfly, there is no need for a parameter, because the current position of the Bfly takes the place of u. But then the specification must be changed, and probably the Bfly should be back at its original tile after each iteration of the loop. Make sure that is in the specification!

Breadth-First Search (BFS) BFS visits all neighbors first before visiting their neighbors. It goes level by level. Use a queue instead of a stack  stack: last-in, first-out (LIFO)  queue: first-in, first-out (FIFO) dfs(0) visits in this order: 0, 1, 4, 5, 2, 3, 6 bfs(0) visits in this order: 0,1, 2, 3, 4, 5, 6 45 Breadth-first not good for the Bfly: too much flying back and forth

Summary  We have seen an introduction to graphs and will return to this topic on Thursday  Definitions  Testing for a dag  Depth-first and breadth-first search 44