Directed Acyclic Graph

ajal4u 6,734 views 100 slides Mar 02, 2015
Slide 1
Slide 1 of 100
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100

About This Presentation

longest path in DAG
finding connected component
detecting cycles
topological sort


Slide Content

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

Graph
vertex
edge

Weighted Graph
5
3
-2
5
1
0

Undirected Graph

Complete Graph (Clique)

Path
a
c
d e
b
Length = 4

Cycle
a
c
g
d e
b
f

Directed Acyclic Graph (DAG)

Degree

In-Degree Out-Degree

Disconnected Graph

Connected Components

Formally
A 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) Graph
Multigraph
More than one edge between two
vertices
E is a bag, not a set
Hypergraph
Edges consists of more than two vertex

Example
V = { 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 Vertices
adj(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

city
direct
flight
5
cost

Question
What 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”

computer
network
link

Question
What is the shortest route to send a packet from A to
B?
“SHORTEST PATH PROBLEM”

web page
web link

module
prerequisite

Question
Find a sequence of modules to take that satisfy the
prerequisite requirements.
“TOPOLOGICAL SORT”

Other Applications
Biology
VLSI Layout
Vehicle Routing
Job Scheduling
Facility Location
:
:

Adjacency Matrix
double vertex[][];
1
23
4
1
-3123
1¥4-3
2¥¥¥
3¥1¥

Adjacency List
EdgeList 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
123
2
32
14-3
2
31

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 Tree
if T is empty return
Q = new Queue
Q.enq(T)
while Q is not empty
curr = Q.deq()
print curr.element
if 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 Level
Q = new Queue
Q.enq (v)
v.level = 0
while Q is not empty
curr = Q.deq()
if curr is not visited
mark curr as visited
foreach w in adj(curr)
if w is not visited
w.level = curr.level + 1
Q.enq(w)
A
C
D
B
EF

Search All Vertices
Search(G)
foreach vertex v
mark v as unvisited
foreach vertex v
if v is not visited
BFS(v)

A
C
D
B
EF

A
C
D
B
EF

A
C
D
B
EF

Applications
BFS
shortest path
DFS
longest path in DAG
finding connected component
detecting cycles
topological sort

Definition
A path on a graph G is a sequence of vertices v
0
, v
1
,
v
2
, .. v
n
where (v
i
,v
i+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 s
w.parent: shortest path from s

A
C
D
B
EF
5
51
2
3
1
3
1
4

BFS(s) does not work
Must keep track of smallest distance so far.
If we found a new, shorter path, update the distance.

10
6
2
8
s w
v

Definition
distance(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
6
2
8
s w
v
distance(w) = 8
cost(v,w) = 2
parent(w) = v

A
C
D
B
EF
5
51
2
3
1
3
1
4

0
8
8
8
88
5
51
2
3
1
3
1
4

0
5
8
8
88
5
51
2
3
1
3
1
4

0
5
8
8
88
5
51
2
3
1
3
1
4

0
5
10
8
6
8
5
51
2
3
1
3
1
4

0
5
10
8
6
8
5
51
2
3
1
3
1
4

0
5
8
8
610
5
51
2
3
1
3
1
4

0
5
8
8
610
5
51
2
3
1
3
1
4

0
5
8
8
610
5
51
2
3
1
3
1
4

0
5
8
8
610
5
51
2
3
1
3
1
4

0
5
8
8
610
5
1
2
3
4

Dijkstra’s Algorithm
color all vertices yellow
foreach vertex w
distance(w) = INFINITY
distance(s) = 0

Dijkstra’s Algorithm
while there are yellow vertices
v = yellow vertex with min distance(v)
color v red
foreach neighbour w of v
relax(v,w)

Using Priority Queue
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)
while pq is not empty
v = pq.deleteMin()
foreach neighbour w of v
relax(v,w)

Initialization O(V)
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)

Main Loop
while 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
3
1
-4

0
5
8
8
88
5
31
-2
-3
1
3
1
-4

0
5
8
2
6
8
5
31
-2
-3
1
3
1
-4

0
5
4
2
32
5
31
-2
-3
1
3
1
-4

0
5
1
2
3-1
5
31
-2
-3
1
3
1
-4

Bellman-Ford Algorithm
do |V|-1 times
foreach edge (u,v)
relax(u,v)
// check for negative weight cycle
foreach edge (u,v)
if distance(v) > distance(u) + cost(u,v)
ERROR: has negative cycle

Idea: Use DFS
Denote 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 0
2

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 Path
Run DFS as usual
When a vertex v is finished, set
L(v) = max {L(u) + 1} for all edge (v,u)

Topological Sort
Goal: 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 Sort
Run DFS as usual
When a vertex is finish, push it onto a stack