cs3000:&algorithms&&&data jonathanullman ·...

43
CS3000: Algorithms & Data Jonathan Ullman Lecture 9: Graphs Graph Traversals: DFS Topological Sort Feb 5, 2020

Upload: others

Post on 16-Oct-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

CS3000:&Algorithms&&&DataJonathan&Ullman

Lecture&9:&• Graphs• Graph&Traversals:&DFS• Topological&Sort

Feb&5,&2020

Page 2: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

What’s&Next

Page 3: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

What’s&Next

• Graph&Algorithms:• Graphs: Key&Definitions,&Properties,&Representations• Exploring&Graphs: Breadth/Depth&First&Search

• Applications:&Connectivity,&Bipartiteness,&Topological&Sorting• Shortest&Paths:

• Dijkstra• BellmanSFord&(Dynamic&Programming)

• Minimum&Spanning&Trees:• Borůvka,&Prim,&Kruskal

• Network&Flow:• Algorithms• Reductions&to&Network&Flow

Page 4: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Graphs

Page 5: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Graphs:&Key&Definitions

• Definition:&A&directed&graph ! = #,%• # is&the&set&of&nodes/vertices• % ⊆ #×# is&the&set&of&edges• An&edge&is&an&ordered&( = ), * “from&) to&*”

• Definition: An&undirected&graph ! = #,%• Edges&are&unordered&( = ), * “between&) and&*”

• Simple&Graph:• No&duplicate&edges• No&selfSloops&( = ), )

IsTo

Page 6: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

• How&many&edges&can&there&be&in&a&simpledirected/undirected graph?

n Iv I of nodes

m IE l of edges

Directed 0 E m e n n l

Graph

worked 0 e m en

LGraph

m 0hr7

Page 7: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Graphs&Are&Everywhere

• Transportation&networks• Communication&networks• WWW• Biological&networks• Citation&networks• Social&networks• …

Page 8: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Paths/Connectivity

• A&path is&a&sequence&of&consecutive&edges&in&%• + = ),,- , ,-, ,. , ,., ,/ , … , ,12-,*• + = ) − ,- − ,. − ,/ −⋯− ,12- − *• The&length of&the&path&is&the&#&of&edges

• An&undirected graph&is&connected if&for&every&two&vertices&), * ∈ #,&there&is&a&path&from&) to&*• A&directed graph&is&strongly&connected if&for&every&two&vertices&), * ∈ #,&there&are&paths&from&) to&*and&from&* to&)

s w

Kwok

Page 9: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Cycles

• A&cycle is&a&path&*- − *. −⋯− *1 − *- and&*-, … , *1 are&distinct

ten

D

Page 10: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

• Suppose&an&undirected&graph&! is&connected• True/False?&&! has&at&least&6 − 1 edges

O_0 O

d d to

Typically be assumegraphs are

connected

The m Sun

Page 11: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

• Suppose&an&undirected&graph&! has&6 − 1 edges• True/False?&&! is&connected

simpleA

Is a

n l edges not connected

Page 12: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Trees

• A&simple&undirected&graph&! is&a&tree if:• ! is&connected• ! contains&no&cycles

• Theorem: any&two&of&the&following&implies&the&third• ! is&connected• ! contains&no&cycles• ! has&= 6 − 1 edges

Page 13: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Trees

• Rooted&tree:&choose&a&root&node&8 and&orient&edges&away&from&8• Models&hierarchical&structure

O

l

Page 14: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Phylogeny&Trees

Page 15: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Parse&Trees

if (A[x]==2) then (322 + (a*64 +12)/8)

else fibonacci(n)

if@then@else

==

array&ref 2

A x

power

32 2

+

/

+

12*

a 64

8

fn@call

fibonacci n

Page 16: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Representing&a&Graph

Page 17: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Adjacency&Matrices

• The&adjacency&matrix of&a&graph&! = #,% with&6nodes&is&the&matrix&9 1:6;, 1:6 where

9 <, = = ; >1;;;;; <, = ∈ %;0;;;;; <, = ∉ %

A 1 2 3 41 0 1 1 02 0 0 1 03 0 0 0 04 0 0 1 0

CostSpace:&Θ #.

