��2

��2 - Chapter 2 Exercises 8 Chapter 3 Exercises...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 2 Exercises 8 Chapter 3 Exercises 2 ØR ‚ ´ ª ØR ‚ ´ ª ØR ‚ ´ ª = Exercises 3 ØR ‚ ´ ª xß ‚ ´ ª = Е º ·ª= a)Ø R ‚ ´ ª = b)x ß ‚ ´ ª = Е º ·ª= ØR ‚´ ª Ga B 3 % %l % %»  % X adjacent list Ø R ‚ ´ ª h uØ R ‚ ´ ª adjacent list[u] (u, v)ˆ h adjacent list[u]h vØ R ‚ ´ ª •1• ´ª= йÂ% 3.19 á t s Vì = R DFS Ø R ‚ ´ ª = ØR ‚ ´ ª MyToplogicalOrder(G=(V, E)){ for each node u in G{//O(n) inDegree[u]=0; nodes[u].type = -1; visited[u]=0; } for each edge(u,v) start from node {//O(m) inDegree[v]++; } for each node u in G{//O(n) if inDegree[u]==0 then{ add u to singleIns list; visited[u]=1; } } counter=0; while singleIns is not empty{//BFS, O(m+n) let current be the first node in the singleIns list; remove current from singleIns list; counter++; order[counter]=current; for each edge(current, v) start from current{ remove edge(current, v);//for example, remove v from adjacent list[current] inDegree[v]--; if inDegree[v]==0 then{ add v to singleIns list; visited[v]=1; } } h remove node current from graph G; //for example, set adjacent list[current]=NULL } if counter==n then{ return order; }else then{ //Law 3.19: in every DAG, there is a node v with no incoming edges //every node still in G should be in some cycles arbitrary chose a node u still in G; DFS(u);//O(n) return the subsequence of cyclePath that start and end with the same node; //O(n) } } DFS(u){ visited[u]=1; append u to cyclePath; //notation: the edges be removed is not in the adjacent list any more for each edge(u, v) still in G that start from u{ if visited[v]==1 then{ append v to cyclePath; return cyclePath; }else if visited[v]==0 then{ return DFS(v); } } remove u from cyclePath; } ¸R ‚ ´ ª = %»Â % a O(m+n)h % B3 BFS h DFS ¸ R ‚ ´ ª = ¸R ‚ ´ ª= BFS h while ¸ R ‚ ´ ª n¸ R ‚ ´ ª = for ° z … ´ ª = й % O(m)h ¸ R ‚´ ª O(n)h BFS ¸ R ‚ ´ ª = O(m+n)h DFS ¨ 3.19 ¸ R ‚ ´ ª = z Gh n-counter ¨z á ts Vì = R O(n)h Exercises 4 ¸R ‚ ´ ª °z … ´ ª = consistent ° z … ´ ª = й Â% й Â% consistent ¨z type h judgement S different ¸ R ‚ ´ ª judgements h º Â% át sVì = R judgement ¸ R ‚ ´ ª = same h % judgement h same h different »  %% a B3 % uw … ´ ª= Ð ¹Â% 0h 1 u w … ´ ª = Ð ¹  % connected tree h 0u w … ´ ª = й  % same H T ‚´ ª = 0h different H T ‚ ´ ª = (0+1)%2h H T‚´ª = connected tree H T ‚ ´ ª = falseh H T‚ ´ª ConsistentJudgment(G=(V, E)){ for each node u in G{//O(n) type[u]=-1; visited[u]=0; } treeNum=0;//number of connected trees for each node u in G{//just to chose a node to do the BFS, O(m+n) if visited[u]==0 then{ treeNum++; if treeNum>1 then{//cannot judge two nodes from different connected trees return false; } type[u]=0; visited[u]=1; add u to BFSList; while BFSList is not empty{ let current be the first node in BFSList; remove current from BFSList; for each edge(current, v) start from current{ if the type of the edge is "same" then{ tempType=type[current]; }else if the type of the edge is "different" then{ tempType=(type[current]+1)%2; } if visited[v]==0 then{ type[v]=tempType; add v to BFSList; }else if type[v]!=tempType then{//conflicts happens, not consitent return false; } } } } } return true; } HT‚ ´ª = xß‚ ´ª = Ð •º· ª = BFS O(m+n)h 3 3w … ´ ...
View Full Document

This note was uploaded on 12/07/2010 for the course CS 299 taught by Professor Zhuhong during the Spring '10 term at Fudan University.

Ask a homework question - tutors are online