CS 211 Assignment 4

CS 211 Assignment 4 - CS 211 Assignment 4 Fall 2007 Due...

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

View Full Document Right Arrow Icon
CS 211 Assignment 4, Fall 2007 Due October 25, 2007 11:59:59 PM 1 Big-O Running Time Ordering Arrange the following functions in increasing order of growth rate (where n is the input size): n 2 , 2 n , n log n, 100000000, 5000 log n, n log log n, n!, n log n , n n . 2 Big-O Running Time Determine the running time for the following pieces of code. No explanation is needed. (i) for(int i=0; i<n; i++) total++; (ii) for(int i=0; i<n; i++) { for(int j=0; j<n; j++) total++; } for(int k=0; i<100000000000*n; i++) total++; (iii) for(int i=0; i<n; i++) { for(int j=0; j<n/2; j++) { n = j%n; } n = i%(n+1); } (iv) Assume g(n) is a function that takes O(n) for(int i=0; i< n; i++) for(int j=0; j<i; j++) for(int k=0; k<j; k++) g(n); (v) Assume p>=0 for(int i=0; i < n; i++) { while(i>p) i++; n++; } 3 Nearest Neighbor Problem 3.1 Introduction The purpose of this problem is to practice implementing algorithms on trees and understand
Image of page 1

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

View Full Document Right Arrow Icon
how to use interfaces. To get a better understanding of why tree is a useful data structure, you will implement an Nearest Neighbor algorithm (locating the nearest point to a given point) in two ways: a linked list and a 2D tree. 3.2 2D Trees Two-dimensional trees are dynamic, adaptable data structures which are very similar to binary trees but divide up a geometric space in a manner convenient for use in range searching and other problems. The idea is to build binary search trees with points in the nodes, using the x or y coordinate of the points as keys in an alternating sequence. The algorithm used for inserting points into 2D trees is the same as for normal binary search trees with a small modification: Suppose we want to insert a point p with coordinates ( x; y ) into a 2D tree with root node r . 1. At the root, if p has a smaller y coordinate than the point at the root, recrusively call insert method with p on r ’s left subtree (if one doesn’t exist, set r ’s left child to be p ); else recursively call insert method with p on r ’s right subtree (again, if it doesn’t exist, set p to be r ’s right child). 2. At each level alternate which coordinate is used for checking which subtree to go to: if in the previous level of the tree we used the y coordinate as the key, then in this level use the x coordinate, and in the next level use the y coordinate again, etc.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern