Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 1 (24 points): Purpose: To practice analyzing algorithms for their complexity Degree of Difficulty: Easy to Moderate. Restrictions: This question is homework assigned to
Question 1 (24 points): Purpose: To practice analyzing algorithms for their complexity Degree of Difficulty: Easy to Moderate. Restrictions: This question is homework assigned to students and will be graded. This question shall not be distributed to any person except by the instructors of CMPT 145. Solutions will be made available to students registered in CMPT 145 after the due date. There is no educational or pedagogical reason for tutors or experts outside the CMPT 145 instructional team to provide solutions to this question to a student registered in the course. Students who solicit such solutions are committing an act of Academic Misconduct, according to the University of Saskatchewan Policy on Academic Misconduct. For this question, you'll be presented with a number of scenarios or algorithms. Your task will be to analyze them with respect to their computational complexity. No programming is required for this question. What to Hand In A written document named a7q1.pdf with your analysis answers for each part. Be sure to include your name. NSID. student number, course number and instructor name at the top of all documents. Evaluation . 3 marks per part: Your analysis is coherent and correct. Scenarios (a) (3 points) def search(L, target): # L is a list found = False i = 0 while i b: return 1 elif b> a: return -1 else: return 0 With regard to the function above: . What is the size of the input"? What is its big-O time complexity? Very briefly justify your answer. (d) (3 points) def is-prime (a): #a is an integer if a == 1: return false elif a == 2 or a == 3: return True result = True for i in range (2, a//2): if a % i == 0: result = False return True With regard to the function above: . What is the 'size of the input"? What is its big-O time complexity? Very briefly justify your answer. (e) (3 points) def find_triples (L): # L is a list if len (L) == 0: return 0 L = sorted (L) triples = 0 repeats = 1 prev = L[0] for i in range (1, len(L)): cur L[i] if cur != prev: repeats = 1 else: repeats += 1 if repeats == 3: triples += 1 prev = cur return triples With regard to the function above: What is the 'size of the input"? What is its big-O time complexity? Very briefly justify your answer. (f) (3 points) def interleave (L1, L2): # L1 and L2 are lists i = 0 result = [] while i len (L2): longer = L1 else: longer = L2 for j in range(i, len(longer)): result.append (longer[j]) return result With regard to the function above: . What is the 'size of the input"? What is its big-O time complexity? Very briefly justify your answer. (g) (3 points) Consider a single elimination chess tournament. A single elimination format means that as soon as a player loses a single game, they are out of the tournament. Players who win their games keep playing until only a single undefeated player remains. For example, if we had a tournament with 4 players (Godzilla, Mothra, Gigan and Ghidorah), we would need a total of 3 matches to complete the tournament as follows: Match A: Godzilla vs Mothra Match B: Gigan vs Ghidorah Match C: Winner of A vs Winner of B Let time be measured in terms of the number of games needed to complete the tournament. What is the big-O notation for the time needed to complete a single elimination tournament with N players? Briefly justify your answer. (h) (3 points) Consider a round-robin chess tournament. A round-robin format means that every player must play each other player exactly once. For example, if we had a tournament with 4 players (Godzilla, Mothra, Gigan and Ghidorah), we would need a total of 6 matches to complete the tournament as follows: . Godzilla vs Mothra Godzilla vs Gigan . Godzilla vs Ghidorah Mothra vs Gigan Mothra vs Ghidorah Gigan vs Ghidorah Let time be measured in terms of the number of games needed to complete the tournament. What is the big-O notation for the time needed to complete a round-robin tournament with N players? Briefly justify your
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started