The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Topological Sorting Next: Minimum Spanning Tree Up: Graph Problems: Polynomial-Time Previous: Connected Components Topological Sorting Input description: A directed acyclic graph G=(V,E), also known as a partial order or poset. Problem description: Find a linear ordering of the vertices of V such that for each edge , vertex i is to the left of vertex j. Discussion: Topological sorting arises as a natural subproblem in most algorithms on directed acyclic graphs. Topological sorting orders the vertices and edges of a DAG in a simple and consistent way and hence plays the same role for DAGs that depth-first search does for general graphs. Topological sorting can be used to schedule tasks under precedence constraints. Suppose we have a set of tasks to do, but certain tasks have to be performed before other tasks. These precedence constraints form a directed acyclic graph, and any topological sort (also known as a linear extension) defines an order to do these tasks such that each is performed only after all of its constraints are satisfied. Three important facts about topological sorting are: ● Only directed acyclic graphs can have linear extensions, since any directed cycle is an inherent file:///E|/BOOK/BOOK4/NODE160.HTM (1 of 3) [19/1/2003 1:30:55]
Topological Sorting contradiction to a linear order of tasks. ● Every DAG can be topologically sorted, so there must always be at least one schedule for any reasonable precedence constraints among jobs. ● DAGs typically allow many such schedules, especially when there are few constraints. Consider n jobs without any constraints. Any of the n! permutations of the jobs constitutes a valid linear extension. A linear extension of a given DAG is easily found in linear time. The basic algorithm performs a depthfirst search of the DAG to identify the complete set of source vertices, where source vertices are vertices without incoming edges. At least one such source must exist in any DAG. Note that source vertices can appear at the start of any schedule without violating any constraints. After deleting all the outgoing edges of the source vertices, we will create new source vertices, which can sit comfortably to the immediate right of the first set. We repeat until all vertices have been accounted for. Only a modest amount of care with data structures (adjacency lists and queues) is needed to make this run in O(n+m) time. This algorithm is simple enough that you should be able to code up your own implementation and expect good performance, although implementations are described below. Two special considerations are: ● What if I need all the linear extensions, instead of just one of them? - In certain applications, it is important to construct all linear extensions of a DAG. Beware, because the number of linear extensions can grow exponentially in the size of the graph. Even the problem of counting the number of linear extensions is NP-hard. Algorithms for listing all linear extensions in a DAG are based on backtracking. They build all possible orderings from left to right, where each of the in-degree zero vertices are candidates for the next vertex. The outgoing edges from the selected vertex are deleted before moving on. Constructing truly random linear extensions is a hard problem, but pseudorandom orders can be constructed from left to right by selecting randomly among the in-degree zero vertices. ● What if your graph is not acyclic? - When the set of constraints is not a DAG, but it contains some inherent contradictions in the form of cycles, the natural problem becomes to find the smallest set of jobs or constraints that if eliminated leaves a DAG. These smallest sets of offending jobs (vertices) or constraints (edges) are known as the feedback vertex set and the feedback arc set, respectively, and are discussed in Section . Unfortunately, both of them are NP-complete problems. Since the basic topological sorting algorithm will get stuck as soon as it identifies a vertex on a directed cycle, we can delete the offending edge or vertex and continue. This quick-and-dirty heuristic will eventually leave a DAG. Implementations: Many textbooks contain implementations of topological sorting, including [MS91] file:///E|/BOOK/BOOK4/NODE160.HTM (2 of 3) [19/1/2003 1:30:55]
- Page 419 and 420: Discrete Fourier Transform an algor
- Page 421 and 422: Combinatorial Problems Next: Sortin
- Page 423 and 424: Sorting Next: Searching Up: Combina
- Page 425 and 426: Sorting The simplest approach to ex
- Page 427 and 428: Sorting operations, implying an sor
- Page 429 and 430: Searching large performance improve
- Page 431 and 432: Searching Notes: Mehlhorn and Tsaka
- Page 433 and 434: Median and Selection followed by fi
- Page 435 and 436: Generating Permutations Next: Gener
- Page 437 and 438: Generating Permutations The rank/un
- Page 439 and 440: Generating Permutations generating
- Page 441 and 442: Generating Subsets look right when
- Page 443 and 444: Generating Subsets above for detail
- Page 445 and 446: Generating Partitions Although the
- Page 447 and 448: Generating Partitions Two related c
- Page 449 and 450: Generating Graphs generate: ● Do
- Page 451 and 452: Generating Graphs Combinatorica [Sk
- Page 453 and 454: Calendrical Calculations Next: Job
- Page 455 and 456: Calendrical Calculations Gregorian,
- Page 457 and 458: Job Scheduling ● To assign a set
- Page 459 and 460: Job Scheduling shop scheduling incl
- Page 461 and 462: Satisfiability logic, and automatic
- Page 463 and 464: Satisfiability Next: Graph Problems
- Page 465 and 466: Graph Problems: Polynomial-Time rec
- Page 467 and 468: Connected Components Testing the co
- Page 469: Connected Components discussing gra
- Page 473 and 474: Minimum Spanning Tree Next: Shortes
- Page 475 and 476: Minimum Spanning Tree help you sort
- Page 477 and 478: Shortest Path Next: Transitive Clos
- Page 479 and 480: Shortest Path easier to program tha
- Page 481 and 482: Shortest Path Related Problems: Net
- Page 483 and 484: Transitive Closure and Reduction
- Page 485 and 486: Transitive Closure and Reduction Al
- Page 487 and 488: Matching augmenting paths and stopp
- Page 489 and 490: Matching Combinatorica [Ski90] prov
- Page 491 and 492: Eulerian Cycle / Chinese Postman ar
- Page 493 and 494: Eulerian Cycle / Chinese Postman Ne
- Page 495 and 496: Edge and Vertex Connectivity Severa
- Page 497 and 498: Edge and Vertex Connectivity Next:
- Page 499 and 500: Network Flow programming model for
- Page 501 and 502: Network Flow Combinatorica [Ski90]
- Page 503 and 504: Drawing Graphs Nicely vertices are
- Page 505 and 506: Drawing Graphs Nicely labs.com/orgs
- Page 507 and 508: Drawing Trees such as the map of th
- Page 509 and 510: Planarity Detection and Embedding N
- Page 511 and 512: Planarity Detection and Embedding p
- Page 513 and 514: Graph Problems: Hard Problems ● C
- Page 515 and 516: Clique approximate even to within a
- Page 517 and 518: Independent Set Next: Vertex Cover
- Page 519 and 520: Independent Set Related Problems: C
Topological Sorting<br />
contradiction to a linear order of tasks.<br />
● Every DAG can be topologically sorted, so there must always be at least one schedule for any<br />
reasonable precedence constraints among jobs.<br />
● DAGs typically allow many such schedules, especially when there are few constraints. Consider n<br />
jobs without any constraints. Any of the n! permutations of the jobs constitutes a valid linear<br />
extension.<br />
A linear extension of a given DAG is easily found in linear time. <strong>The</strong> basic algorithm performs a depthfirst<br />
search of the DAG to identify the complete set of source vertices, where source vertices are vertices<br />
without incoming edges. At least one such source must exist in any DAG. Note that source vertices<br />
can appear at the start of any schedule without violating any constraints. After deleting all the outgoing<br />
edges of the source vertices, we will create new source vertices, which can sit comfortably to the<br />
immediate right of the first set. We repeat until all vertices have been accounted for. Only a modest<br />
amount of care with data structures (adjacency lists and queues) is needed to make this run in O(n+m)<br />
time.<br />
This algorithm is simple enough that you should be able to code up your own implementation and expect<br />
good performance, although implementations are described below. Two special considerations are:<br />
● What if I need all the linear extensions, instead of just one of them? - In certain applications, it is<br />
important to construct all linear extensions of a DAG. Beware, because the number of linear<br />
extensions can grow exponentially in the size of the graph. Even the problem of counting the<br />
number of linear extensions is NP-hard.<br />
<strong>Algorithm</strong>s for listing all linear extensions in a DAG are based on backtracking. <strong>The</strong>y build all<br />
possible orderings from left to right, where each of the in-degree zero vertices are candidates for<br />
the next vertex. <strong>The</strong> outgoing edges from the selected vertex are deleted before moving on.<br />
Constructing truly random linear extensions is a hard problem, but pseudorandom orders can be<br />
constructed from left to right by selecting randomly among the in-degree zero vertices.<br />
● What if your graph is not acyclic? - When the set of constraints is not a DAG, but it contains<br />
some inherent contradictions in the form of cycles, the natural problem becomes to find the<br />
smallest set of jobs or constraints that if eliminated leaves a DAG. <strong>The</strong>se smallest sets of<br />
offending jobs (vertices) or constraints (edges) are known as the feedback vertex set and the<br />
feedback arc set, respectively, and are discussed in Section . Unfortunately, both of them are<br />
NP-complete problems.<br />
Since the basic topological sorting algorithm will get stuck as soon as it identifies a vertex on a<br />
directed cycle, we can delete the offending edge or vertex and continue. This quick-and-dirty<br />
heuristic will eventually leave a DAG.<br />
Implementations: Many textbooks contain implementations of topological sorting, including [MS91]<br />
file:///E|/BOOK/BOOK4/NODE160.HTM (2 of 3) [19/1/2003 1:30:55]