h4-sol - Homework 4 Solutions Fundamental Algorithms,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: Homework 4 Solutions Fundamental Algorithms, Spring 2008, Professor Yap Due: Wed Apr 2, in class. HOMEWORK with SOLUTION, prepared by Instructor and T.A.s INSTRUCTIONS: • Please read questions carefully. When in doubt, please ask. • Please write succinctly, to the point. Every sentence must be an complete English sentence. • You may post general questions to the homework discussion forum in class website. Also, bring your questions to recitation on Monday. 1. (10 Points) Question 2.3 in Lect.IV (algorithm to determine if two closed paths p and q are equivalent). Give an algorithm which, given two closed paths p = ( v − v 1 −···− v k ) and q = ( u − u 1 −···− u ℓ ), de- termine whether they represent the same cycle (i.e., are equivalent). The complexity of your algorithm should be O ( k 2 ) in general, but O ( k ) for when q is a simple cycle. NOTE: Assume that vertices are integers, and the closed path p = ( v −···− v k ) is represented by an array of integers p [0 ..k ], where p [ i ] = v i and p [0] = p [ k ]. 1 SOLUTION: We introduce the following procedures: Procedure Find returns the position of vertex v in path p if found, − 2 otherwise. int Find (int v , path p , int z (starting position) ) i := z ; while ( i ≤ length ( p )) if ( v = p [ i ]) return i ; i := i + 1; return − 1; Procedure Match returns True/False depending on whether path p matches path q starting at q [ j ] modulo length L . Bool Match (path p , path q , int j , int L ) for i = 0 to L if p [ i ] negationslash = q [ j + i mod L ] return False; return True; Procedure Equal will determine if two given cycles are equivalent: Bool Equal (path p , path q ) if length ( p ) negationslash = length ( q ) return False; L := length ( p ); j := 0; while j ≥ j := Find ( p [0] ,q,j ); if j < 0 return False; if Match ( p,q,j,L...
View Full Document

This note was uploaded on 05/11/2010 for the course COMPUTER S 301 taught by Professor . during the Spring '10 term at Kadir Has Üniversitesi.

Page1 / 4

h4-sol - Homework 4 Solutions Fundamental Algorithms,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online