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

Similar documents
Copyright 2000, Kevin Wayne 1

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

3.1 Basic Definitions and Applications

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

Copyright 2000, Kevin Wayne 1

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

Algorithm Design and Analysis

3.1 Basic Definitions and Applications

Algorithms: Lecture 10. Chalmers University of Technology

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

3.4 Testing Bipartiteness

Algorithm Design and Analysis

Goals! CSE 417: Algorithms and Computational Complexity!

Lecture 3: Graphs and flows

CSE 421 DFS / Topological Ordering

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

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

CS 310 Advanced Data Structures and Algorithms

Objec&ves. Wrap up: Implemen&ng BFS and DFS Graph Applica&on: Bipar&te Graphs. Get out your BFS implementa&on handouts. Feb 2, 2018 CSCI211 - Sprenkle

CS 491 CAP Introduction to Graphs and Search

Graph Algorithms. Definition

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

Graph: representation and traversal

Graph Traversals. CS200 - Graphs 1

W4231: Analysis of Algorithms

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

Outline. Graphs. Divide and Conquer.

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

Graph Representation

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

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Graph Algorithms Using Depth First Search

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Fundamental Graph Algorithms Part Four

Searching in Graphs (cut points)

Trees. Arash Rafiey. 20 October, 2015

CS4800: Algorithms & Data Jonathan Ullman

Graph. Vertex. edge. Directed Graph. Undirected Graph

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

CSE 421 Applications of DFS(?) Topological sort

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

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

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2019

Graph Representations and Traversal

Fundamental Graph Algorithms Part Three

Lecture 10 Graph algorithms: testing graph properties

Algorithm Design and Analysis

CS Lunch. Grace Hopper??? O()? Slides06 BFS, DFS, Bipartite.key - October 3, 2016

Announcements. HW3 is graded. Average is 81%

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Solutions to relevant spring 2000 exam problems

Applications of BDF and DFS

Adjacent: Two distinct vertices u, v are adjacent if there is an edge with ends u, v. In this case we let uv denote such an edge.

Depth First Search (DFS)

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

Lecture 9 Graph Traversal

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Lecture 22 Tuesday, April 10

CSI 604 Elementary Graph Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

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

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Chapter 3. Graphs. 3.1 Basic Definitions and Applications

CS4800: Algorithms & Data Jonathan Ullman

DFS & STRONGLY CONNECTED COMPONENTS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Strongly Connected Components

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

Figure 1: A directed graph.

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

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

CSE 417: Algorithms and Computational Complexity

22.3 Depth First Search

GRAPHS Lecture 17 CS2110 Spring 2014

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Broadcast: Befo re 1

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Introduction to Algorithms

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

Lecture 26: Graphs: Traversal (Part 1)

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 11: Graphs and Trees. March 23, 2008

CS302 - Data Structures using C++

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Graphs. Ouline. Graphs 10/3/2018. An Introduction. A graph G = (V,E) is composed of: An edge e = (u,v) is a pair of vertices

Algorithms: Graphs. Amotz Bar-Noy. Spring 2012 CUNY. Amotz Bar-Noy (CUNY) Graphs Spring / 95

Keys to Success: CAR Theorem CHAPTER 3 GRAPHS. Outline. Basics. Definitions and applications. Iris Hui-Ru Jiang Fall 2014.

Basic Graph Definitions

(Re)Introduction to Graphs and Some Algorithms

Transcription:

Chapter 3 - Graphs

Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. 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 2

Google maps Transportation graph. Nodes: street addresses Edges: streets/highways 3

Web graph. Nodes: web pages. Edges: hyperlinks. World Wide Web http://googlesystem.blogspot.com/2007/05/worldwide-web-as-seen-by-google.html 4

Social network graph. Node: people. Edge: relationship. Social Networks http://bvlg.blogspot.com/2007/04/twittervrienden.html http://people.oii.ox.ac.uk/hogan/2010/01/new-pinwheel-network-layout/ 5

