2c03-review - 00084

2c03-review - 00084 - directed graph. Assume that the cost...

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

View Full Document Right Arrow Icon
SE2C03. Sample solutions to the assignment 5. Total of this assignment is 75pts. 100% equals 65. Each assignment is worth 5%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. Solutions to all questions except #5 start from next page. 1.[10] 2.[10] 3.[10] 4.[5] 5.[10] A simple cycle in a given graph (V,E), is a sequence of vertices: v 1 , v 2 , . .. , v n , such that (v i ,v i+1 ) in E for i=1,. ..,n-1, v 1 = v n , and v i v j for i j and i 1, j n (i.e. all vertices different). Modify Floyd’s algorithm (for the all-pairs shortest path problem) to find a minimum-cost simple cycle in a weighted
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: directed graph. Assume that the cost adjacency matrix of the digraph is given. The algorithm should run in O(n 3 ) time. A possible solution: Procedure Cycle( G=(V,E)) for each vertex v in V do mark[v]:=unvisited; for each vertex v in V do if mark[v]=unvisited then Cycle1(V,null); Procedure Cycle1(V,p) {p is the parent of v in the DFS tree} mark[v]:=visited; for each vertex w adjacent to v do if mark[w]=unvisited then Cycle1(V,w) e l s e i f w p then begin print(cycle exists); exit end Hence for a matrix representation we have O(n 3 ), however we can do better if G is represented as adjacency list (as in DSF). Then the algorithm checks at most n edges, so time is O(2n)=O(n). 6.[10] 7.[10] 8.[10]...
View Full Document

Ask a homework question - tutors are online