lecture 33: directed graph connectivity

66
LECTURE 33: DIRECTED GRAPH CONNECTIVITY CSC 213 – Large Scale Programming

Upload: lala

Post on 22-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

CSC 213 – Large Scale Programming. Lecture 33: Directed Graph Connectivity. Today’s Goals. Examine new properties of DirectedGraph s What reaching & reachable mean for a Graph How humans go about computing these properties Algorithms for computers to compute these - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture  33: Directed  Graph Connectivity

LECTURE 33:DIRECTED GRAPH CONNECTIVITY

CSC 213 – Large Scale Programming

Page 2: Lecture  33: Directed  Graph Connectivity

Today’s Goals

Examine new properties of DirectedGraphs What reaching & reachable mean for a Graph

How humans go about computing these properties

Algorithms for computers to compute these Examine what meant by DAG and why

you care Simple ways to develop schedules will be

examined How can algorithm tell when a schedule

impossible

Page 3: Lecture  33: Directed  Graph Connectivity

Directed Graph

Only directed edges Replace undirected

edge with 2 directed edges

Relationships go in only one direction One-way streets Flights Scheduling

A

C

E

B

D

Page 4: Lecture  33: Directed  Graph Connectivity

Directed Graph

Only directed edges Replace undirected

edge with 2 directed edges

Relationships go in only one direction One-way streets Flights Scheduling Talking to your ex

Page 5: Lecture  33: Directed  Graph Connectivity

Directed Graph Properties

Each edge goes one-way (a,b) connects a to b (a,b) does not connect b to a

Can discuss in-edges & out-edges (a,b) is out-edge for a (a,b) is in-edge for b

Adjacency-based Graph classes can change Use 2 Sequences for adjacency-list vertices Define source & target dimension in adjacency-

matrixincidentEdges returns both in-edges & out-edges

a

c

e

b

d

Page 6: Lecture  33: Directed  Graph Connectivity

Reachability

e, a, d reachable from c

a

c

e

b

d

f

Page 7: Lecture  33: Directed  Graph Connectivity

Reachability

e, a, d reachable from c c reaches e

a

c

e

b

d

f

Page 8: Lecture  33: Directed  Graph Connectivity

Reachability

e, a, d reachable from c c reaches e c reaches e & e incident upon d, a

a

c

e

b

d

f

Page 9: Lecture  33: Directed  Graph Connectivity

Reachability

e, a, d reachable from c c reaches e c reaches e & e is incident upon d, a d, a out-edges to c only

a

c

e

b

d

f

Page 10: Lecture  33: Directed  Graph Connectivity

Reachability

a, c, d, e, f reachable from b

a

c

e

b

d

f

Page 11: Lecture  33: Directed  Graph Connectivity

Reachability

a, c, d, e, f reachable from b Path exists from b to every vertex

a

c

e

b

d

f

Page 12: Lecture  33: Directed  Graph Connectivity

Reachability

a, c, d, e, f reachable from b Path exists from b to every vertex Actually have multiple paths to most

vertices

a

c

e

b

d

f

Page 13: Lecture  33: Directed  Graph Connectivity

Transitive Closure of G

Transitive closure of G usually written as G*

b

a

d

c

e

G G*

Page 14: Lecture  33: Directed  Graph Connectivity

Transitive Closure of G

Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however

b

a

d

c

e

G G*

b

a

d

c

e

Page 15: Lecture  33: Directed  Graph Connectivity

Transitive Closure of G

Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however

Edge in G* if target reachable from source in G

b

a

d

c

e

G G*

b

a

d

c

e

Page 16: Lecture  33: Directed  Graph Connectivity

Transitive Closure of G

Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however

Edge in G* if target reachable from source in G

b

a

d

c

e

G G*

b

a

d

c

e

Page 17: Lecture  33: Directed  Graph Connectivity

Transitive Closure of G

Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however

Edge in G* if target reachable from source in G

b

a

d

c

e

G G*

b

a

d

c

e

Page 18: Lecture  33: Directed  Graph Connectivity

Transitive Closure of G

Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however

Edge in G* if target reachable from source in G

b

a

d

c

e

G G*

b

a

d

c

e

Page 19: Lecture  33: Directed  Graph Connectivity

Transitive Closure of G

Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however

Edge in G* if target reachable from source in G

b

a

d

c

e

G G*

b

a

d

c

e

Page 20: Lecture  33: Directed  Graph Connectivity

Computing Transitive Closure Use dynamic programming to compute

this This solution known as Floyd-Warshall

Algorithm But how fast is it?

Page 21: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall’s Algorithm

Number G’s vertices from 1 to n Algorithm will compute n directed graphs Set G0=G to initialize this algorithm Graph of transitive closure is end result (Gn

= G*) All n directed graphs have same

vertices Gk contains all edges in Gk-1 (and Gk-2, Gk-3 ,…,

G0) Gk also has edge (vi,vj) if edges (vi,vk) & (vk,vj)

