Lecture28MRM

Download Report

Transcript Lecture28MRM

Course Review
15-211
Fundamental Structures
of Computer Science
Margaret Reid-Miller
29 April 2004
What was this course about?
How to solve computing problems.
• Problem analysis, to abstract away details
and divide into smaller subproblems.
• Mathematical foundations for precise
formulations of problems and solutions.
• Data structures and algorithms to solve
problems correctly and efficiently.
• Java programming and modular software
construction for good implementations.
The science in CS
Not “hacking”, but:
• Thinking about problems abstractly.
• Selecting good data structures and
obtaining correct and fast algorithms.
• Implementing programs that are
understandable and correct.
Topics Covered
Simple list algorithms
Solving simple recurrences
Dictionaries: Binary trees, tries, hash
tables
Priority queues and binary heaps
Data compression
Sorting
String matching
Topics Covered ctd..
Graphs
Greedy algorithms
Dynamic programming
Game trees
Client-server computing
Equivalence relations and union find
Computational geometry
Studying for the Final Exam
Review all material first
• Lectures, programs, quizzes
Do sample finals – under time
restrictions
Old Finals are in
blackboard/assignments
Types and difficulty of problems may
be similar
List Algorithms
List Interface
Reversing a list
• rev(L) = L if |L|=1
• rev(L) = append(first(L),rev(tail(L)))
• What is the complexity?
Solving Recurrences, Asymptotics
Solve T(n) = 3 T(n/3) + 1
Prove the correctness
• By induction
• Or other methods
Big-Oh, little-oh, Big-Omega, Theta
Binary Trees
1. Traverse the tree inorder
2. Do exactly two rotations so that
the tree is balanced
3. Consider a binary tree of height h
a. What are the max and min number of
nodes?
b. What are the max and min number of
leaves?
4. Perfect, full, complete trees
AVL trees
Dictionaries
Keypad IM Trie
4
5
9
…
I
4
6
6
5
3
8
8
3
like
you
Separate chaining
love
5
…
9
lovely
 Build chains when keys hash to the
same table location
0
1
2
3
4
5
6
7
8
9
10
11
12
As
with
long
key
int
hash
foo
foo
link
char
url
open
list
in
onto
type
queue
test
info
fail
find
Hashing
Describe why hashing a string to sum
of its characters is not a good idea.
Assume S = a1a2….an is a string
Define H(S) =  ai2i-1 (i=1..n)
Describe a way to efficiently calculate
H(S)
Priority Queues and Heaps
Suppose you implement a priority
queue using following
• Unsorted array
• Linked list (smallest at front)
• Heap
What is the complexity in each case
for
• deleteMin
• insert
Binary heaps
Representing complete binary trees
• Arrays (1-based)
 Parent at position i
 Children at 2i (and 2i+1).
1 2 3
4 5 6 7
8 9 10
1
2
4
8
5
9
Percolation down
3
10
6
7
• Bubble the transplanted leaf value down
the tree until the heap order property is
satisfied.
1
2
-14
16
24
21
65 26
32 31
19 68
31
14
24
65 26
16
21
32
19 68
Compression and Huffman’s
Compare two images
Tree representation
One image is 400K the other is 1100K. Which is which?
• Represent prefix free
codes as full binary trees
0
• Full: every node
 Is a leaf, or
 Has exactly 2 children.
• The encoding is then a
(unique) path from the
root to a leaf.
0
0
c
1
a
1
d
1
b
a=1, b=001, c=000, d=01
LZW compression
Binary LZW: Compression example
10010110011
^
Input:
0
Dictionary:
0
1
0
1
1
1
0
0
2
1
3
0
4
Output:
1034
1
5
Data Compression
Encode “I AM SAM SAM I AM SAM SAM”
using
• Huffman compression
• LZW
• In each case calculate the compression ratio
Is it possible to apply Huffman after
applying LZW?
If so apply Huffman to output generated by
LZW above
Sorting and lower bounds
N2 vs Nlog N
Quicksort idea
 Choose a pivot.
N^2
Nlog N
Upper and lower bounds
T(N) = O(f(N))
T(N) = (g(N))
 Rearrange so that
pivot is in the
“right” spot.
 Recurse on each
half and conquer!
cf(N)
T(N)
dg(N)
N
Sorting
Insert the random set of numbers {10, 12, 5, 15,
8, 14,11,7} into a BST with AVL properties.
Fred has suggested that he can sort a list using
hash table. The idea is the following. Assume
that there are n numbers in the set. He would
find the max and min and create a hash table of
size (max-min). Then Fred would simply run an
iterator I from min to max and use the hash map
H(I) = I mod (max-min) if I is in the set. He
claims that this is a sorting algorithm of O(n).
What can you tell Fred? Politely please.
String matching
Brute Force
KMP
0000000000000000000000000001
0000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
00000000000000000000000000This is a string
01

