CSE 421: Introduction to Algorithms

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Coloring Warm-Up. A graph is 2-colorable iff it has no odd length cycles 1: If G has an odd-length cycle then G is not 2- colorable Proof: Let v 0, …,
Introduction to Graphs
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 9 Instructor: Paul Beame.
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
CSCI 256 Data Structures and Algorithm Analysis Lecture 4 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 6.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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.
Introduction to Algorithms
CSC 172 DATA STRUCTURES.
Data Structures and Algorithm Analysis Lecture 5
Graphs – Breadth First Search
IOI/ACM ICPC Training 4 June 2005.
Graphs Chapter 20.
Graphs Motivation and Terminology Representations Traversals
Chapter 22 Elementary Graph Algorithms
Graphs Lecture 19 CS2110 – Spring 2013.
Spanning Trees.
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Graph theory Definitions Trees, cycles, directed graphs.
Lecture 12 Graph Algorithms
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Greedy Algorithms / Interval Scheduling Yin Tat Lee
CSE 421: Introduction to Algorithms
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graph Algorithms Using Depth First Search
Graph.
Planarity Testing.
CSE 421: Introduction to Algorithms
Breadth-First Search (BFS)
Lecture 10 Algorithm Analysis
Graphs Chapter 13.
Advanced Algorithms Analysis and Design
Elementary graph algorithms Chapter 22
Search Related Algorithms
Chapter 22: Elementary Graph Algorithms I
Lectures on Graph Algorithms: searching, testing and sorting
Basic Graph Algorithms
Depth-First Search Graph Traversals Depth-First Search DFS.
CSE 373 Data Structures Lecture 16
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
CSE 373: Data Structures and Algorithms
Richard Anderson Winter 2009 Lecture 6
Text Book: Introduction to algorithms By C L R S
Lecture 11 CSE 331 Sep 21, 2017.
Lecture 12 CSE 331 Sep 22, 2014.
CSE 417: Algorithms and Computational Complexity
Elementary graph algorithms Chapter 22
Lecture 12 Algorithm Analysis
3.2 Graph Traversal.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

CSE 421: Introduction to Algorithms Breadth First Search Yin Tat Lee

Properties of BFS Claim: All nontree edges join vertices on the same or adjacent levels of the tree Proof: Consider an edge {𝑥,𝑦} Say 𝑥 is first discovered and it is added to level 𝑖. We show y will be at level 𝑖 or 𝑖+1 This is because when vertices incident to 𝑥 are considered in the loop, if 𝑦 is still undiscovered, it will be discovered and added to level 𝑖+1.

Properties of BFS Lemma: All vertices at level 𝑖 of BFS(𝑠) have shortest path distance 𝑖 to 𝑠. Claim: If 𝐿 𝑣 =𝑖 then shortest path ≤𝑖 Pf: Because there is a path of length 𝑖 from 𝑠 to 𝑣 in the BFS tree Claim: If shortest path =𝑖 then 𝐿 𝑣 ≤𝑖 Pf: If shortest path =𝑖, then say 𝑠= 𝑣 0 , 𝑣 1 ,…, 𝑣 𝑖 =𝑣 is the shortest path to v. By previous claim, 𝐿 𝑣 1 ≤𝐿 𝑣 0 +1 𝐿 𝑣 2 ≤𝐿 𝑣 1 +1 … 𝐿 𝑣 𝑖 ≤𝐿 𝑣 𝑖−1 +1 So, 𝐿 𝑣 𝑖 ≤𝑖. This proves the lemma.

Why Trees? Trees are simpler than graphs Many statements can be proved on trees by induction So, computational problems on trees are simpler than general graphs This is often a good way to approach a graph problem: Find a "nice" tree in the graph, i.e., one such that non-tree edges have some simplifying structure Solve the problem on the tree Use the solution on the tree to find a “good” solution on the graph

CSE 421: Introduction to Algorithms Application of BFS Yin Tat Lee

BFS Application: Connected Component We want to answer the following type questions (fast): Given vertices 𝑢,𝑣 is there a path from 𝑢 to 𝑣 in 𝐺? Idea: Create an array 𝐴 such that For all 𝑢 in the same connected component, 𝐴[𝑢] is same. Therefore, question reduces to If 𝐴 𝑢 =𝐴[𝑣]?

BFS Application: Connected Component Initial State: All vertices undiscovered, 𝑐 = 0 For 𝑣 = 1 to 𝑛 do If state(𝑣) != fully-explored then Run BFS(𝑣) Set 𝐴 𝑢 ←𝑐 for each 𝑢 found in BFS(𝑣) 𝑐=𝑐+1 Note: We no longer initialize to undiscovered in the BFS subroutine Total Cost: 𝑂(𝑚+𝑛) In every connected component with 𝑛 𝑖 vertices and 𝑚 𝑖 edges BFS takes time 𝑂 𝑚 𝑖 + 𝑛 𝑖 . Note: one can use DFS instead of BFS.

