Title: The Power of Computing Algorithms
1The Power of Computing Algorithms
- Daryle J. Serrant Past Undergraduate Student
- Debasis Mitra dmitra_at_cs.fit.edu
- Florida Institute of Technology
- Computer Science
2What is an Algorithm?
Cooking lt- Recipes Building construction lt- Blue
print Software lt- Algorithm
3Example 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
4Problem 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
5A 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!
6The 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/w
orkaholics/joke-1704.htm/
7Day 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
Addition Counter 1
Maximum Subsequence Day 1
8Day 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 0 3
Addition Counter 2
9Day 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 0 3 4 7
Max Sum 3
Addition Counter 3
Maximum Subsequence Day 1
Max Sum is now 7 and Maximum Subsequence is now
Day 1 - 2
10Day 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 0 3
Addition Counter 4
11Day 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 0 3 4
Addition Counter 5
12Day 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 0 3 4 - 8 -1
Max Sum 7
Addition Counter 6
Maximum Subsequence Day 1 - 2
No change
13Day 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 0 3
Addition Counter 7
14Day 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 0 3 4
Addition Counter 8
15Day 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 0 3 4 8
Addition Counter 9
16Day 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 0 3 4 8 1 0
Max Sum 7
Addition Counter 10
Maximum Subsequence Day 1 - 2
No changes
17Day 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 3
Addition Counter 11
18Day 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 3 4
Addition Counter 12
19Day 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 3 4 8
Addition Counter 13
20Day 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 3 4 8 1
Addition Counter 14
21Day 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 3 4 8 1 9 9
Max Sum 7
Addition Counter 15
Maximum Subsequence Day 1 - 2
Max Sum is now 9 and Maximum Subsequence is Day 1
Day 5
22Day 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 0 3
Addition Counter 16
23Day 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 0 3 4
Addition Counter 17
24Day 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 0 3 4 8
Addition Counter 18
25Day 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 0 3 4 8 1
Addition Counter 19
26Day 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 0 3 4 8 1 9
Addition Counter 20
27Day 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 0 3 4 8 1 9 2 7
Max Sum 9
Addition Counter 21
Maximum Subsequence Day 1 - 5
No change
28Day 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 0 3
Addition Counter 22
29Day 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 0 3 4
Addition Counter 23
30Day 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 0 3 4 8
Addition Counter 24
31Day 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 0 3 4 8 1
Addition Counter 25
32Day 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 0 3 4 8 1 9
Addition Counter 26
33Day 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 0 3 4 8 1 9 2
Addition Counter 27
34Day 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 0 3 4 8 1 9 2 3 10
Max Sum 9
Addition Counter 28
Maximum Subsequence Day 1 - 5
Max Sum is now 10 and Maximum Subsequence is Day
1 - 7
35Day 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 0 3
Addition Counter 29
36Day 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 0 3 4
Addition Counter 30
37Day 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 0 3 4 8
Addition Counter 31
38Day 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 0 3 4 8 1
Addition Counter 32
39Day 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 0 3 4 8 1 9
Addition Counter 33
40Day 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 0 3 4 8 1 9 2
Addition Counter 34
41Day 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 0 3 4 8 1 9 2 3
Addition Counter 35
42Day 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 0 3 4 8 1 9 2 3 1 9
Max Sum 10
Addition Counter 36
Maximum Subsequence Day 1 - 7
No changes
43Day 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 10
Addition Counter 37
Maximum Subsequence Day 1 - 7
No changes
44Day 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 0 4
Addition Counter 38
45Day 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 0 4 8 -4
Max Sum 10
Addition Counter 39
Maximum Subsequence Day 1 - 7
No changes
46Day 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 0 4
Addition Counter 40
47Day 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 0 4 8
Addition Counter 41
48Day 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 0 4 8 1 -3
Max Sum 10
Addition Counter 42
Maximum Subsequence Day 1 - 7
No changes
49Day 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 0 4
Addition Counter 43
50Day 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 0 4 8
Addition Counter 44
51Day 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 0 4 8 1
Addition Counter 45
52Day 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 0 4 8 1 9 6
Max Sum 10
Addition Counter 46
Maximum Subsequence Day 1 - 7
No changes
53Day 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 0 4
Addition Counter 47
54Day 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 0 4 8
Addition Counter 48
55Day 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 0 4 8 1
Addition Counter 49
56Day 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 0 4 8 1 9
Addition Counter 50
57Day 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 0 4 8 1 9 2 4
Max Sum 10
Addition Counter 51
Maximum Subsequence Day 1 - 7
No changes
58Day 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 0 4
Addition Counter 52
59Day 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 0 4 8
Addition Counter 53
60Day 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 0 4 8 1
Addition Counter 54
61Day 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 0 4 8 1 9
Addition Counter 55
62Day 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 0 4 7 1 9 2
Addition Counter 56
63Day 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 0 4 8 1 9 2 3 7
Max Sum 10
Addition Counter 57
Maximum Subsequence Day 1 - 7
No changes
64Day 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 0 4
Addition Counter 58
65Day 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 0 4 8
Addition Counter 59
66Day 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 0 4 8 1
Addition Counter 60
67Day 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 0 4 8 1 9
Addition Counter 61
68Day 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 0 4 8 1 9 2
Addition Counter 62
69Day 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 0 4 8 1 9 2 3
Addition Counter 63
70Day 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 0 4 8 1 9 2 3 1 6
Max Sum 10
Addition Counter 64
Maximum Subsequence Day 1 - 7
No changes
71Day 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
Addition Counter 65
Maximum Subsequence Day 1 - 7
No changes
72Day 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 0 8 -8
Addition Counter 66
73Day 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 0 8 1 -7
Max Sum 10
Addition Counter 67
Maximum Subsequence Day 1 - 7
No changes
74Day 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 0 8 -8
Addition Counter 68
75Day 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 0 8 1
Addition Counter 69
76Day 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 0 8 1 9 2
Max Sum 10
Addition Counter 70
Maximum Subsequence Day 1 - 7
No changes
77Day 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 0 8
Addition Counter 71
78Day 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 0 8 1
Addition Counter 72
79Day 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 0 8 1 9
Addition Counter 73
80Day 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 0 8 1 9 2 0
Max Sum 10
Addition Counter 74
Maximum Subsequence Day 1 - 7
No changes
81Day 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 8
Addition Counter 75
82Day 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 8 1
Addition Counter 76
83Day 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 8 1 9
Addition Counter 77
84Day 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 8 1 9 2
Addition Counter 78
85Day 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 8 1 9 2 3 3
Max Sum 10
Addition Counter 79
Maximum Subsequence Day 1 - 7
No changes
86Day 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 0 8
Addition Counter 80
87Day 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 0 8 1
Addition Counter 81
88Day 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 0 8 1 9
Addition Counter 82
89Day 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 0 8 1 9 2
Addition Counter 83
90Day 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 0 8 1 9 2 3
Addition Counter 84
91Day 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 0 8 1 9 2 3 1 2
Max Sum 10
Addition Counter 85
Maximum Subsequence Day 1 - 7
No changes
92Day 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
Addition Counter 86
Maximum Subsequence Day 1 - 7
No changes
93Day 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 0 1
Addition Counter 87
94Day 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 0 1 9 10
Max Sum 10
Addition Counter 88
Maximum Subsequence Day 1 - 7
No changes
95Day 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 0 1
Addition Counter 89
96Day 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 0 1 9
Addition Counter 90
97Day 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 0 1 9 2 8
Max Sum 10
Addition Counter 91
Maximum Subsequence Day 1 - 7
No changes
98Day 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 0 1
Addition Counter 92
99Day 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 0 1 9
Addition Counter 93
100Day 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 0 1 9 2
Addition Counter 94
101Day 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 0 1 9 2 3 11
Max Sum 10
Addition Counter 95
Maximum Subsequence Day 1 - 7
Max Sum is 11 and Maximum Subsequence is now Day
4 Day 7
102Day 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 0 1
Addition Counter 96
103Day 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 0 1 9
Addition Counter 97
104Day 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 0 1 9 2
Addition Counter 98
105Day 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 0 1 9 2 3
Addition Counter 99
106Day 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 0 1 9 2 3 1 10
Max Sum 11
Addition Counter 100
Maximum Subsequence Day 4 - 7
No changes
107Day 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
Addition Counter 101
Maximum Subsequence Day 4 - 7
No changes
108Day 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 0 9
Addition Counter 102
109Day 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 0 9 2 7
Max Sum 11
Addition Counter 103
Maximum Subsequence Day 4 - 7
No changes
110Day 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 0 9
Addition Counter 104
111Day 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 0 9 2
Addition Counter 105
112Day 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 0 9 2 3 10
Max Sum 11
Addition Counter 106
Maximum Subsequence Day 4 - 7
No changes
113Day 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 0 9
Addition Counter 107
114Day 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 0 9 2
Addition Counter 108
115Day 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 0 9 2 3
Addition Counter 109
116Day 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 0 9 2 3 1 9
Max Sum 11
Addition Counter 110
Maximum Subsequence Day 4 - 7
No changes
117Day 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
Addition Counter 111
Maximum Subsequence Day 1 - 7
No changes
118Day 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 0 2
Addition Counter 112
119Day 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 0 2 3 1
Max Sum 11
Addition Counter 113
Maximum Subsequence Day 4 - 7
No changes
120Day 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 0 2
Addition Counter 114
121Day 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 0 2 3
Addition Counter 115
122Day 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 0 2 3 1 0
Max Sum 11
Addition Counter 116
Maximum Subsequence Day 4 - 7
No changes
123Day 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
Addition Counter 117
Maximum Subsequence Day 4 - 7
No changes
124Day 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 0 3
Addition Counter 118
125Day 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 0 3 1 2
Max Sum 11
Addition Counter 119
Maximum Subsequence Day 4 - 7
No changes
126Day 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
Addition Counter 120
Maximum Subsequence Day 4 - 7
No changes
127Analysis
- 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, dont 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
128Improve!
- Do we really need to run the black/middle pointer
over and over again on same numbers? - As the red/right pointer moves cannt we just add
that number to the previous sum?
129The Conservative Approach
- The Workaholic approach requires a lot of
addition operations - So much, that its 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
12472906CFTOKEN15878700
130Day 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
131Day 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
Addition Counter 2
Max Sum is now 7 and Maximum Subsequence is Day 1
Day 2
132Day 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
Addition Counter 3
No changes
133Day 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
Addition Counter 4
No changes
134Day 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
Addition Counter 5
Max Sum is now 9 and Maximum Subsequence is Day 1
Day 5
135Day 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
Addition Counter 6
No changes
136Day 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
Addition Counter 7
Max Sum is now 10 and Maximum Subsequence is Day
1 Day 7
137Day 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
Addition Counter 8
No changes
138Day 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
Addition Counter 9
No changes
139Day 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
Addition Counter 10
No changes
140Day 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
Addition Counter 11
No changes
141Day 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
Addition Counter 12
No changes
142Day 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
Addition Counter 13
No changes
143Day 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
Addition Counter 14
No changes
144Day 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
Addition Counter 15
No changes
145Day 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
Addition Counter 16
No changes
146Day 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
Addition Counter 17
No changes
147Day 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
Addition Counter 18
No changes
148Day 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
Addition Counter 19
No changes
149Day 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
Addition Counter 20
No changes
150Day 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
Addition Counter 21
No changes
151Day 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
Addition Counter 22
No changes
152Day 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
Addition Counter 23
No changes
153Day 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
Addition Counter 24
No changes
154Day 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
Addition Counter 25
Max Sum is 11 and Maximum Subsequence is now Day
4 Day 7
155Day 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
Addition Counter 26
No changes
156Day 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
Addition Counter 27
No changes
157Day 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
Addition Counter 28
No changes
158Day 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
Addition Counter 29
No changes
159Day 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
Addition Counter 30
No changes
160Day 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
Addition Counter 31
No changes
161Day 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
Addition Counter 32
No changes
162Day 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
Addition Counter 33
No changes
163Day 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
Addition Counter 34
No changes
164Day 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
Addition Counter 35
No changes
165Day 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
Addition Counter 36
No changes
166Some 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
167The Butchers 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 doesnt require too
much addition, solve those problems, and combine
the results to get the final result. - Let us call this approach the Butchers approach.
Photo source http//www.reluctantgourmet.com/stea
k_information.htm
168Combine results from smaller problems
Day 1
Day 2
Day 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
169Solve Max Subsequence for Day 1 Day 4
Day 1
Day 2
Day 4
Day 5
Day 6
Day 7
Day 8
Day 3
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
170Solve Max Subsequence for Day 5 Day 8
Day 1
Day 2
Day 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Right Max Sum (Day 5 Day 8) 10
Left Max Sum (Day 1 Day 4) 7
Left Max Subsequence Day Day 2
Right Max Subsequence Day 5 Day 7
Addition Counter 18
171Which side has the Largest Sum?
Day 1
Day 2
Day 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Right Max Sum (Day 5 Day 8) 10
Left Max Sum (Day 1 Day 4) 7
Left Max Subsequence Day Day 2
Right Max Subsequence Day 5 Day 7
Left Max Sum gt Right Max Sum? No
Addition Counter 18
172Which side has the Largest Sum?
Day 1
Day 2
Day 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Right Max Sum (Day 5 Day 8) 10
Left Max Sum (Day 1 Day 4) 7
Left Max Subsequence Day Day 2
Right Max Subsequence Day 5 Day 7
Left Max Sum gt Right Max Sum? No
Max Side Subsequence Day 5 Day 7
Addition Counter 18
173Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
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
174Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
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
175Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
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
176Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
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
177Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Left Max Border Sum 1
Sum 9
Mid Start 4
Right Max Border Sum 0
Mid End Day 5
Right Max Border Sum is now 9
Addition Counter 22
178Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Left Max Border Sum 1
Sum 9 2 7
Mid Start 4
Right Max Border Sum 9
Mid End Day 5
No changes made to Right Max Border Sum
Addition Counter 23
179Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Left Max Border Sum 1
Sum 7 3 10
Mid Start 4
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
180Find 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Left Max Border Sum 1
Sum 10 1 9
Mid Start 4
Right Max Border Sum 10
Mid End Day 7
No changes made to Right Max Border Sum
Addition Counter 25
181Finding 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 4
Day 5
Day 6
Day 7
Day 8
Day 3
3
4
-8
1
9
-2
3
-1
Right Max Border Sum 10
Left Max Border Sum 1
Mid Start 4
Mid End Day 7
Mid Max Sum Left Max Border