A graph of blogosphere links http://datamining.typepad.com/gallery/blog-map-gallery.html 6

Additional Graph Applications Graph transportation Nodes street intersections highways Edges communication computers Network connections World Wide Web web pages hyperlinks social people relationships scheduling tasks precedence 7

Graph Representation: Adjacency Matrix Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. Two representations of each edge. Space proportional to n 2. Checking if (u, v) is an edge takes Θ(1) time. Identifying all edges takes Θ(n 2 ) time. 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 8

Graph Representation: Adjacency List Adjacency list. Node indexed array of lists. Two representations of each edge. Space proportional to m + n. Checking if (u, v) is an edge takes O(deg(u)) time. Identifying all edges takes Θ(m + n) time. degree = number of neighbors of u 1 2 3 2 3 1 3 4 5 1 2 5 7 8 4 2 5 5 6 2 3 4 6 5 7 3 8 8 3 7 9

Paths and Connectivity Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1, v 2,, v k-1, v k with the property that each consecutive pair v i, v i+1 is joined by an edge in G. Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v. 10

Cycles Def. A cycle is a path v 1, v 2,, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct. cycle C = 1-2-4-5-3-1 11

Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. How many edges does a tree have? Given a set of nodes, build a tree step wise every time you add an edge, you must add a new node to the growing tree, WHY? how many edges to connect n nodes? 12

Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. G is connected. G does not contain a cycle. G has n-1 edges. 13

Rooted Trees Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. Importance. Models hierarchical structure. root r parent of v v child of v a tree the same tree, rooted at 1 14

Phylogenetic Trees Phylogeny. Describes the evolutionary history of species. http://www.whozoo.org/mammals/carnivores/cat_phylogeny.htm 15

3.2 Graph Traversal

Connectivity s-t connectivity problem. Given two node s and t, is there a path between s and t? s-t shortest path problem. Given two node s and t, what is the length of the shortest path between s and t? 17

Breadth First Search BFS intuition. Explore outward from s, adding nodes one "layer" at a time. BFS algorithm. L 0 = { s }. s L 1 L 2 L n-1 L 1 = all neighbors of L 0. L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge to a node in L 1. L i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L i. Theorem. For each i, L i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer. 18

Breadth First Search Property. Let T be a BFS tree of G, and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. L 0 L 1 L 2 L 3 19

BFS - implementation bfs(v) : q queue of nodes to be processed q.enque(v) mark v as explored while(q is non empty) : u = q.dequeue() for (each node v adjacent to u) : if v is unexplored : mark v as explored q.enqueue(v) Claim: this implementation explores nodes in order of their appearance in BFS layers

Breadth First Search: Analysis bfs(v) : q queue of nodes to be processed q.enque(v) mark v as explored while(q is non empty) : u = q.dequeue() for (each node v adjacent to u) : if v is unexplored : mark v as explored q.enqueue(v) Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency list representation. Proof: when we consider node u, there are deg(u) incident edges (u, v) total time processing edges is Σ u V deg(u) = 2m each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v) 21

Connected Components Connected graph. There is a path between any pair of nodes. Connected component of a node s. The set of all nodes reachable from s. Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }. 22

Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. Node: pixel. Edge: two neighboring lime pixels. Blob: connected component of lime pixels. recolor lime green blob to blue 23

Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. Node: pixel. Edge: two neighboring lime pixels. Blob: connected component of lime pixels. recolor lime green blob to blue 24

Connected Components Given two nodes s, and t, what can you say about their connected components? 25

Connected Components A generic algorithm for finding connected components: s R u v R = {s} # the connected component of s is initially s. while there is an edge (u,v) where u is in R and v is not in R: add v to R Theorem. Upon termination, R is the connected component containing s. BFS: explore in order of distance from s. DFS: explore in a different way. 26

DFS: Depth First Search Explores edges from the most recently discovered node; backtracks when reaching a dead-end.

