Objec&ves Directed Graphs Topological Orderings of DAGs Feb 5, - - PDF document

objec ves
SMART_READER_LITE
LIVE PREVIEW

Objec&ves Directed Graphs Topological Orderings of DAGs Feb 5, - - PDF document

2/5/18 Objec&ves Directed Graphs Topological Orderings of DAGs Feb 5, 2018 CSCI211 - Sprenkle 1 Graph Summary So Far What do we know about graphs? Feb 5, 2018 CSCI211 - Sprenkle 2 1 2/5/18 Graph Summary So Far What do


slide-1
SLIDE 1

2/5/18 1

Objec&ves

  • Directed Graphs
  • Topological Orderings of DAGs

Feb 5, 2018 1 CSCI211 - Sprenkle

Graph Summary So Far

  • What do we know about graphs?

Feb 5, 2018 CSCI211 - Sprenkle 2

slide-2
SLIDE 2

2/5/18 2

Graph Summary So Far

  • What do we know about graphs?

Ø Representa&on: Adjacency List, Space O(n+m) Ø Connec&vity

  • BFS, DFS – O(n+m)
  • Can apply BFS for Bipar&te

Feb 5, 2018 CSCI211 - Sprenkle 3

DIRECTED GRAPHS

Second verse, similar to the first. But directed!

4 Feb 5, 2018 CSCI211 - Sprenkle

slide-3
SLIDE 3

2/5/18 3

Directed Graphs G = (V, E)

  • Edge (u, v) goes from node u to node v
  • Example: Web graph - hyperlink points from one

web page to another

Ø Directedness of graph is crucial Ø Modern web search engines exploit hyperlink structure to rank web pages by importance

Feb 5, 2018 CSCI211 - Sprenkle 5

Represen&ng Directed Graphs

  • For each node, keep track of

Ø Out edges (where links go) Ø In edges (from where links come in) Ø Space required?

  • Could only store out edges

Ø Figure out in edges with increased computa&on/&me Ø Useful to have both in and out edges

Feb 5, 2018 CSCI211 - Sprenkle 6

slide-4
SLIDE 4

2/5/18 4

Rock Paper Scissors Lizard Spock

Feb 5, 2018 CSCI211 - Sprenkle 7

CONNECTIVITY IN DIRECTED GRAPHS

8 Feb 5, 2018 CSCI211 - Sprenkle

slide-5
SLIDE 5

2/5/18 5

Graph Search

  • How does reachability change with

directed graphs?

  • Example: Web crawler
  • 1. Start from web page s.
  • 2. Find all web pages linked from s, either directly or

indirectly.

Feb 5, 2018 CSCI211 - Sprenkle 9

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

Graph Search

  • Directed reachability. Given a node s, find all

nodes reachable from s.

  • Directed s-t shortest path problem. Given two

nodes s and t, what is the length of the shortest path between s and t?

Ø Not necessarily the same as tàs shortest path

  • Graph search. BFS and DFS extend naturally to

directed graphs

Ø Trace through out edges Ø Run in O(m+n) &me

Feb 5, 2018 CSCI211 - Sprenkle 10

1 2 5 4 7 3 6

slide-6
SLIDE 6

2/5/18 6

Problem

  • Find all nodes with paths to s

Ø Rather than paths from s to other nodes

Feb 5, 2018 CSCI211 - Sprenkle 11

Problem/Solu&on

  • Problem. Find all nodes with paths to s
  • Solu&on. Run BFS on in edges instead of out

edges

Feb 5, 2018 CSCI211 - Sprenkle 12

slide-7
SLIDE 7

2/5/18 7

DAGS AND TOPOLOGICAL ORDERING

13 Feb 5, 2018 CSCI211 - Sprenkle

Directed Acyclic Graphs

  • Def. A DAG is a directed graph that contains

no directed cycles.

  • Example. Precedence constraints:

edge (vi, vj) means vi must precede vj

Ø Course prerequisite graph: course vi must be taken before vj Ø Compila&on: module vi must be compiled before vj Ø Pipeline of compu&ng jobs: output of job vi needed to determine input of job vj

Feb 5, 2018 CSCI211 - Sprenkle 14

v2 v3 v6 v5 v4 v7 v1

a DAG:

slide-8
SLIDE 8

2/5/18 8

Problem: Valid Ordering

  • Given a set of tasks with dependencies,

what is a valid order in which the tasks could be performed?

Feb 5, 2018 CSCI211 - Sprenkle 15

v2 v3 v6 v5 v4 v7 v1

Topological Ordering

  • Problem: Given a set of tasks with dependencies,

what is a valid order in which the tasks could be performed?

  • Def. A topological order of a directed graph

G = (V, E) is an ordering of its nodes as v1, v2, …, vn such that for every directed edge (vi, vj), i < j.

Feb 5, 2018 CSCI211 - Sprenkle 16

a DAG a topological ordering All edges point “forward”

v2 v3 v6 v5 v4 v7 v1 v1 v2 v3 v4 v5 v6 v7