Connected Components Lesson: We can execute any algorithm on disconnected graphs by running it on each connected component. We can use the previous algorithm to detect connected components. There is no overhead, because the algorithm runs in time 𝑂(𝑚+𝑛). So, from now on, we can (almost) always assume the input graph is connected.

Cycles in Graphs Claim: If an 𝑛 vertices graph 𝐺 has at least 𝑛 edges, then it has a cycle. Proof: If 𝐺 is connected, then it cannot be a tree. Because every tree has 𝑛−1 edges. So, it has a cycle. Suppose 𝐺 is disconnected. Say connected components of G have 𝑛 1 ,…, 𝑛 𝑘 vertices where 𝑛 1 +…+ 𝑛 𝑘 =𝑛 Since 𝐺 has ≥𝑛 edges, there must be some 𝑖 such that a component has 𝑛 𝑖 vertices with at least 𝑛 𝑖 edges. Therefore, in that component we do not have a tree, so there is a cycle.

Bipartite Graphs Definition: An undirected graph 𝐺=(𝑉,𝐸) is bipartite if you can partition the node set into 2 parts (say, blue/red or left/right) so that all edges join nodes in different parts i.e., no edge has both ends in the same part. Application: Scheduling: machine=red, jobs=blue Stable Matching: men=blue, wom=red a bipartite graph

Testing Bipartiteness Problem: Given a graph 𝐺, is it bipartite? Many graph problems become: Easier/Tractable if the underlying graph is bipartite (matching) Before attempting to design an algorithm, we need to understand structure of bipartite graphs. v2 v2 v3 v1 v4 v6 v5 v4 v3 v5 v6 v7 v1 v7 a bipartite graph G another drawing of G

An Obstruction to Bipartiteness Lemma: If 𝐺 is bipartite, then it does not contain an odd length cycle. Proof: We cannot 2-color an odd cycle, let alone 𝐺. ? bipartite (2-colorable) not bipartite (not 2-colorable)

A Characterization of Bipartite Graphs Lemma: Let 𝐺 be a connected graph, and let 𝐿0, …, 𝐿 𝑘 be the layers produced by BFS(𝑠). Exactly one of the following holds. (i) No edge of 𝐺 joins two nodes of the same layer, and 𝐺 is bipartite. (ii) An edge of 𝐺 joins two nodes of the same layer, and 𝐺 contains an odd-length cycle (and hence is not bipartite). L1 L2 L3 L1 L2 L3 Case (i) Case (ii)

A Characterization of Bipartite Graphs Lemma: Let 𝐺 be a connected graph, and let 𝐿0, …, 𝐿 𝑘 be the layers produced by BFS(𝑠). Exactly one of the following holds. (i) No edge of 𝐺 joins two nodes of the same layer, and 𝐺 is bipartite. (ii) An edge of 𝐺 joins two nodes of the same layer, and 𝐺 contains an odd-length cycle (and hence is not bipartite). Proof. (i) Suppose no edge joins two nodes in the same layer. By previous lemma, all edges join nodes on adjacent levels. Bipartition: blue = nodes on odd levels, red = nodes on even levels. L1 L2 L3 Case (i)

A Characterization of Bipartite Graphs Lemma: Let 𝐺 be a connected graph, and let 𝐿0, …, 𝐿 𝑘 be the layers produced by BFS(𝑠). Exactly one of the following holds. (i) No edge of 𝐺 joins two nodes of the same layer, and 𝐺 is bipartite. (ii) An edge of 𝐺 joins two nodes of the same layer, and 𝐺 contains an odd-length cycle (and hence is not bipartite). Proof. (ii) Suppose {𝑥, 𝑦} is an edge & 𝑥,𝑦 in same level 𝐿 𝑗 . Let 𝑧= their lowest common ancestor in BFS tree. Let 𝐿 𝑖 be level containing 𝑧. Consider cycle that takes edge from 𝑥 to 𝑦, then tree from 𝑦 to 𝑧, then tree from 𝑧 to 𝑥. Its length is 1+ 𝑗−𝑖 +(𝑗−𝑖), which is odd. z = lca(x, y)

Obstruction to Bipartiteness Corollary: A graph 𝐺 is bipartite if and only if it contains no odd length cycles. Furthermore, one can test bipartiteness using BFS. bipartite (2-colorable) not bipartite (not 2-colorable)

Summary of last lecture BFS(𝑠) implemented using queue. Edges into then-undiscovered vertices define a tree – the “Breadth First spanning tree” of 𝐺 Level 𝑖 in the tree are exactly all vertices 𝑣 s.t., the shortest path (in 𝐺) from the root 𝑠 to 𝑣 is of length 𝑖 All nontree edges join vertices on the same or adjacent layers of the tree Applications: Shortest Path Connected component Test bipartiteness / 2-coloring

Depth First Search Yin Tat Lee CSE 421 Depth First Search Yin Tat Lee

Depth First Search Follow the first path you find as far as you can go; back up to last unexplored edge when you reach a dead end, then go as far you can Naturally implemented using recursive calls or a stack

