CS 312 Midterm

5.0(1)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/71

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

72 Terms

1
New cards

Fibonacci Sequence Algorithm

if n<2 then

   return n

return Fib(n-1) + Fib(n-2) 

2
New cards

Fibonacci Redux

Fib2(n)
   fib[0] = 0
   fib[1] = 1
   for i = 2 to n do
      fib[i] = fib[i-1] + fib[i-2]
   return fib[n]

3
New cards

f(n) in O(g(n))

lim f(n)/g(n) = 0

4
New cards

f(n) in THEATA(g(n))

lim f(n)/g(n) = Real Number

5
New cards

f(n) in OMEGA(g(n))

lim f(n)/g(n) = Infinity

6
New cards

Handy Rules for Big-O

  1. Constants don’t matter

  2. Polynomial order matters

  3. Exponential dominates polynomial

  4. Exponential base matters

  5. Polynomial dominates logarithm

  6. Logarithm base doesn’t matter

7
New cards

Addition Time Complexity

O(n)

8
New cards

Basic Multiplication Time Complexity

O(n2)

9
New cards

Modular Addition Time Complexity

O(n)

10
New cards

Modular Multiplication Time Complexity

O(n2)

11
New cards

Modular Division Time Complexity

O(n3)

12
New cards

Modular Exponentiation Algorithm

if y = 0 then
   return 1
