Вы находитесь на странице: 1из 15

NoteFromAaron:Iamlockingtheguidetoviewonlyuntil

afterthesecondclassiscompletedwiththeexam.Thanksfor
beinghonestguys!

2720StudyGuideExam1
ExamDate:02/12/15[TODAY]

LINKTOPROFESSORSSTUDYGUIDE

LINKTO"INTROTOALGORITHMS"TEXTBOOK

Welcome!
Pleasecontribute
whereyoucanandfeelfreetodeleteanythingand
rewriteitforeaseofreading.IwillbeginaddingtopicsandexplanationsasIgo,but
pleasefeelfreetoaddifyoureaheadofme!
Aaron

Apparentlythetestwillbe80%writingcode.No,wehavenoideawhat
codeshemightwantustowrite.Yes,thatsridiculous.Butjustbe
forewarned.
ifIhavetowriteoutmergesortImightgetupandleave

IfanybodyunderstandwhatshemeansbyRecursionFormula,anexplanation
wouldbegreatlyappreciated:D
Didntwelearnamethodtosolvethecomplexityofarecursiveformulabeforewe
learnedthemastertheorem?Ithinkthatmightbewhatsheistalkingabout

Thetwothingswehavetousearethesubstitutionmethodandtherecursiontree
method.Thesubstitutionmethodinvolves1)makingagoodguessaboutwhatit
couldbeandthen2)usingmathematicalinductiontosolveit.
Therecursiontreeisusedwhenwecantaccuratelymakeagoodguessforthefirst
step.Thismethodgivesusamoreaccurateguessthatwewillthenuseinthe
substitutionmethod

KeyConcepts/Ideas
InsertionSort
BinarySearch
RecursionFormula
TimeComplexity
MasterTheorem
Mergesort
Bubblesort
LinkedList
Stack/Queue

Howdoesitbecome22(1/(2^k)inHW1prob
4below

Cananyoneexplainwhatshemeansbygeneralizationofcase2ofthemastertheoremonher
studyguide?

Ifyoulookatwikipediascasetwo,thatstheextendedcase2.Itallowsyoutosolveanythinglike
T(n)=aT(n/b)+nlogn

Isitinthetextbookanywhere?Ineedabitmoreexplanationthanwikipediaoffers...

Doweneedtoknowtheextended?Orjustthegeneralized?
Mightbebettertoknowtheextended.Itsthesameasthegeneralizedbecausewhenk==0,thatlograised
tokbecomes1whichmakesitthesame.Butonsecondthought,Idontknowhowtoapplyittothe
differentmastersmethod(theonefromtheyoutubevideo)soImightnotworryaboutit.

INSERTIONSORT
GeneralInfo:
Bestcase:O(n)(linear)b/citstillhastogothrougheachelement
2
Worstcase:O(n
)(quadratic)
Everyiterationoftheloopwillscanandshifttheentiresortedportionoverbeforeinsertion.
Efficientforsortingasmallnumberofelements
Examplerun:
3
7495261
3
7
495261
37

4
95261
347

9
5261
3479

5
261
34579

2
61
234579

6
1
2345679

1
12345679

Needtoknow:

Algorithm:
voidinsertionSort(int*array,intlength){
intj,temp
for(inti=1i<lengthi++){
j=i
while(j>0&&array[j]<array[j1]){
//swap
temp=array[j]
array[j]=array[j1]
array[j1]=temp
j
}
}
}

BINARYSEARCH
GeneralInfo:
Bestcase:O(1)
Whentheminandmaxarethesameandtheloopsneverinitiate.
Worstcase:O(logn)
Needtoknow:
Algorithm:
Iterative:
int
binary_search
(
intA[],
intkey,
intimin,
intimax){
// 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

returnimid;

// determine which subarray to search

else

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,
intimax){
// 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)

/ key is in upper subset


/
return

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

else

// key has been found

return

imid;

MERGESORT
GeneralInfo:
Bestcase:O(n)
Worstcase/AverageCase:O(nlogn)

Needtoknow:

Algorithm(THISISWRONG...Caretoexplainwhy?...sorryitsnotwrong,itsjustnothowwelearned
inclass...cool,causeIalreadymemorizedthisoneandwaspanicking..haha):
voidmerge(
int
*,
int
*,
int
,
int
,
int
);

voidmergesort(
int*a,
int
*b,
intlow,
inthigh)
{
intpivot;

if

(low<high)
{
pivot=(low+high)/2;
mergesort(a,b,low,pivot);
mergesort(a,b,pivot+1,high);
merge(a,b,low,pivot,high);
}
}

voidmerge(
int*a,
int*b,
intlow,
intpivot,
inthigh)

{
nth,i,j,k;
i
h=low;
i=low;
j=pivot+1;

hile
w
((h<=pivot)&&(j<=high))
{
if

(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else

{
b[i]=a[j];
j++;
}
i++;
}
if

(h>pivot)
{
for

(k=j; k<=high; k++)


{
b[i]=a[k];
i++;
}
}
else

{
for

(k=h; k<=pivot; k++)


{
b[i]=a[k];
i++;
}
}
for

(k=low; k<=high; k++) a[k]=b[k];

BUBBLESORT
GeneralInfo:
BestCase
(whenlistisalreadysorted)
:O(n)
2
Worst/Averagecase:
(n
)
Needtoknow:

Algorithm:
intmain(){
//declaring array

intarray[5];

cout<<
"Enter 5 numbers randomly : "
<<endl;
for

(
inti=0; i<5; i++){
//Taking input in array

cin>>array[i];
}
cout<<endl;
cout<<
"Input array is: "
<<endl;

or
f
(
intj=0; j<5; j++)
{
//Displaying Array

cout<<
"\t\t\tValue at "
<<j<<
" Index: "
<<array[j]<<endl;
}
cout<<endl;
// Bubble Sort Starts Here

inttemp;

for

(
inti2=0; i2<=4; i2++)
{
for

(
intj=0; j<4; j++)
{
//Swapping element in if statement

if

(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
// Displaying Sorted array

cout<<
" Sorted Array is: "
<<endl;
for

(
inti3=0; i3<5; i3++)
{
cout<<
"\t\t\tValue at "
<<i3<<
" Index: "
<<array[i3]<<endl;
}
return0;

LINKEDLIST
GeneralInfo:
Here'sanawesomeresourcefromStanfordonlinkedlists.
Needtoknow:
Basicallyalinkedlistisacollectionofnodeswitheachnodecontainingonlythedataitstores,aswellasthe
addressforthenextnode.ToiteratethroughaLinkedList,youmuststartattheheadandgotothenext
nodeuntilyougettotheend(NULL).

Algorithm:
structNode {
//create node structure
intdata;

Node

* next;
};

voidinsertFront(
struct
Node**head,
intn) {
//add new node to front of the linked
list
Node*newNode =

new
Node
;
newNode->
data= n;
newNode->
next= *head;
*head = newNode;
}

RECURSIONFORMULA
GeneralInfo:

Needtoknow:

Algorithm:


TIMECOMPLEXITY

GeneralInfo:
ThinkofBigOas(controlledby):
ThinkofBigas(controls):
ThinkofBigas(thesameas):

2
2
xisbigOofx
f(x)=O(x
)
2
xiscontrolledbyx
2
2
x
isbigofx
f(x
)=(x)
2
x
controlsx
2
2
2
2
x
isbigofx

f(x
)=(x
)
2
2
x
isthesameasx

InLaymansTerms...

O:
Thefunctionf(n)takeslessthanorasmuchtimeas...
i.e.Thetheoreticalupperboundswetalkaboutwithsortingmethods.

Omega:
Thefunctiontakesasmuchtimeas...ormore.
i.e.thetheoreticalbestcasescenarioswetalkaboutwithsortingmethods.

Theta:
Thefunctiontakesexactly...time,variableonlybyaconstantmultiplier.
i.e.tightasymptoticbounds,asfoundinproblemstowhichthemastermethodcanbeapplied.

Thinkofthe
capital
lettersas
>=
or
<=.
Thinkofthe
lowercase
lettersas
>
or
<.

Needtoknow:
Isee6linesand7functionsontherightsidedafuq?
O(1)isrightunderO(logn)ifyouzoomintothe
veryendofthegraphyoucanseeahintofblue
thx
Ifyouarehavingissuesseeingthecolors/lines,justknowthat:<yourock
n
2
O(n!)>O(2
)>O(n
)>O(nlogn)>O(n)>O(logn)>O(1)

Algorithm:

MASTERTHEOREM
GeneralInfo:

Needtoknow:

CouldsomeoneexplainwhatT(1)=cmeans?Andwhychas
tobe>0?

Sure!Tisarecurrencerelationrepresentinghowlongittakestocompleteagivenmethod,right?So
T(x)isaformulathatrepresentshowlongitwilltakeforthatmethodtocompletextimes.Below,cis
anumericvalue.Ithastobe>0becausetimecantbenegative.AndT(1)=cisarequirement
becausethemethodneedstotakeaconstantamountoftimetoruneachtime,otherwisethiswhole
thingwouldbepointless.

Thanks!

Let T (n) beamonotonicallyincreasingfunctionthatsatisfies:


T (n) = aT (nb) + f(n)
T (1) = c

where a1, b2, c > 0 .If f (n)(nd ) where d0 ,then


T (n) = (nd)
if a < bd
T (n) = (ndlogn) if a = bd
T (n) = (nlogba) if a > bd
Thispowerpoint/pdfisprettyhelpfulforbreakingitdown:
http://cse.unl.edu/~choueiry/S06235/files/MasterTheorem.pdf

Practiceproblems:
http://www.csd.uwo.ca/~moreno/CS433CS9624/Resources/master.pdf

Cansomeoneexplaintheadditionalrequirementsforthethird
caseoftheMasterTheorem?Inthebookitsays
andif
a*f(n/b)c*f(n)forsomeconstantc<1andallsufficientlylarge
n

^^^Allitssayingisthataslongasyoucanmakeupacvaluelessthanone,andannvalue
greaterthanone,thatwillfitintothatequationwiththea,b,andf(n)valuesfromtheoriginal
recurrencerelation,youcanusecase3.

HOWDOWESOLVE#4?T(n)=2^n(n/2)+n^n

cantusemastermethodb/ca,(2^n),isnotaconstant
^thatstheanswer,unsolvable
(
http://www.csd.uwo.ca/~moreno/CS424/Ressources/master.pd
f
).
wellitmightbesolvable,butnotbyusingthemaster
theorem.(<thisguy)

StudyGuidefromprofessor

QandA
Problem4HW1:
4.FindoutthetimecomplexityforT(n)=T(n/2)+c*nandT(1)=c0,wherecandc0areconstants.

Howdoesitbecome22(1/(2^k)?

Вам также может понравиться