CSCI2720-Exam1StudyGuide

Embed Size (px)

Citation preview

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    1/15

    Note From Aaron: I am locking the guide to view only until

    after the second class is completed with the exam. Thanks for

    being honest guys! 

    2720 Study Guide - Exam 1

    Exam Date: 02/12/15 [ TODAY ]  

    LINK TO PROFESSOR’S “STUDY GUIDE 

    LINK TO "INTRO TO ALGORITHMS" TEXTBOOK 

    Welcome! Please contribute 

    where you can and feel free to delete anything andrewrite it for ease of reading. I will begin adding topics and explanations as I go, but

     please feel free to add if you’re ahead of me!

    -Aaron 

    Apparently the test will be “80% writing code”. No, we have no idea what

    code she might want us to write. Yes, that’s ridiculous. But just be

    forewarned.

    if I have to write out merge sort I might get up and leave

    If anybody understand what she means by “Recursion Formula”, an explanationwould be greatly appreciated :D

    Didn’t we learn a method to solve the complexity of a recursive formula before welearned the master theorem? I think that might be what she is talking about

    The two things we have to use are the substitution method and the recursion-tree

    method. The substitution method involves 1) making a good guess about what itcould be and then 2)using mathematical induction to solve it.The recursion-tree is used when we can’t accurately make a good guess for the first

    step. This method gives us a more accurate guess that we will then use in thesubstitution method

    https://drive.google.com/file/d/0B37Y32GSfQwiY1J0aVRmVDFpVTQ/view?usp=sharinghttps://drive.google.com/file/d/0B37Y32GSfQwiY1J0aVRmVDFpVTQ/view?usp=sharing

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    2/15

    Key Concepts/Ideas Insertion Sort

    Binary Search

    Recursion Formula

    Time Complexity

    Master TheoremMerge sort 

    Bubble sort

    Linked List

    Stack/Queue

    How does it become 2 - 2 (1/(2^k) in HW1 prob

    4 below 

    - Can anyone explain what she means by “generalization of case 2 of the master theorem” on her

    study guide?

    If you look at wikipedia’s case two, that’s the extended case 2. It allows you to solve anything like

    T(n) = aT(n/b) + nlogn

    Is it in the textbook anywhere? I need a bit more explanation than wikipedia offers...

    Do we need to know the extended? Or just the generalized?

    Might be better to know the extended. It’s the same as the generalized because when k == 0, that log raised

    to k becomes 1 which makes it the same. But… on second thought, I don’t know how to apply it to thedifferent masters method(the one from the youtube video) so I might not worry about it.

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    3/15

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    4/15

     

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    5/15

    BINARY SEARCH

    General Info:

    ● Best case: O(1)

    ○ When the min and max are the same and the loops never initiate.

    ● Worst case: O(log n) 

    Need to know:Algorithm:

    Iterative: 

    int  binary_search ( int  A[], int  key, int imin, int imax){

    // continue searching while [imin,imax] is not empty

    while (imax >= imin){

    // calculate the midpoint for roughly equal partition

    int  imid = (imax + imin)/2;

    if (A[imid] == key)

    // key found at index imid

    return   imid;

    // determine which subarray to searchelse if  (A[imid] < key)

    // change min index to search upper subarray

    imin = imid + 1;

    else

    // change max index to search lower subarray

    imax = imid - 1;

    }

    // key was not found

    return  KEY_NOT_FOUND;

    }

    Recursive:

    int  binary_search ( int  A[], int  key,  int imin,  int imax){

    // test if array is empty

    if (imax < imin)

    // set is empty, so return value showing not found

    return  KEY_NOT_FOUND;

    else 

    {// calculate midpoint to cut set in half

    int  imid = (max + min)/2;

    // three-way comparison

    if  (A[imid] > key)

    // key is in lower subset

    return  binary_search (A, key, imin, imid - 1);

    else if  (A[imid] < key)

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    6/15

      // key is in upper subset

    return  binary_search (A, key, imid + 1, imax);

    else

    // key has been found

    return 

    imid;}

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    7/15

    MERGE SORT

    General Info:

    ○ Best case: O(n)

    ○ Worst case/Average Case: O(nlogn)

    Need to know:

    Algorithm(THIS IS WRONG...Care to explain why?...sorry it’s not wrong, it’s just not how we learned

    in class...cool, cause I already “memorized” this one and was panicking.. haha): 

    void 

    merge( 

    int 

    *,int*, 

    int 

    int 

    ,int 

    ); 

    void  mergesort( int  *a, int *b, int  low, int high)

    {

    int  pivot;

    if(low

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    8/15

    {

    int  h,i,j,k;

    h=low;

    i=low;

    j=pivot+1;

    while((h

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    9/15

    BUBBLE SORT

    General Info:

    Best Case (when list is already sorted): O(n)

    Worst/Average case: О(n 

    2 ) 

    Need to know:

    Algorithm: 

    int  main(){

    //declaring array

    int  array[5];

    cout

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    10/15

    LINKED LIST

    General Info: Here's an awesome resource from Stanford on linked lists. 

    Need to know:

    Basically a linked list is a collection of nodes with each node containing only the data it stores, as well as the

    address for the next node. To iterate through a Linked List, you must start at the head and go to the next

    node until you get to the end (NULL).

    Algorithm:

    struct Node { //create node structure

    int  data;

    Node * next;

    }; 

    void  insertFront( struct  Node  **head, int  n) { //add new node to front of the linked

    list

    Node  *newNode = new  Node ;

    newNode-> data   = n;

    newNode-> next   = *head;

    *head = newNode;

    http://cslibrary.stanford.edu/103/LinkedListBasics.pdf

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    11/15

    RECURSION FORMULA

    General Info:

    Need to know:

    Algorithm:

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    12/15

     

    TIME COMPLEXITY

    General Info:

    Think of Big O as (controlled by): x is big O of x 2 f(x) = O(x  2 )

    x is controlled by x

    2

     Think of Big Ω as (controls): x  2  is big Ω of x f(x 2 ) = Ω(x)

    x  2  controls x

    Think of Big Θ as (the same as): x 

    2  is big Θ of x2  f(x

     

    2 ) = Θ(x

     

    2 )

    x  2  is the same as x2

    In Layman’s Terms... 

    O: The function f(n) takes less than or as much time as ...

    i.e. The theoretical upper bounds we talk about with sorting methods.

    Omega: The function takes as much time as ... or more.

    i.e. the theoretical “best case” scenarios we talk about with sorting methods.

    Theta: The function takes exactly ... time, variable only by a constant multiplier.

    i.e. “tight asymptotic bounds”, as found in problems to which the master method can be applied.

    Think of the capital  letters as >=   or  or O(n 2 ) > O(nlogn) > O(n) > O(logn) > O(1)

    Algorithm

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    13/15

    MASTER THEOREM

    General Info:

    Need to know:

    Could someone explain what T(1) = c means? And why c hasto be >0?

    Sure! T is a recurrence relation representing how long it takes to complete a given method, right? So

    T(x) is a formula that represents how long it will take for that method to complete x times. Below, c is

    a numeric value. It has to be >0 because time can’t be negative. And T(1) = c is a requirement

    because the method needs to take a constant amount of time to run each time, otherwise this whole

    thing would be pointless.

    Thanks!

    Let be a monotonically increasing function that satisfies:(n)T   

    (n) aT ( )  f  (n)T    =bn +

    (1) c T    =

    where . If where , then≥ 1,  b ≥ 2,  c  0a   > (n)∈ Θ(n   ) f     d  

    ≥ 0d   

    (n) Θ(n  )T    = d  if ba   <d 

     

    if(n)T    = (n   log  n)Θ   d    ba   =d  

    if(n)T    = (n   )Θ   log   ab   ba   >d  

    This powerpoint/pdf is pretty helpful for breaking it down:

    http://cse.unl.edu/~choueiry/S06-235/files/MasterTheorem.pdf  

    Practice problems: http://www.csd.uwo.ca/~moreno/CS433-CS9624/Resources/master.pdf  

    Can someone explain the additional requirements for the third

    case of the Master Theorem? In the book it says “and if

    a*f(n/b) ≤ c*f(n) for some constant c

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    14/15

     

    --- can’t use master method b/c a, (2^n), is not a constant

    ^ thats the answer, unsolvable

    ( http://www.csd.uwo.ca/~moreno/CS424/Ressources/master.pd

    f  

    ).well it might be solvable, but not by using the master

    theorem.(

  • 8/9/2019 CSCI2720-Exam1StudyGuide

    15/15

    Q and AProblem 4 HW1:

    4. Find out the time complexity for T(n) = T(n/2) + c*n and T(1) = c0, where c and c0 are constants.

    How does it become 2 - 2 (1/(2^k)?