Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2018:journals:ahmadh:ch3 [2018/02/07 03:12] – ahmadh | courses:cs211:winter2018:journals:ahmadh:ch3 [2018/02/07 04:06] (current) – ahmadh | ||
|---|---|---|---|
| Line 203: | Line 203: | ||
| G has a topological ordering, then G is a DAG. | G has a topological ordering, then G is a DAG. | ||
| - | Proof: | + | Proof: |
| - | Suppose, by way of contradiction, that G has a topological ordering v_1, v_2, ..., v_n, and also has a cycle C. Let v_i be the lowest-indexed | + | ==== 3.6.1 Designing and Analyzing the Algorithm ==== |
| + | |||
| + | To come up with an efficient algorithm to compute the topological ordering in a graph, we must first establish and prove the following theory: | ||
| + | |||
| + | In every DAG G, there is a node v with no incoming edges. | ||
| + | |||
| + | Proof: Let G be a directed graph in which every node has at least one incoming edge. We show how to find a cycle in G; this will prove the claim. We pick any node v, and begin following edges backward from v: sihce v has at least | ||
| + | one incoming edge (u, v), we can walk backward to u; then, since u has at least one incoming edge (x, u), we can walk backward to x; and so on. We can continue this process indefinitely, | ||
| + | |||
| + | Theroem: | ||
| + | |||
| + | If G is a DAG, then G has a topological ordering. | ||
| + | |||
| + | (The proof for this theorem can be found on page 102 of the textbook.) | ||
| + | |||
| + | We can use the following algorithm to compute the topological ordering of a graph G: | ||
| + | |||
| + | Find a node v with no incoming edges and order it first O(n) | ||
| + | Delete v from G O(n) | ||
| + | Recursively compute a topological ordering of G-{v} O(n) | ||
| + | and append this order after v O(1) | ||
| + | |||
| + | This algorithm runs in O(n^2). | ||
| + | |||
| + | We can improve this running time to O(m+n) by modifying the algorithm slightly as follows: | ||
| + | |||
| + | We declare a node to be " | ||
| + | (a) for each node w, the number of incoming edges that w has from active nodes; and | ||
| + | (b) the set S of all active nodes in G that have no incoming edges from other active nodes. | ||
| + | At the start, all nodes are active, so we can initialize (a) and (b) with a single pass through the nodes and edges. Then, each iteration consists | ||
| + | |||
| + | ==== 3.6.2 Comments ==== | ||
| + | |||
| + | I feel like this section | ||
