Directed Acyclic Graph

Post on 16-Jul-2015

154 views 1 download

Transcript of 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

Graphvertex

edge

Weighted Graph

5

3

-2

5

1

0

Undirected Graph

Complete Graph (Clique)

Patha

c

d e

b

Length = 4

Cyclea

c

g

d e

b

f

Directed Acyclic Graph (DAG)

Degree

In-Degree Out-Degree

Disconnected Graph

Connected Components

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

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

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

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

citydirect flight

5

cost

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”

computernetworklink

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

B?

“SHORTEST PATH PROBLEM”

web pageweb link

moduleprerequisite

QuestionFind a sequence of modules to take that satisfy the

prerequisite requirements.

“TOPOLOGICAL SORT”

Other ApplicationsBiologyVLSI LayoutVehicle RoutingJob SchedulingFacility Location

::

Adjacency Matrixdouble vertex[][];

1

23

4

1

-31 2 3

1 ∞ 4 -3

2 ∞ ∞ ∞

3 ∞ 1 ∞

Adjacency ListEdgeList vertex[];

1

23

4

1

-3

1

2

3

3 -3 2 4

2 1

neighbour cost

“Avoid Pointers in Competition..”

1

23

4

1

-3

1 2 32

3 2

1 4 -32

3 1

A

C

D

B

EF

A

C

D

B

EF

A

C

D

B

EF

A

C

D

B

EF

A

C

D

B

EF

A

C

D

B

EF

A

C

D

B

EF

A

C

D

B

EF

0

1

2

2

23

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

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

Search All VerticesSearch(G)

foreach vertex vmark v as unvisited

foreach vertex vif v is not visited BFS(v)

A

C

D

B

EF

A

C

D

B

EF

A

C

D

B

EF

ApplicationsBFS

shortest path

DFSlongest path in DAGfinding connected componentdetecting cyclestopological sort

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

A

C

D

B

EF

ShortestPath(s)Run BFS(s)

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

A

C

D

B

EF

5

51

2

3

1

31

4

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

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

10

62

8

s w

v

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

10

62

8

s w

vdistance(w) = 8

cost(v,w) = 2

parent(w) = v

A

C

D

B

EF

5

51

2

3

1

31

4

0

8

8

8

88

5

51

2

3

1

31

4

0

5

8

8

88

5

51

2

3

1

31

4

0

5

8

8

88

5

51

2

3

1

31

4

0

5

10

8

6

8

5

51

2

3

1

31

4

0

5

10

8

6

8

5

51

2

3

1

31

4

0

5

8

8

610

5

51

2

3

1

31

4

0

5

8

8

610

5

51

2

3

1

31

4

0

5

8

8

610

5

51

2

3

1

31

4

0

5

8

8

610

5

51

2

3

1

31

4

0

5

8

8

610

5

1

2

3

4

Dijkstra’s Algorithmcolor all vertices yellowforeach vertex w

distance(w) = INFINITYdistance(s) = 0

Dijkstra’s Algorithmwhile there are yellow vertices

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

relax(v,w)

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)

Initialization O(V)foreach vertex w

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

Main Loopwhile pq is not empty

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

relax(v,w)

0

8

8

8

88

5

31

-2

-3

1

31

-4

0

5

8

8

88

5

31

-2

-3

1

31

-4

0

5

8

2

6

8

5

31

-2

-3

1

31

-4

0

5

4

2

32

5

31

-2

-3

1

31

-4

0

5

1

2

3-1

5

31

-2

-3

1

31

-4

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

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

Mark a vertex as “visited” = vertex is finished

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

A

C

D

B

EF

unvisited

visiting

visited

unvisited

visiting

visited

Tree Edge

Backward Edge

Forward Edge

Cross Edge

(in a DAG)

A

C

D

B

EF

1 0

A

C

D

B

EF

unvisited

visiting

visited

1 02

A

C

D

B

EF

unvisited

visiting

visited

1 0

3

2

A

C

D

B

EF

unvisited

visiting

visited

1 0

3 4

5

2

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

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

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

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

Topological Sort ACBEFD ACBEDF ACDBEF

A

C

D

B

EF

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