Lookup:&Θ 1 timeList&Neighbors:&Θ # time

2 1

3 4

na

m

momG n

Page 18: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Adjacency&Lists&(Undirected)

• The&adjacency&list of&a&vertex&* ∈ # is&the&list&9[*]of&all&) s.t.& *, ) ∈ %

2 1

3 4

9 1 = 2,39 2 = 1,39 3 = 1,2,49 4 = 3

A

Space ntm

Lookup 0 It deglii j

list Neighbors Olltdeglit

Page 19: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Given a graphG YE and a node u c V

the degree of v is the of neighbors

degocu

degv

vIt deg

v Fa I t Feu degli

n t m

Page 20: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020
Page 21: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Adjacency&Lists&(Directed)

• The&adjacency&list of&a&vertex&* ∈ # are&the&lists• 9GHI[*] of&all&) s.t.& *, ) ∈ %• 9JK[*] of&all&) s.t.& ), * ∈ %

2 1

3 4

9GHI 1 = 2,39GHI 2 = 39GHI 3 = ;9GHI 4 = 3

9JK 1 = ;9JK 2 = 19JK 3 = 1,2,49JK 4 = ;

such that

O

Page 22: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Exploring&a&Graph

Page 23: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Exploring&a&Graph

• Problem:&Is&there&a&path&from&L to&M?• Idea: Explore&all&nodes&reachable&from&L.

• Two&different&search&techniques:• Breadth@First&Search: explore&nearby&nodes&before&moving&on&to&farther&away&nodes

• Depth@First&Search: follow&a&path&until&you&get&stuck,&then&go&back

Page 24: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Exploring&a&Graph

• BFS/DFS are&general&templates&for&graph&algorithms• Extensions&of&Breadth@First&Search:

• 2SColoring&(Bipartiteness)• Shortest&Paths• Minimum&Spanning&Tree&(Prim’s&Algorithm)

• Extensions&of&Depth@First&Search:• Topological&Sorting

Page 25: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

DepthSFirst&Search&(DFS)

Page 26: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

DepthSFirst&Search

G = (V,E) is a graphexplored[u] = 0 �u

DFS(u):explored[u] = 1

for ((u,v) in E):if (explored[v]=0):parent[v] = uDFS(v)

u c

a b

n

mi

Explores every node thatis connected to u

The parent pomees forms a tree no cycles

for every v reachablefrom u is a unique u v

path formed by the red edges

Page 27: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

DepthSFirst&Search

G = (V,E) is a graphexplored[u] = 0 �u

DFS(u):explored[u] = 1

for ((u,v) in E):if (explored[v]=0):parent[v] = uDFS(v)

u c

a b

RunningT.me

a

t.li

For everynode vEV we make one call to DFS u

If we call DFS v ther the time to execute the call

is 0C It degv Time is EZolitdegh

O ntm

Page 28: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

DepthSFirst&Search

u c

a b

• Fact: The&parentSchild&edges&form&a&(directed)&tree• Each&edge&has&a&type:• Tree&edges:&(), O), (), Q), (Q, R)

• These&are&the&edges&that&explore&new&nodes• Forward&edges:&(), R)

• Ancestor&to&descendant• Backward&edges:& O, )

• Descendant&to&ancestor• Implies&a&directed&cycle!

• Cross&edges:&(Q, O)• No&ancestral&relation

IEe

parentedges

3A

semi

Page 29: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

a b

e f

• DFS&starting&from&node&O• Search&in&alphabetical&order• Label&edges&with&{tree,forward,backward,cross}

c d

g h

Page 30: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

PostSOrdering

G = (V,E) is a graphexplored[u] = 0 �u

DFS(u):explored[u] = 1

for ((u,v) in E):if (explored[v]=0):parent[v] = uDFS(v)

post-visit(u)

u c

a b

• Maintain&a&counter&clock,& initially&set&clock = 1• post-visit(u):

set postorder[u]=clock, clock=clock+1

Vertex Post@Order

Page 31: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

PreSOrdering

G = (V,E) is a graphexplored[u] = 0 �u

DFS(u):explored[u] = 1

pre-visit(u)

for ((u,v) in E):if (explored[v]=0):parent[v] = uDFS(v)

