Download presentation

Presentation is loading. Please wait.

Published byKaiden Brister Modified over 2 years ago

1
CS 410 Applied Algorithms Applied Algorithms Lecture #7 Counting

2
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.

3
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.

4
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

5
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)]]

6
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. [x 1, x 2, x 3, x 4 ] -- Unknowns where x 1 x 2, x 2 x 3, x 3 x 4 [6,7,8,9,10] -- Given numbers sorted x 1 + x 2 = 6 x 1 + x 3 = 7 -- Why?

7
CS 410 Applied Algorithms x m + x n = 8 What are the possible values for m and n? x 1 + x 4 = 8 x 2 + x 3 = 8 Why isn’t x 2 + x 4 = 8, a possiblility?

8
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.

9
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 ys next2 ((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)]

10
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)

11
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)]

12
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]

13
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)

14
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)]

15
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)] [(x 1 +x 2 =6),(x 1 +x 3 =7),(x 1 +x 4 =8), (x 2 +x 3 =9),(x 2 +x 4 =10),(x 3 +x 4 =11)] How can we test this?

16
CS 410 Applied Algorithms Every path contains all pair-wise sums So they must contain the following (x 1 +x 2 =A) (x 1 +x 3 =B) (x 2 +x 3 =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)]

17
CS 410 Applied Algorithms (x 1 +x 2 =A) (x 1 +x 3 =B) (x 2 +x 3 =C) (x 1 +x 2 ) + (x 1 +x 3 ) - (x 2 +x 3 ) = A + B – C 2 * x 1 = A + B – C Solving for x1 we get x 1 = (A+B-C)/2 Algorithm find the 3 pairs in the path and solve

18
CS 410 Applied Algorithms Code solveX1 :: Int -> Int -> Int -> Path -> Maybe Int solveX1 x12 x13 x23 ((1,2,w):ys) = solveX1 w x13 x23 ys solveX1 x12 x13 x23 ((1,3,w):ys) = solveX1 x12 w x23 ys solveX1 x12 x13 x23 ((2,3,w):ys) = solveX1 x12 x13 w ys solveX1 x12 x13 x23 (y:ys) = solveX1 x12 x13 x23 ys solveX1 x12 x13 x23 [] = if even sum then Just(sum `div` 2) else Nothing where sum = x12+x13-x23

19
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)]

20
CS 410 Applied Algorithms Code solveRest :: Int -> Path -> [(Int,Int)] -> [(Int,Int)] solveRest x1 [] pairs = pairs solveRest x1 ((1,xn,sum):xs) pairs = solveRest x1 xs ((xn,sum - x1):pairs) solveRest x1 (x:xs) pairs = solveRest x1 xs pairs solve [] = Nothing solve (p:paths) = case solveX1 0 0 0 p of Just x1 -> Just(map snd (sort(solveRest x1 p [(1,x1)]))) Nothing -> solve paths

21
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 –

22
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 solution Submit 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.

Similar presentations

OK

Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna

Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on the land of dense forest Download ppt on yoga and meditation Ppt on self help groups in india Download ppt on ceramic disc brake Ppt on review of literature paper Ppt on p&g products images Free download ppt on human evolution Ppt on waves tides and ocean currents lesson Ppt on indian stock market basics Ppt on wireless network technologies