Lecture 6: Topological Ordering and Greedy Algorithms in Graph Theory, Study notes of Computer Science

A set of notes from a university lecture on algorithm design and analysis, specifically covering topological ordering and greedy algorithms in the context of directed graphs. The lecture covers the concept of a topological ordering of a directed graph, the lemma that a directed graph with a topological ordering is a directed acyclic graph (dag), and the algorithm for finding a topological ordering. The lecture also discusses the concept of greedy algorithms and provides examples of their application in the interval scheduling problem. Several diagrams and explanations of algorithms and lemmas.

Typology: Study notes

Pre 2010

Uploaded on 09/24/2009

koofers-user-yg0
koofers-user-yg0 🇺🇸

5

(1)

10 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Adam Smith
Algorithm Design and Analysis
LECTURE 6
Topological ordering
Greedy Algorithms
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Lecture 6: Topological Ordering and Greedy Algorithms in Graph Theory and more Study notes Computer Science in PDF only on Docsity!

9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Adam Smith

Algorithm Design and Analysis LECTURE 6

  • Topological ordering
  • Greedy Algorithms

Notes

  • Read on your own
    • Graph bipartiteness
    • DFS implementation
  • Homework notation:
    • log a

(n) = (log n)

a

  • Useful property: 9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

!x" = smallest integer ≥ x

= “ceiling”

x ≤ "x# < x + 1

Strong Connectivity: Algorithm

Lemma : G is strongly connected if and only if for any node s , every other node t has paths to and from s. Theorem. Can determine if G is strongly connected in O(m + n) time. Pf.  Pick any node s.  Run BFS from s in G.  Run BFS from s in Grev.  Return true iff all nodes reached in both BFS executions.  Correctness follows immediately from the previous lemma. reverse orientation of every edge in G strongly connected not strongly connected

Directed Acyclic Graphs

Def. An DAG is a directed graph that contains no directed cycles. Ex. Precedence constraints: edge (vi, vj) means vi must precede vj. Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v 1 , v 2 , …, vn so that for every edge (vi, vj) we have i < j. a DAG a topological ordering v 2 v 3 v 6 v 5 v 4 v 7 v 1 v 1 v 2 v 3 v 4 v 5 v 6 v 7

Directed Acyclic Graphs

Lemma. If G has a topological order, then G is a DAG. Pf. (by contradiction)  Suppose that G has a topological order v 1 , …, vn and that G also has a directed cycle C. Let's see what happens.  Let vi be the lowest-indexed node in C, and let vj be the node just before vi; thus (vj, vi) is an edge.  By our choice of i, we have i < j.  On the other hand, since (vj, vi) is an edge and v 1 , …, vn is a topological order, we must have j < i, a contradiction. ▪ v 1 vi vj vn the supposed topological order: v 1 , …, vn the directed cycle C

Directed Acyclic Graphs

Lemma. If G has a topological order, then G is a DAG. Q. Does every DAG have a topological ordering? Q. If so, how do we compute one?

Directed Acyclic Graphs

Lemma. If G is a DAG, then G has a topological ordering. Pf. (by induction on n)  Base case: true if n = 1.  Given DAG on n > 1 nodes, find a node v with no incoming edges.  G - { v } is a DAG, since deleting v cannot create cycles.  By inductive 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. ▪ DAG v

Topological Sorting Algorithm: Running Time

Theorem. Algorithm finds a topological order in O(m + n) time. Pf.  Maintain the following information:

  • count[w] = remaining number of incoming edges
  • S = set of remaining nodes with no incoming edges  Initialization: O(m + n) via single scan through graph.  Update: to delete v
  • remove v from S
  • decrement count[w] for all edges from v to w, and add w to S if c count[w] hits 0
  • this is O(1) per edge ▪ Alternative algorithm: Modification of DFS (exercise?)

9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Greedy Algorithms

  • Build up a solution to an optimization problem at

each step shortsightedly choosing the option that

currently seems the best.

  • Sometimes good
  • Often does not work

9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Interval Scheduling Problem

  • Job j starts at s j

and finishes at f

j

  • Two jobs are compatible if they do not overlap.
  • Find: maximum subset of mutually compatible jobs. 0 1 2 3 4 5 6 7 8 9 10 11 f g h e a b c d Time

9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Greedy: Counterexamples

for earliest start time
for shortest interval
for fewest conflicts

Next lecture

  • We will see that adding jobs greedily in order of

earliest finishing time gives an optimal solution

9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne