Datastructures Flashcards
Randomized algorithm
An algorithm whose behavior is not only determined by the input, but also by the values by a random number generator.
Give the formal definition of the sorting problem
Input: A sequence of n numbers (a1, …, an)
Output: A permutation (a’1, …, a’n) of the input sequence such that a’1 <_ … <_ a’n.
The instance of a problem consists of…
the input needed to compute a solution to the problem.
When is an algorithm for a computational problem correct?
When it halts for every problem instance and outputs the correct solution.
What does n! denote?
The factorial function.
For n=6, n! = 6 x 5 x 4 x 3 x 2 x 1
data structure
A way to store and organize data in order to facilitate access and modification.
How much time does the algorithm insertion sort take to sort n items?
c1 * n^2
with c1 = constant
How much time does the algorithm merge sort take to sort n items?
c2 * n * log2(n)
with c2 = constant
keys
the numbers to be sorted in the sorting problem
What is the form of the input in the sorting problem?
An array with n elements.
satellite data
The data that the input keys are associated with.
record
The key and the satellite data.
insertion sort
An algorithm for sorting a small number of elements. A key is compared to each value and inserted when a value is encountered that is less or equal to the key.
What are the 3 things you need to show in a loop invariant?
1) initialization
2) maintenance
3) termination
What are objects composed of?
attributes
Analyzing an algorithm
Predicting the resources an algorithm requires.
RAM
Random-Access Machine
What 3 instructions does the RAM model contain?
1) arithmetic
2) data movement
3) control
What are arithmetic instructions? (7)
add, subtract, multiply, divide,
remainder, floor, ceiling
What are data movement instructions? (3)
load, store, copy
What are control instructions?
conditional and unconditional branch, subroutine call and return.
What are the data types in the RAM model?
integer, floating point and character
How do we assume integers are represented as data?
As c * log2 (n) bits
with c = a constant bigger than or equal to 1.
DIvide-and-conquer method
Breaking the problem into several subproblems that are similar to the original, but smaller in size. Solve the problems recursively and combine the solutions to create solution to original problem.
Recusive algorithm
An algorithm that calls itself repeatedly to solve a related problem.
Permutatie
Ordening. Aantal is gelijk aan de factorial.
Postconditie
Beschrijft wat het betekent voor de output om correct te zijn.
Conditie C
De while/if/for opdracht
Body B
Wat er in de loop staat.
Terminatie T
De return opdracht van algoritme.
Theta-notation means…
Roughly proportional
When do we consider one algorithm to be more efficient than another?
When its worst case running time has a lower order of growhth.
Geef de stappen voor bewijs met inductie voor een statement als
‘Voor alle gehele getallen n>_ b geldt de eigenschap P(n)’.
1) We gebruiken inductie. Definieer P(n) = eigenschap.
2) Basisstap: bewijs dat P(b) geldt.
3) Inductiestap: bewijs dat voor een willekeurig getal >_b, zeg k, als P(k), dan P(k+1).
4) Concludeer waar voor alle n>_b.
Harmonische sommatie
Convergeert monotoon naar nul, maar bereikt 0 nooit.
Inductieve inferentie
De redeneermethode waarmee je uit een aantal voorbeelden een algemene regels probeert op te stellen. Niet logisch.
Deductieve inferentie
Een redeneermethode die leidt tot logisch geldende conclusies.
PROP
De verzameling propositielogsiche formules.
Rekenkundige reeks
Een sommatie van opeenvolgende termen met een constant verschil.
Meetkundige reeks
Een sommatie van opeenvolgende termen met een constante verhouding.
Harmonische reeks
Voor positieve, gehele n is het nde harmonishe getal de som van 1/1, 1/2, 1/3… tot 1/n.
Lineariteit in sommaties
De sommatie van (ca.k + b.k) is gelijk aan
c de sommatie van a.k + de sommatie van b.k
De sommatie van kwadraten is gelijk aan…
(n(n+1)(2n+1) ) / 6
De sommatie van n^3 is gelijk aan…
(n^2 * (n+1)^2) / 4
Geometrische reeks
De sommatie 1 + x + x^2 + x^3 + x^… + x^n
De geometrische sommatie is gelijk aan…
(x^(n+1) - 1) / (x-1)
When is a function f(n) monotonically increasing?
If m <_ n implies f(m) <_ f(n).
When is a function f(n) monotonically decreasing?
If m<n implies f(m) > f(n).
When is a function f(n) strictly increasing?
If m < n implies f(m) < f(n).
When is a function f(n) strictly decreasing?
If m< n implies f(m) > f(n).
Wat is |x|?
The greatest integer less than or equal to x.
Wat is |- x -|?
The least integer greater than or equal to x.
What property do the floor and ceiling function have?
They are monotonically increasing.
Floor & ceiling property:
For any integer n, we have
|n| = n = |- n -|
Floor & ceiling property.
For any real number x, we have
x-1 < |x| <_ x <_ |-x-| < x+1
|-x| ==
minus |-x-|
minus |x| ==
|- -x -|
For any real number x >_ 0 and integers a,b > 0, we have
|- (|- x/a -|) / b -| =…
|- x / ab -|
For any real number x>0 and integers a,b >0, we have
| (|_ x/a _|) / b _| ==
|_ x / ab _|
For any real number x>0 and integers a,b>0, we have:
|- a/b -| <_ …
(a + (b-1)) / b
For any real number x>0 and integers a,b>0, we have:
|_ a/b | > …
(a - (b-1)) / b
For any integer n and real number x, we have:
|_ n+x _| ==
n + |x|
For any integer n and real number x, we have:
|- n+x -| ==
n + |-x-|
For any integer a and any positive integer n, the value a mod n is…
the remainder of the quotient a/n.
a mod n ==
a - n |_ a/n _|
If (a mod n) = (b mod n), we write…
and say…
a = b (mod n)
a is equivalent to b, modulo n.
Given a nonnegative integer d, a polynomial in n of degree d is a function p(n) of the form…
p(n) = SUM(i=0, d) a.i * (n^i)
What are the coefficients of the polynomial
p(n) = SUM(i=0, d) a.i * (n^i)
for a nonnegative integer d?
The constants a.0, a…, a.d. With a.d not equal to zero.
A polynomial is asymptotically positive if and only if…
a.d > 0
For an asymptotically positive polynomial p(n) of degree d, we have p(n) ==
THETA (n^d)
For any real constant a>_0, the function n^a is …
monotonically increasing.
For any real constant a<_0, the function n^a is…
Monotonically decreasing.
We say that a function f(n) is polynomially bounded if…
f(n) == O (n^k) for some constant k.
For all real a>0, m and n, we have:
a^0 =
1
For all real a>0, m and n, we have:
a^1 =
a
For all real a>0, m and n, we have:
a^(-1) =
1/a
For all real a>0, m and n, we have:
(a^m) ^n =
a ^ mn
or
(a^n) ^m
For all real a>0, m and n, we have:
(a^m)(n^m) =
a^(m+n)
For all n and a>_1, the function a^n is…
monotonically increasing in n
We assume 0^0 =
1
For all real constants a>1 and b, we can relate the rates of growth of polynomials and exponentials by…
LIM(n to infinity) of (n^b) / (a^n) = 0
and n^b = o(a^n).
Any exponential function with a base strictly greater than 1 grows…
faster than a polynomial functino.
For all real x, we have
e^x =
1 + x + ((x^2)/2!) + ((x^3)/3!) + …. =
SUM(i=0, infinity) (x^i)/i!
For all real x, we have the inequality x+1 <_ …
e^x
Equality , so z+1 = e>x, holds only when x=0.
When |x| <_ 1, we have the approximation with e^x:
1+x <_ e^x <_ 1 + x + x^2
When x->0, the approximation of e^x by x+1 is…
e^x = x + 1 + THETA(x^2)
lg n ==
log.2 n
Binary logarithm
lg n
log.2 n
Natural logarithm
ln n
log.e n
Exponentiation
lg^k n
(lg n )^k
lg^k n ==
(lg n)^k
lg lg n ==
lg(lg n)
Composition
lg lg n
lg(lg n)
For any constant b>1, the function log.b n is undefined if
n<_0
For any constant b>1, the function log.b n is strictly increasing if
n>0
For any constant b>1, the function log.b n is negative if
0 < n < 1
For any constant b>1, the function log.b n is positive if…
n>1
For any constant b>1, the function log.b n is zero if…
n=1.
For all real a>0, b>0, c>0 and n, we have…
a =
b^(log.b a)
For all real a>0, b>0, c>0 and n, we have..
log.c (ab) ==
log.c a + log.c b
For all real a>0, b>0, c>0 and n, we have…
log.b (a^n) =
n log.b a
For all real a>0, b>0 and c>0, we have
log.b a =
log.c a / log.c b
or
1/ (log.a b)
For all real a>0, b>0, c>0 and n, we have..
log.b (1/a) =
-log.b a
For all real a>0, b>0, c>0 and n, we have..
a^(log.b c) =
c^(log.b a)
Changing the base of a logarithm from one constant to another changes the value of the logarithm…
by only a constant factor.
For x>-1, the inequalites with logarithms hold…
x/(1+x) <_ ln (1+x) <_ x
A function f(n) is polylogarithmically bounded for some constant k if f(n) =…
(running time thing)
O(lg^k n)
For all real constants a>0 and b, we have…
lg.b n =
o(n^a)
The factorial n! is recursively
defined for n>_0 as
1 if n=0
n(n-1)! if n>0
Give Stirling’s approximation,
n! =
ROOT(2pi*n) * (n/e)^n * (1 + THETA(1/n))
n! = o..
o(n^n)
n! = omega(…
omega(2^n)
lg(n!) = theta(..
theta(n lg n)
For all n>_1 it holds that
n! =
ROOT(2pi*n) * (n/e)^n * e^(alpha.n)
with
1/(12n+1) < alpha.n < 1/12n
We define the Fibonacci numbers F.i for i>_0 as …
0 if i=0
1 if i=1
F.(i-1) + F.(i-2) if i>-2
golden ratio
phi
conjugate of the golden ratio
phi met een dakje erop
Phi (golden ratio) =
(1+ROOT(5)) /2
(phi)^ =
(conjugate of golden ratio)
(1-ROOT(5))/2
In golden ratios,
F.i =
(phi.i - phi.i^) / ROOT(5)
Substitution method in 2 steps
1) Guess the form of the solution using symbolic constants.
2) Use mathematical induction to show that the solution works and find the constants
Master recurrence
Describes the running time of a divide-and-conquer algorithm that divides a problem of size n into a subproblems, each of size n/b < n.
How much time does the divide-and-conquer algorithm take to solve each subproblem?
T(n/b) time, with n= the size of the original problem and b= the size of the subproblem.
What does the driving function f(n) entail?
The cost of dividing a problem into subproblems and the cost of combining the solution to get a final solution.
For a>0, b>1 and T(n) = aT(n/b) + f(n), what is the first part of the master theorem?
If there exists a constant epsilon>0 such that f(n) = O(n^(log.b a - epsilon)), then T(n) = THETA(n^(log.b a)).
For a>0, b>1 and T(n) = aT(n/b) + f(n), what is the second part of the master theorem?
If there exists a constant k>_0 such that f(n) = THETA(n^(log.b a lg^k n)), then T(n) = THETA(n^(log.b a lg^(k+1) n)).
For a>0, b>1 and T(n) = aT(n/b) + f(n), what is the third part of the master theorem?
If there exists a constant epsilon>0 such that f(n) = OMEGA(n^(log.b a + epsilon)), and if f(n) additionally satisfies the regularity condition, then T(n) = THETA(f(n)).
Regularity condition for f(n) in the master theorem.
a f(n/b) <_ c f(n)
for some constant c<1 and all sufficiently large n.
Watershed function
n^(log.b a)
How do you use the master method?
Determine which of the three cases applies and then write down the answer.
Geef drie voorbeelden van het verdeel-en-heers algoritme:
binair zoeken
merge sort
quicksort
What is the worst-case running time of the quicksort algorithm on an input array of n numbers?
THETA(n^2).
Why is quicksort often the best algorithm for sorting?
The expected running time is THETA(n lg n) when all numbers are distinct, and the constant factors in that notation are small.
comparison sorts
Algorithms that determine the sorted order only based on comparing input elements.
Algoritme
Een oplossing voor een computationeel probleem, gedefinieerd als een relatie tussen input en output.
Een algoritme is niet hetzelfde als een … in C#.
Een methode
Een datastructuur is niet hetzelfde als een … in C#
een object
Linear zoeken
Bij het begin beginnen en doorgaan tot het eind of totdat je x gevonden hebt.
ZOEK-LINEAR (A,x)
i=0
while i < A.length
if A[i] == x
return i
i++1
return -1
Hoelang duurt ZOEK-LINEAR (A,x) voor een bepaalde x en A.length = n?
Als x niet in A: n vergelijkingen
Als x in A: n vergelijkingen of 1 vergelijking (ligt aan positie van x in A) of n/2 vergelijkingen (gemiddelde optie).
Hoe werkt binair zoeken globaal?
Zet index i helemaal links en index j helemaal rechts. Zoek het midden (m). Als x<m, gooi rechterhelft weg en als x>m, gooi linkerhelft weg. Herhaal totdat i==j-1.
BINAIR-ZOEKEN (A, x)
i = 0
j= A.length -1
while i < j
m = |_ (i+j) / 2 _|
if A[m] < x
i = m+1
else
j = m
if A[i] == x
return i
else
return -1
Correctheid
De juiste output bij de juiste input.
Loop invariant
Als P(k) betekent dat eigenschap P waar is voorafgaand aan iteratie k van de loop, dan is P een loop invariant als P(k) geldt voor alle k >_1.
Hoe gebruik je inductie om een loop invariant te bewijzen?
Basisstap: Bewijs dat de loop invariant geldt in iteratie 0 of 1.
Inductisstap: te bewijzen is als P(k), dan P(k+1). Neem aan dat P(k). Bewijs P(k+1) door te gebruiken dat P(k) en P(+1) = P(k+1).
Hoe toon je terminatie van een loop aan?
Beargumenteer dat de variant 0 wordt.
INSERTION-SORT (A)
for j = 2 to A.length
….. key = A[j]
…. i = j-1
…. while i>0 and A[i] > key
…. …. A[i+1] = A[i]
…. …. i = i-1
…. A[i+1] = key
Wat is de invariant van insertion sort?
Voorafgaand aan iteratie k>_1 van de for-opdracht bevat de subarray A[1…k] de elementen die in A[1…k] zaten, maar dan gesorteerd.
ARRAY-SOM(A)
som = 0
teller = 0
while teller < A.length
som = som + A[teller]
teller = teller + 1
return som
Dingen die je optelt
Termen
Dingen die je vermenigvuldigt
factoren
Als de objecten in een rij getallen zijn, dan is een reeks…
de sommatie van de termen van de rij.
Lege sommatie
Een sommatie van bijv i=1 tot i=0. Uitkomst is 0.