Trevor Brown DC 2338, Office hour M3-4pm

Slides:



Advertisements
Similar presentations
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, …,
Advertisements

 Theorem 5.9: Let G be a simple graph with n vertices, where n>2. G has a Hamilton circuit if for any two vertices u and v of G that are not adjacent,
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Tirgul 8 Graph algorithms: Strongly connected components.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Yangjun Chen 1 Bipartite Graphs What is a bipartite graph? Properties of bipartite graphs Matching and maximum matching - alternative paths - augmenting.
The main idea of the article is to prove that there exist a tester of monotonicity with query and time complexity.
1 Data Structures DFS, Topological Sort Dana Shapira.
Yangjun Chen 1 Bipartite Graph 1.A graph G is bipartite if the node set V can be partitioned into two sets V 1 and V 2 in such a way that no nodes from.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
CS621: Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 3 - Search.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
8.4 Closures of Relations Definition: The closure of a relation R with respect to property P is the relation obtained by adding the minimum number of.
15.082J & 6.855J & ESD.78J September 30, 2010 The Label Correcting Algorithm.
EMIS 8374 Shortest Path Trees Updated 11 February 2008 Slide 1.
 2004 SDU 1 Lecture5-Strongly Connected Components.
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.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
1 Section 4.4 Inductive Proof What do we believe about nonempty subsets of N? Since  N, <  is well-founded, and in fact it is linear, it follows that.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Introduction to Algorithms
Data Structures and Algorithm Analysis Lecture 5
Graphs – Breadth First Search
Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Data Structures, Algorithms & Complexity
Lecture 16 Strongly Connected Components
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
Bipartite Graphs What is a bipartite graph?
Graph Algorithms Using Depth First Search
CSCE 411 Design and Analysis of Algorithms
Planarity Testing.
CSE 421: Introduction to Algorithms
CS 3343: Analysis of Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Breadth-First Search The Breadth-first search algorithm
Elementary graph algorithms Chapter 22
Applications of DFS Topological sort (for directed acyclic graph)
Chapter 22: Elementary Graph Algorithms I
Lectures on Graph Algorithms: searching, testing and sorting
Basic Graph Algorithms
Chapter 22: Elementary Graph Algorithms
Shortest-Path Property 4.1
CSE 421: Introduction to Algorithms
Bipartite Graph 1. A graph G is bipartite if the node set V can be partitioned into two sets V1 and V2 in such a way that no nodes from the same set are.
Trevor Brown DC 2338, Office hour M3-4pm
Trevor Brown DC 2338, Office hour M3-4pm
Trevor Brown DC 2338, Office hour M3-4pm
Advanced Algorithms Analysis and Design
Trevor Brown DC 2338, Office hour M3-4pm
Elementary graph algorithms Chapter 22
3.2 Graph Traversal.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm CS 341: Algorithms Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm

This time BFS Complexity Proof of optimal distances Depth-first search (DFS)

BFS: Time and space complexity

Complexity? With adjacency list? With adjacency matrix?

BFS: Proof of optimal distances

Definitions: discovering and processing a node We use 𝒖<𝒗 to denote “𝒖 discovered before 𝒗” Observe: A node must be discovered before it can be processed! Discover (enqueue) starting node s Start processing node u Corollary: if 𝑢<𝑣 then 𝒖 is processed before 𝒗 (and vice versa) Discover (enqueue) node v Let’s use 𝒅 𝒖 as shorthand for 𝒅𝒊𝒔𝒕 𝒖 Finish processing node u

Proof of Optimal BFS distances: Claim 1 of 3 Lemma 1: if 𝒖<𝒗 then 𝒅 𝒖 ≤ 𝒅 𝒗 Proof by contradiction: assume ∃𝑢,𝑣 such that 𝒖<𝒗 but 𝒅 𝒖 > 𝒅 𝒗 Let 𝑣 be the earliest discovered node such that, for some 𝑢, we have 𝑢<𝑣 and 𝑑 𝑢 > 𝑑 𝑣 Consider predecessors 𝑢′ of 𝑢 and 𝑣′ of 𝑣 Note 𝑑 𝑢′ = 𝑑 𝑢 −1, and 𝑑 𝑢 > 𝑑 𝑣 , so 𝑑 𝑢′ ≥ 𝑑 𝑣 Also, 𝑑 𝑣′ = 𝑑 𝑣 −1, so 𝑑 𝑣′ < 𝑑 𝑢′ It turns out 𝑑 𝑣′ < 𝑑 𝑢′ implies 𝒗 ′ <𝒖′. Why? Since 𝒗 ′ < 𝒖 ′ , we know 𝑣 ′ is processed before 𝑢 ′ And, 𝑣 is discovered while processing 𝑣′, which is before 𝑢′ is processed, which is when 𝑢 is discovered. So 𝒗<𝒖! If not, then we have 𝒖 ′ < 𝒗 ′ . But 𝑣 ′ <𝑣, so neither 𝑣 ′ nor 𝑢′ can violate the lemma. So we have 𝑢 ′ < 𝑣 ′ ⇒ 𝒅 𝒖′ ≤ 𝒅 𝒗′ Which is a contradiction!

Proof of Optimal BFS distances: Claim 2 of 3 Lemma 2: if there is an edge {𝒖,𝒗}, then 𝒅 𝒖 − 𝒅 𝒗 ≤𝟏 Proof by cases: WLOG suppose 𝑢<𝑣 Case 1: 𝑣 is white when we process 𝑢 Then 𝑑 𝑣 = 𝑑 𝑢 +1. QED

