GRAPHS Lecture 17 CS2110 Spring 2014

Similar documents
GRAPHS Lecture 19 CS2110 Spring 2013

Graphs - I CS 2110, Spring 2016

CSI 604 Elementary Graph Algorithms

GRAPH ALGORITHMS Lecture 19 CS 2110 Spring 2019

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

Algorithm Design and Analysis

Basic Graph Definitions

Outline. Graphs. Divide and Conquer.

Graph Traversals. CS200 - Graphs 1

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Goals! CSE 417: Algorithms and Computational Complexity!

CS 310 Advanced Data Structures and Algorithms

Index. stack-based, 400 A* algorithm, 325

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Lecture 3: Graphs and flows

CS 206 Introduction to Computer Science II

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms

Konigsberg Bridge Problem

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Algorithms (part 3 of CSC 282),

Introduction to Algorithms

Algorithms: Lecture 10. Chalmers University of Technology

Graphs - II CS 2110, Spring 2016

Lecture 20: Other Algorithms on Graphs. Undirected Trees. Spanning Trees

The Konigsberg Bridge Problem

CS4800: Algorithms & Data Jonathan Ullman

Total Score /1 /20 /41 /15 /23 Grader

Copyright 2000, Kevin Wayne 1

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

CSE 417: Algorithms and Computational Complexity

Lecture 10 Graph algorithms: testing graph properties

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Algorithm Design and Analysis

22.3 Depth First Search

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Algorithm Design and Analysis

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

CS302 - Data Structures using C++

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

Data Structures and Algorithms. Chapter 7. Graphs

Graphs. Edges may be directed (from u to v) or undirected. Undirected edge eqvt to pair of directed edges

Homework Assignment #3 Graph

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Graphs - II CS 2110, Spring 2016

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Minimum Spanning Trees

Lecture 22 Tuesday, April 10

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Basic Graph Algorithms (CLRS B.4-B.5, )

W4231: Analysis of Algorithms

Graph Search Methods. Graph Search Methods

CS4800: Algorithms & Data Jonathan Ullman

Graph: representation and traversal


Lecture 26: Graphs: Traversal (Part 1)

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Graph. Vertex. edge. Directed Graph. Undirected Graph

Homework No. 4 Answers

Applications of BDF and DFS

Info 2950, Lecture 16

3.1 Basic Definitions and Applications

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Problem Score Maximum MC 34 (25/17) = 50 Total 100

CSE 100: GRAPH ALGORITHMS

Graph Algorithms (part 3 of CSC 282),

Graphs Introduction and Depth first algorithm

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

CMSC 380. Graph Terminology and Representation

Graph and Digraph Glossary

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Minimum Spanning Trees Ch 23 Traversing graphs

Data Structures and Algorithms. Werner Nutt

Graphs. There is no restriction on the relation E. Directed graphs also known as digraphs

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Transcription:

GRAPHS Lecture 17 CS2110 Spring 2014

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

These are Graphs 3 K 5 K 3,3 =

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

Graph Definitions 5 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 n 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) 6 b c d a f e 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

Example Undirected Graph 7 An undirected graph is just like a directed graph, except the edges are unordered pairs (sets) {u,v} Example: b c d a 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 } } e f

Some Graph Terminology 8 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 u b v c 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 u w v outdegree of u: 4 indegree of v: 3 degree of w: 2

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

More Graph Terminology 10 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 cycles: (b, c, e, b) (b, c, e, b, c, e, b) simple cycle: (c, e, b, c) graph shown is not a dag e c d Question: is (d) a cycle? No. A cycle must have at least one edge

Is this a dag? 11 b c d a e f 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

Is this a dag? 12 b c d a e f 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

Is this a dag? 13 b c d e f 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

Is this a dag? 14 d c e f 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

Is this a dag? 15 d e f 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

Is this a dag? 16 e f 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

Is this a dag? 17 f 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

Topological Sort 18 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 1 2 3 0 5 4 Useful in job scheduling with precedence constraints

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

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

An Application of Coloring 21 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 c d a e f

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

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

Detecting Planarity 24 Kuratowski's Theorem K 5 K 3,3 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)

Detecting Planarity 25 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 26 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 27 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.

28 Bipartite Graphs 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

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

Traveling Salesperson 30 Boston Ithaca 224 132 New York 210 1078 Washington 660 419 1356 1002 1323 1556 London 1214 1380 Copenhagen 160 512 189 216 Amsterdam 1322 Paris 505 Munich 441 Atlanta 1202 Rome Find a path of minimum distance that visits every city

Representations of Graphs 31 1 2 4 3 Adjacency List 1 2 4 Adjacency Matrix 1 2 3 4 1 0 1 0 1 2 3 4 3 2 3 2 0 0 1 0 3 0 0 0 0 4 0 1 1 0

Adjacency Matrix or Adjacency List? 32 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 33 Search depth-first search breadth-first search Shortest paths Dijkstra's algorithm Minimum spanning trees Prim's algorithm Kruskal's algorithm

Depth-First Search 34 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 35 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. 1 2 3 0 5 6 4 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 36 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. 1 2 3 Suppose 2 is already visited, others unvisited. The nodes that are REACHABLE from node 1 are 1, 0, 5 0 5 6 4 The nodes that are REACHABLE from 4 are 4, 5, 6.

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

Depth-First Search 38 /** 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); 1 2 3 Let u be 1 The nodes to be visited are 0, 2, 3, 5 Have to do dfs on all unvisited neighbors of u 0 5 6 4

Depth-First Search 39 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } 0 visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); 1 2 3 5 6 4 Let u be 1 The nodes to be visited are 0, 2, 3, 5 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 40 /** 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: There may be a different way (other than array visited) to know whether a node has been visited Example: Instead of using recursion, use a loop and maintain the stack yourself.

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

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