DFS(s) – Recursive version Initialization: mark all vertices undiscovered DFS(𝑣) Mark 𝑣 discovered for each edge {𝑣,𝑥} if (𝑥 is undiscovered) Mark 𝑥 discovered 𝑥→parent=𝑢 DFS(𝑥) Mark 𝑣 fully-discovered

Non-Tree Edges in DFS BFS tree ≠ DFS tree, but, as with BFS, DFS has found a tree in the graph s.t. non-tree edges are "simple" in some way. All non-tree edges join a vertex and one of its descendants/ancestors in the DFS tree

DFS(A) A,1 B J C G H K L D F I M E Color code: undiscovered discovered fully-explored DFS(A) A,1 Suppose edge lists at each vertex are sorted alphabetically Call Stack (Edge list): A (B,J) B J C G H K L D F I M st[] = {1} E

DFS(A) A,1 B,2 J C G H K L D F I M E Color code: undiscovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J C G H K L D F I M st[] = {1,2} E

DFS(A) A,1 B,2 J C,3 G H K L D F I M E Color code: undiscovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J C,3 G H K L D F I M st[] = {1,2,3} E

DFS(A) A,1 B,2 J C,3 G H K L D,4 F I M E Color code: undiscovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) B,2 J C,3 G H K L D,4 F I M st[] = {1,2,3,4} E

DFS(A) A,1 B,2 J C,3 G H K L D,4 F I M E,5 Color code: undiscovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) B,2 J C,3 G H K L D,4 F I M st[] = {1,2,3,4,5} E,5

DFS(A) A,1 B,2 J C,3 G H K L D,4 F,6 I M E,5 Color code: undiscovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) B,2 J C,3 G H K L D,4 F,6 I M st[] = {1,2,3,4,5,6} E,5

DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G (C,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6,7} E,5

DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) G (C,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6,7} E,5

DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) F (D,E,G) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5,6} E,5

DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) E (D,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4,5} E,5

DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) D (C,E,F) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3,4} E,5

DFS(A) A,1 B,2 J C,3 G,7 H K L D,4 F,6 I M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J C,3 G,7 H K L D,4 F,6 I M st[] = {1,2,3} E,5

DFS(A) A,1 B,2 J C,3 G,7 H,8 K L D,4 F,6 I M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J C,3 G,7 H,8 K L D,4 F,6 I M st[] = {1,2,3,8} E,5

DFS(A) A,1 B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) I (H) B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M st[] = {1,2,3,8,9} E,5

DFS(A) A,1 B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J C,3 G,7 H,8 K L D,4 F,6 I,9 M st[] = {1,2,3,8} E,5

DFS(A) A,1 B,2 J,10 C,3 G,7 H,8 K L D,4 F,6 I,9 M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 G,7 H,8 K L D,4 F,6 I,9 M st[] = {1,2,3,8, 10} E,5

DFS(A) A,1 B,2 J,10 C,3 G,7 H,8 K,11 L D,4 F,6 I,9 M E,5 Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) B,2 J,10 C,3 G,7 H,8 K,11 L D,4 F,6 I,9 M st[] = {1,2,3,8,10,11} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M st[] = {1,2,3,8,10,11,12} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) M (L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11,12,13} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) L (J,K,M) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11,12} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) K (J,L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8,10,11} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) J (A,B,H,K,L) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8, 10} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) H (C,I,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3,8} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) C (B,D,G,H) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2,3} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B (A,C,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1,2} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) A (B,J) B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {1} E,5

Color code: undiscovered discovered fully-explored DFS(A) A,1 Call Stack: (Edge list) TA-DA!! B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 st[] = {} E,5

DFS(A) Edge code: Tree edge Back edge A,1 B,2 J,10 C,3 G,7 H,8 K,11 L,12 D,4 F,6 I,9 M,13 E,5

A,1 Edge code: Tree edge Back edge No Cross Edges! DFS(A) B,2 C,3 H,8 D,4 J,10 E,5 I,9 By the way, suppose I’ve run dfs and each node has a dfs# Suppose I then hand you an edge and tell you the dfs#’s of the two endpoints, how do you know which of the endpoints is the ancestor? K,11 F,6 L,12 G,7 M,13

Properties of (undirected) DFS Like BFS(𝑠): DFS(𝑠) visits 𝑥 iff there is a path in G from 𝑠 to 𝑥 So, we can use DFS to find connected components Edges into then-undiscovered vertices define a tree – the "depth first spanning tree" of G Unlike the BFS tree: The DF spanning tree isn't minimum depth Its levels don't reflect min distance from the root Non-tree edges never join vertices on the same or adjacent levels

Non-Tree Edges in DFS Lemma: For every edge {𝑥,𝑦}, if {𝑥,𝑦} is not in DFS tree, then one of 𝑥 or 𝑦 is an ancestor of the other in the tree. Proof: Suppose that 𝑥 is visited first. Therefore DFS(𝑥) was called before DFS(𝑦) Since {𝑥,𝑦} is not in DFS tree, 𝑦 was visited when the edge {𝑥,𝑦} was examined during DFS(𝑥) Therefore 𝑦 was visited during the call to DFS(𝑥) so 𝑦 is a descendant of 𝑥.