graph algorithms: topological sort

42
Graph Algorithms: Topological Sort pological sorting problem: given a directed, c graph G = (V, E) , find a linear ordering rtices such that or all (v, w) E, v precedes w in the orderi A B C F D E

Upload: cora

Post on 21-Jan-2016

160 views

Category:

Documents


4 download

DESCRIPTION

Graph Algorithms: Topological Sort. The topological sorting problem: given a directed, acyclic graph G = ( V , E ) , find a linear ordering of the vertices such that for all ( v , w )  E , v precedes w in the ordering. B. C. A. F. D. E. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w) E, v precedes w in the ordering.

A

BC

F

D E

Page 2: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w) E, v precedes w in the ordering.

A

BC

F

D

EA D

E

FB C

Page 3: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w) E, v precedes w in the ordering.

A

BC

F

D

EA D

E

FB C

Any linear ordering in whichall the arrows go to the right.

Page 4: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w) E, v precedes w in the ordering.

A

BC

F

D

FA D

E

EB C

Any linear ordering in whichall the arrows go to the right.

This is not a topological ordering.

Page 5: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Identify the subset of vertices that have no incoming edge.

A

BC

F

D E

Page 6: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—why?)

A

BC

F

D E

Page 7: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—because the graph is acyclic.)

A

BC

F

D E

Page 8: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. Select one of them.

A

BC

F

D E

Page 9: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Remove it, and its outgoing edges, and add it to the output.

A

BC

F

D E

Page 10: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, . . .

A

BC

F

D E

Page 11: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, . . .

A

BC

F

D E

Page 12: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A

BC

F

D E

Page 13: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A

BC

F

D E

Page 14: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A B

C

F

D E

Page 15: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A B

C

F

D E

Page 16: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A B CF

D E

Page 17: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A B CF

D E

Page 18: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A B CF D

E

Page 19: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A B CF D

E

Page 20: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output.

A B CF D E

Page 21: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: finished!

A B CF D EA

BC

F

D E

Page 22: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound?

A B CF D EA

BC

F

D E

Page 23: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound: Break down into total time to:

Find vertices with no predecessors: ?Remove edges: ?Place vertices in output: ?

A B CF D EA

BC

F

D E

Page 24: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound: Break down into total time to:

Find vertices with no predecessors: ?Remove edges: O(|E|)Place vertices in output: ?

A B CF D EA

BC

F

D E

Page 25: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound: Break down into total time to:

Find vertices with no predecessors: ?Remove edges: O(|E|)Place vertices in output: O(|V|)

A B CF D EA

BC

F

D E

Page 26: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

Find vertices with no predecessors: ?

A

BC

F

D E

A

B

C

D

E

F

B D

E

ED

C

Assume an adjacency list representation:

Page 27: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:

…and initialize and maintain for each vertex its no. of predecessors.

A

BC

F

D E

A

B

C

D

E

F

B D

E

ED

C00

1

2

1

2

0

1

0

2

2

1

Page 28: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

Find vertices with no predecessors: ?

A

BC

F

D E

Time for each vertex: O(|V|)

A

B

C

D

E

F

B D

E

ED

C

0

1

0

2

2

1

Page 29: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

Find vertices with no predecessors: ?

A

BC

F

D E

Total time: O(|V| )2

A

B

C

D

E

F

B D

E

ED

C

0

1

0

2

2

1

Page 30: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound: Break down into total time to:

Find vertices with no predecessors: O(|V| )Remove edges: O(|E|)Place vertices in output: O(|V|)

2

Page 31: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound: Break down into total time to:

Find vertices with no predecessors: O(|V| )Remove edges: O(|E|)Place vertices in output: O(|V|)

2

Total: O(|V| + |E|)2

Page 32: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound: Break down into total time to:

Find vertices with no predecessors: O(|V| )Remove edges: O(|E|)Place vertices in output: O(|V|)

2

Total: O(|V| + |E|)2

Too much!

Page 33: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:We need a faster way to do this step: Find vertices with no predecessors.

Page 34: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:Key idea: initialize and maintain a queue (or stack)holding pointers to the vertices with 0 predecessors

A

BC

F

D E

A

B

C

D

E

F

B D

E

ED

C00

1

2

1

2

0

1

0

2

2

1

Page 35: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:As each vertex is removed, update the predecessorcounts, and for any vertex whose count has becomezero, put it in the queue.

A

BC

F

D E

A

B

C

D

E

F

B D

E

ED

C00

1

2

1

2

0

1

0

2

2

1

Page 36: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:As each vertex is removed, update the predecessorcounts, and for any vertex whose count has becomezero, put it in the queue.

BC

D E

A

B

C

D

E

F

E

ED

C

0

0

1

1

2

0

0

0

1

2

1

F

Output: A

No scan is required, so O(1).

Page 37: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:As each vertex is removed, update the predecessorcounts, and for any vertex whose count has becomezero, put it in the queue.

BC

D E

A

B

C

D

E

F

E

ED

C0

1

1

2

0

0

0

1

2

1

Output: A F

Page 38: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:As each vertex is removed, update the predecessorcounts, and for any vertex whose count has becomezero, put it in the queue.

C

D E

A

B

C

D

E

F

E

ED

1

0

2

0

0

0

1

2

0

Output: A F B

Page 39: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:As each vertex is removed, update the predecessorcounts, and for any vertex whose count has becomezero, put it in the queue.

D E

A

B

C

D

E

F

E0 1

0

0

0

0

1

0

Output: A F B C

Page 40: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:As each vertex is removed, update the predecessorcounts, and for any vertex whose count has becomezero, put it in the queue.

E

A

B

C

D

E

F

0

0

0

0

0

0

0

Output: A F B C D

Page 41: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm:As each vertex is removed, update the predecessorcounts, and for any vertex whose count has becomezero, put it in the queue.

Finished!

A

B

C

D

E

F

0

0

0

0

0

0

Output: A F B C D E

Page 42: Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort

The topological sorting algorithm: Time bound: Now the time for each part is

Find vertices with no predecessors: O(|V|)Remove edges: O(|E|)Place vertices in output: O(|V|)

Total: O(|V|+|E|)

Linear in |V|+|E|.Much better!