A worse case example:
196 + 14 = 210
comparisons
KMP
28+14 = 42 comparisons -
B-M
This is a string
-
-
-
g
-
n
i
-
r
t
tring
16 comparisons
tring
8 comparisons
Graphs
What is breadth first traversal?
What is depth first traversal?
What data structures can be used to
implement each one?
Fred claims that if the degree of each
node in an undirected graph of n
nodes is (n-1), then graph has n(n1) edges. Can this statement be
true?
Graphs
Dijkstra’s algorithm
Graphs — an overview
2
f
c
4
2
1
s
Vertices (aka nodes)
DTW
2273
1987
BOS
211
190
PIT
344
318
JFK
2145
2462
Weights
LAX
Undirected Edges
4
b
5
618
SFO
2
a
e
1
1
g
d
a e f g
4 6 6 
Visited
s (D = 0)
b (D = 2)
d (D = 3)
c (D = 4)
Greedy Algorithms
60
40
60
20
50
40
40
40
50
50
60
10
20
20
100
20
30
50
40
150
Find the Shortest Path from Node 1 to every other node
Dynamic Programming
Dependent subproblems, recursive
solutions, memoizing, explicit tables.
Knapsack
Longest Common Subsequence
Matrix Multiplication
Floyd-Warshall-Kleene
Dynamic Programming
Consider a sequence of n numbers, A = {a1,
a2, a3, ..., an}. A subsequence of a
given sequence is just the given
sequence with 0 or more of the elements
removed.
Let's find an algorithm that finds the
subsequence, A' = {ai1, ai2, ai3, ...aik} of A
that maximizes ai1 - ai2 + ai3 - ... +- aik
(i.e. the sign alternates).
Game trees
A Tic Tac Toe Game Tree
moves
moves
moves
Alpha Beta Example
10
Max
Min
10
10
Max
Min
10
2
7
 =7
12
12
 = 10
7
2
7
 > !
Games
2-person, deterministic, complete
information, ...
Backtracking
MiniMax
Alpha-beta pruning
Heuristics, iterative deepening, move order,
tricks, ...
Client-Server Computing
Socket communication
Sockets provide a bi-directional communication
channel from one process to another.
process
write
read
Host
process
network
socket
read
write
socket
Client-server interaction
Host
Server
Messages are queued
• at sending socket until transmitted across the network and
• at receiving socket until received by the receiving
process.
listenSocket =
ServerSocket (port)
14
connectionSocket
=
listenSocket .accept()
read request(s) from
connectionSocket
write reply(s) to
connectionSocket
connectionSocket
Client
TCP
session
clientSocket =
Socket( hostid ,port)
send request(s) to
clientSocket
read reply(s) from
clientSocket
.close()
clientSocket .close()
18
Union-find
Union, v.0
1
3
2
 {1,2}{0,3}{4}{5}
{1,2,0,3}{4}{5}
1
 union(0,2)
s: 3 -1 1 -1 -1 -1
s’: 3 -1
0
1
1
2
1 -1 -1
3
4
5
4
5
0
2
4
3
5
0
before
after
public void union(int x, int y){
s[find(x)] = find(y);
}
Trick 2: Path compression
 find flattens trees
• Redirect nodes to point directly to the root
 Example: find(0)
1
2
4
3
5
1
2
3
0
4 5
0
 Do this whenever traversing a path from
node to root.
Computational Geometry
Intersections
How do we check if two lines intersect?
A +  (B - A) = C +  (D – C)
Two linear equations, two unknowns , 
Add constraints on  for
intersection of rays, line segments.
B
Gift Wrapping
C
A
D
Then find the other extremal points as follows:
tie a string to p0 and then wrap it all around P.
p3
p2
p4
p0
p1
extremal
p0
p1
p2
p3
p4
Computational Geometry
Points, lines, rays, line segments
Intersection, turns
Membership in region
Jarvis' March
Graham Scan
Divide-and-Conquer (QuickHull, ...)
Lower Bound
Grades
1 “warmup”.
39 points.
3 small homeworks.
50 points each.
3 large homeworks.
100 points each.
3 quizzes.
20 points each.
Midterm exam.
125 points.
Final exam.
275 points.
TA discretion.
50 points.
Final Exam
Tuesday, May 4, 5:30 – 8:30 pm
McConomy
(*not* in DH)
Sugary stuff will be provided ...
Make sure not to be late.
Final Exam
Closed book, no calculators, cell
phones, pagers, IM, …
You can bring 1 (one) page of notes.
Conclusion
Review all material
Do all sample quizzes and finals
Good luck!!