100
In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite. Paul Dirac

Directed Acyclic Graph

Embed Size (px)

Citation preview

Page 1: Directed Acyclic Graph

In science one tries to tell people, in such a way as to be understood by everyone, something

that no one ever knew before. But in poetry, it's the exact opposite.

Paul Dirac

Page 2: Directed Acyclic Graph

Graphvertex

edge

Page 3: Directed Acyclic Graph

Weighted Graph

5

3

-2

5

1

0

Page 4: Directed Acyclic Graph

Undirected Graph

Page 5: Directed Acyclic Graph

Complete Graph (Clique)

Page 6: Directed Acyclic Graph

Patha

c

d e

b

Length = 4

Page 7: Directed Acyclic Graph

Cyclea

c

g

d e

b

f

Page 8: Directed Acyclic Graph

Directed Acyclic Graph (DAG)

Page 9: Directed Acyclic Graph

Degree

Page 10: Directed Acyclic Graph

In-Degree Out-Degree

Page 11: Directed Acyclic Graph

Disconnected Graph

Page 12: Directed Acyclic Graph

Connected Components

Page 13: Directed Acyclic Graph

FormallyA weighted graph G = (V, E, w), where V is the set of vertices E is the set of edges w is the weight function

Page 14: Directed Acyclic Graph

Variations of (Simple) GraphMultigraph

More than one edge between two vertices

E is a bag, not a set Hypergraph

Edges consists of more than two vertex

Page 15: Directed Acyclic Graph

ExampleV = { a, b, c }E = { (a,b), (c,b), (a,c) }w = { ((a,b), 4), ((c, b), 1), ((a,c),-3) }

a

bc

4

1

-3

Page 16: Directed Acyclic Graph

Adjacent Verticesadj(v) = set of vertices adjacent to v adj(a) = {b, c}

adj(b) = {}adj(c) = {b}

∑v |adj(v)| = |E|

adj(v): Neighbours of v

a

bc

4

1

-3

Page 17: Directed Acyclic Graph
Page 18: Directed Acyclic Graph

citydirect flight

5

cost

Page 19: Directed Acyclic Graph

QuestionWhat is the shortest way to travel between A and

B?“SHORTEST PATH PROBLEM”

How to mimimize the cost of visiting n cities such that we visit each city exactly once, and finishing at the city where we start from?

“TRAVELING SALESMAN PROBLEM”

Page 20: Directed Acyclic Graph

computernetworklink

Page 21: Directed Acyclic Graph

QuestionWhat is the shortest route to send a packet from A to

B?

“SHORTEST PATH PROBLEM”

Page 22: Directed Acyclic Graph

web pageweb link

Page 23: Directed Acyclic Graph

moduleprerequisite

Page 24: Directed Acyclic Graph

QuestionFind a sequence of modules to take that satisfy the

prerequisite requirements.

“TOPOLOGICAL SORT”

Page 25: Directed Acyclic Graph

Other ApplicationsBiologyVLSI LayoutVehicle RoutingJob SchedulingFacility Location

::

Page 26: Directed Acyclic Graph
Page 27: Directed Acyclic Graph

Adjacency Matrixdouble vertex[][];

1

23

4

1

-31 2 3

1 ∞ 4 -3

2 ∞ ∞ ∞

3 ∞ 1 ∞

Page 28: Directed Acyclic Graph

Adjacency ListEdgeList vertex[];

1

23

4

1

-3

1

2

3

3 -3 2 4

2 1

neighbour cost

Page 29: Directed Acyclic Graph

“Avoid Pointers in Competition..”

1

23

4

1

-3

1 2 32

3 2

1 4 -32

3 1

Page 30: Directed Acyclic Graph
Page 31: Directed Acyclic Graph

A

C

D

B

EF

Page 32: Directed Acyclic Graph

A

C

D

B

EF

Page 33: Directed Acyclic Graph

A

C

D

B

EF

Page 34: Directed Acyclic Graph

A

C

D

B

EF

Page 35: Directed Acyclic Graph

A

C

D

B

EF

Page 36: Directed Acyclic Graph

A

C

D

B

EF

Page 37: Directed Acyclic Graph

A

C

D

B

EF

Page 38: Directed Acyclic Graph

A

C

D

B

EF

Page 39: Directed Acyclic Graph

0

1

2

2

23

Page 40: Directed Acyclic Graph

Level-Order on Treeif T is empty returnQ = new QueueQ.enq(T)while Q is not empty

curr = Q.deq()print curr.elementif T.left is not empty

Q.enq(curr.left) if curr.right is not empty

Q.enq(curr.right)

1

4 5

3

6

9 08

2

7

Page 41: Directed Acyclic Graph

Calculating LevelQ = new QueueQ.enq (v)v.level = 0while Q is not empty

curr = Q.deq()if curr is not visited

mark curr as visitedforeach w in adj(curr)

if w is not visitedw.level = curr.level + 1Q.enq(w)

A

C

D

B

EF

Page 42: Directed Acyclic Graph

Search All VerticesSearch(G)

foreach vertex vmark v as unvisited

foreach vertex vif v is not visited BFS(v)

Page 43: Directed Acyclic Graph
Page 44: Directed Acyclic Graph

A

C

D

B

EF

Page 45: Directed Acyclic Graph

A

C

D

B

EF

Page 46: Directed Acyclic Graph

A

C

D

B

EF

Page 47: Directed Acyclic Graph
Page 48: Directed Acyclic Graph

ApplicationsBFS

shortest path