Proof of Optimal BFS distances: Claim 2 of 3 Lemma 2: if there is an edge {𝒖,𝒗}, then 𝒅 𝒖 − 𝒅 𝒗 ≤𝟏 Proof by cases: WLOG suppose 𝑢<𝑣 Case 2: 𝑣 is grey when we process 𝑢 Since 𝑣 is not white, we did not discover it from 𝑢 We discovered 𝑣 earlier when processing some 𝑣 ′ ≠𝑢 Since 𝑣′ was processed before 𝑢, we have 𝑣 ′ <𝑢 So, by Lemma 1 we have 𝑑 𝑣 ′ ≤ 𝑑 𝑢 . Also note 𝑑 𝑣 = 𝑑 𝑣 ′ +1. Rearrange to get 𝑑 𝑣 ′ = 𝑑 𝑣 −1. Substituting 𝑑 𝑣 ′ into 𝑑 𝑣 ′ ≤ 𝑑 𝑢 we get 𝒅 𝒖 ≥ 𝒅 𝒗 −𝟏 Also, since 𝑢<𝑣, Lemma 1 implies 𝒅 𝒖 ≤ 𝒅 𝒗 So, 𝒅 𝒗 −𝟏≤ 𝒅 𝒖 ≤ 𝒅 𝒗 . QED

Proof of Optimal BFS distances: Claim 2 of 3 Lemma 2: if there is an edge {𝒖,𝒗}, then 𝒅 𝒖 − 𝒅 𝒗 ≤𝟏 Proof by cases: WLOG suppose 𝑢<𝑣 Case 3: 𝑣 is black when we process 𝑢 Then 𝑣 is finished processing before 𝑢 Therefore 𝑣<𝑢 This contradicts our assumption that 𝑢<𝑣. QED

Proof of optimal distances: Claim 3 of 3 Theorem: 𝒅 𝒗 is the length of the shortest path from 𝒔 to 𝒗 Let 𝛿 𝑣 denote the length of the shortest path from 𝑠 to 𝑣 The path 𝑣→𝜋 𝑣 →𝜋 𝜋 𝑣 →…→𝑠 has distance 𝑑 𝑣 , so 𝜹 𝒗 ≤ 𝒅 𝒗 . We show 𝜹 𝒗 ≥ 𝒅 𝒗 by induction on the value of distance 𝛿 𝑣 Base case: suppose 𝛿 𝑣 =0. Then 𝑣=𝑠 and 𝑑 𝑣 =0, so 𝛿 𝑣 ≥ 𝑑 𝑣 . Inductive hypothesis: “if 𝛿 𝑣 =𝑘−1 then 𝛿 𝑣 ≥ 𝑑 𝑣 .” We prove: “if 𝛿 𝑣 =𝑘 then 𝛿 𝑣 ≥ 𝑑 𝑣 .” (So, suppose 𝑑 𝑣 =𝑘) Then ∃ a shortest path 𝑠→ 𝑣 1 →…→ 𝑣 𝑘−1 → 𝑣 𝑘 =𝑣 with length 𝛿 𝑣 =𝑘 By Lemma 2, we have 𝑑 𝑣 𝑘 ≤ 𝑑 𝑣 𝑘−1 +1, so 𝑑 𝑣 ≤ 𝑑 𝑣 𝑘−1 +1 By the inductive hypothesis, we have 𝛿 𝑣 𝑘−1 ≥ 𝑑 𝑣 𝑘−1 so 𝑑 𝑣 𝑘−1 = 𝛿 𝑣 𝑘−1 =𝑘−1 So 𝑑 𝑣 ≤ 𝑑 𝑣 𝑘−1 +1 becomes 𝑑 𝑣 ≤ 𝑘−1 +1=𝑘. Rearranging to 𝑘≥ 𝑑 𝑣 and substituting 𝛿 𝑣 =𝑘 we get 𝜹 𝒗 ≥ 𝒅 𝒗 . QED

Outputting an optimal path

How to represent a grid graph? Game AI: path finding in a grid-graph BFS from here 5 4 3 4 3 2 5 1

Game AI: path finding with waypoints Divide game world into linear paths, then send game characters in straight lines between waypoints Use BFS to find shortest sequence of waypoints (with fewest waypoints)

shortest path to a mouse cursor around obstacles User interfaces: shortest path to a mouse cursor around obstacles

How to Output an actual path Suppose you want to output a path from 𝑠 to 𝑣 with minimum distance (not just the distance to 𝑣) Algorithm (what do you think?) Similar to extracting an answer from a DP array! Work backwards through the predecessors Start at 𝑣 (set 𝑐𝑢𝑟:=𝑣) Inside a loop, visit the predecessor node (by printing 𝑐𝑢𝑟 and then setting 𝑐𝑢𝑟≔𝜋 𝑐𝑢𝑟 ) Stop when you hit 𝑠 (when 𝑐𝑢𝑟=𝑠) Note: path is printed in reverse! Solution?

Preview: doing this quickly, on a large scale

Recall the Pac-man example Shortest path to here? Recall the Pac-man example 5 5 4 3 Destination 𝑣 BFS from here 4 4 3 3 2 2 Predecessor 𝜋 𝑣 Each time you visit a predecessor, push it into a stack 5 1 Predecessor 𝜋 𝜋 𝑣 1 I.e., push 𝑣=5, then push 𝜋 𝑣 =4, then push 𝜋 𝜋 𝑣 =3, then 2, … At the end, pop all off the stack. This gives 0, 1, 2, …, 5 = the path!

Depth First Search (DFS)

?

Preview: Visualizing DFS https://www.cs.usfca.edu/~galles/visualization/DFS.html

Copying to blackboard, then back to the code

For your notes…