Directed Graph and Topological Sort - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Directed Graph and Topological Sort

Description:

A graph is directed if direction is assigned to each edge. The directed ... The difference is that given a pair of vertices u and v, ... the system hangs. ... – PowerPoint PPT presentation

Number of Views:412
Avg rating:3.0/5.0
Slides: 30
Provided by: math81
Category:

less

Transcript and Presenter's Notes

Title: Directed Graph and Topological Sort


1
Directed Graph and Topological Sort
  • A graph is directed if direction is assigned to
    each edge. The directed edges are called arcs.
  • The difference is that given a pair of vertices u
    and v, there are two possible arcs (u,v) and
    (v,u). This is different from the case of
    undirected graph in which only one edge u,v is
    possible.
  • So the maximum number of edges in a directed
    graph with n vertices is n(n-1) instead of
    n(n-1)/2.

b
v
u
a
2
Representations
  • The adjacency matrix and adjacency list
    representation for undirected graphs easily
    generalize to the case of directed graphs.

3
Directed Acyclic Graph (DAG)
  • A directed path is a sequence of vertices (v0,
    v1, , vk) such that (vi, vi1) is an arc.
  • A directed cycle is a directed path such that
    first, and the last vertices are the same.
  • A directed graph (DAG) is acyclic if it does not
    contain any directed cycle.

4
Indegree and outdegree
  • Define the indegree of a vertex v to be the
    number of arcs directed into v.
  • Define the outdegree of a vertex v to be number
    of arcs directed out of v.
  • For any vertex v, the outdegree of v can be
    computed by scanning through the adjacency list
    of v. Summing over all vertices, this takes time
    proportional to O(nm).

a
d
indegree(v)3 outdegree(v)2
v
b
e
c
5
Indegree and outdegree
  • To compute the indegrees of the vertices, it has
    to be done somewhat indirectly.
  • First, initialize indegree for each vertex to 0.
  • Scan through each adjacency list of v.
  • For each neighbour of v (w) (there is an arc from
    v to w), the indegree of w is increased by 1.
  • After we process all adjacency lists, we get the
    indegree for each vertex.
  • Running time O(nm).

6
Directed Graphs Usage
  • Directed graphs are often used to represent
    order-dependent tasks.
  • That is we cannot start a task before another
    task finishes.
  • We can model this task dependent constraint using
    arcs.

Task x has to wait until task a, b, and c are all
finished. After then task x can start.
Similarly, task y cannot start until task x and
task c have completed.
x
a
y
c
b
7
Directed Graphs Usage
CS department course structure
104
180
171
151
221
342
252
201
211
251
271
M132
M111
231
272
361
381
303
343
341
327
334
336
362
332
How many indegree(171)? How many outdegree(171)?
8
Directed Graphs Usage
  • Such order-dependent (or inter-dependent) systems
    should not have cycles.
  • Otherwise, a directed cycle would imply a
    circular dependency among some task!
  • For example, if task a depends on task b, and
    task b also depends on a, then it turns out we
    cannot start task a and task b forever!
  • This would mean that the system hangs.
  • For directed acyclic graphs (DAGs) we need a
    mechanism to sort all the vertices to show a
    linear progression or schedule of all the tasks.

b
a
9
Topological Sort
  • Topological sort is an algorithm for a directed
    acyclic graph to compute such linear progression
    (schedule).
  • It can be thought of a way to linearly order the
    vertices so that the linear order respects the
    ordering relation implied by the arcs.

One possible linearly order for the tasks 0,
1, 6, 4, 3, 2, 5, 7, 8, 9. Another possible
linearly order for the tasks 0, 1, 4, 6, 2,
7, 5, 3, 8, 9.
10
Topological Sort General ideas
  • Select a vertex i (task) that has zero indegree
    (this task does not depend on any other task) ?
    so we can start this task.
  • If a vertex i (task) is selected, then for other
    vertices (tasks), the dependency on task i is
    removed.
  • It follows, we should remove all its outgoing
    arcs from i.
  • If (i, j) is an arc, then indegree(j) needs to be
    decreased by 1.
  • So, we repeat the algorithm by select another
    vertex which has zero indegree.

d
d
a
select i
a
b
i
e
b
i
e
c
c
11
Topological Sort -- Algorithm
Store all the tasks which can be selected.
Find all tasks that can be selected.
Reduce indegree(w) by 1
Place w to Q if indegree(w)0
12
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Q 0
13
Example
Indegree
-1
3
6
8
-1
0
7
2
9
1
-1
5
4
Dequeue 0 ? Q ? remove 0s arcs
adjust indegree of neighbors
Decrement 0sneighbors
OUTPUT 0
14
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Dequeue 0? Q ? Q 6, 1, 4
OUTPUT 0
15
Example
Indegree
3
-1
6
8
-1
0
7
2
9
1
5
4
Dequeue 6 ? Q 1, 4 Remove arcs ..
Adjust indegree of 6s neighbors
Adjust neighborsindegree
OUTPUT 0 6
16
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Dequeue 6 ? Q1, 4 ? Q 1, 4, 3
OUTPUT 0 6
17
Example
Indegree
-1
3
6
8
0
7
2
9
1
5
4
Dequeue 1 ? Q 4, 3 Adjust neighbors
OUTPUT 0 6 1
18
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Dequeue 1 ? Q4, 3 ? Q 4, 3, 2
OUTPUT 0 6 1
19
Example
Indegree
3
6
8
0
7
2
9
-1
1
5
4
Dequeue 4 ? Q 3, 2 Adjust neighbors
indegree
OUTPUT 0 6 1 4
20
Example
Indegree
3
6
8
0
7
2
9
1
5
4
-1
Dequeue 3 ? Q 2 Adjust 3s neighbors
OUTPUT 0 6 1 4 3
21
Example
Indegree
3
6
8
0
7
2
9
-1
1
-1
5
4
Dequeue 2 Q Adjust 2s neighbors
OUTPUT 0 6 1 4 3 2
22
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Dequeue 2 ? Q ?Q 7, 5
OUTPUT 0 6 1 4 3 2
23
Example
Indegree
3
6
8
0
7
2
9
1
5
4
-1
Dequeue 7 ? Q 5 Adjust neighbors
OUTPUT 0 6 1 4 3 2 7
24
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Dequeue 7? Q 5 ? Q 5, 8
OUTPUT 0 6 1 4 3 2 7
25
Example
Indegree
3
6
8
0
7
2
9
1
5
4
-1
Dequeue 5 ? Q 8
OUTPUT 0 6 1 4 3 2 7 5
26
Example
Indegree
3
6
8
0
7
2
9
1
5
4
-1
Dequeue 8 ? Q
OUTPUT 0 6 1 4 3 2 7 5 8
27
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Dequeue 8 ? Q ? Q 9
OUTPUT 0 6 1 4 3 2 7 5 8
28
Example
Indegree
3
6
8
0
7
2
9
1
5
4
Dequeue 9 ? Q ? Done
OUTPUT 0 6 1 4 3 2 7 5 8 9
29
Example
OUTPUT 0 6 1 4 3 2 7 5 8 9
  • Are there any other topologically orders?
  • Suppose after the algorithm has completed
    (corresponds to Q is empty),
  • there is some vertex whose indegree is not
    zero. What does that
  • mean?
Write a Comment
User Comments (0)
About PowerShow.com