Coordinating labeling of nodes, but numbering is not known for just DAG

slide-9
SLIDE 9

2/5/18 9

Topological Ordering Example

  • Given a set of tasks with dependencies,

what is a valid order in which the tasks could be performed?

Ø Example: Course prerequisites

  • Values of the nodes vs. their ids
  • A topological order of a directed graph

G = (V, E) is an ordering of its nodes as v1, v2, …, vn such that for every directed edge (vi, vj), i < j.

Feb 5, 2018 CSCI211 - Sprenkle 17

Towards a Solu&on

  • Start by showing that if G has a topological order,

then G is a DAG

  • Eventually, we’ll show the other direc&on:

if G is a DAG, then G has a topological order

Feb 5, 2018 CSCI211 - Sprenkle 18

slide-10
SLIDE 10

2/5/18 10

Directed Acyclic Graphs

  • Lemma. If G has a topological order,

then G is a DAG.

  • Proof plan: Try to show that G has a topological
  • rder even though G has a cycle

Feb 5, 2018 CSCI211 - Sprenkle 19

v1 vi vj vn

the supposed topological order: v1, …, vn the directed cycle C

Why isn’t this valid?

DAGs & Topological Orderings

  • Lemma. If G has a topological order, then G is a DAG.
  • Pf. (by contradic&on)

Ø Suppose that G has a topological order v1, …, vn and that G also has a directed cycle C.

Feb 5, 2018 CSCI211 - Sprenkle 20

v1 vi vj vn

the directed cycle C the supposed topological order: v1, …, vn

What can we say about that cycle and the nodes, edges in the cycle?

slide-11
SLIDE 11

2/5/18 11

DAGs & Topological Orderings

  • Lemma. If G has a topological order, then G is a DAG.
  • Pf. (by contradic&on)

Ø Suppose that G has a topological order v1, …, vn and that G also has a directed cycle C. Ø Let vi be the lowest-indexed node in C, and let vj be the node on C just before vi; thus (vj, vi) is an edge Ø By our choice of i (lowest-indexed node), i < j Ø Since (vj, vi) is an edge and v1, …, vn is a topological order, we must have j < i

  • a contradic&on. ▪

Feb 5, 2018 CSCI211 - Sprenkle 21

v1 vi vj vn

the directed cycle C the supposed topological order: v1, …, vn

Directed Acyclic Graphs

  • Does every DAG have a topological ordering?

Ø If so, how do we compute one?

Feb 5, 2018 CSCI211 - Sprenkle 22

slide-12
SLIDE 12

2/5/18 12

Directed Acyclic Graphs

  • Does every DAG have a topological ordering?

Ø If so, how do we compute one?

  • What do we need to be able to create a

topological ordering?

Ø What are some characteris&cs of this graph?

Feb 5, 2018 CSCI211 - Sprenkle 23

v1 v2 v3 v4 v5 v6 v7

Directed Acyclic Graphs

  • Does every DAG have a topological ordering?

Ø If so, how do we compute one?

  • What do we need to be able to create a

topological ordering?

Ø What are some characteris&cs of this graph?

Feb 5, 2018 CSCI211 - Sprenkle 24

v1 v2 v3 v4 v5 v6 v7

Need a place to start: a node with no incoming edges (no dependencies) Note that both v1 and v2 have no incoming edges

slide-13
SLIDE 13

2/5/18 13

Towards a Topological Ordering

Feb 5, 2018 CSCI211 - Sprenkle 25

Do we know there is always a node with no incoming edges? Goal: Find an algorithm for finding the TO Idea: 1st node is one with no incoming edges

Towards a Topological Ordering

  • Lemma. If G is a DAG,

then G has a node with no incoming edges

Ø This is our star&ng point of the topological ordering

Feb 5, 2018 CSCI211 - Sprenkle 26

How to prove?

slide-14
SLIDE 14

2/5/18 14

Towards a Topological Ordering

  • Lemma. If G is a DAG,

then G has a node with no incoming edges

  • Proof idea: Consider if there is no node without

incoming edges

Ø Restated: All nodes have incoming edges. Ø What contradic&on are we looking for?

Feb 5, 2018 CSCI211 - Sprenkle 27

Towards a Topological Ordering

  • Lemma. If G is a DAG,

then G has a node with no incoming edges.

  • Pf. (by contradic&on)

Ø Suppose that G is a DAG and every node has at least one incoming edge Ø Pick any node v, and follow edges backward from v.

  • Since v has at least one incoming edge (u, v), we can walk backward to u

Ø Since u has at least one incoming edge (t, u), we can walk backward to t Ø Repeat un&l we visit a node, say w, twice

  • Has to happen at least by step n+1 (Why?)

Ø Let C denote the sequence of nodes encountered between successive visits to w. C is a cycle, which is a contradic&on to G is a DAG ▪

Feb 5, 2018 CSCI211 - Sprenkle 28

w t u v

slide-15
SLIDE 15

2/5/18 15

Punng it all together: Crea&ng a topological order

