10 views

Uploaded by Muhammad Ikhsan Rangkuti

Di Graphs

save

You are on page 1of 9

**© 2004 Goodrich, Tamassia
**

Directed Graphs

JFK

BOS

MIA

ORD

LAX

DFW

SFO

Directed Graphs 2

© 2004 Goodrich, Tamassia

Digraphs (§ 12.4)

A digraph is a graph

whose edges are all

directed

Short for “directed graph”

Applications

one - way streets

flights

task scheduling A

C

E

B

D

Directed Graphs 3

© 2004 Goodrich, Tamassia

Digraph Properties

A graph G=(V,E) such that

Each edge goes in one direction:

Edge (a,b) goes from a to b, but not b to a.

If G is simple, m < n*(n-1).

If we keep in-edges and out-edges in separate

adjacency lists, we can perform listing of in-

edges and out-edges in time proportional to

their size.

A

C

E

B

D

Directed Graphs 4

© 2004 Goodrich, Tamassia

Digraph Application

Scheduling: edge (a,b) means task a must be

completed before b can be started

The good life

ics141 ics131 ics121

ics53

ics52

ics51

ics23 ics22 ics21

ics161

ics151

ics171

Directed Graphs 5

© 2004 Goodrich, Tamassia

Directed DFS

We can specialize the

traversal algorithms (DFS and

BFS) to digraphs by

traversing edges only along

their direction

In the directed DFS

algorithm, we have four

types of edges

discovery edges

back edges

forward edges

cross edges

A directed DFS starting a a

vertex s determines the

vertices reachable from s

A

C

E

B

D

Directed Graphs 6

© 2004 Goodrich, Tamassia

Reachability

DFS tree rooted at v: vertices reachable

from v via directed paths

A

C

E

B

D

F

A

C

E D

A

C

E

B

D

F

Directed Graphs 7

© 2004 Goodrich, Tamassia

Strong Connectivity

Each vertex can reach all other vertices

a

d

c

b

e

f

g

Directed Graphs 8

© 2004 Goodrich, Tamassia

Pick a vertex v in G.

Perform a DFS from v in G.

If there’s a w not visited, print “no”.

Let G’ be G with edges reversed.

Perform a DFS from v in G’.

If there’s a w not visited, print “no”.

Else, print “yes”.

Running time: O(n+m).

Strong Connectivity

Algorithm

G:

G’:

a

d

c

b

e

f

g

a

d

c

b

e

f

g

Directed Graphs 9

© 2004 Goodrich, Tamassia

Maximal subgraphs such that each vertex can reach

all other vertices in the subgraph

Can also be done in O(n+m) time using DFS, but is

more complicated (similar to biconnectivity).

Strongly Connected

Components

{ a , c , g }

{ f , d , e , b }

a

d

c

b

e

f

g

Directed Graphs 10

© 2004 Goodrich, Tamassia

Transitive Closure

Given a digraph G, the

transitive closure of G is the

digraph G* such that

G* has the same vertices

as G

if G has a directed path

from u to v (u ≠ v), G*

has a directed edge from

u to v

The transitive closure

provides reachability

information about a digraph

B

A

D

C

E

B

A

D

C

E

G

G*

Directed Graphs 11

© 2004 Goodrich, Tamassia

Computing the

Transitive Closure

We can perform

DFS starting at

each vertex

O(n(n+m))

If there's a way to get

from A to B and from

B to C, then there's a

way to get from A to C.

Alternatively ... Use

dynamic programming:

The Floyd-Warshall

Algorithm

Directed Graphs 12

© 2004 Goodrich, Tamassia

Floyd-Warshall

Transitive Closure

Idea #1: Number the vertices 1, 2, …, n.

Idea #2: Consider paths that use only

vertices numbered 1, 2, …, k, as

intermediate vertices:

k

j

i

Uses only vertices

numbered 1,…,k - 1

Uses only vertices

numbered 1,…,k - 1

Uses only vertices numbered 1,…,k

(add this edge if it’s not already in)

Directed Graphs 13

© 2004 Goodrich, Tamassia

Floyd-Warshall’s Algorithm

Floyd-Warshall’s algorithm

numbers the vertices of G as

v

1

, …, v

