The Power of Computing Algorithms

advertisement
The Power of
Computing Algorithms
Daryle J. Serrant – Past Undergraduate Student
Debasis Mitra – dmitra@cs.fit.edu
Florida Institute of Technology
Computer Science
What is an Algorithm?
Cooking <- Recipes
Building construction <- Blue print
Software <- Algorithm
Example Problem 1
Best Return on a Stock
•
We have daily stock prices over
a period:
• Gains as positive
• Loss as negative
•
When should we have bought
and when should we have sold
Problem Example 2
Maximum Customers visited in a
Store
• # Customers visited & left every hour is tracked
• Incoming number as positive
• Leaving number as negative
• Between which hours maximum #customers were in the store
A Computing Problem:
Maximum Subsequence
•
What is common between the two examples?
•
Can you find the maximum sub-sequence
of the following numbers:
3, 4, -8, 1, 9, -2, 3, -1
Answer: {1, 9, -2, 3} for the max sum 11.
Question: How fast can we get the answer?
Answer: Depends on the ALGORITHM!
The Workaholic Approach
•
Go over every possible subsequence:
Day 1 – Day 2, Day 1 – Day 3, etc.
•
Find the subsequence with the largest sum.
Image Source: http://www.grinningplanet.com/2003/workaholics/joke-1704.htm/
Day 1 – Day 1
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 + 3 = 3
Max Sum: 3
Maximum Subsequence: Day 1
Addition
Counter:
1
Day 1 – Day 2
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
2
Day 1 – Day 2
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 = 7
Max Sum: 3
Maximum Subsequence: Day 1
Max Sum is now 7 and Maximum Subsequence is now Day 1 - 2
Addition
Counter:
3
Day 1 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
4
Day 1 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4
Addition
Counter:
5
Day 1 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 - 8 = -1
Max Sum: 7
Maximum Subsequence: Day 1 - 2
No change
Addition
Counter:
6
Day 1 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
7
Day 1 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4
Addition
Counter:
8
Day 1 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8
Addition
Counter:
9
Day 1 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 = 0
Max Sum: 7
Maximum Subsequence: Day 1 - 2
No changes
Addition
Counter:
10
Day 1 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
11
Day 1 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4
Addition
Counter:
12
Day 1 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8
Addition
Counter:
13
Day 1 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1
Addition
Counter:
14
Day 1 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9 = 9
Max Sum: 7
Maximum Subsequence: Day 1 - 2
Max Sum is now 9 and Maximum Subsequence is Day 1 – Day 5
Addition
Counter:
15
Day 1 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
16
Day 1 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4
Addition
Counter:
17
Day 1 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8
Addition
Counter:
18
Day 1 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1
Addition
Counter:
19
Day 1 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9
Addition
Counter:
20
Day 1 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9 – 2 = 7
Max Sum: 9
Maximum Subsequence: Day 1 - 5
No change
Addition
Counter:
21
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
22
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4
Addition
Counter:
23
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8
Addition
Counter:
24
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1
Addition
Counter:
25
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9
Addition
Counter:
26
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9 – 2
Addition
Counter:
27
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9 – 2 + 3 = 10
Max Sum: 9
Maximum Subsequence: Day 1 - 5
Max Sum is now 10 and Maximum Subsequence is Day 1 - 7
Addition
Counter:
28
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
29
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4
Addition
Counter:
30
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8
Addition
Counter:
31
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1
Addition
Counter:
32
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9
Addition
Counter:
33
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9 – 2
Addition
Counter:
34
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9 – 2 + 3
Addition
Counter:
35
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 + 4 – 8 + 1 + 9 – 2 + 3 – 1 = 9
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
36
Day 2 – Day 2
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 + 4 = 4
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
37
Day 2 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4
Addition
Counter:
38
Day 2 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 = -4
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
39
Day 2 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4
Addition
Counter:
40
Day 2 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8
Addition
Counter:
41
Day 2 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 = -3
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
42
Day 2 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4
Addition
Counter:
43
Day 2 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8
Addition
Counter:
44
Day 2 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1
Addition
Counter:
45
Day 2 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9 = 6
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
46
Day 2 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4
Addition
Counter:
47
Day 2 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8
Addition
Counter:
48
Day 2 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1
Addition
Counter:
49
Day 2 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9
Addition
Counter:
50
Day 2 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9 – 2 = 4
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
51
Day 2 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4
Addition
Counter:
52
Day 2 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8
Addition
Counter:
53
Day 2 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1
Addition
Counter:
54
Day 2 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9
Addition
Counter:
55
Day 2 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 7 + 1 + 9 – 2
Addition
Counter:
56
Day 2 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9 – 2 + 3 = 7
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
57
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4
Addition
Counter:
58
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8
Addition
Counter:
59
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1
Addition
Counter:
60
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9
Addition
Counter:
61
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9 – 2
Addition
Counter:
62
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9 – 2 + 3
Addition
Counter:
63
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 4 – 8 + 1 + 9 – 2 + 3 – 1 = 6
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
64
Day 3 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 – 8 = -8
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
65
Day 3 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 = -8
Addition
Counter:
66
Day 3 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 = -7
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
67
Day 3 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 = -8
Addition
Counter:
68
Day 3 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1
Addition
Counter:
69
Day 3 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9 = 2
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
70
Day 3 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8
Addition
Counter:
71
Day 3 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1
Addition
Counter:
72
Day 3 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9
Addition
Counter:
73
Day 3 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9 – 2 = 0
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
74
Day 3 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8
Addition
Counter:
75
Day 3 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1
Addition
Counter:
76
Day 3 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9
Addition
Counter:
77
Day 3 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9 – 2
Addition
Counter:
78
Day 3 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9 – 2 + 3 = 3
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
79
Day 3 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8
Addition
Counter:
80
Day 3 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1
Addition
Counter:
81
Day 3 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9
Addition
Counter:
82
Day 3 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9 – 2
Addition
Counter:
83
Day 3 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9 – 2 + 3
Addition
Counter:
84
Day 3 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 8 + 1 + 9 – 2 + 3 – 1 = 2
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
85
Day 4 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 + 1 = 1
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
86
Day 4 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1
Addition
Counter:
87
Day 4 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9 = 10
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
88
Day 4 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1
Addition
Counter:
89
Day 4 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9
Addition
Counter:
90
Day 4 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9 – 2 = 8
Max Sum: 10
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
91
Day 4 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1
Addition
Counter:
92
Day 4 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9
Addition
Counter:
93
Day 4 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9 – 2
Addition
Counter:
94
Day 4 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9 – 2 + 3 = 11
Max Sum: 10
Maximum Subsequence: Day 1 - 7
Max Sum is 11 and Maximum Subsequence is now Day 4 – Day 7
Addition
Counter:
95
Day 4 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1
Addition
Counter:
96
Day 4 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9
Addition
Counter:
97
Day 4 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9 – 2
Addition
Counter:
98
Day 4 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9 – 2 + 3
Addition
Counter:
99
Day 4 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 1 + 9 – 2 + 3 – 1 = 10
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
100
Day 5 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 + 9 = 9
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
101
Day 5 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9
Addition
Counter:
102
Day 5 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9 – 2 = 7
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
103
Day 5 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9
Addition
Counter:
104
Day 5 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9 – 2
Addition
Counter:
105
Day 5 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9 – 2 + 3 = 10
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
106
Day 5 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9
Addition
Counter:
107
Day 5 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9 – 2
Addition
Counter:
108
Day 5 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9 – 2 + 3
Addition
Counter:
109
Day 5 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 9 – 2 + 3 – 1 = 9
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
110
Day 6 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 – 2 = -2
Max Sum: 11
Maximum Subsequence: Day 1 - 7
No changes
Addition
Counter:
111
Day 6 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 2
Addition
Counter:
112
Day 6 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 2 + 3 = 1
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
113
Day 6 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 2
Addition
Counter:
114
Day 6 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 2 + 3
Addition
Counter:
115
Day 6 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 – 2 + 3 – 1 = 0
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
116
Day 7 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 + 3 = 3
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
117
Day 7 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3
Addition
Counter:
118
Day 7 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start
End
Sum: 0 + 3 – 1 = 2
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
119
Day 8 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Start End
Sum: 0 – 1 = -1
Max Sum: 11
Maximum Subsequence: Day 4 - 7
No changes
Addition
Counter:
120
Analysis
•
Each sub-sequence between green & red ptrs is aggregated,
at most 8 addition counting operations per sub-sequence,
•
… by using the black pointer
•
There is 32 subsequences in total
•
That is a lot of adding to do, don’t you think?
•
Imagine having to find the maximum sub-sequence over100
days.
Or, maybe 20,000 days.
•
Given n days of stock data, the number of addition
operations that you will perform using the workaholic
approach will be in the ballpark of around n3
Improve!
•
Do we really need to run the black/middle pointer over and
over again on same numbers?
•
As the red/right pointer moves cann’t we just add that
number to the previous sum?
The Conservative Approach
•
The Workaholic approach requires a lot
of addition operations
•
So much, that it’s impractical to use
for large data sets
•
We can save a lot of time by using
accumulated sums from the previous
sub-sequences.
•
Let us call this the Conservative
approach.
Photo source: http://www.cosmeo.com/login.cfm?CFID=12472906&CFTOKEN=15878700
Day 1 – Day 1
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 + 3 = 3
Max Sum: 3
Maximum Subsequence: Day 1 – Day 1
Addition
Counter:
1
Day 1 – Day 2
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 3 + 4 = 7
Max Sum: 3
Maximum Subsequence: Day 1 – Day 1
Max Sum is now 7 and Maximum Subsequence is Day 1 – Day 2
Addition
Counter:
2
Day 1 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 7 – 8 = -1
Max Sum: 7
Maximum Subsequence: Day 1 – Day 2
No changes
Addition
Counter:
3
Day 1 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: -1 + 1 = 0
Max Sum: 7
Maximum Subsequence: Day 1 – Day 2
No changes
Addition
Counter:
4
Day 1 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 + 9 = 9
Max Sum: 7
Maximum Subsequence: Day 1 – Day 2
Max Sum is now 9 and Maximum Subsequence is Day 1 – Day 5
Addition
Counter:
5
Day 1 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 9 – 2 = 7
Max Sum: 9
Maximum Subsequence: Day 1 – Day 5
No changes
Addition
Counter:
6
Day 1 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 7 + 3 = 10
Max Sum: 9
Maximum Subsequence: Day 1 – Day 7
Max Sum is now 10 and Maximum Subsequence is Day 1 – Day 7
Addition
Counter:
7
Day 1 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 10 – 1 = 9
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
8
Day 2 – Day 2
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 + 4 = 4
Max Sum: 11
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
9
Day 2 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 4 – 8 = -4
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
10
Day 2 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: -4 + 1 = -3
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
11
Day 2 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: -3 + 9 = 6
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
12
Day 2 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 6 – 2 = 4
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
13
Day 2 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 4 + 3 = 7
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
14
Day 2 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 7 – 1 = 6
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
15
Day 3 – Day 3
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 – 8 = 8
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
16
Day 3 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: -8 + 1 = -7
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
17
Day 3 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: -7 + 9 = 2
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
18
Day 3 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 2 – 2 = 0
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
19
Day 3 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 + 3 = 3
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
20
Day 3 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 3 – 1 = 2
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
21
Day 4 – Day 4
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 + 1 = 1
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
22
Day 4 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 1 + 9 = 10
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
23
Day 4 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 10 – 2 = 8
Max Sum: 10
Maximum Subsequence: Day 1 – Day 7
No changes
Addition
Counter:
24
Day 4 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 8 + 3 = 11
Max Sum: 11
Maximum Subsequence: Day 1 – Day 7
Max Sum is 11 and Maximum Subsequence is now Day 4 – Day 7
Addition
Counter:
25
Day 4 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 11 – 1 = 10
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
26
Day 5 – Day 5
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 + 9 = 9
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
27
Day 5 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 9 – 2 = 7
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
28
Day 5 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 7 + 3 = 10
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
29
Day 5 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 10 – 1 = 9
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
30
Day 6 – Day 6
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 – 2 = -2
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
31
Day 6 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: -2 + 3 = 1
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
32
Day 6 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 1 – 1 = 0
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
33
Day 7 – Day 7
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 + 3 = 3
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
34
Day 7 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 3 – 1 = 2
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
35
Day 8 – Day 8
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
Sum: 0 – 1 = -1
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
36
Some more Analysis
•
We are now only required to do 1 additional counting
operation for each sub-sequence compared to at most 8 in
the workaholic approach
•
For larger data sets, the number of addition operations will
be in the ball park of around n2, where n is the size of the
set.
• Workaholic approach had this as n3
The Butcher’s Approach
•
The Conservative approach
reduced the number of additions
we needed to do by a factor of n.
•
This is good, but we can do even
better than this.
•
Instead of solving the entire
problem as a whole, how about
cutting the problem down into
smaller pieces that we can solve
that doesn’t require too much
addition, solve those problems,
and combine the results to get
the final result.
•
Let us call this approach the
Butcher’s approach.
Photo source: http://www.reluctantgourmet.com/steak_information.htm
Combine results from smaller problems
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Solve Max Subsequence for Day 1 – Day 4
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Left Max Sum (Day 1 – Day 4) = 7
Left Max Subsequence = Day 1 – Day 2
Addition
Counter:
9
Solve Max Subsequence for Day 5 – Day 8
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Left Max Sum (Day 1 – Day 4) = 7
Right Max Sum (Day 5 – Day 8) = 10
Left Max Subsequence = Day – Day 2
Right Max Subsequence = Day 5 – Day 7
Addition
Counter:
18
Which side has the Largest Sum?
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Left Max Sum (Day 1 – Day 4) = 7
Right Max Sum (Day 5 – Day 8) = 10
Left Max Subsequence = Day – Day 2
Right Max Subsequence = Day 5 – Day 7
Left Max Sum > Right Max Sum? No
Addition
Counter:
18
Which side has the Largest Sum?
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Left Max Sum (Day 1 – Day 4) = 7
Right Max Sum (Day 5 – Day 8) = 10
Left Max Subsequence = Day – Day 2
Right Max Subsequence = Day 5 – Day 7
Left Max Sum > Right Max Sum? No
Max Side Subsequence = Day 5 – Day 7
Addition
Counter:
18
Find the Middle Max Subsequence
Find the starting point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Sum = 1
Left Max Border Sum = 0
Mid Start = Day 4
Left Max Border Sum is now 1
Addition
Counter:
18
Find the Middle Max Subsequence
Find the starting point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Sum = 1 – 8 = -7
Left Max Border Sum = 1
Mid Start = Day 4
No changes made to Left Max Border Sum
Addition
Counter:
19
Find the Middle Max Subsequence
Find the starting point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Sum = -7 + 4 = -3
Left Max Border Sum = 1
Mid Start = Day 4
No changes made to Left Max Border Sum
Addition
Counter:
20
Find the Middle Max Subsequence
Find the starting point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Sum = -3 + 3 = 0
Left Max Border Sum = 1
Mid Start = Day 4
No changes made to Left Max Border Sum
Addition
Counter:
21
Find the Middle Max Subsequence
Find the ending point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
Left Max Border Sum = 1
Mid Start = 4
1
9
-2
3
-1
Sum = 9
Right Max Border Sum = 0
Mid End= Day 5
Right Max Border Sum is now 9
Addition
Counter:
22
Find the Middle Max Subsequence
Find the ending point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
Left Max Border Sum = 1
Mid Start = 4
1
9
-2
3
-1
Sum = 9 – 2 = 7
Right Max Border Sum = 9
Mid End= Day 5
No changes made to Right Max Border Sum
Addition
Counter:
23
Find the Middle Max Subsequence
Find the ending point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
Left Max Border Sum = 1
Mid Start = 4
1
9
-2
3
-1
Sum = 7 + 3 = 10
Right Max Border Sum = 9
Mid End= Day 5
Right Max Border Sum is now 10 and Mid End is Day 7
Addition
Counter:
24
Find the Middle Max Subsequence
Find the ending point
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
Left Max Border Sum = 1
Mid Start = 4
1
9
-2
3
-1
Sum = 10 – 1 = 9
Right Max Border Sum = 10
Mid End= Day 7
No changes made to Right Max Border Sum
Addition
Counter:
25
Finding Middle Max Subsequence
Compute Middle Max Sum
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Left Max Border Sum = 1
Right Max Border Sum = 10
Mid Start = 4
Mid End= Day 7
Mid Max Sum = Left Max Border Sum + Right Max Border Sum = 11
Range: Day 4 through Day 7
Addition
Counter:
25
Find Max Subsequence for Day 1 – Day 8
Compare subsequence sums
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Left Max Border Sum = 1
Right Max Border Sum = 10
Mid Start = 4
Mid End= Day 7
Mid Max Sum = Left Max Border Sum + Right Max Border Sum = 11
Mid Max Sum > Max Side Sum? Yes
Addition
Counter:
25
Find Max Subsequence for Day 1 – Day 8
Max Side Subsequence = Day 5 – Day 7
Left Max Sum = 7
Right Max Sum = 10
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Left Max Border Sum = 1
Right Max Border Sum = 10
Mid Start = 4
Mid End= Day 7
Left Max Border Sum + Right Max Border Sum = 11
Max Sum is 11
Max Subsequence is Day 4 – Day 7
Addition
Counter:
25
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Day 1 Day 2
3
4
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Day 1 Day 2
3
4
Day 1
3
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Day 1 Day 2
3
4
Day 1
3
> 0 ? Yes
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Day 1 Day 2
3
4
Left Max Sum = 3
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Day 1 Day 2
3
Left Max Sum = 3
4
Day 2
4
> 0 ? Yes
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Day 1 Day 2
3
4
Left Max Sum = 3
Left Max Subsequence = Day 1 – Day 1
Right Max Sum = 4
Right Max Subsequence = Day 2 – Day 2
Max Side Subsequence = Day 2 – Day 2
Addition
Counter:
0
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Max Side Subsequence = Day 2 – Day 2
Right Max Sum = 4
Left Max Sum = 3
Day 1 Day 2
3
4
Left Max Border Sum = 0 + 3
midStart = 1
Addition
Counter:
1
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Max Side Subsequence = Day 2 – Day 2
Right Max Sum = 4
Left Max Sum = 3
Day 1 Day 2
3
4
Left Max Border Sum = 3
Right Max Border Sum = 0 + 4
midStart = 1
midStart = 2
Addition
Counter:
2
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Max Side Subsequence = Day 2 – Day 2
Right Max Sum = 4
Left Max Sum = 3
Day 1 Day 2
3
4
Left Max Border Sum = 3
Right Max Border Sum = 0 + 4
midStart = 1
midStart = 2
Left Max Border Sum + Right Max Border Sum = 7
Max Sum is 7
Maximum Subsequence is Day 1 – Day 2
Addition
Counter:
2
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Day 3 Day 4
-8
1
Addition
Counter:
2
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Day 3 Day 4
-8
1
Day 3
-8
Addition
Counter:
2
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Day 3 Day 4
-8
1
Day 4
1
> 0 ? Yes
Addition
Counter:
2
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Day 3 Day 4
-8
1
Left Max Sum = 0
Left Max Subsequence = Day 3 – Day 3
Right Max Sum = 1
Right Max Subsequence = Day 4 – Day 4
Max Side Subsequence = Day 4 – Day 4
Addition
Counter:
2
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Max Side Subsequence = Day 4 – Day 4
Left Max Sum = 0
Right Max Sum = 1
Day 3 Day 4
-8
Left Max Border Sum = 0
Mid Start = Day 3
1
Addition
Counter:
3
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Max Side Subsequence = Day 4 – Day 4
Left Max Sum = 0
Right Max Sum = 1
Day 3 Day 4
-8
Left Max Border Sum = 0
Mid Start = Day 3
1
Right Max Border Sum = 1
Mid End = Day 4
Addition
Counter:
4
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Max Side Subsequence = Day 4 – Day 4
Left Max Sum = 0
Right Max Sum = 1
Day 3 Day 4
-8
Left Max Border Sum = 0
Mid Start = Day 3
1
Right Max Border Sum = 1
Mid End = Day 4
Max Sum is 1
Maximum Subsequence = Day 3 – Day 4
Addition
Counter:
5
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Sum = 7
Left Max Subsequence = Day 1 – Day 2
Right Max Sum = 1
Right Max Subsequence = Day 3 – Day 4
Max Side Subsequence = Day 1 – Day 2
Addition
Counter:
5
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Max Side Subsequence = Day 1 – Day 2
Left Max Sum = 7
Right Max Sum = 1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Sum = 4
Left Max Border Sum = 0
Mid Start = Day 2
Left Max Border Sum is now 4
Addition
Counter:
6
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Max Side Subsequence = Day 1 – Day 2
Left Max Sum = 7
Right Max Sum = 1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Sum = 4 + 3 = 7
Left Max Border Sum = 4
Mid Start = Day 2
Left Max Border Sum is now 7 and Mid Start is now Day 1
Addition
Counter:
7
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Max Side Subsequence = Day 1 – Day 2
Left Max Sum = 7
Right Max Sum = 1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Border Sum = 7
Sum = -8
Mid Start = Day 1
Mid End = Day 3
Right Max Border Sum = 0
No change to Right Max Border Sum
Addition
Counter:
8
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Max Side Subsequence = Day 1 – Day 2
Left Max Sum = 7
Right Max Sum = 1
Day 1 Day 2 Day 3 Day 4
3
4
-8
1
Left Max Border Sum = 7
Sum = -8 + 1 = --7
Mid Start = Day 1
Right Max Border Sum = 0
Mid End = Day 3
No change to Right Max Border Sum or Mid End
Addition
Counter:
9
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Max Side Subsequence = Day 1 – Day 2
Left Max Sum = 7
Right Max Sum = 1
Day 1 Day 2 Day 3 Day 4
3
4
Left Max Border Sum = 7
Mid Start = Day 1
-8
1
Right Max Border Sum = 0
Mid End = Day 3
Addition
Counter:
9
Solving for Left Half
Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Day 8
3
4
-8
1
9
-2
3
-1
Max Side Subsequence = Day 1 – Day 2
Left Max Sum = 7
Right Max Sum = 1
Day 1 Day 2 Day 3 Day 4
3
4
Left Max Border Sum = 7
Mid Start = Day 1
-8
1
Right Max Border Sum = 0
Mid End = Day 3
Left Max Border Sum + Right Max Border Sum = 7
Max Sum is 7
Max Subsequence is Day 1 – Day 2
Addition
Counter:
9
Analysis of Butcher’s Approach
•
•
The amount of addition needed for the Butcher’s approach
compared to the Workaholic and Conservative approach is
extremely small:
•
The simplest maximum subsequence problem is one
involving one day. These small problems require no
addition.
•
The amount of addition needed from smaller sub
problems to slightly larger sub problems grows slowly
because we’re doing fewer re-calculations of the same
numbers than the last two approaches.
•
We’re simply combining results of the smaller problems.
For larger sized problems, the amount of addition that needs
to be done is around the ball park of n*log2(n)
Fourth Algorithm: The Smart A$$
Approach
•
We can do even better than the
Butcher’s method by making a key
observation about the maximum
sub-sequence problem – we will
NEVER want a negative stock sum!
•
If that’s the case, then we can just
ignore additions that become
negative when we solve the
problem.
•
I call this approach the Smart A$$
approach!
•
This algorithm is very similar to the
Conservative approach. However
unlike conservative approach, we
ignore subsequences with negative
accumulated sums.
Photo source: http://www.toonpool.com/cartoons/speed_57641
mstart mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cstart cend
Sum: 3
Sum < 0? No
Max Sum: 3
Maximum Subsequence: Day 1 – Day 1
Addition
Counter:
1
mstart
mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cstart
cend
Sum: 3 + 4 = 7
Sum < 0? No
Max Sum was: 3
Maximum Subsequence was: Day 1 – Day 1
Max Sum is now 7 and Maximum Subsequence is Day 1 – Day 2
Addition
Counter:
2
mstart
mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cend
cstart
Sum: 7 – 8 = -1
Sum < 0? Yes
Max Sum: 7
Maximum Subsequence: Day 1 – Day 2
Sum is now 0
Addition
Counter:
3
mstart
mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cend cstart
Sum: 0 + 1 = 1
Sum < 0? No
Max Sum: 7
Maximum Subsequence: Day 1 – Day 2
No changes
Addition
Counter:
4
mstart
mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cstart
cend
Sum: 1 + 9 = 10
Sum < 0? No
Max Sum: 7
Maximum Subsequence: Day 1 – Day 2
Max Sum is now 10 and Maximum Subsequence is Day 4 – Day 5
Addition
Counter:
5
mstart
mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cstart
cend
Sum: 10 – 2 = 8
Sum < 0? No
Max Sum: 10
Maximum Subsequence: Day 4 – Day 5
No changes
Addition
Counter:
6
mstart
mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cstart
cend
Sum: 8 + 3 = 11
Sum < 0? No
Max Sum: 10
Maximum Subsequence: Day 4 – Day 5
Max Sum is 11 and Max Subsequence is Day 4 – Day 7
Addition
Counter:
7
mstart
mend
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
3
4
-8
1
9
-2
3
-1
cstart
cend
Sum:11 – 1 = 10
Sum < 0? No
Max Sum: 11
Maximum Subsequence: Day 4 – Day 7
No changes
Addition
Counter:
8
Last But Not Least – The Analysis
•
The total number of addition operations needed for the
Smart A$$ approach is…. 8.
Amazing isn’t it!
•
Workaholic = 120, Conservative = 36, Butcher’s = 25
•
For 1000 days:
• Workaholic = 167,167,000
• Conservative = 500,500
• Butcher’s = 9,976
• SmartA$$ = 1,000
•
The Smart A$$ approach is the most efficient algorithm
•
The amount of addition required is around the size of the
data set n.
Thanks,
but!
Daryle J. Serrant
Debasis Mitra
Your feedback will be much appreciated.
1. Is this talk useful to you?
2. If yes, can you describe, how?
3. If no, can you provide any suggestion on what we
needed to do to explain algorithms more?
5. Any suggestion on where else may we present this?
6. Do you have any question for us?
Debasis Mitra, Computer Science, FIT – dmitra@cs.fit.edu
Daryle J. Serrant – serrantd2008@my.fit.edu
Download