CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.

Slides:



Advertisements
Similar presentations
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Advertisements

CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Single Source Shortest Paths
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Polynomial Time Approximation Schemes Presented By: Leonid Barenboim Roee Weisbert.
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
Tirgul 8 Graph algorithms: Strongly connected components.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Applied Discrete Mathematics Week 12: Trees
1 Mazes In The Theory of Computer Science Dana Moshkovitz.
A general approximation technique for constrained forest problems Michael X. Goemans & David P. Williamson Presented by: Yonatan Elhanani & Yuval Cohen.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Is the following graph Hamiltonian- connected from vertex v? a). Yes b). No c). I have absolutely no idea v.
Complexity 1 Mazes And Random Walks. Complexity 2 Can You Solve This Maze?
Minimum Spanning Trees
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
CSE 20 DISCRETE MATH Prof. Shachar Lovett Clicker frequency: CA.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
9/23/2015ACM-ICPC1 Maximum Flow Problem Source t fg e 42 Target (sink) flow capacity b s c a de 2/3/ actual flow 1/2/ 1/ 2/ 1.Actual flow  capacity.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Approximating the Minimum Degree Spanning Tree to within One from the Optimal Degree R 陳建霖 R 宋彥朋 B 楊鈞羽 R 郭慶徵 R
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
Connectivity. Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
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.
TU/e Algorithms (2IL15) – Lecture 8 1 MAXIMUM FLOW (part II)
CSE 20 – Discrete Mathematics
Minimal Spanning Trees
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Minimal Spanning Trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Planarity Testing.
CSE 421: Introduction to Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Chapter 22: Elementary Graph Algorithms I
Basic Graph Algorithms
CS 583 Analysis of Algorithms
Minimum Spanning Trees
Advanced Algorithms Analysis and Design
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License. Based on a work at Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics: 1. Graph algorithm: Reachability 2

Algorithms  We will see today how to use induction to argue about algorithms  This is mainly what you will be using it for, both in class and later in your jobs  We focus here on graph algorithms, but similar ideas apply to many other domains: chip optimization problems, bio-informatics, routing internet traffic, etc. 3

What is an algorithm?  An algorithm is an efficient way to process information  It has inputs  It needs to produce an output 4

Reachability  Input:  undirected graph G  two designated vertices: s (source) and t (target)  Output: is there a path in G from s to t 5

Reachability  Example: is t reachable from s? A. Yes B. No 6 s t II

Reachability  Example: is t reachable from s? A. Yes B. No 7 II s t

Reachability 8  Potential application: sidewalk reachability  G – sidewalk graph  Source: s = CSE building  Target: t = Sea world  Can I safely walk from the CSE building to Sea world?

Algorithm Intuition  Intuition: gradually discover all vertices reachable from s  Each vertex v has a label L(v)  L(v)=yes means it is reachable from s  L(v)=? means we don’t know yet  At the end, we found all vertices reachable from s  if L(v)=? then it is not reachable from s  Return yes if L(t)=yes; no otherwise 9

Algorithm Intuition  Recall: in an undirected graph G, the neighbors of a vertex v are all vertices u where u,v is an edge  Boundary = set of vertices whose neighbors were not explored yet 10

Reachability Algorithm  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no 11

Running example 12 ? ? ?? Y ? s t a b c d  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no Initializing labels B={s}

Running example 13 Y ? ?? Y ? s t a b c d Loop beginning: B={s} Choose v=s in step 3.1 Neighbors = {a} Neighbors labeled with ? = {a} Loop end: B={a}  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Running example 14 Y Y YY Y ? s t a b c d Loop beginning: B={a} Choose v=a in step 3.1 Neighbors = {b,c,d} Neighbors labeled with ? = {b,c,d} Loop end: B={b,c,d}  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Running example 15 Y Y YY Y ? s t a b c d Loop beginning: B={b,c,d} Choose v=b in step 3.1 Neighbors = {a,c} Neighbors labeled with ? = {} Loop end: B={c,d}  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Running example 16 Y Y YY Y ? s t a b c d Loop beginning: B={c,d} Choose v=c in step 3.1 Neighbors = {a,b} Neighbors labeled with ? = {} Loop end: B={d}  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Running example 17 Y Y YY Y Y s t a b c d Loop beginning: B={d} Choose v=d in step 3.1 Neighbors = {a,t} Neighbors labeled with ? = {t} Loop end: B={t}  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Running example 18 Y Y YY Y Y s t a b c d Loop beginning: B={t} Choose v=t in step 3.1 Neighbors = {d} Neighbors labeled with ? = {} Loop end: B={}. DONE. RETURN YES.  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  We will prove that the algorithm is correct  Theorem: the algorithm always terminates. It returns yes if and only if t is reachable from s  It will be useful to argue about how the boundary and the labels evolve during the life cycle of the algorithm  We will break the proof to a series of simpler claims, which together will imply the proof 19

Correctness proof  Definitions:  B i = boundary in the i-th loop iteration  L i (v) = the label of vertex v in the i-th loop iteration  So initially… A. B 1 ={} B. B 1 ={s} C. B 1 ={t} D. B 1 =G E. Other 20  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Definitions:  B i = boundary in the i-th loop iteration  L i (v) = the label of vertex v in the i-th loop iteration  Initially  B 1 = {s}  L 1 (s)=yes  L 1 (v)=? for all v≠s 21  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 1: if L i (v)=yes then L j (v)=yes for all j>I  Proof: try and prove yourself first 22  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 1: if L i (v)=yes then L j (v)=yes for all j>i 23  Proof: We only change labels in step where we set L(u)=yes. So labels never change back from yes to ?.  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 2: if v  B i then L i (v)=yes.  Proof: try and prove yourself first (hint: use induction on i) 24  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 2: if v  B i then L i (v)=yes. 25  Proof: by induction on i Base: i=1. B 1 ={s} and L 1 (s)=yes. Inductive step: assume for i, prove for i+1. Let u  B i+1. If u  B i then we are done by induction + claim 1. If not, then u was added in step in the i-th iteration, which also set L i+1 (u)=yes.  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 3: if L i (v)=yes then v is reachable from s.  Proof: try and prove yourself first (hint: use induction on i) 26  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 3: if L i (v)=yes then v is reachable from s. 27  Proof: by induction on i Base: i=1. L 1 (v)=yes only for v=s. Inductive step: Assume for i, prove for i+1. Let u be such that L i+1 (u)=yes. If L i (u)=yes we are done by induction hypothesis. Otherwise, L i+1 (u) was set to yes in step in the i-th loop iteration since u is a neighbor of some v  B i. By claim 2, L i (v)=yes. By induction, v is reachable for s. Hence, u is also reachable for s.  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 4: the algorithm terminates (eg doesn’t run forever) 28  Proof: in each step i, either  New vertex was marked yes  or|B i+1 |<|B i | Let k be the latest step in which a vertex was marked yes. Then after additional |B k | steps the algorithm must terminate.  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Correctness proof  Claim 5: If L i (v)=yes then for all neighbors u of v, L j (u)=yes for some j≥i. 29  Proof: by contradiction Let u be a neighbor of v, and let i be minimal such that L i (v)=yes. Then v  B i. By the time the algorithm ends, we removed all vertices from B. In particular, at some time step j we chose v. This means that either u is already labeled yes (i.e. L j (u)=yes) or that in this loop it is marked by yes (and then L j+1 (u)=yes).

Correctness proof  Claim 6: if v is reachable from s then L i (v)=yes for some i. In particular, L(v)=yes at the end. 30  Proof: induction on distance from s. Assume v has distance k from s, i.e. there is a path s=v 0,v 1,…,v k =v. Base case: k=0. v=s. Inductive step: L i (v k-1 )=yes for some i by induction hypothesis. By claim 5, L j (v k )=yes for some j≥i.  Input: undirected graph G, vertices s,t  Output: is there a path in G from s to t 1. Initialize labels: L(s)=yes; L(v)=? for all v≠s 2. Initialize boundary: B={s} 3. While B is not empty: 3.1 Choose a vertex v  B 3.2 Remove v from B 3.3 For all neighbors u of v in G: If L(u)=? then: set L(u)=yes and add u to B 4. If L(t)=yes return yes; otherwise return no

Summary of claims  Claim 1: if L i (v)=yes then L j (v)=yes for all j>i  Claim 2: if v  B i then L i (v)=yes.  Claim 3: if L i (v)=yes then v is reachable from s.  Claim 4: the algorithm terminates (eg doesn’t run forever)  Claim 5: If L i (v)=yes then for all neighbors u of v, L j (u)=yes for some j≥i.  Claim 6: if v is reachable from s then L i (v)=yes at the end. In particular, L(v)=yes at the end. 31

Proof of theorem  L(v) = label at the end. Well defined by claim 4  If L(v)=yes then v is reachable from s  Claim 3  If L(v)=? then v is not reachable from s  Claim 6 32

More properties of algorithm  A more careful analysis shows that…  If G has n vertices, the algorithm terminates after at most n steps  The number of “basic operations” (set insertion, deletion) is at most the number of edges of G  A simple variant can compute not only reachability, but the distance of all the vertices from s 33

Conclusions  Our first serious algorithm  You will see much more in next classes: 21, 100, 101, 105 and in domain-specific classes  Analysis used various proof techniques: induction, contradiction, proof by cases 34