cs32w11finalsol

cs32w11finalsol - CS32: Introduction to Computer Science II...

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

View Full Document Right Arrow Icon
Discussion 2C Notes (Week 10, March 11) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Solutions to Final Practice Problems [Big-O] 1. Suppose there is an array of N (~ 10,000) elements in a random order. You want to run a search and look for a certain item. Using what you have learned in this course, what is the best you can do if: (a) you run a search once? (“Is there 5 in the array?”) O(N) , using linear search. (b) you run a search 10,000 times? (“Is there 5? 16? 73? . ..”) O(N) . You can pre-process your array by traversing it and adding each element into a hash table. This process takes O(N). But after doing this once, you can simply perform your searches on this hash table, which takes O(1) per search. (Of course, the hash table must be pretty big!) 2. Consider two vector<int> ’s x and y , each having N distinct integers. We want to merge x and y to create a third vector z , such that z has all integers that x and y have. Like x and y , z should not have any duplicate numbers. We are not concerned about keeping the elements in x , y , or z in any certain order. Here is one algorithm: { z.clear(); z.reserve(x.size() + y.size()); for (int i = 0; i < x.size(); ++i) z.push_back(x[i]); for (int j = 0; j < y.size(); ++j) { bool duplicate = false; for (int i = 0; i < x.size(); ++i) { if (y[j] == x[i]) { duplicate = true; break; } } if (!duplicate) z.push_back(y[j]); } } (a) What is the complexity of this algorithm? O(N 2 ) CS32: Introduction to Computer Science II Winter 2011 Copyright 2011 Brian Choi Week 10, Page 1/9
Background image of page 1

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

View Full DocumentRight Arrow Icon
(b) Here is a different implementation of merge . What is its complexity? O(N log N) { z.clear(); z.reserve(x.size() + y.size()); for (int i = 0; i < x.size(); i++) // O(N) z.push_back(x[i]); for (int j = 0; j < y.size(); j++) // O(N) z.push_back(y[j]); sort(z.begin(), z.end()); // O(N log N) int last = 0; for (int k = 1; k < z.size(); k++) // O(N) { if (z[last] != z[k]) { last++; z[last] = z[k]; } } z.resize(last + 1); } (c) Which one performs better, (a) or (b)? (b) , because O(N log N) < O(N 2 ) (d) (Open-ended question) Is there any algorithm for merge that performs better than the version in (b)? One possible idea is to create a hash table, and store all elements in x and y into this hash table, while eliminating duplicates (if the same value is in the hash table already, ignore the value). Then you can iterate through elements in this hash table and store them into z. This takes O(N). CS32: Introduction to Computer Science II Winter 2011 Copyright 2011 Brian Choi Week 10, Page 2/9
Background image of page 2
[Trees] 3. Write nodeCount , a recursive function that counts the number of nodes in a tree rooted at node . This is a general tree, where a node can have a variable number of children. Use the following Node structure. struct Node
Background image of page 3

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

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

Page1 / 9

cs32w11finalsol - CS32: Introduction to Computer Science II...

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

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