CSE 421 Applications of DFS(?) Topological sort

Similar documents
4.1 Interval Scheduling

CSE 421 DFS / Topological Ordering

Design and Analysis of Algorithms

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

Goals! CSE 417: Algorithms and Computational Complexity!

CSE 521: Design and Analysis of Algorithms I

3.4 Testing Bipartiteness

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

Objec&ves. Review. Directed Graphs: Strong Connec&vity Greedy Algorithms

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

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

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

Greedy Algorithms. T. M. Murali. January 28, Interval Scheduling Interval Partitioning Minimising Lateness

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

Algorithm Design and Analysis

Copyright 2000, Kevin Wayne 1

3.1 Basic Definitions and Applications

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

Applied Algorithm Design Lecture 3

Copyright 2000, Kevin Wayne 1

Algorithms Dr. Haim Levkowitz

Chapter 16. Greedy Algorithms

Lecture 3: Graphs and flows

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

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

Design and Analysis of Algorithms

3.1 Basic Definitions and Applications

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Greedy Algorithms. Previous Examples: Huffman coding, Minimum Spanning Tree Algorithms

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Greedy algorithms is another useful way for solving optimization problems.

Lecture outline. Graph coloring Examples Applications Algorithms

Algorithm Design and Analysis

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

Finding Strongly Connected Components

CSE 417: Algorithms and Computational Complexity

CS170 Discussion Section 4: 9/18

Design and Analysis of Algorithms

Chapter 16: Greedy Algorithm

Lecture 20 : Trees DRAFT

The Konigsberg Bridge Problem

15.4 Longest common subsequence

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

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

Announcements. HW3 is graded. Average is 81%

Tutorial 6-7. Dynamic Programming and Greedy

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

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

CS4800: Algorithms & Data Jonathan Ullman

Algorithm Design and Analysis

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

16 Greedy Algorithms

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Greedy Algorithms CLRS Laura Toma, csci2200, Bowdoin College

Design and Analysis of Algorithms

CSE 373: Data Structures and Algorithms

Strongly Connected Components

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

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

CSE 417 Network Flows (pt 4) Min Cost Flows

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

22.3 Depth First Search

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms

Graph Algorithms Using Depth First Search

GRAPHS Lecture 17 CS2110 Spring 2014

16.Greedy algorithms

Abstract. A graph G is perfect if for every induced subgraph H of G, the chromatic number of H is equal to the size of the largest clique of H.

Main approach: always make the choice that looks best at the moment.

Lecture 10 Graph algorithms: testing graph properties

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Fundamental Graph Algorithms Part Three

CS261: Problem Set #1

15-451/651: Design & Analysis of Algorithms November 4, 2015 Lecture #18 last changed: November 22, 2015

GRAPHS Lecture 19 CS2110 Spring 2013

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

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

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

Info 2950, Lecture 16