z = modexp(x, y//2, N)
if y is even then
   return z**2(mod N)
else
   return x * z**2 (mod N)

13
New cards

Modular Exponentiation Time Complexity

O(n3)

14
New cards

Primality Test (Fermat)

PrimeTest(N)
   choose int(a) in range(N)
   if a**(N-1) (mod N) == 1 then
      return 'Yes'
   else
      return 'No'

PrimeTest2(N)
   for i = 1 to k do
      if PrimeTest(N) = 'No' then
         return 'No'
   return 'Yes'

15
New cards

Generating Primes Time Complexity

O(n4)

16
New cards

RSA Process

  1. Pick two primes p, q and let N = pq

  2. Choose e relatively prime to (p -1)(q -1)

  3. Find d such that de mod((p -1)(q -1)) = 1

  4. Encrypt message x as xe mod N = y

  5. Decrypt code y as yd mod N = (xe)d mod N = xde mod N = x

17
New cards

Euclid’s Algorithm

if b = 0 then
   return a
return Euclid(b, a%b)

18
New cards

Extended Euclid’s Algorithm

if b = 0 then
   return (1,0,a)
x, y, z = extEuclid(b, a%b)
return (y, x - ((a//b)*y), z)

19
New cards

Modular Division Lemma

For any positive integers a and b, the Extended Euclid algorithm returns integers x, y, and z such that GCD(a,b) = z = ax + by

20
New cards

Total RSA Time Complexity

O(n4)

21
New cards

Miller-Rabin Algorithm

def miller_rabin(N: int, k: int) -> str:
    for i in range(k):
        a = random.randint(1, N-1)
        y = N - 1
        x = mod_exp(a, y, N)
        if x != 1:
            return 'composite'
        while x == 1 and y%2 == 0:
            y = y//2
            x = mod_exp(a, y, N)
        if x != N-1 and x != 1:
            return 'composite'
    return 'prime'

22
New cards

Probability of getting the correct answer from Fermat

1− 1/(2𝑘)

23
New cards

Probability of getting the correct answer from Miller-Rabin

1− 1/(4𝑘)

24
New cards

Divide and Conquer Time Complexity

T(n) = a* T(n/b) + O(divide & combine time)

25
New cards

Master Theorem

knowt flashcard image
26
New cards

Merge & Merge Sort Algorithms

MergeSort(x)
   if len(x) <= 1 then
      return x
   left = MergeSort(x[:len(x)//2])
   right = MergeSort(x[len(x)//2:])
   y = Merge(left, right)
   return y

Merge(l, r)
   result = []
   while len(l) or len(r) do
      if l[0] <= r[0] then
         result.append(l[0])
         l.delete(0)
      else
         result.append(r[0])
         r.delete(0)
   return result

27
New cards

D&C Multiplication Time Complexity

O(n2)

28
New cards

D&C Faster Multiplication Time Complexity

O(n1.59)

29
New cards

MergeSort Time Complexity

O(n log(n))

30
New cards

General Median Time Complexity

O(n log(n))

31
New cards

Faster D&C Median Time Complexity

O(n)

32
New cards

Matrix Multiplication Time Complexity

O(n3)

33
New cards

Convex Hull Find Upper Tangent Algorithm

p = rightmost point in L 
q = leftmost point in R
temp = line(p,q)
done = False
while not done do
   done = True
   while temp is not upper tangent to L do
      r = counterclockwise neighbor of p
      temp = line(r, q)
      p = r
      done = False
   while temp is not upper tangent to R do
      r = clockwise neighbor to q
      temp = line(p,r)
      q = r
      done = False
return temp

34
New cards

Convex Hull Algorithm

if len(points) < 3 then
   return points
mid = len(points)//2
L = convexHull(points[:mid])
R = convexHull(points[mid:])
upL, upR = findUpper(L,R)
lowL, lowR = findLower(L,R)
combined = L[:upL]+R[upR:lowR]+L[lowL:]
return combined

35
New cards

Depth-First Search of Undirected Graph

DFS(G)
   for all v in V do
      visited(v) = False
   for all v in V do
      if not visited(v) then
         Explore(v)

Explore(G, v)
   visited(v) = True
   previsit(v)
   for each edge (u,v) in E do
      if not visited(v) then
         Explore(v)
   postvisit(v)

36
New cards

Depth First Search Data Structure

Stack

37
New cards

Tree Edge

upre <= vpre <= vpost <= upost

38
New cards

Forward Edge

upre <= vpre <= vpost <= upost

39
New cards

Back Edge

vpre <= upre <= upost <= vpost

40
New cards

Cross Edge

vpre <= vpost <= upre <= upost

41
New cards

A directed graph has a cycle if and only if ___________

the DFS tree has a backward edge

42
New cards

How to Linearize a DAG

  1. Do DFS then linearize by decreasing post order

  2. Iteratively add source vertices to the linearization and remove them from the graph

43
New cards

Strongly Connected Component definition

A set of vertices that are all connected to each other

44
New cards

Finding Strongly Connected Components Algorithm

G_r = ReverseEdges(G)
post = DFS(G_r)
scc = DFS(G,post)
return scc

45
New cards

Strongly Connected Components Time Complexity

O(|V| + |E|)

46
New cards

Depth First Search Time Complexity

O(|V| + |E|)

47
New cards

Breadth-First Search Algorithm

for all u in V do
   dist(u) = infinity
dist(s) = 0
Q.inject(s)
while Q in not empty do
   u = Q.eject()
   for all edges (u,v) in E do
      if dist(v) = infinity then
         Q.inject(v)
         dist(v) = dist(u) + 1
return dist

48
New cards

BFS Time Complexity

O(|V| + |E|)

49
New cards

Dijkstra’s Algorithm

for all u in V do
   dist(u) = infinity
   prev(u) = null
dist(s) = 0
H.makequeue(V, dist)
while H is not empty do
   u = H.deletemin()
   for all edged(u,v) in E do
      if dist(v) > dist(u) + l(u,v) then
         dist(v) = dist(u) + l(u,v)
         prev(v) = u
         H.decreasekey(v)
return dist,prev

50
New cards

Dijkstra Time Complexity

O(MakeQueue + DeleteMin * |V| + DecreaseKey * |E|)

51
New cards

Dijkstra Space Complexity

O(|V| + |E|)

52
New cards

Array Priority Queue Function Times

  • insert: O(1)

  • decrease-key: O(1)

  • delete-min: O(n)

  • make-queue: O(n)

53
New cards

Binary Heap Priority Queue Function Times

  • insert: O(log(n))

  • decrease-key: O(log(n))

  • delete-min: O(log(n))

  • make-queue: O(n log(n))

54
New cards

Breadth First Search Data Structure

Priority Queue

55
New cards

Bellman-Ford Algorithm

for all u in V do
   dist(u) = infinity
   prev(u) = null
dist(s) = 0
for i = 1 to |V|-1 do
   for all edges (u,v) in E do
      if dist(v) > dist(u) + l(u,v) then
         dist(v) = dist(u) + l(u,v)
         prev(v) = u
return dist,prev

56
New cards

Bellman-Ford Time Complexity

O(|E| * |V|)

57
New cards

Greedy Philosophy

  • Build up solution piece by piece

  • Always choose the next piece that offers the most obvious and immediate benefit without violating given constraints

58
New cards

Prim’s Algorithm

for all u in V do
   cost(u) = infinity
   prev(u) = null
choose random node u_0
cost(u_0) = 0
H.makequeue(V) {costs as keys}
while H is not empty do
   v = H.deletemin()
   for each (v,z) in E do
      if cost(z) > l(v,z) then
         cost(z) = l(v,z)
         prev(z) = v
         H.decreasekey(z)
return cost,prev

59
New cards

Prim’s Algorithm Time Complexity

O(MakeQueue + DeleteMin * |V| + DecreaseKey * |E|)

60
New cards

Kruskal’s Algorithm

for all u in V do
   makeset(u)
X = {}
sort edges e by weight
for all edges {u,v} in E in increasing order of weight do
   if find(u) != find(v) then
      X = X U {u,v}
      union(u,v)
return X

61
New cards

Kruskal’s Algorithm Time Complexity

O(|E| log(|V|))

62
New cards

Array implementation of PQ is better in _______ graphs.

Dense

63
New cards

Binary heap implementation of PQ is better in _______ graphs.

Sparse

64
New cards

Huffman Algorithm

T = null
H.makequeue(S) #S is a set of symbols, frequency f as keys
for k = |S| to 2|S| - 2 do
   i = H.deletemin()
   j = H.deletemin()
   f(k) = f(i) + f(j)
   H.insert(k)
   k.left = i
   k.right = j
   T.addnode(k)
return T

65
New cards

Huffman Algorithm Time Complexity

O(n log(n))

66
New cards

E[bits/character] =

Σ f(s) c(s)

67
New cards

Entropy =

-Σ fi log2(fi)

68
New cards

Huffman works best when entropy is _____________

Low

69
New cards

Horn Formula

T = {}
for x in Formula do
   T.insert(x:False)
while there is an unsatisfied implication in F using T do
   T[z] = True
if all negative clauses are satisfied by T then
   return T
else
   return None

70
New cards

2-SAT Algorithm (in words)

Set up nodes in graph for positive and negative variables. Add edges based on implications. Look for cyclical pattern(s) in the graph.

71
New cards

Set Cover Theorem

Given set cover problem P = (U, S), if the optimal set cover is C*, then the greedy algorithm finds a cover C such that |C| <= |C*| ln|U|

72
New cards

Set Cover Corollary

No approximation algorithm got the set cover problem can do better than the greedy algorithm, unless P = NP.