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

cs 401
SMART_READER_LITE
LIVE PREVIEW

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 !


slide-1
SLIDE 1

CS 401

Greedy Algorithms

Xiaorui Sun

1

slide-2
SLIDE 2

Directed Acyclic Graphs (DAG)

Def: A DAG is a directed acyclic graph, i.e.,

  • ne that contains no directed cycles.

Def: A topological order of a directed graph G = (V, E) is an

  • rdering of its nodes as !", !$, … , !& so that for every edge

(!(, !)) we have + < -.

2

a DAG

2 3 6 5 4 7 1

a topological ordering of that DAG– all edges left-to-right

1 2 3 4 5 6 7

slide-3
SLIDE 3

DAGs: A Sufficient Condition

3

G has a topological order G is a DAG

?

slide-4
SLIDE 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.

4

w x u v

C

w x u v

slide-5
SLIDE 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. 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

Reminder: Always remove vertices/edges to use hypothesis

slide-6
SLIDE 6

A Characterization of DAGs

6

G has a topological order G is a DAG

slide-7
SLIDE 7

7

Topological Order Algorithm 1: Example

2 3 6 5 4 7 1

slide-8
SLIDE 8

8

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

Topological Order Algorithm 1: Example

2 3 6 5 4 7 1 1 2 3 4 5 6 7

Running time: O(n+m)

  • Adjacency list
  • Maintain # outgoing edge for each node
slide-9
SLIDE 9

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

slide-10
SLIDE 10

Greedy Algorithms

slide-11
SLIDE 11

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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

Interval Scheduling

Time 1 2 3 4 5 6 7 8 9 1 1 1

f g h e a b c d h e b

slide-16
SLIDE 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.

16

Time 1 2 3 4 5 6 7 8 9 10 11

f g h e a b c d h e b

slide-17
SLIDE 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