1 Non greedy algorithms (which we should have covered

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

Analysis of Algorithms - Greedy algorithms -

TU/e Algorithms (2IL15) Lecture 2. Algorithms (2IL15) Lecture 2 THE GREEDY METHOD

Matching Theory. Figure 1: Is this graph bipartite?

Minimum Spanning Trees Ch 23 Traversing graphs

We ve done. Now. Next

ESO207A: Data Structures and Algorithms End-semester exam

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

ECE608 - Chapter 16 answers

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

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

W4231: Analysis of Algorithms

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

CSC 373: Algorithm Design and Analysis Lecture 3

CS521 \ Notes for the Final Exam

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16

LECTURES 3 and 4: Flows and Matchings

CSE 417 Branch & Bound (pt 4) Branch & Bound

Transcription:

CSE 421 Applications of DFS(?) Topological sort Yin Tat Lee 1

Precedence Constraints In a directed graph, an edge (i, j) means task i must occur before task j. Applications Course prerequisite: course i must be taken before j Compilation: must compile module i before j Computing overflow: output of job i is part of input to job j Manufacturing or assembly: sand it before paint it 2

Directed Acyclic Graphs (DAG) Def: A directed acyclic graph (DAG) is a graph that contains no directed cycles. Def: A topological order of a directed graph G = (V, E) 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. 2 3 6 5 4 1 2 3 4 5 6 7 7 1 a DAG a topological ordering of that DAG all edges left-to-right 3

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

DAGs: A Sufficient Condition G has a topological order? G is a DAG 5

Every DAG has a source node Lemma: If G is a DAG, then G has a node with no incoming edges (i.e., a source). The proof is similar to tree has n 1 edges. Proof. (by contradiction) Suppose that G is a DAG and it has no source Pick any node v, and begin following edges backward from v. Since v has at least one incoming edge (u, v) we can walk backward to u. Then, since u has at least one incoming edge (x, u), we can walk backward to x. Repeat until we visit a node, say w, twice. Let C be the sequence of nodes encountered between successive visits to w. C is a cycle. C w x u v 6

DAG => Topological Order Lemma: If G is a DAG, then G has a topological order Proof. (by induction on n) Base case: true if n = 1. Hypothesis: Every DAG with n 1 vertices has a topological ordering. Inductive Step: Given DAG with n > 1 nodes, find a source node v. G {v} is a DAG, since deleting v cannot create cycles. Reminder: Always remove vertices/edges to use IH By hypothesis, G {v} has a topological ordering. Place v first in topological ordering; then append nodes of G {v} in topological order. This is valid since v has no incoming edges. 7

A Characterization of DAGs G has a topological order G is a DAG 8

Example 2 3 6 5 4 7 1 9

Example 2 3 6 5 4 1 2 3 4 5 6 7 7 1 Topological order: 1, 2, 3, 4, 5, 6, 7 10

Summary for last few classes Terminology: vertices, edges, paths, connected component, tree, bipartite Vertices vs Edges: m = O(n 2 ) in general, m = n 1 for trees BFS: Layers, queue, shortest paths, all edges go to same or adjacent layer DFS: recursion/stack; all edges ancestor/descendant Algorithms: Connected Comp, bipartiteness, topological sort Techniques: Induction on vertices/layers 11

CSE 421 Interval Scheduling Yin Tat Lee 12

Greedy Algorithms Hard to define exactly but can give general properties Solution is built in small steps Decisions on how to build the solution are made to maximize some criterion without looking to the future Want the best current partial solution as if the current step were the last step May be more than one greedy algorithm using different criteria to solve a given problem 13

Greedy Strategy Goal: Given currency denominations: 1, 5, 10, 25, 100, give change to customer using fewest number of coins. Ex: 34. Cashier's algorithm: At each iteration, give the largest coin valued the amount to be paid. Ex: $2.89. 14

Greedy is not always Optimal Observation: Greedy algorithm is sub-optimal for US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, 1500. Counterexample. 140. Greedy: 100, 34, 1, 1, 1, 1, 1, 1. Optimal: 70, 70. Lesson: Greedy is short-sighted. Always chooses the most attractive choice at the moment. But this may lead to a deadend later. 15

Greedy Algorithms Greedy algorithms Easy to produce Fast running times Work only on certain classes of problems Hard part is showing that they are correct Two methods for proving that greedy algorithms do work Greedy algorithm stays ahead At each step any other algorithm will have a worse value for some criterion that eventually implies optimality Exchange Argument Can transform any other solution to the greedy solution at no loss in quality 16

Interval Scheduling a b c 0 1 2 3 4 5 6 7 8 9 1 0 d e f g h 1 1 Time

Interval Scheduling Job j starts at s(j) and finishes at f(j). Two jobs compatible if they don t overlap. Goal: find maximum subset of mutually compatible jobs. a b c d e f g h 0 1 2 3 4 5 6 7 8 9 10 11 Time 18

Greedy Strategy Sort the jobs in some order. Go over the jobs and take as much as possible provided it is compatible with the jobs already taken. Main question: What order? Does it give the Optimum answer? Why? 19

Possible Approaches for Inter Sched Sort the jobs in some order. Go over the jobs and take as much as possible provided it is compatible with the jobs already taken. [Shortest interval] Consider jobs in ascending order of interval length f j s(j). [Earliest start time] Consider jobs in ascending order of start time s(j). [Earliest finish time] Consider jobs in ascending order of finish time f(j). 20

Greedy Alg: Earliest Finish Time Consider jobs in increasing order of finish time. Take each job provided it s compatible with the ones already taken. Sort jobs by finish times so that f(1) f(2)... f(n). A for j = 1 to n { if (job j compatible with A) A A {j} } return A Implementation. O(nlog n). Remember job j that was added last to A. Job j is compatible with A if s j f(j ). 21

Greedy Alg: Example 0 B A C E D H 1 2 3 4 5 6 7 8 9 10 11 F G Time B A C ED F G H 0 1 2 3 4 5 6 7 8 9 10 11 22

Correctness The output is compatible. (This is by construction.) How to prove it gives maximum number of jobs? Let i 1, i 2, i 3, be jobs picked by greedy (ordered by finish time) Let j 1, j 2, j 3, be an optimal solution (ordered by finish time) How about proving i k = j k for all k? No, there can be multiple optimal solutions. Idea: Prove that greedy outputs the best optimal solution. Given two compatible orders, which is better? The one finish earlier. How to prove greedy gives the best? Induction: it gives the best during every iteration. 23

Correctness Theorem: Greedy algorithm is optimal. Proof: (technique: Greedy stays ahead ) Let i 1, i 2, i 3,, i k be jobs picked by greedy, j 1, j 2, j 3,, j m those in some optimal solution in order. We show f(i r ) f(j r ) for all r, by induction on r. Base Case: i 1 chosen to have min finish time, so f(i 1 ) f(j 1 ). IH: f(i r ) f j r for some r IS: Since f i r f j r s(j r+1 ), j r+1 is among the candidates considered by greedy when it picked i r+1, & it picks min finish, so f i r+1 f(j r+1 ) Observe that we must have k m, else j k+1 is among (nonempty) set of candidates for i k+1. 24

Interval Partitioning Technique: Structural

Interval Partitioning Lecture j starts at s(j) and finishes at f(j). Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Room 4 e j Room 3 c d g Room 2 b h Room 1 a f i 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 26

Interval Partitioning Note: graph coloring is very hard in general, but graphs corresponding to interval intersections are simpler. Room 4 Room 3 Room 2 Room 1 C A B D E G F H J I e j c d g b h a f i 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 27

A Better Schedule This one uses only 3 classrooms c d f j b g i a e h 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 28

A Greedy Algorithm Greedy algorithm: Consider lectures in increasing order of finish time: assign lecture to any compatible classroom. Sort intervals by finish time so that f 1 f 2... f n. d 0 for j = 1 to n { if (lect j is compatible with some classroom k, 1 k d) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d d + 1 } Correctness: This is wrong! 29

Example a b c 0 1 2 3 4 5 6 d Time Greedy by finish time gives: a c b a b OPT: d c 0 1 2 3 4 5 6 d Time 0 1 2 3 4 5 6 Time 30

A Greedy Algorithm Greedy algorithm: Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Sort intervals by starting time so that s 1 s 2... s n. d 0 for j = 1 to n { if (lect j is compatible with some classroom k, 1 k d) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d d + 1 } Implementation: Exercise! 31

A Structural Lower-Bound on OPT Def. The depth of a set of open intervals is the maximum number that contains any given time. Key observation. Number of classrooms needed depth. Ex: Depth of schedule below = 3 schedule below is optimal. Q. Does there always exist a schedule equal to depth of intervals? c d f j b g i a e h 9 9:30 10 10:30 11 11:30 12 12:30 1 1:30 2 2:30 3 3:30 4 4:30 Time 32

Correctness Observation: Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem: Greedy algorithm is optimal. Proof (exploit structural property). Let d = number of classrooms that the greedy algorithm allocates. Classroom d is opened because we needed to schedule a job, say j, that is incompatible with all d 1 previously used classrooms. Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than s(j). Thus, we have d lectures overlapping at time s j + ε, i.e. depth d OPT Observation all schedules use depth classrooms, so d = depth and greedy is optimal 33