Upload
kaiden-brister
View
227
Download
3
Embed Size (px)
Citation preview
CS 410 Applied Algorithms
Exam Next Week
• Don’t Forget• We will use half of next weeks class time
for an exam.• The exam will be closed book. You may
bring one sheet (8.5 x 11.0 inch) of paper with notes on it.
• The exam will cover the reading, assignments and lectures upto the day of the exam.
CS 410 Applied Algorithms
Quiz
• We have a 10 minute quiz today on the reading – Chapter 5.
• We will continue to have quizzes until the class gets an average score high enough to convince me that people are doing the reading.
CS 410 Applied Algorithms
Pairsumonious
• [2, 4, 5, 6] -- Unknown original numbers
• [ 2+4, 2+5, 2+6, 4+5, 4+6, 5+6 ]
-- all pair wise sums
• [6,7,8,9,10,11] -- Given numbers
CS 410 Applied Algorithms
Computing pair-wise positions
• N = 4[[(1,2),(1,3),(1,4)], [(2,3),(2,4)], [(3,4)]]
• N = 5[[(1,2),(1,3),(1,4),(1,5)], [(2,3),(2,4),(2,5)], [(3,4),(3,5)], [(4,5)]]
CS 410 Applied Algorithms
Sort Assumptions
• Assume that both the unknown list is sorted, and that the given list is sorted. We can sort it, if it isn’t.
• [x1, x2, x3, x4 ] -- Unknowns
• where x1 x2, x2 x3, x3 x4
• [6,7,8,9,10] -- Given numbers sorted
• x1 + x2 = 6
• x1 + x3 = 7 -- Why?
CS 410 Applied Algorithms
xm + xn = 8
• What are the possible values for m and n?
• x1 + x4 = 8
• x2 + x3 = 8
• Why isn’t x2 + x4 = 8, a possiblility?
CS 410 Applied Algorithms
Enumerate the possibilities (X3+X4 = 11) (X2+X4 = 10) (X2+X3 = 9) (X1+X4 = 8)(X1+X2 = 6) (X1+X3 = 7) (X2+X3 = 8) (X1+X4 = 9) (X2+X4 = 10) (X3+X4 = 11)
Every path from root to leaf is a possible solution.
CS 410 Applied Algorithms
Given a Table choose the next nodes in the tree
[[(1,2),(1,3),(1,4)], [(2,3),(2,4)], [(3,4)]]
next2 :: Table -> [((Int,Int),Table)]next2 [] = []next2 [(x:xs)] = [(x,[xs])]next2 ([]:ys) = next2 ysnext2 ((x:xs):(y:ys):zs) = case order x y of Less -> [(x,xs:(y:ys):zs)] Greater -> [(y,(x:xs):ys:zs)] Incomp -> [(x,xs:(y:ys):zs),(y,(x:xs):ys:zs)]
CS 410 Applied Algorithms
Generate All Paths
paths :: [Int] -> Path ->
((Int,Int),Table) -> [Path]
paths (s:sums) path ((i,j),xs) =
case next2 xs of
[] -> [reverse ((i,j,s):path)]
zs -> concat(map (paths sums ((i,j,s):path)) zs)
CS 410 Applied Algorithms
Two Paths
• [(1,2,6),(1,3,7),(1,4,8),(2,3,9),(2,4,10),(3,4,11)]• [(1,2,6),(1,3,7),(2,3,8),(1,4,9),(2,4,10),(3,4,11)]
CS 410 Applied Algorithms
Example of size 5
[[(1,2),(1,3),(1,4),(1,5)], [(2,3),(2,4),(2,5)], [(3,4),(3,5)], [(4,5)]] • when n= 5 • sums = [-1,0,-1,-2,1,0,-1,1,0,1]• [-2,-1,-1,-1,0,0,0,1,1,1]
CS 410 Applied Algorithms
(X4+X5 = 1)
(X3+X5 = 1)
(X3+X4 = 1)
(X2+X5 = 0)
(X2+X3 = 0)
(X1+X5 = -1)
(X2+X4 = 0)
(X3+X4 = 0)
(X2+X5 = 1)
(X3+X5 = 1)
(X4+X5 = 1)
(X1+X4 = -1)
(X4+X5 = 1)
(X3+X5 = 1)
(X3+X4 = 1)
(X2+X5 = 0)
(X2+X4 = 0)
(X1+X5 = 0)
(X3+X4 = 0)
(X2+X5 = 1)
(X3+X5 = 1)
(X4+X5 = 1)
(X2+X3 = -1)
(X4+X5 = 1)
(X3+X5 = 1)
(X2+X5 = 1)
(X3+X4 = 0)
(X2+X4 = 0)
(X1+X5 = 0)
(X2+X5 = 0)
(X3+X4 = 1)
(X3+X5 = 1)
(X4+X5 = 1)
(X1+X2 = -2)
(X1+X3 = -1)
(X4+X5 = 1)
(X3+X5 = 1)
(X3+X4 = 1)
(X2+X5 = 0)
(X2+X4 = 0)
(X1+X5 = 0)
(X3+X4 = 0)
(X2+X5 = 1)
(X3+X5 = 1)
(X4+X5 = 1)
(X1+X4 = -1)
(X2+X3 = -1)
(X4+X5 = 1)
(X3+X5 = 1)
(X2+X5 = 1)
(X3+X4 = 0)
(X2+X4 = 0)
(X1+X5 = 0)
(X2+X5 = 0)
(X3+X4 = 1)
(X3+X5 = 1)
(X4+X5 = 1)
CS 410 Applied Algorithms
Possible paths from root to leaf[(1,2,-2),(1,3,-1),(1,4,-1),(1,5,-1),(2,3,0),(2,4,0),(2,5,0),(3,4,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(1,4,-1),(1,5,-1),(2,3,0),(2,4,0),(3,4,0),(2,5,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(1,4,-1),(2,3,-1),(1,5,0),(2,4,0),(2,5,0),(3,4,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(1,4,-1),(2,3,-1),(1,5,0),(2,4,0),(3,4,0),(2,5,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(1,4,-1),(2,3,-1),(2,4,0),(1,5,0),(2,5,0),(3,4,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(1,4,-1),(2,3,-1),(2,4,0),(1,5,0),(3,4,0),(2,5,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(2,3,-1),(1,4,-1),(1,5,0),(2,4,0),(2,5,0),(3,4,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(2,3,-1),(1,4,-1),(1,5,0),(2,4,0),(3,4,0),(2,5,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(2,3,-1),(1,4,-1),(2,4,0),(1,5,0),(2,5,0),(3,4,1),(3,5,1),(4,5,1)]
[(1,2,-2),(1,3,-1),(2,3,-1),(1,4,-1),(2,4,0),(1,5,0),(3,4,0),(2,5,1),(3,5,1),(4,5,1)]
CS 410 Applied Algorithms
Is a path feasible?
• [(1,2,6),(1,3,7),(1,4,8),(2,3,9),(2,4,10),(3,4,11)]
• [(x1+x2=6),(x1+x3=7),(x1+x4=8),
(x2+x3=9),(x2+x4=10),(x3+x4=11)]
How can we test this?
CS 410 Applied Algorithms
Every path contains all pair-wise sums
• So they must contain the following (x1+x2=A) (x1+x3=B) (x2+x3=C)
• N=4 examples– [(1,2,6),(1,3,7),(1,4,8),(2,3,9),(2,4,10),(3,4,11)]– [(1,2,6),(1,3,7),(2,3,8),(1,4,9),(2,4,10),(3,4,11)]
• N=5 example[(1,2,-2),(1,3,-1),(1,4,-1),(2,3,-1),(2,4,0),(1,5,0),(2,5,0),(3,4,1),(3,5,1),(4,5,1)]
CS 410 Applied Algorithms
• (x1+x2=A) (x1+x3=B) (x2+x3=C)
(x1+x2) + (x1+x3) - (x2+x3) = A + B – C
2 * x1 = A + B – C
Solving for x1 we get x1 = (A+B-C)/2
Algorithm find the 3 pairs in the path and solve
CS 410 Applied Algorithms
CodesolveX1 :: Int -> Int -> Int -> Path -> Maybe IntsolveX1 x12 x13 x23 ((1,2,w):ys) = solveX1 w x13 x23 yssolveX1 x12 x13 x23 ((1,3,w):ys) = solveX1 x12 w x23 yssolveX1 x12 x13 x23 ((2,3,w):ys) = solveX1 x12 x13 w yssolveX1 x12 x13 x23 (y:ys) = solveX1 x12 x13 x23 yssolveX1 x12 x13 x23 [] = if even sum then Just(sum `div` 2) else Nothing where sum = x12+x13-x23
CS 410 Applied Algorithms
Every path also contains
• N=4 examples– [(1,2,6),(1,3,7),(1,4,8),(2,3,9),(2,4,10),(3,4,11)]– [(1,2,6),(1,3,7),(2,3,8),(1,4,9),(2,4,10),(3,4,11)]
• N=5 example[(1,2,-2),(1,3,-1),(1,4,-1),(2,3,1),(2,4,0),(1,5,0),(2,5,0),(3,4,1),(3,5,1),(4,5,1)]
CS 410 Applied Algorithms
CodesolveRest :: Int -> Path -> [(Int,Int)] -> [(Int,Int)]solveRest x1 [] pairs = pairssolveRest x1 ((1,xn,sum):xs) pairs = solveRest x1 xs ((xn,sum - x1):pairs)solveRest x1 (x:xs) pairs = solveRest x1 xs pairs
solve [] = Nothingsolve (p:paths) = case solveX1 0 0 0 p of Just x1 -> Just(map snd (sort(solveRest x1 p [(1,x1)]))) Nothing -> solve paths
CS 410 Applied Algorithms
In Class Problems
• How Many Fibs? 6.6.1– Page 137 of the text– We will write this together as a class
• Expressions 6.6.4– Page 140 of the text–
CS 410 Applied Algorithms
Today’s Assignments
Read for next time
Chapter 7 of the text. pp 147-166 Be prepared to answer questions in class next Friday from
the reading.
Programming assignment
• 6.6.8 Steps• Page 145• Write a solutionSubmit your solution (until you get it right)Hand in both your program, and the judge output.Those who volunteer to discuss their program get class participation points.
Email me solutions before noon on Friday, May 6.