Results 1 to 2 of 2

Thread: Big-Oh notation

  1. #1
    VBSpike
    Guest

    Unhappy

    Does anybody know a simple way to learn how to use the Big-Oh notation?????????
    I have a homework due Thursday that involves determining the complexity of various algorithms. While some of them are pretty easy, I'm having difficulties with more complex ones. My book does a really bad job explaining the problem (basically it doesn't say anything about actual complexity computing), and I'm kinda stuck. If anyone here knows how to do this stuff please help!!!!!!!

    Here are some examples from my homework if someone is interested (it's written in Java):

    a) Compute the complexity (in terms of num) of the following method.
    Assume that the methods readObject() and close() in ObjectInputStream
    have constant complexity O(1).
    Code:
    static Phone[] readPhoneList(File fname, int num) throws IOException,
        ClassNotFoundException {
        ObjectInputStream listln = 
            new ObjectInputStream(new FileInputStream(fname));
        Phone record;
        Phone[] newList = new Phone[num + 1];
        int i = 1;
        while (i <= num) {
            record = (Phone)listln.readObject();
    	newList[i] = record;
    	i++;
        }
        listln.close();
        return newList;
    }
    b) Compute the complexity (in terms of noOfParticipants) of the following
    method. Assume that the method simulateGame(t1, t2) has constant
    complexity O(1).
    Code:
    public void simulateLeague() throws NotEnoughTeamsException {
        if (participantIndex == noOfParticipants)
            for (int i = 0; i < noOfParticipants; i++)
                for (int j = i + 1; j < noOfParticipants; j++)
                    simulateGame(participatingTeams[i],
                                 participatingTeams[j]);
            else
                throw new NotEnoughTeamsException("Not enough teams in league");
        }
    c) Compute the complexity (in terms of n) of the following method. Assume
    that A is an array of length n, that the method sort(A, n) has complexity
    O(nlogn), and that the methods Random() and nextInt() have constant
    complexity O(1).
    Code:
    void (int[] A, int n) {
        Random rd = new Random();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                A[i] = rd.nextInt();
            sort(A, n);
        }
    }
    d) Compute the complexity (in terms of n) of the following piece of code:
    Code:
    sum = 0;
    if (Math.odd(n)) {
        for (int i = 0; i < n; i++)
            sum += 1;
    }
    else
        sum += n;
    e) In a sorted array we can perform binary search to find a desired element.
    A binary search algorithm keeps track of an index interval [lo, hi] in the
    array within which the desired element resides. In each step, the interval
    is divided into two parts by computing the index mid = (lo + hi)/2. If the
    element at the index mid equals the desired element, we have found it and
    do not need to continue the search. If the desired element is smaller, we
    know that it resides in the interval [lo, mid - 1], and if it is larger,
    we know it is in the interval [mid + 1, hi].
    Compute the complexity (in terms of noOfElems) of the method binarySearch.
    Is binary search better than linear search (stepping through each element
    in the array until we find the desired element)?
    Code:
    int binarySearch(int k) {
        // searches for element k in the array v
        // if k is found, output the index in v which contains k
        // otherwise, return -1
        int out = -1;
        int lo = 0;
        int hi = noOfElems - 1;
    
        while (lo <= hi) {
            mid = (lo + hi)/2;
            if (k == v[mid]) {
                out = mid;
    	    break;
            }
            if (k < v[mid])
                hi = mid - 1;
            else
                lo = mid + 1;
        }
    
        return out;
    }
    f) Compute the complexity (in terms of n) of the following piece of code.
    Assume that the array A contains some permutation of the numbers 0 to
    (n - 1), i.e., A contains all numbers from 0 to (n - 1), but in random
    order.
    Code:
    sum3 = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; A[j] != i; j++)
            sum3 += 1;
    g) Compute the complexity (in terms of n) of the following method:
    Code:
    void complicatedOperation(int n) {
        int x = 1;
        int sum = 0;
        while (x < n) {
            sum += x;
            x *= 5;
        }
    h) Compute the complexity (in terms of n) of the following piece of code:
    Code:
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < n; j++)
            for (int k = 0; k < i; k++)
    	    values[i][j] = k;
    i) Compute the complexity (in terms of noOfElems) of the following method.
    Hint: The sum of all elements b^i for which i ranges from 0 to k,
    sum_(i = 0)^k (b^i) = (b^(k + 1) - 1)/(b - 1)
    Code:
    void reallyComplicatedOperation() {
        int x = 1;
        while (x < noOfElems) {
            for (int i = 0; i < x; i++)
                values[i][x] = 10;
            x *= 2;
        }
    }

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    What do you have to do, just give the answers or do yuo have to show some kind of working? I don't really know how you'd show the working, but this is what I think their complexities are:

    a) O(N)
    b) O(N2)
    c) O(N*(N+N*log(N)) = O(N2+N2Log(N)) = O(N2)
    d) O(N/2) = O(N)
    e) O(Log(N)), better than linear search (O(N))
    f) O(N2/2) = O(N2)
    g) Don't know
    h) O(N)
    i) Don't know

    I could work out (g) and (i) but it would take me a long time.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width