n

and computes a

series of digraphs G

0

, …, G

n

G

0

=G

G

k

has a directed edge (v

i

, v

j

)

if G has a directed path from

v

i

to v

j

with intermediate

vertices in the set {v

1

, …, v

k

}

We have that G

n

= G*

In phase k, digraph G

k

is

computed from G

k − 1

Running time: O(n

3

),

assuming areAdjacent is O(1)

(e.g., adjacency matrix)

AlgorithmFloydWarshall(G)

Input digraph G

Output transitive closure G* of G

i ←1

for all v ∈ G.vertices()

denote v as v

i

i ←i + 1

G

0

←G

for k ←1 to n do

G

k

←G

k − 1

for i ←1 to n (i ≠ k) do

for j ←1 to n (j ≠ i, k) do

if G

k − 1

.areAdjacent(v

i

, v

k

) ∧

G

k − 1

.areAdjacent(v

k

, v

j

)

if ¬G

k

.areAdjacent(v

i

, v

j

)

G

k

.insertDirectedEdge(v

i

, v

j

, k)

return G

n

Directed Graphs 14

© 2004 Goodrich, Tamassia

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

Directed Graphs 15

© 2004 Goodrich, Tamassia

Floyd-Warshall, Iteration 1

JFK

BOS

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

Directed Graphs 16

© 2004 Goodrich, Tamassia

Floyd-Warshall, Iteration 2

JFK

BOS

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

Directed Graphs 17

© 2004 Goodrich, Tamassia

Floyd-Warshall, Iteration 3

JFK

BOS

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

Directed Graphs 18

© 2004 Goodrich, Tamassia

Floyd-Warshall, Iteration 4

JFK

BOS

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

Directed Graphs 19

© 2004 Goodrich, Tamassia

Floyd-Warshall, Iteration 5

JFK

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

BOS

Directed Graphs 20

© 2004 Goodrich, Tamassia

Floyd-Warshall, Iteration 6

JFK

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

BOS

Directed Graphs 21

© 2004 Goodrich, Tamassia

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAX

DFW

SFO

v

2

v

1

v3

v

4

v5

v6

v

7

BOS

Directed Graphs 22

© 2004 Goodrich, Tamassia

DAGs and Topological Ordering

A directed acyclic graph (DAG) is a

digraph that has no directed cycles

A topological ordering of a digraph

is a numbering

v

1

, …, v

n

of the vertices such that for every

edge (v

i

, v

j

), we have i < j

Example: in a task scheduling

digraph, a topological ordering a

task sequence that satisfies the

precedence constraints

Theorem

A digraph admits a topological

ordering if and only if it is a DAG

B

A

D

C

E

DAG G

B

A

D

C

E

Topological

ordering of G

v

1

v

2

v

3

v

4

v

5

Directed Graphs 23

© 2004 Goodrich, Tamassia

write c.s. program

play

Topological Sorting

Number vertices, so that (u,v) in E implies u < v

wake up

eat

nap

study computer sci.

more c.s.

work out

sleep

dream about graphs

A typical student day

1

2

3

4 5

6

7

8

9

10

11

make cookies

for professors

Directed Graphs 24

© 2004 Goodrich, Tamassia

Note: This algorithm is different than the

one in Goodrich-Tamassia

Running time: O(n + m). How…?

Algorithm for Topological Sorting

Method TopologicalSort(G)

H ←G // Temporary copy of G

n ←G.numVertices()

while H is not empty do

Let v be a vertex with no outgoing edges

Label v ←n

n ←n - 1

Remove v from H

Directed Graphs 25

© 2004 Goodrich, Tamassia

Topological Sorting

Algorithm using DFS

Simulate the algorithm by using

depth-first search

O(n+m) time.

AlgorithmtopologicalDFS(G, v)

Input graph G and a start vertex v of G

Output labeling of the vertices of G

in the connected component of v

setLabel(v, VISITED)

for all e ∈ G.incidentEdges(v)

if getLabel(e) = UNEXPLORED

w ←opposite(v,e)

if getLabel(w) = UNEXPLORED

setLabel(e, DISCOVERY)

topologicalDFS(G, w)

else

{e is a forward or cross edge}

Label v with topological number n

