Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
courses:cs211:winter2012:journals:jeanpaul:chapterthreesectionvi [2012/01/31 04:10] – mugabej | courses:cs211:winter2012:journals:jeanpaul:chapterthreesectionvi [2012/02/21 01:34] (current) – [Designing And Analyzing the Algorithm] mugabej | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== 3.6 Directed Acyclic Graphs and topological Ordering ====== | ====== 3.6 Directed Acyclic Graphs and topological Ordering ====== | ||
+ | \\ | ||
+ | If a directed graph has no cycles, it's called a //Directed Acyclic Graph// | ||
- | If a directed graph has no cycles, it's called | + | ==== Designing And Analyzing the Algorithm ==== |
+ | \\ | ||
+ | The first node // | ||
+ | In addition, if G is a DAG, then G has a topological ordering.\\ | ||
+ | \\ | ||
+ | |||
+ | | ||
+ | \\ | ||
+ | |||
+ | To computer a topological ordering on G:\\ | ||
+ | Find a node //v// with no incoming edges and order it first\\ | ||
+ | Delete //v// from G\\ | ||
+ | Recursively compute | ||
+ | and append this order after //v//\\ | ||
+ | \\ | ||
+ | |||
+ | **Analysis**: | ||
+ | \\ | ||
+ | --> | ||
+ | --> If G contains Θ(n²) edges, then it's linear in the size of the input and the running time is not really bad\\ | ||
+ | --> But if we have a number of edges m less than n< | ||
+ | \\ | ||
+ | To get a O(m+n) time:\\ | ||
+ | \\ | ||
+ | --> We declare a node to be " | ||
+ | -->For each node //w//, the number of incoming edges that //w// has from active nodes \\ | ||
+ | --> And a set S of all active nodes in G that have no incoming edges from other active nodes.\\ | ||
+ | \\ | ||
+ | At the start, we initialize both of the above things with a single pass through the graph since all of the nodes are active. Thus S consists of all of the nodes in G.\\ | ||
+ | --> With each iteration, we select a node //v// from the set S and delete it.\\ | ||
+ | -->After deleting //v// from S, we go through all of the nodes //w// to which //v// had an edge, and subtract one from the number of active incoming edges that we are maintaining for //w//.\\ | ||
+ | \\ | ||
+ | --> If this causes the number of active edges to //w// to drop to zero, then we add //w// to the set //w//.\\ | ||
+ | --> Proceeding | ||
+ | Thus we get a O(m+n) time. \\ | ||
+ | \\ | ||
+ | Since I wrote this after doing Problem set 4, everything made more sense and the section became really interesting. I give it a 9/10. | ||