Posted by: jeffc | October 16, 2010

Lecture 20 10/15/10

(Guest post by Jeffrey Chu)

Lemma2: If G is a graph, then G has a topological sorting.
Recall:
Topological Sorting: ordering of the vertices
v1, v2,….,vn
for all(vi,vj) in E i < j
Q: Given topological sorting
v1,….,vn
# of incoming edges to v1? answer: 0 because all edges go forward and v1 is the first node.
Lemma3: If G is a DAG then there always exist a v in V such that v has 0 incoming edges.
Algo:
Topo-Sort(G)
1) Find a v such that v has 0 incoming edges
2) Compute G’ = G\{v} <- remove v + all edges incident to v
3) Output: v, Topo-Sort(G)
Correctness of the algo
1) Can find v by Lemma3
3) Claim: G’ is algo a DAG
Prove by induction on # vertices in G.
-> n = 1 (checked)
-> n > 1
|v(G’)| = |v(G)| -1
Topo-Sort(G’) is a topological sorting for G’
Prove by Contradiction
prove by picture
-there is a cycle
-if there is a cycle in G’ then there is a cycle in G
-v(G’) subset v(G)
-E(G’) subset E(G)
Proof idea for Lemma3:
G is a DAG
For contradiction,
assume for all v in V
v has at least one incoming edge.
prove by picture:
-every v has incoming edge
w1 <- w2 <- w3 <- w4 ….. wn <- wn+1
as |v| = n
=> two wi & wj repeat            i.e. wi = wj
gives you a cycle, contradicts that G is a DAG
Running Time:
# calls to Topo-sort? answer: n times
Claim: step 1 + step 2 is O(n)
over all is O(n^2)
G -> adjacency list
check if incoming link list is empty
O(1) to check
so for n nodes its O(n) time (step 1)
(Step 2)maintain array Present[v] = T if u in v(G)

Leave a comment

Categories