DFSlongest path in DAGfinding connected componentdetecting cyclestopological sort

Page 49: Directed Acyclic Graph

DefinitionA path on a graph G is a sequence of vertices v0, v1, v2, .. vn where (vi,vi+1)∈E

The cost of a path is the sum of the cost of all edges in the path. A

C

D

B

EF

Page 50: Directed Acyclic Graph

A

C

D

B

EF

Page 51: Directed Acyclic Graph

ShortestPath(s)Run BFS(s)

w.level: shortest distance from sw.parent: shortest path from s

Page 52: Directed Acyclic Graph

A

C

D

B

EF

5

51

2

3

1

31

4

Page 53: Directed Acyclic Graph

BFS(s) does not workMust keep track of smallest distance so far.

If we found a new, shorter path, update the distance.

Page 54: Directed Acyclic Graph

10

62

8

s w

v

Page 55: Directed Acyclic Graph

Definitiondistance(v) : shortest distance so far from s to v

parent(v) : previous node on the shortest path so far from s to v

cost(u, v) : the cost of edge from u to v

Page 56: Directed Acyclic Graph

10

62

8

s w

vdistance(w) = 8

cost(v,w) = 2

parent(w) = v

Page 57: Directed Acyclic Graph

A

C

D

B

EF

5

51

2

3

1

31

4

Page 58: Directed Acyclic Graph

0

8

8

8

88

5

51

2

3

1

31

4

Page 59: Directed Acyclic Graph

0

5

8

8

88

5

51

2

3

1

31

4

Page 60: Directed Acyclic Graph

0

5

8

8

88

5

51

2

3

1

31

4

Page 61: Directed Acyclic Graph

0

5

10

8

6

8

5

51

2

3

1

31

4

Page 62: Directed Acyclic Graph

0

5

10

8

6

8

5

51

2

3

1

31

4

Page 63: Directed Acyclic Graph

0

5

8

8

610

5

51

2

3

1

31

4

Page 64: Directed Acyclic Graph

0

5

8

8

610

5

51

2

3

1

31

4

Page 65: Directed Acyclic Graph

0

5

8

8

610

5

51

2

3

1

31

4

Page 66: Directed Acyclic Graph

0

5

8

8

610

5

51

2

3

1

31

4

Page 67: Directed Acyclic Graph

0

5

8

8

610

5

1

2

3

4

Page 68: Directed Acyclic Graph

Dijkstra’s Algorithmcolor all vertices yellowforeach vertex w

distance(w) = INFINITYdistance(s) = 0

Page 69: Directed Acyclic Graph

Dijkstra’s Algorithmwhile there are yellow vertices

v = yellow vertex with min distance(v)color v redforeach neighbour w of v

relax(v,w)

Page 70: Directed Acyclic Graph

Using Priority Queueforeach vertex w

distance(w) = INFINITYdistance(s) = 0pq = new PriorityQueue(V)

while pq is not emptyv = pq.deleteMin()foreach neighbour w of v

relax(v,w)

Page 71: Directed Acyclic Graph

Initialization O(V)foreach vertex w

distance(w) = INFINITYdistance(s) = 0pq = new PriorityQueue(V)

Page 72: Directed Acyclic Graph

Main Loopwhile pq is not empty

v = pq.deleteMin()foreach neighbour w of v

relax(v,w)

Page 73: Directed Acyclic Graph

0

8

8

8

88

5

31

-2

-3

1

31

-4

Page 74: Directed Acyclic Graph

0

5

8

8

88

5

31

-2

-3

1

31

-4

Page 75: Directed Acyclic Graph

0

5

8

2

6

8

5

31

-2

-3

1

31

-4

Page 76: Directed Acyclic Graph

0

5

4

2

32

5

31

-2

-3

1

31

-4

Page 77: Directed Acyclic Graph

0

5

1

2

3-1

5

31

-2

-3

1

31

-4

Page 78: Directed Acyclic Graph

Bellman-Ford Algorithmdo |V|-1 timesforeach edge (u,v)

relax(u,v)

// check for negative weight cycleforeach edge (u,v)if distance(v) > distance(u) + cost(u,v)

ERROR: has negative cycle

Page 79: Directed Acyclic Graph
Page 80: Directed Acyclic Graph

Idea: Use DFSDenote vertex as “visited”, “unvisited” and “visiting”

Mark a vertex as “visited” = vertex is finished

Page 81: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 82: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 83: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 84: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 85: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 86: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 87: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 88: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 89: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

Page 90: Directed Acyclic Graph

unvisited

visiting

visited

Tree Edge

Backward Edge

Forward Edge

Cross Edge

Page 91: Directed Acyclic Graph

(in a DAG)

Page 92: Directed Acyclic Graph

A

C

D

B

EF

1 0

Page 93: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

1 02

Page 94: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

1 0

3

2

Page 95: Directed Acyclic Graph

A

C

D

B

EF

unvisited

visiting

visited

1 0

3 4

5

2

Page 96: Directed Acyclic Graph

Longest PathRun DFS as usualWhen a vertex v is finished, set

L(v) = max {L(u) + 1} for all edge (v,u)

Page 97: Directed Acyclic Graph
Page 98: Directed Acyclic Graph

Topological SortGoal: Order the vertices, such that if there is a path

from u to v, u appears before v in the output.

Page 99: Directed Acyclic Graph

Topological Sort ACBEFD ACBEDF ACDBEF

A

C

D

B

EF

Page 100: Directed Acyclic Graph

Topological SortRun DFS as usualWhen a vertex is finish, push it onto a stack