cs 401
play

CS 401 Greedy Algorithms Xiaorui Sun 1 Directed Acyclic Graphs - PowerPoint PPT Presentation

CS 401 Greedy Algorithms Xiaorui Sun 1 Directed Acyclic Graphs (DAG) Def: A DAG is a directed acyclic graph, i.e., one that contains no directed cycles. Def: A topological order of a directed graph G = (V, E) is an ordering of its nodes as !


  1. CS 401 Greedy Algorithms Xiaorui Sun 1

  2. Directed Acyclic Graphs (DAG) Def: A DAG is a directed acyclic graph, i.e., one that contains no directed cycles. Def: A topological order of a directed graph G = (V, E) is an ordering of its nodes as ! " , ! $ , … , ! & so that for every edge (! ( , ! ) ) we have + < - . 2 3 6 5 4 1 2 3 4 5 6 7 7 1 a topological ordering of that DAG– a DAG all edges left-to-right 2

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

  4. Every DAG has a source node Lemma: If ! is a DAG, then ! has a node with no incoming edges (i.e., a source). Proof. (by contradiction) Suppose that ! is a DAG and it has no source Pick any node " , and begin following edges backward from " . Since " has at least one incoming edge ($, ") we can walk backward to $ . Then, since $ has at least one incoming edge (', $) , we can walk backward to ' . 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. w x u v w x u v C 4

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

  6. A Characterization of DAGs G has a G is a DAG topological order 6

  7. Topological Order Algorithm 1: Example 2 3 6 5 4 7 1 7

  8. Topological Order Algorithm 1: Example 2 3 6 5 4 1 2 3 4 5 6 7 7 1 Topological order: 1, 2, 3, 4, 5, 6, 7 Running time: O(n+m) • Adjacency list • Maintain # outgoing edge for each node 8

  9. Graph Algorithm Summary Definition of graph: directed, undirected Terminology: path, cycle, tree, degree, connected component Graph traversal: • BFS: Order nodes in successive layers based on distance from ! • DFS: More natural approach for exploring a maze; Topological order: order vertices according to precedence constraints 9

  10. Greedy Algorithms

  11. 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 11

  12. 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. 12

  13. 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 dead- end later. 13

  14. 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 Structural: Come up with a lower bound Exchange Argument • Can transform any other solution to the greedy solution at no loss in quality 14

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

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

  17. Greedy Strategy Sort the jobs in some order. Go over the jobs and take jobs that are compatible with the previous jobs already taken. Main question: • What order? • Does it give the Optimum answer? • Why? 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend