Scientific Programming: Algorithms (part B) Programming paradigms - - - PowerPoint PPT Presentation

scientific programming algorithms part b
SMART_READER_LITE
LIVE PREVIEW

Scientific Programming: Algorithms (part B) Programming paradigms - - - PowerPoint PPT Presentation

Scientific Programming: Algorithms (part B) Programming paradigms - continued - Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor] Greedy algorithms Independent intervals Independent


slide-1
SLIDE 1

Scientific Programming: Algorithms (part B)

Programming paradigms - continued -

Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor]

slide-2
SLIDE 2

Greedy algorithms

slide-3
SLIDE 3

Independent intervals

slide-4
SLIDE 4

Independent intervals

these three intervals are not maximal!

intervals are open on the right, hence these are disjoint

slide-5
SLIDE 5

Independent intervals

slide-6
SLIDE 6

Path to the solution

slide-7
SLIDE 7

Optimal substructure

b0 an+1

ends at -∞ starts at +∞

S[i,j]

slide-8
SLIDE 8

Optimal substructure

i j k S[i,k] S[k,j]

  • ptimal solution A[i,j]
  • nce found k that belongs

to the optimal solution A[i,j], we need to solve the two smaller intervals

slide-9
SLIDE 9

Optimal substructure

i j k S[i,k] S[k,j]

  • ptimal solution A[i,j]
  • nce found k that belongs

to the optimal solution A[i,j], we need to solve the two smaller intervals

k

We want to prove that if A[i,j] contains the optimal solution of S[i,j] and k is in A[i,j] then it optimally solves S[i,k] and S[k,j]. By contradiction: A[i,j] S[i,k] k S[k,j]

  • ex. if S[i,k] is better than the corresponding

intervals in A[i,j] → A[i,j] is not optimal

slide-10
SLIDE 10

Optimal substructure

i j k S[i,k] S[k,j]

  • ptimal solution A[i,j]
  • nce found k that belongs

to the optimal solution A[i,j], we need to solve the two smaller intervals

k

We want to prove that if A[i,j] contains the optimal solution of S[i,j] and k is in A[i,j] then it optimally solves S[i,k] and S[k,j]. By contradiction: A[i,j] S[i,k] k S[k,j]

  • ex. if S[i,k] is better than the corresponding

intervals in A[i,j] → A[i,j] is not optimal

slide-11
SLIDE 11

Recursive formula

  • therwise

because we chose interval K

slide-12
SLIDE 12

Dynamic programming

top-down: DP[0,n]

slide-13
SLIDE 13

Complexity

slide-14
SLIDE 14

Greedy choice

Proof

slide-15
SLIDE 15

Greedy choice

Proof

slide-16
SLIDE 16

Greedy choice

Consequences of the theorem

m’ m

slide-17
SLIDE 17

Greedy algorithm

#first greedy choice #other greedy choices

Complexity? If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-18
SLIDE 18

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-19
SLIDE 19

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-20
SLIDE 20

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-21
SLIDE 21

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-22
SLIDE 22

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-23
SLIDE 23

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-24
SLIDE 24

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-25
SLIDE 25

Greedy algorithm

#first greedy choice #other greedy choices

Complexity If input not sorted: O(n log n + n) = O(n log n) If input sorted: O(n)

slide-26
SLIDE 26

Genome rearrangements

Transformation of mouse gene order into human gene order on Chr X (biggest synteny blocks)

[3, 5, 2, 4, 1] [3, 2, 5, 4, 1] [3, 2, 1, 4, 5] [1, 2, 3, 4, 5]

slide-27
SLIDE 27

Genome rearrangements

slide-28
SLIDE 28

Greedy solution

slide-29
SLIDE 29

Greedy solution

In list: [2, 4, 1, 3, 0] [0, 3, 1, 4, 2] [0, 1, 3, 4, 2] [0, 1, 2, 4, 3] [0, 1, 2, 3, 4] In list: [5, 0, 1, 2, 3, 4] [0, 5, 1, 2, 3, 4] [0, 1, 5, 2, 3, 4] [0, 1, 2, 5, 3, 4] [0, 1, 2, 3, 5, 4] [0, 1, 2, 3, 4, 5]

Simple but not optimal! Approximated algorithms exist...

In list: [5, 0, 1, 2, 3, 4] [4, 3, 2, 1, 0, 5] [0, 1, 2, 3, 4, 5]

slide-30
SLIDE 30

Backtracking

slide-31
SLIDE 31

Typical problems

we explore all possible solutions building/enumerating them and counting or stopping when we find one

slide-32
SLIDE 32

Typical problems

slide-33
SLIDE 33

Typical problems

slide-34
SLIDE 34

Build all solutions

slide-35
SLIDE 35

Backtracking

Approach

  • Try to build a solution, if it works you are done else undo it

and try again

  • “keep trying, you’ll get luckier”

Needs a systematic way to explore the search space looking for the admissible solution(s)

slide-36
SLIDE 36

General scheme

slide-37
SLIDE 37

Partial solutions

slide-38
SLIDE 38

Decision tree

Note: the decision tree is “virtual” we do not need to store it all...

slide-39
SLIDE 39

Decision tree

Note: the decision tree is “virtual” we do not need to store it all...

slide-40
SLIDE 40

Decision tree

Note: the decision tree is “virtual” we do not need to store it all...

slide-41
SLIDE 41

Decision tree

process or ignore the solution Note: the decision tree is “virtual” we do not need to store it all...

slide-42
SLIDE 42

Decision tree

solution ignored Note: the decision tree is “virtual” we do not need to store it all...

slide-43
SLIDE 43

Decision tree

Note: the decision tree is “virtual” we do not need to store it all...

slide-44
SLIDE 44

Decision tree

Note: the decision tree is “virtual” we do not need to store it all...

slide-45
SLIDE 45

Decision tree

Note: the decision tree is “virtual” we do not need to store it all...

slide-46
SLIDE 46

Decision tree

Note: the decision tree is “virtual” we do not need to store it all... solution processed

slide-47
SLIDE 47

Pruning

Note: the decision tree is “virtual” we do not need to store it all... Even though the tree might be exponential, with pruning we might not need to explore it all

slide-48
SLIDE 48

Pruning

Even though the tree might be exponential, with pruning we might not need to explore it all

slide-49
SLIDE 49

General schema to find a solution (modify as you like)

S is the list of choices n is the maximum number of choices i is the index of the choice I am currently making … other inputs

1. We build a next choice with choices(...) based on the previous choices S[0:i-1]: the logic of the code goes here 2. For each possible choice, we memorize the choice in S[i] 3. If S[i] is admissible then we process it and we can either stop (if we needed at least

  • ne solution) or continue to the next one (return false)

4. In the latter case we keep going calling enumeration again to compute choice i+1 The recursive call will test all solutions unless they return true

slide-50
SLIDE 50

Enumeration

slide-51
SLIDE 51

Subsets problem

subsets([0, 0, 0, 0, 0],5,0) Calling: subsets([1, 0, 0, 0, 0],5,1) subsets([1, 0, 0, 0, 0],5,1) Calling: subsets([1, 1, 0, 0, 0],5,2) subsets([1, 1, 0, 0, 0],5,2) Calling: subsets([1, 1, 1, 0, 0],5,3) subsets([1, 1, 1, 0, 0],5,3) Calling: subsets([1, 1, 1, 1, 0],5,4) subsets([1, 1, 1, 1, 0],5,4) S:[1, 1, 1, 1, 1] c:1 i:4 1 1 1 1 1 S:[1, 1, 1, 1, 0] c:0 i:4 1 1 1 1 0 Calling: subsets([1, 1, 1, 0, 0],5,4) subsets([1, 1, 1, 0, 0],5,4) S:[1, 1, 1, 0, 1] c:1 i:4 1 1 1 0 1 S:[1, 1, 1, 0, 0] c:0 i:4 1 1 1 0 0 Calling: subsets([1, 1, 0, 0, 0],5,3) subsets([1, 1, 0, 0, 0],5,3) ...

False: we want all solutions choice: keep or discard element an admissible solution has decided if to keep or discard all elements

slide-52
SLIDE 52

Subsets problem

( → i.e. 2^n sets, printing each costs n)

slide-53
SLIDE 53

Subsets problem

( → i.e. 2^n sets, printing each costs n) ( → 11111 first and then values decrease...)

slide-54
SLIDE 54

Subsets problem:iterative code

n=3, k=1

slide-55
SLIDE 55

Subsets problem:iterative code

What is the complexity of this iterative code?

creation of the subsets (cost: O(n)) all subsets (cost: O(2^n)) printing subsets (cost: O(n))

How many solutions are we testing?

no pruning… can we improve this?

n=3, k=1

slide-56
SLIDE 56

Subsets problem: bactracking

Still generates 2^n subsets, for each it will count how many 1s are present and finally print only the ones having a correct number of 1s.

What is the complexity of this backtracking code? How many solutions are we testing?

no pruning… can we improve this?

n=3, k=1

count how many 1s we want all solutions admissible solutions have k 1s

slide-57
SLIDE 57

Subsets problem: bactracking & pruning

What is the complexity of this iterative code?

generate only solutions that can potentially be admissible!

X

n=3, k=1

slide-58
SLIDE 58

Sudoku

slide-59
SLIDE 59

Sudoku: pseudocode

slide-60
SLIDE 60

Sudoku: pseudocode

slide-61
SLIDE 61

Sudoku: python code

slide-62
SLIDE 62

8 queens puzzle

slide-63
SLIDE 63

8 queens puzzle