course review 15-211 fundamental structures of computer science margaret reid-miller 29 april 2004
TRANSCRIPT
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
The single rotation
§ For the case of insertion into left subtree of left child:
Z
YX
ZYX
Deepest node of X has depth 2 greater than deepest node of Z.
Depth reduced by 1
Depth increased by 1
Double rotation
§ For the case of insertion into the right subtree of the left child.
Z
X
Y1 Y2
ZX Y1 Y2
Dictionaries
Keypad IM Trie4 5 9
4 6 6
5 8 8
3 3
I
like love
you
5
9
lovely
…
…
Separate chaining
§ Build chains when keys hash to the same table location
0123456789
101112
Aswithlongkeyint
hash
find
in
testinfo
foo
linkchar
onto
fail
foo
urlopen
type
list
queue
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).
2
1
98
4
10
5 76
3
1 2 3 4 5 6 7 8 9 10
Percolation down
• Bubble the transplanted leaf value down the tree until the heap order property is satisfied.
14
31
2665
24
32
21 6819
1614
--
2665
24
32
21 6819
16
31
1 2
Compression and Huffman’s
Compare two images
One image is 400K the other is 1100K. Which is which?
Tree representation
• Represent prefix free codes as full binary trees
• Full: every node§ Is a leaf, or§ Has exactly 2 children.
• The encoding is then a (unique) path from the root to a leaf.
c
a
b
d0
0
0
1
1
1
a=1, b=001, c=000, d=01
LZW compression
Binary LZW: Compression example
10010110011Input:^
0 1
Dictionary:
Output: 1034
1 2
0 1
0
0 1
3
0 1
4 5
0 1
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
N^2Nlog N
Quicksort idea
§ Choose a pivot.
§ Rearrange so that pivot is in the “right” spot.
§ Recurse on each half and conquer!
Upper and lower bounds
N
d× ( )g N( )T N
( ) = T N O( ( ))f N( ) = T N Ω( ( ))g N
c× ( )f 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
KMP B -M
16 comparisons
This is a string
-----------tring
This is a string
--
-gn
irt
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(n-1) edges. Can this statement be true?
Graphs
Undirected
Graphs — an overview
PIT
BOS
JFK
DTW
LAX
SFO
Vertices (aka nodes)
Edges
1987
2273
3442145
2462
618
211
318
190
Weights
Dijkstra’s algorithm
Visiteds (D = 0)b (D = 2)d (D = 3)c (D = 4)
a e f g
4 6 6 ∞
s
f
a
b
d
e
c
g
4
2
5
1
11
4
2
2
33
How Can We Generate a MST?
a
ce
d
b2
45
9
6
4
5
5
a
ce
d
b2
45
9
6
4
5
5
Greedy Algorithms
100
5060
3050
60
20
50
20
6040
50
150
40
20
40
20
40 40
10
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
movesAlpha Beta Example
10 2 12 2 7
10 12 7
10 7
10Max
Max
Min
Min
α = 10
β =7
α > β!
Games
2-person, deterministic, complete information, ...
Backtracking
MiniMax
Alpha-beta pruning
Heuristics, iterative deepening, move order, tricks, ...
Client-Server Computing
14
Socket communication
process
writeread socket
process
read writesocket
network
Host Host
Sockets provide a bi-directional communicationchannel from one process to another.
Messages are queued• at sending socket until transmitted across the network and• at receiving socket until received by the receiving process.
18
Client-server interaction
listenSocket = ServerSocket(port)
connectionSocket = listenSocket.accept()
read request(s) from connectionSocket
write reply(s) to connectionSocket
connectionSocket .close()
clientSocket = Socket( hostid ,port)
send request(s) to clientSocket
read reply(s) from clientSocket
clientSocket .close()
Server
Client
TCPsession
Union-find
Union, v.0
13
0
42
5§ {1,2}{0,3}{4}{5}{1,2,0,3}{4}{5}
§ union(0,2)s: 3 -1 1 -1 -1 -1 before
s’: 3 -1 1 1 -1 -1 after0 1 2 3 4 5
public void union(int x, int y){s[find(x)] = find(y);
}
1 30
42
5
Trick 2 : Path compression
§ find flattens trees• Redirect nodes to point directly to the root
§ Example: find(0)
§ Do this whenever traversing a path from node to root.
13
0
42
5 10
42
53
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
A
D
CB
Gift Wrapping
Then find the other extremal points as follows:tie a string to p0 and then wrap it all around P.
extremalp0
p1p2p3
p4
p0p1
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”.
3 small homeworks.
3 large homeworks.
3 quizzes.
Midterm exam.
Final exam.
TA discretion.
39 points.
50 points each.
100 points each.
20 points each.
125 points.
275 points.
50 points.
SUBJECT TO CHANGE
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!!