n ←n - 1

AlgorithmtopologicalDFS(G)

Input dag G

Output topological ordering of G

n ←G.numVertices()

for all u ∈ G.vertices()

setLabel(u, UNEXPLORED)

for all e ∈ G.edges()

setLabel(e, UNEXPLORED)

for all v ∈ G.vertices()

if getLabel(v) = UNEXPLORED

topologicalDFS(G, v)

Directed Graphs 26

© 2004 Goodrich, Tamassia

Topological Sorting Example

Directed Graphs 27

© 2004 Goodrich, Tamassia

Topological Sorting Example

9

Directed Graphs 28

© 2004 Goodrich, Tamassia

Topological Sorting Example

8

9

Directed Graphs 29

© 2004 Goodrich, Tamassia

Topological Sorting Example

7

8

9

Directed Graphs 30

© 2004 Goodrich, Tamassia

Topological Sorting Example

7

8

6

9

Directed Graphs 31

© 2004 Goodrich, Tamassia

Topological Sorting Example

7

8

5 6

9

Directed Graphs 32

© 2004 Goodrich, Tamassia

Topological Sorting Example

7

4

8

5 6

9

Directed Graphs 33

© 2004 Goodrich, Tamassia

Topological Sorting Example

7

4

8

5 6

3

9

Directed Graphs 34

© 2004 Goodrich, Tamassia

Topological Sorting Example

2

7

4

8

5 6

3

9

Directed Graphs 35

© 2004 Goodrich, Tamassia

Topological Sorting Example

2

7

4

8

5 6

1

3

9

- Triangle_ .Poly FilesUploaded byIgor Gjorgjiev
- COMP211slides_7Uploaded byalexisthe
- Two shorter and simpler proofs in spectral graph theoryUploaded bydragance106
- n05Uploaded bymariahalo
- VertexUploaded byJasonYu
- TreesUploaded bySarah Ferguson
- CheatsheetUploaded byDan Huang
- QuestionUploaded bySahil Garg
- 1-soutenementUploaded byjhdmss
- A Note on Path Signed DiGraphsUploaded byDon Hass
- Multiscale Texture SynthesisUploaded byDan Hauer
- AAD Practicals 3Uploaded byed3uzacoi1zy
- JUNG2 TutorialUploaded byspzunited
- graceful.pdfUploaded byMuflih Mubarak Darmadi
- SMU MCA AssignmentsUploaded byNarendra Dev
- miningceUploaded byapi-19969042

- LK 2 KK dUploaded byMuhammad Ikhsan Rangkuti
- Soal Dan Pembahasan Un Matematika SMP 20121Uploaded byMuhammad Ikhsan Rangkuti
- Asal Usul πUploaded byMuhammad Ikhsan Rangkuti
- Lampiran 18. KI dan KD K-13 SD-MI. PPKn.pdfUploaded byMuhammad Ikhsan Rangkuti
- 01-ks-supervisi-akademik.docUploaded byMuhammad Ikhsan Rangkuti
- 2-OPTIMASI EKONOMI.docUploaded byMuhammad Ikhsan Rangkuti
- soal-dan-pembahasan-un-matematika-smp-2012.pdfUploaded byMuhammad Ikhsan Rangkuti
- 1. CONTOH SOAL LATIHAN MATEMATIKA FAKTORISASI SUKU ALJABAR KELAS 8 SMP.docxUploaded byMuhammad Ikhsan Rangkuti
- Jadwal, Nama, Kebutuhan LT IIUploaded byMuhammad Ikhsan Rangkuti
- Modul Bangun RuangUploaded byMuhammad Ikhsan Rangkuti
- Instrumen Pengumpulan Data Dan InformasiUploaded byMuhammad Ikhsan Rangkuti
- latihan soal uas matematika ekonomi pembahasan-130110222459-phpapp01Uploaded byMuhammad Ikhsan Rangkuti
- SOAL UH MTK SMP KELAS 7 BAB OPERASI ALJABAR SEMESTER 1.pdfUploaded byMuhammad Ikhsan Rangkuti
- KAK_Blockgrant_KKG_dan_MGMP_P4TKSB.pdfUploaded byMuhammad Ikhsan Rangkuti