Feb 5, 2018 CSCI211 - Sprenkle 29

Ideas?

Topological Ordering Algorithm

Feb 5, 2018 CSCI211 - Sprenkle 30

30

Find a node v with no incoming edges Order v first Delete v from G Recursively compute a topological ordering of G-{v} and append this order after v

How do we know this works?

slide-16
SLIDE 16

2/5/18 16

Directed Acyclic Graphs

  • Lemma. If G is a DAG, then G has a topological
  • rdering.
  • Pf. (by induc&on on n)

Ø Base case:

Feb 5, 2018 CSCI211 - Sprenkle 31

v

DAG

Directed Acyclic Graphs

  • Lemma. If G is a DAG, then G has a topological
  • rdering.
  • Pf. (by induc&on on n)

Ø Base case: true if n = 1 Ø Given DAG on n > 1 nodes, find a node v with no incoming edges

Feb 5, 2018 CSCI211 - Sprenkle 32

DAG

v

slide-17
SLIDE 17

2/5/18 17

Directed Acyclic Graphs

  • Lemma. If G is a DAG, then G has a topological
  • rdering.
  • Pf. (by induc&on 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 because dele&ng v cannot create cycles Ø Also know, by induc&ve hypothesis, G - { v } has a topological ordering Ø Place v first in topological ordering Ø Append nodes of G - { v } in topological order.

  • valid since v has no incoming edges. ▪

Feb 5, 2018 CSCI211 - Sprenkle 33

DAG DAG

v

Topological Ordering Algorithm

  • Lemma. If G is a DAG,

then G has a topological ordering.

  • Algorithm:

Feb 5, 2018 CSCI211 - Sprenkle 34

Find a node v with no incoming edges Order v first Delete v from G Recursively compute a topological ordering of G-{v} and append this order after v

slide-18
SLIDE 18

2/5/18 18

Topological Ordering Algorithm: Example

35

v1

Topological order:

v2 v3 v6 v5 v4 v7 v1

Feb 5, 2018 CSCI211 - Sprenkle

Topological Ordering Algorithm: Example

36

v2

Topological order: v1

v2 v3 v6 v5 v4 v7

Feb 5, 2018 CSCI211 - Sprenkle

slide-19
SLIDE 19

2/5/18 19

Topological Ordering Algorithm: Example

37

v3

Topological order: v1, v2

v3 v6 v5 v4 v7

Feb 5, 2018 CSCI211 - Sprenkle

Topological Ordering Algorithm: Example

38

v4

Topological order: v1, v2, v3

v6 v5 v4 v7

Feb 5, 2018 CSCI211 - Sprenkle

slide-20
SLIDE 20

2/5/18 20

Topological Ordering Algorithm: Example

39

v5

Topological order: v1, v2, v3, v4

v6 v5 v7

Feb 5, 2018 CSCI211 - Sprenkle

Topological Ordering Algorithm: Example

40

v6

Topological order: v1, v2, v3, v4, v5

v6 v7

Feb 5, 2018 CSCI211 - Sprenkle

slide-21
SLIDE 21

2/5/18 21

Topological Ordering Algorithm: Example

41

v7

Topological order: v1, v2, v3, v4, v5, v6

v7

Feb 5, 2018 CSCI211 - Sprenkle

Topological Ordering Algorithm: Example

42

Topological order: v1, v2, v3, v4, v5, v6, v7.

v2 v3 v6 v5 v4 v7 v1 v1 v2 v3 v4 v5 v6 v7 Feb 5, 2018 CSCI211 - Sprenkle

slide-22
SLIDE 22

2/5/18 22

Topological Order Run&me

  • Where are the costs?
  • How would we implement?

Feb 5, 2018 CSCI211 - Sprenkle 43

Find a node v with no incoming edges Order v first Delete v from G Recursively compute a topological ordering of G-{v} and append this order after v

Topological Order Run&me

  • Find a node without incoming edges and delete

it: O(n)

  • Repeat on all nodes

à O(n2)

Feb 5, 2018 CSCI211 - Sprenkle 44

Can we do better?

Find a node v with no incoming edges Order v first Delete v from G Recursively compute a topological ordering of G-{v} and append this order after v

O(n) O(n) O(n) O(1) O(1)

slide-23
SLIDE 23

2/5/18 23

Topological Sor&ng Algorithm: Running Time

  • Theorem. Find a topological order in O(m + n)

&me

  • Pf.

Ø Maintain the following informa&on:

  • count[w] = remaining number of incoming edges
  • S = set of remaining nodes with no incoming edges

Ø Ini&aliza&on: O(m + n) via single scan through graph Ø Algorithm:

  • Select a node v from S, remove v from S
  • Decrement count[w] for all edges from v to w

Ø Add w to S if count[w] = 0

Feb 5, 2018 CSCI211 - Sprenkle 45

Looking Ahead

  • Wiki due Tuesday at 11:59 p.m.

Ø Sec&ons 3.2-3.6

  • Problem Set 4 due Friday

Feb 5, 2018 CSCI211 - Sprenkle 46