Algorithms and Data
4: Algorithm Design and
Basic Data Structures
Professor Kevin Gold
Goals of Today
Begin developing tools of algorithm design,
including writing good pseudocode and proving
correctness.
Review some key data structures and algorithms
on
Algorithms and Data
1: Introduction
Professor Kevin Gold
What is an Algorithm?
Input
An algorithm is any well-defined
computational procedure that takes
some value or set of values as input
and produces some value or set of
values as output (CLRS)*
Not al
Algorithms and Data
3.2: Refreshing Java
Professor Kevin Gold
Hello, Terminal
Well assume you can either run things from the
command line or fake it with your Integrated
Development Environment (IDE)
Search for Terminal on Mac or run cmd on Windows
This s
Algorithms and Data
2: Running Time Analysis
Professor Kevin Gold
More Precise Running Time
Analysis
Weve seen some broad categories of running time
(quadratic, linear, logarithmic) but running times can
have many flavors
How do we decide whether a differ
Algorithms and Data
3.1: Algorithm Analysis
Practice
Professor Kevin Gold
Rules of Thumb for Running
Time Analysis
If subroutine A is executed, and then subroutine B is
executed, the big-O is the maximum of the two big-Os.
I do O(n) operations, then I do
Dynamic Programming Solution to the
Check Board Problem
Cheng Li, Virgil Pavlu, Javed Aslam
Check Board Problem
We are given a check board with m rows and n columns. There is a cost (penalty) P [i, j] associated with
square (i, j). We start at any square
Dynamic Programming Solution to the
Coin Change Problem
Javed Aslam, Cheng Li, Virgil Pavlu
Coin Change Problem
Make change for n cents using minimum number of coins of denominations d1 , d2 , ., dk , where d1 < d2 <
. < dk , and d1 = 1.
Methodology
(1) C
Dynamic Programming Solution to the
Longest Common Subsequence Problem
Cheng Li, Virgil Pavlu
[this solution follows Introduction to Algorithms book by Cormen et al]
Longest Common Subsequence Problem
Given two sequences X =< x1 , x2 , . . . , xm > and Y
Dynamic Programming Solution to the
Discrete Knapsack Problem
Cheng Li, Virgil Pavlu, Javed Aslam
Discrete Knapsack Problem
Given a set of items, labelled with 1, 2, ., n, each with a weight wi and a value vi , determine the items to
include in a knapsack
Dynamic Programming Solution to the
Matrix-Chain Multiplication Problem
Javed Aslam, Cheng Li, Virgil Pavlu
[this solution follows Introduction to Algorithms book by Cormen et al]
Matrix-Chain Multiplication Problem
Given a chain < A1 , A2 , . . . , An >
Week 9
Assignment 5
After further review
Apparently, I have not explained well the package
mechanism. And, unlike with Java, the compiler will
not warn you if you have it messed up.
In order for unit tests to work, main programs to
run, etc. you need to e
Week 8 part one
Assignment 5 review
We had to create three methods in MonadOps and, of course,
test them.
Its not OK to change the specification!
Please use the standard project structure (doesnt matter if you
use Eclipse, IntelliJ, sbt, maven, or some co
Week 8 part two
Parsing (continued)
Our previous parser (from Week 8A) worked fine
but it had the following issues:
If output doesnt parse correctly, we will get an
appropriate message. But, if anything goes wrong in
the logic behind the parsers (e.g. div
Week 7 part two
We just cant get away from
this stuff
Last week we went over this stuff at the end
In general, we have ugly/awkward types like:
Future[Seq[Try[URL] (Future and Try are somewhat redundant)
Seq[Future[URL] (It isnt that convenient to have to
Week 5 part one
Assignment 3 wrap-up (2)
def map3[A,B,C,D](a: Option[A], b: Option[B], c: Option[C])(f: (A,B,C) => D): Option[D] =
for cfw_
Many of you used map and flatMap which
aa <- a
isnt wrong of course but it isnt elegant
bb <- b
either.
cc <- c
yi
Week 7 part one
Review of Mid-term
issues (and others)
For-comprehensions (again!)
Take a look at this (typical) code fragment from assignment
4 (no names!)
where text has type Seq[Try[String]:
val word = for cfw_
word <- text
words <- word match cfw_
cas
Week 6 part one
Review of Scala
syntax, concepts
Scala syntactic sugar
Unary functions (i.e. instances of Function1):
f(x)
most collection-type containers implement Function1, e.g. List, Map, Set but not
Option, Try, Either.
A unary method on an object (l
Week 5 part two
Tail Recursion and iteration
a diversion
Lets take a look at perhaps the most obvious recursive
function, factorial:
scala> def badFactorial(x: Int): Long = if (x<=1) 1 else x*badFactorial(x-1)
badFactorial: (x: Int)Long
This isnt tail-rec
Week 4 part two
Monoids, Functors
and Monads
Monoids, Functors and
Monads
These terms come from category theory. But really,
their definitions as far as Scala is concerned are
quite simple:
for instance, a monoid is just the type of thing we were
getting
Parallel Processing (1)
Modern computers have multiple cores and are typically arrayed in clusters. This
allows us (in practice, forces us) to do things in parallel. There are two types of
parallel processing and Scala can be effective in both:
immutable
Week 3 Tuesday
More on Lists
Lists and their methods
Recap:
package edu.neu.coe.scala.list
trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A] (head: A, tail: List[A]) extends List[A]
object List cfw_
def apply[A](as: A*): List[A] =
i
Week 4 part one
Assignment 2 wrap-up
object RNG cfw_
See how simple this is?
type SRNG[A] = Stream[RNG[A]
def rngs[A](r: RNG[A]): SRNG[A] = Stream.cons(r, rngs(r.next)
See how simple this
def values[A](s: SRNG[A]): Stream[A] = s map cfw__.value
def values
Week 3 part two
Three parts:
Types
Homework (30m on Assignment 2)
Functions
Types
Defining Types, etc.
type, trait, class, object, case class, abstract class, value class:
what do they all mean? whats the difference?
type is to class what val is to value
The Scala REPL
read-evaluate-print-loop
three alternative ways to run it:
command line: scala
Eclipse (or IntelliJ): Show View/Scala Interpreter
Eclipse (or IntelliJ): New Scala Worksheet
Exercise
Write a function in Scala to evaluate factorial(20)
You ma
Big Data Systems
Engineering with Scala
Robin Hillyard, M.A (Oxon), Ph.D (Cantab)
About me
Wrote my first program (in Fortran) in 1967
solve cos(x) = sech(x)
it worked first time.
B.A/M.A Engineering Science (Oxford) 1973
Ph.D. Computer Science (Cambridge