## Recursion

Normally, recursion involves a function calling itself repeatly. Hence the function stack grows. However, the compiler would know to avoid the growth of stack (hence preventing stack overflow because of that) if the recursion is a tail recursion. Tail recursion is a recursion that,

- The recursive function is called in the
`return`

statement - The recursive function in the return statement is not part of a larger expression

For example, the following is *not* a tail recursion:

```
int factorial(int n) {
if (n>0) {
return n*factorial(n-1);
} else {
return 1;
};
};
// Call: factorial(10);
```

but the following is a tail recursion:

```
int factorial(int n, int a) {
if (n>0) {
return factorial(n-1,a*n);
} else {
return a;
};
};
// Call: factorial(10,1);
```

## Big-O notation

Some situation wherein common complexities occur:

Complexity | Example |
---|---|

Fetching the first element from a set of data | |

Splitting a set of data in half repeatly | |

Traversing a set of data | |

Splitting a set of data in half repeatly and traversing each half | |

Traversing a set once for each member of another set of equal size | |

Generating all possible subsets of a set of data | |

Generating all possible permutations of a set of data |

## Automata

Finite automata:

- We have NFA (non-deterministic) and DFA (deterministic), but they are equivalent in function

Regular language: Some NFA recognizes it

- Discribed by regular expression
- RE is equivalent to some NFA
- GNFA (generalized NFA): An NFA that allows RE in addition to symbols on arrows

Non-regular language: No finite automata can recognize it

- Non-regular language can be verified by pumping lemma
- Pumping lemma: is a regular language if and only if:
- is a language
- such that and , and
- ; and
- ; and

Context-free language: Described by Context-Free gammar, i.e. substitution rules

- substitution rules consists of
*variables*and*terminals*(i.e. symbols) - derivation: The sequence of substitution according to the rules
- The derivation of CFR can be shown using
*parse trees* - A CFL is
*ambiguous*if the grammar generates the same string in different ways

Push-down automata (PDA): A finite automata with a stack

- CFG is equivalent to PDA
- Pumping lemma for CFL: is a CFL if and only if:
- is a language
- such that and , and
- ; and
- ; and

Turing machine: A finite automata with a tape of infinite size for read/write

- Turing-recognizable language: Some Turing machine accepts the language
- Decidable: Some Turing decider accepts it
- Decider either accepts or rejects an input, but never loops indefinitely

Oracle: An external device capable of reporting whether any string

Relation of languages:

Regular Context-free Decidable Turing-recognizable

## Complexities

P: Class of languages for which membership can be *decided* in polynomial time

NP: Class of languages for which membership can be *verified* in polynomial time

NP-Complete: A set of problems for which if a polynomial time algorithm exists for any of these problems, all NP problems are polynomial time solvable

- i.e., if NP-complete = P, then P=NP
- Work by Stephen Cook and Leonid Levin

Examples of NP-complete problems:

- Satisfiability problem
- For a boolean expression, such as , it is satisfiable if for some assignment to its variables.
- Satisfiability problem: Provided some boolean expression , determine whether is satisfiable

- 3SAT problem
- A satisfiability problem of 3-conjunctive normal form, i.e. 3 ORed-triples joined by ANDs

- -Clique: Determine if a graph has a -clique
- Reducible from 3SAT problem

- Vertex-cover problem:
- Vertex-cover is a subset of nodes of a graph G which every edge of G touches one of those nodes
- For a graph , determine if a -node vertex-cover exists

- Hamiltonian path problem
- For a graph , determine if a path exists that traverses every node

- Subset sums problem
- Given a collection of numbers, determine if a subset of them adds to have a certain sum

## Probability

Given a DTMC, with one absorbing state. The transition matrix is like:

where is a sub-stochastic matrix (i.e. the one with row sum less than 1). Hence the -th power of transition matrix is:

and we have the *fundamental matrix*:

and the -th entry in denotes the *expected number of visit* to state
before the absorbing state, given we start with state .

## Computer Architecture

The following is a very brief and good piece of tutorial on pipelining and cache:

http://www.cs.iastate.edu/~prabhu/Tutorial/title.html