Jan 29
Goals for this week:
- practice tools : shell (command line), python, R
$ R
> x = seq(0, 2*pi, by=0.1) # an array of numbers
> plot(x, sin(x)) # ?plot for more details, i.e. type="l"
Python's [1,2,3] is R's c(1,2,3).
- discuss chapter 1 material
- concepts: correctness, heuristic, problem statement, algorithm representation, counter examples, recursion, induction, combinatorics
- examples: traveling salesman, scheduling
- look at chapter 1 exercises (and/or assign for homework):
- 5 - knapsack counterexamples
- 9 - bubblesort correctness
- 20 - estimate words in textbook
- 25 - estimate sorting times
- 26 - implement (and measure) TSP heuristics
- review math of logs and exponents
Materials:
minimal interactive version of "generate data and plot"
$ python -ic '' # In python,
>>> f = [0,1] # generate data,
>>> for i in xrange(2,250):
... f.append(f[i-1] + f[i-2])
...
>>> csv = open('data.csv','w') # output to text file.
>>> csv.write("n,fib\n")
>>> for j in range(20):
... i = 100 + 7*j
... csv.write("{},{}\n".format(i, f[i]))
...
>>> exit()
$ R --no-save --quiet # in R,
> data = read.csv('data.csv') # read in data,
> fit = lm(log(data$fib) ~ data$n) # find best-fit curve,
> ypredict = exp(predict(fit))
> plot(data, log="y") # plot it
> lines(data$n, ypredict, col="blue") # with best-fit curve.
> q()