DFS: Depth First Search Explores edges from the most recently discovered node; backtracks when reaching a dead-end. Recursively: DFS(u): mark u as Explored and add u to R for each edge (u,v) : if v is not marked Explored : DFS(v)

DFS - nonrecursively DFS(u): mark u as Explored and add u to R for each edge (u,v) : if v is not marked Explored : DFS(v) DFS(v) : s stack of nodes to be processed s.push(v) mark v as Explored while(s is non empty) : u = s.pop() for (each node v adjacent to u) : if v is not Explored : mark v as Explored s.push(v) 29

DFS - Analysis DFS(v) : s stack of nodes to be processed s.push(v) mark v as Explored while(s is non empty) : u = s.pop() for (each node v adjacent to u) : if v is not Explored : mark v as Explored s.push(v) Theorem. The above implementation of DFS runs in O(m + n) time if the graph is given by its adjacency list representation. Proof: Same as in BFS 30

3.4 Testing Bipartiteness

Bipartite Graphs Def. An undirected graph G = (V, E) is bipartite if the nodes can be colored red or blue such that every edge has one red and one blue end. Applications. Scheduling: machines = red, jobs = blue. a bipartite graph 32

Testing Bipartiteness Testing bipartiteness. Given a graph G, is it bipartite? Many graph problems become tractable if the underlying graph is bipartite (independent set) A graph is bipartite if it is 2-colorable v 2 v 3 v 2 v 4 v 1 v 6 v 5 v 4 v 5 v 3 v 6 v 7 v 1 v 7 a bipartite graph G another drawing of G 33

Algorithm for testing if a graph is bipartite Pick a node s and color it blue Its neighbors must be colored red. Their neighbors must be colored blue. Proceed until the graph is colored. Check that there is no edge whose ends are the same color. L 1 L 2 L 3 34

An Obstacle to Bipartiteness Which of these graphs is 2-colorable? 35

An Obstacle to Bipartiteness Lemma. If a graph G is bipartite, it cannot contain an odd cycle. Proof. Not possible to 2-color the odd cycle, let alone G. bipartite (2-colorable) not bipartite (not 2-colorable) 36

Bipartite Graphs Lemma. Let G be a connected graph, and let L 0,, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer. G is bipartite. (ii) An edge of G joins two nodes of the same layer. G contains an odd-length cycle (and hence is not bipartite). L 1 L 2 L 3 L 1 L 2 L 3 Case (i) Case (ii) 37

Bipartite Graphs Lemma. Let G be a connected graph, and let L 0,, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer. G is bipartite. (ii) An edge of G joins two nodes of the same layer. G contains an odd-length cycle (and hence is not bipartite). Proof. (i) Suppose no edge joins two nodes in the same layer. I.e. all edges join nodes on adjacent layers. Bipartition: red = nodes on odd levels, blue = nodes on even levels. L 1 L 2 L 3 Case (i) 38

Bipartite Graphs Lemma. Let G be a connected graph, and let L 0,, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer. G is bipartite. (ii) An edge of G joins two nodes of the same layer. G contains an odd-length cycle (and hence is not bipartite). Proof. (ii) Suppose (x, y) is an edge with x, y in same level L j. Let z = lca(x, y) = lowest common ancestor. Let L i be level containing z. Consider cycle that takes edge from x to y, then path from y to z, then path from z to x. Its length is 1 + (j-i) + (j-i), which is odd. z = lca(x, y) (x, y) path from y to z path from z to x 39

Obstruction to Bipartiteness Corollary. A graph G is bipartite iff it contains no odd length cycle. 5-cycle C bipartite (2-colorable) not bipartite (not 2-colorable) 40

3.5 Connectivity in Directed Graphs

Directed graph. G = (V, E) Directed Graphs Edge (u, v) goes from node u to node v. Example. Web graph - hyperlink points from one web page to another. Modern web search engines exploit hyperlink structure to rank web pages by importance. 42