u c

a b

• Maintain&a&counter&clock,& initially&set&clock = 1• pre-visit(u):

set preorder[u]=clock, clock=clock+1

Vertex Pre@Order

Page 32: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

a b

e f

• Compute&the&post@order of&this&graph• DFS&from&S,&search&in&alphabetical&order

c d

g h

Vertex a b c d e f g hPost@Order

Page 33: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

a b

e f

• Compute&the&post@order of&this&graph• DFS&from&S,&search&in&alphabetical&order

c d

g h

Vertex a b c d e f g hPost@Order 8 7 5 4 6 1 2 3

Page 34: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

a b

e f

• Observation:&if&postorder[u]&<&postorder[v]&then&(u,v)&is&a&backward&edge

c d

g h

Vertex a b c d e f g hPost@Order 8 7 5 4 6 1 2 3

Page 35: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Ask&the&Audience

• Observation:&if&postorder[u]&<&postorder[v]&then&(u,v)&is&a&backward&edge• DFS(u)&can’t&finish&until&its&children&are&finished

• If&(u,v)&is&a&tree&edge,&then&postorder[u]&>&postorder[v]• If&(u,v)&is&a&forward&edge,&then&postorder[u]&>&postorder[v]

• If&postorder[u]&<&postorder[v],&then&DFS(u)&finishes&before&DFS(v),&thus&DFS(v)&is&not&called&by&DFS(u)

• When&we&ran&DFS(u),&we&must&have&had&explored[v]=1• Thus,&DFS(v)&started&before&DFS(u)

• DFS(v)&started&before&DFS(u)&but&finished&after• Can&only&happen&for&a&backward&edge

Page 36: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Topological&Ordering

Page 37: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Directed&Acyclic&Graphs&(DAGs)

• DAG:&A&directed graph&with&no&directed&cycles• Can&be&much&more&complex&than&a&forest

Page 38: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Directed&Acyclic&Graphs&(DAGs)

• DAG:&A&directed&graph&with&no&directed&cycles• DAGs&represent&precedence relationships

• A&topological&ordering of&a&directed&graph&is&a&labeling&of&the&nodes&from&*-,… , *K so&that&all&edges&go&“forwards”,&that&is& *J, *T ∈ % ⇒ = > <• ! has&a&topological&ordering&⇒ ! is&a&DAG

Page 39: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Directed&Acyclic&Graphs&(DAGs)

• Problem&1:&given&a&digraph&!,&is&it&a&DAG?• Problem&2: given&a&digraph&!,&can&it&be&topologically&ordered?

• Thm: ! has&a&topological&ordering&⟺! is&a&DAG• We&will&design&one&algorithm&that&either&outputs&a&topological&ordering&or&finds&a&directed&cycle

Page 40: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Topological&Ordering

• Observation:&the&first&node&must&have&no&inSedges

• Observation: In&any&DAG,&there&is&always&a&node&with&no&incoming&edges

Page 41: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Topological&Ordering

• Fact: In&any&DAG,&there&is&a&node&with&no&incoming&edges• Thm: Every&DAG&has&a&topological&ordering• Proof&(Induction):

Page 42: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Fast&Topological&Ordering

• Claim:&ordering&nodes&by&decreasing&postorder&gives&a&topological&ordering• Proof:• A&DAG&has&no&backward&edges• Suppose&this&is&not a&topological&ordering• That&means&there&exists&an&edge&(u,v)&such&that&postorder[u]&<&postorder[v]• We&showed&that&any&such&(u,v)&is&a&backward&edge• But&there&are&no&backward&edges,&contradiction!

Page 43: CS3000:&Algorithms&&&Data JonathanUllman · CS3000:&Algorithms&&&Data JonathanUllman Lecture&9:& • Graphs • Graph&Traversals:&DFS • Topological&Sort Feb&5,2020

Topological&Ordering&(TO)

• DAG:&A&directed&graph&with&no&directed&cycles• Any&DAG&can&be&toplogically ordered

• Label&nodes&*-, … , *K so&that& *J, *T ∈ % ⟹ = > <

• Can&compute&a&TO&in&Y 6 +[ time&using&DFS• Reverse&of&postSorder&is&a&topological&order