analysis of connectivity in directed graph algorithms ... · fang song texas a&m u fall’19...

13
Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 Fang Song Texas A&M U Connectivity in directed graph Topological sort Credit: based on slides by A. Smith & K. Wayne

Upload: others

Post on 21-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Fang SongTexas A&M U

Fall’19 CSCE 629

Analysis of Algorithms

W, 09/18/19

Lecture 8

Fang SongTexas A&M U

• Connectivity in directed graph• Topological sort

Credit: based on slides by A. Smith & K. Wayne

Page 2: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Directed graphs

§! = ($, &)• Edge ( → * leaves node ( and enters node *• Adjacency matrix (asymmetric)• Adjacency list: track out-going edges (or two for in and out)

1

1

2 4

5

6

3 7

!

Directed graph Node Directed edgetransportation Street intersection One-way street

web webpage hyperlink

scheduling task prereq

cell phone person call

citation article Citation

§ Some examples … ,-./01 2 = 3 , ,-.45 2 = {1,4}

Page 3: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Connectivity in directed graphs

§Directed reachability. Find all nodes reachable from a node s.• BFS/DFS applies naturally• ! ↝ #: there is a path from s to t. Need not be # ↝ !

2

§Application: web crawler. • Start from web page !. Find all web pages linked from s,

either directly or indirectly.

1

2 45

6

3 7

$ 1

2 54

%&' 1

2 54

3 6

1

2⋯

)&'1

2 54

3 6

Page 4: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Strong connectivity

§Def. ! and " are mutually reachable (! ↭ ") if ! ↝ " & " ↝ !.

3

§Def. A graph is strongly connected if every pair of nodes is mutually reachable. Lemma. Let % be any node. & is strongly connected iff. every node is reachable from %, and % is reachable from every node.

§Obs. If ! ↭ " and " ↭ ', then ! ↭ '.

Proof. [Show both “if” and “only if”] • ⇒ (only if) Follows from definition of “strongly connected”. • ⇐ (if) for any two nodes !, ":

! ↝ " by following ! ↝ % then % ↝ "" ↝ ! by following " ↝ % then % ↝ !

%

!

"

Page 5: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Testing strong connectivity

Proof (construction of an algorithm; fill in the analysis on your own)

4

Theorem. There is an !(# + %) time algorithm that determines if 'is strongly connected.

1. Pick any node s2. Run BFS from s on '3. Run BFS from s on ()*+4. Return true if all nodes

reached in both BFS runs

Strongly connected

12 4

3

',-. : reverse orientation of every edge in '

'1

2 4

3

Page 6: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Strong components

§Def. A strong component is a maximalsubset of mutually reachable nodes.

5

Theorem. There is an !(# + %) time algorithm that finds all strong components.

§Obs. For any two nodes ' and ( in a directed graph, their strong components are either identical or disjoint.

Page 7: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Directed acyclic graphs (DAG)

§Def. A DAG is a directed graph that contains no directed cycles.

6

§Application: precedence constrains. • Course prerequisite: course ! must be taken before "• Compilation: module ! must be compiled before "• Pipelein of computing jobs: output of job ! determins input of job "

Page 8: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Topological order

7

§Def. A topological order of a directed graph is an ordering of its nodes as !", … , !%, so that for every edge !& → !( we have ) < +.

12 4

56

3 7

!,!- !.

!/!0

!1 !"

!, !-!.!/ !0 !1!"

A topological order

all edges go from left to right

Page 9: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

8

1. Does every DAG have a topological order?

2. If ! has a topological order, is ! necessarily a DAG?

Page 10: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

9

2. If ! has a topological order, is ! necessarily a DAG?

Lemma2. If " has a topological order, then " is a DAG.

Proof. [by contradiction]• Suppose " has a topological order #$,… , #'; and " also has a directed cycle (. • Let #) be the lowest-indexed node in (, #* be the node right before #) (in ();

thus #* → #) is an edge.• By our choice , < ..• But #$,… , #' a topological order; if #* → #) an edge, then . < ,. Contradiction!

#) #* #'#$Directed cycle (

Page 11: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

10

1. Does every DAG have a topological order?Lemma1. A DAG ! has a node with no entering edges.

Proof (of corollary) given Lemma1. [by induction]• Base case: true if " = 1.• Given DAG on " > 1 nodes, find a node & with no entering edges [Lemma1]• ! − {&} is a DAG, since deleting & cannot create cycles. • By inductive hypothesis, ! − {&} has a topological ordering.• Place & first; then append nodes of ! − & in topological order. [valid because & has no entering edges]

Corollary. If ! is a DAG, then ! has a topological order.

&

DAG

Page 12: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

Topological sorting algorithm

Theorem. TopSort computs a topological order in !(# +%) time

11

TopSort(')// ()*#+(,) = remaining number of incoming edges// . = set of remaining nodes with no incoming edges// V[1,…,n] topological order1. Initialize . and ()*#+(⋅) for all nodes2. For 0 ∈ S

Append v to VFor all , with 0 → , // delete v from G

()*#+ , − −If ()*#+ , == 0, add , to .

! 1 per edge

!(# +%) a single scan of adjacency list

Page 13: Analysis of Connectivity in directed graph Algorithms ... · Fang Song Texas A&M U Fall’19 CSCE 629 Analysis of Algorithms W, 09/18/19 Lecture 8 •Connectivity in directed graph

!

Completing the argument

12

Lemma1. A DAG " has a node with no entering edges.

Proof. [by contradiction]• Suppose " is a DAG, and every node has at least one entering edge. • Pick any node #, and follow edges backward from #. Repeat till we visit a node,

say $ twice. (# ← & ← '⋯ ← $⋯ ← $)• Let ! be the sequence of nodes between successive visits to w.à ! is a cycle! Contradiction!

&'$ # ⋯⋯