Graph Search Directed reachability. Given a node s, find all nodes reachable from s. Web crawler. Start from web page s. Find all web pages linked from s, either directly or indirectly. BFS and DFS extend naturally to directed graphs. Given a path from s to t, not guaranteed there is a path from t to s. 43

Strong Connectivity Def. Nodes u and v are mutually reachable if there is a path from u to v and also a path from v to u. Def. A graph is strongly connected if every pair of nodes is mutually reachable. Lemma. Let s be any node. G is strongly connected iff every node is reachable from s, and s is reachable from every node. Proof. Follows from definition. Proof. Path from u to v: concatenate u-s path with s-v path. Path from v to u: concatenate v-s path with s-u path. s u v 44

Strong Connectivity: Algorithm Theorem. Can determine if G is strongly connected in O(m + n) time. Proof. Pick any node s. Run BFS from s in G. Run BFS from s in G rev. reverse orientation of every edge in G Return true iff all nodes reached in both BFS executions. Correctness follows immediately from previous lemma. strongly connected not strongly connected 45

3.6 DAGs and Topological Ordering

Graphs Describing Precedence Examples: prerequisites for a set of courses dependencies between programs dependencies between jobs Precedence constraints. Edge (v i, v j ) means task v i must occur before v j. Want an ordering of the nodes that respects the precedence relation Example: An ordering of CS courses The graph does not contain cycles. Why?

Directed Acyclic Graphs Def. A Directed Acyclic Graph (DAG) is a directed graph that contains no directed cycles. Def. A topological order of a directed graph G is an ordering of its nodes as v 1, v 2,, v n so that for every edge (v i, v j ) we have i < j. v 2 v 3 v 6 v 5 v 4 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 7 v 1 a DAG a topological ordering 48

Directed Acyclic Graphs Lemma. If G has a topological order, then G is a DAG. Proof. (by contradiction) Suppose that G has a topological order v 1,, v n and that G also has a directed cycle C. Let v i be the lowest-indexed node in C, and let v j be the node just before v i ; thus (v j, v i ) is an edge. By our choice of i, we have i < j. On the other hand, since (v j, v i ) is an edge and v 1,, v n is a topological order, we must have j < i, a contradiction. the directed cycle C v 1 v i v j v n the supposed topological order: v 1,, v n 49

Directed Acyclic Graphs Lemma. If G has a topological order, then G is a DAG. Q. Does every DAG have a topological ordering? Q. If so, how do we compute one? 50

Directed Acyclic Graphs Lemma. If G is a DAG, then G has a node with no incoming edges. Proof. (by contradiction) Suppose that G is a DAG and every node has at least one incoming edge. Pick any node v, and begin following edges backward from v. Repeat. After n + 1 we will have visited a node, say w, twice. Let C denote the sequence of nodes encountered between successive visits to w. C is a cycle. w x u v 51

Directed Acyclic Graphs Lemma. If G is a DAG, then G has a topological ordering. Proof. (by induction on n) Base case: true if n = 1. Given DAG on n > 1 nodes, find a node v with no incoming edges. G - { v } is a DAG, since deleting v cannot create cycles. By induction hypothesis, G - { v } has a topological ordering. Place v first in topological ordering; append nodes of G - { v } in topological order. 52

Algorithm: Topological Sort: Algorithm keep track of # incoming edges per node while (nodes left) : extract one with 0 incoming subtract one from all its adjacent nodes Running time? Better way? 53

Topological Sort: Algorithm Running Time Theorem. Algorithm can be implemented to run in O(m + n) time. Proof. Maintain the following information: count[w] = remaining number of incoming edges S = set of nodes with no incoming edges Initialization: O(m + n) via single scan through graph. Update: to delete v remove v from S for each edge (v, w) : decrement count[w] and add w to S if count[w] hits 0 this is O(1) per edge 54