in Gk-1

Takes O(n3) time with adjacency matrix Better to use “brute force” if few edges

exist

Page 22: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall’s Algorithm

Number G’s vertices from 1 to n Algorithm will compute n directed graphs Set G0=G to initialize this algorithm Graph of transitive closure is end result (Gn

= G*) All n directed graphs have same

vertices Gk contains all edges in Gk-1 (and Gk-2, Gk-3 ,…,

G0) Gk also has edge (vi,vj) if edges (vi,vk) & (vk,vj)

in Gk-1

Takes O(n3) time with adjacency matrix Better to use “brute force” if few edges

exist

Page 23: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G0

V6

V7

V5

V4

V1

V3

V2

Page 24: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G1

V6

V7

V5

V4

V1

V3

V2

Page 25: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G1

V6

V7

V5

V4

V1

V3

V2

Page 26: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G1

V6

V7

V5

V4

V1

V3

V2

Page 27: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G1

V6

V7

V5

V4

V1

V3

V2

Page 28: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G1

V6

V7

V5

V4

V1

V3

V2

Page 29: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G2

V6

V7

V5

V4

V1

V3

V2

Page 30: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G2

V6

V7

V5

V4

V1

V3

V2

Page 31: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G3

V6

V7

V5

V4

V1

V3

V2

Page 32: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G3

V6

V7

V5

V4

V1

V3

V2

Page 33: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G3

V6

V7

V5

V4

V1

V3

V2

Page 34: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G3

V6

V7

V5

V4

V1

V3

V2

Page 35: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G3

V6

V7

V5

V4

V1

V3

V2

Page 36: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G4

V6

V7

V5

V4

V1

V3

V2

Page 37: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G4

V6

V7

V5

V4

V1

V3

V2

Page 38: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G4

V6

V7

V5

V4

V1

V3

V2

Page 39: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G4

V6

V7

V5

V4

V1

V3

V2

Page 40: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G4

V6

V7

V5

V4

V1

V3

V2

Page 41: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G4

V6

V7

V5

V4

V1

V3

V2

Page 42: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G5

V6

V7

V5

V4

V1

V3

V2

Page 43: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G5

V6

V7

V5

V4

V1

V3

V2

Page 44: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G5

V6

V7

V5

V4

V1

V3

V2

Page 45: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G5

V6

V7

V5

V4

V1

V3

V2

Page 46: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G6

V6

V7

V5

V4

V1

V3

V2

Page 47: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G6

V6

V7

V5

V4

V1

V3

V2

Page 48: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G7

V6

V7

V5

V4

V1

V3

V2

Page 49: Lecture  33: Directed  Graph Connectivity

Floyd-Warshall Example – G*

V6

V7

V5

V4

V1

V3

V2

Page 50: Lecture  33: Directed  Graph Connectivity

Directed Acyclic Graph

Often called a DAG Number & sort

vertices Topological order

found Each edge (vi,vj) has

i < j Finds valid

schedules… …or proves cannot

exist!

b

a

d

c

e

G (is a DAG)

b

a

d

c

e

A valid ordering of G

1

2

3

4 5

Page 51: Lecture  33: Directed  Graph Connectivity

Directed Acyclic Graph

Often called a DAG Number & sort

vertices Topological order

found Each edge (vi,vj) has

i < j Finds valid

schedules… …or proves cannot

exist!

b

a

d

c

e

G (is a DAG)

b

a

d

c

e

Another valid ordering of G

2

1

3

4 5

Page 52: Lecture  33: Directed  Graph Connectivity

Topological Sorting

Edges connect smaller to larger vertex numberswake up

studyeat

class study

homeworkwork

study

go to bed dream about classwork

Professor’s expectation of student’s day

1

2 3

4 5

67

8

9 10

Page 53: Lecture  33: Directed  Graph Connectivity

Topological Sort Algorithm

Algorithm topologicalSort(Graph<V,E> G) H // Make a copy of Gm new Vertex[G.numVertices()]stack new …Stack<Vertex>() // Push onto stack any vertex in H with no outgoing edges n G.numVertices() while n ≥ 1 do

v stack.pop() n n – 1m[n] v

foreach e in v.incidentEdges() ≠ 0 do w H.opposite(e, v)

// Remove e from H if w has no outgoing edges then stack.push(w) return m

Page 54: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 55: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 56: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 57: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 58: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 59: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 60: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 61: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 62: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 63: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 64: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 65: Lecture  33: Directed  Graph Connectivity

Topological Sorting Example

1

7

4

8

56

2

3

9

Page 66: Lecture  33: Directed  Graph Connectivity

For Next Lecture

Weekly assignment out & due tomorrow

Programming assignment #3 plans due Friday Please do not wait to start working on

these ideas For Wednesday, read 13.5.2-13.5.3 &

13.6.2 Find quickest way to get someplace, can it

be done? How can we find the spanning tree that

weighs least?