1 00:00:00,950 --> 00:00:03,710 The technique of derived variables 2 00:00:03,710 --> 00:00:06,650 comes up in analyzing state machines. 3 00:00:06,650 --> 00:00:09,830 So let's just take a quick look at it together. 4 00:00:09,830 --> 00:00:13,620 So a derived variable is simply a function 5 00:00:13,620 --> 00:00:16,870 on the states of a state machine that assigns some value 6 00:00:16,870 --> 00:00:19,000 to the states. 7 00:00:19,000 --> 00:00:23,310 So it's just that kind of a function mapping. 8 00:00:23,310 --> 00:00:26,290 If the values happen to be, say, the non-negative integers, 9 00:00:26,290 --> 00:00:27,920 it's called non-negative integer value, 10 00:00:27,920 --> 00:00:30,350 but it could be real value, complex value, 11 00:00:30,350 --> 00:00:35,130 and even take on other kinds of odd kinds of values, 12 00:00:35,130 --> 00:00:36,470 not necessarily numerical. 13 00:00:36,470 --> 00:00:41,270 No pun there-- not odd numbers, but unusual values. 14 00:00:41,270 --> 00:00:44,060 So let's look at the example of the robot on the grid. 15 00:00:44,060 --> 00:00:46,870 The states were pairs of non-negative integers giving 16 00:00:46,870 --> 00:00:49,740 the coordinates of where the robot was. 17 00:00:49,740 --> 00:00:52,400 And one of the derived variables that we 18 00:00:52,400 --> 00:00:54,710 found was real useful was the sum 19 00:00:54,710 --> 00:01:01,210 value, sigma, of a state, which is defined to be x plus y. 20 00:01:01,210 --> 00:01:03,980 And this would be a non-negative integer 21 00:01:03,980 --> 00:01:06,030 valued derived variable. 22 00:01:06,030 --> 00:01:09,820 So the word "derived" comes because we're making it up. 23 00:01:09,820 --> 00:01:11,770 It's not part of the specification 24 00:01:11,770 --> 00:01:15,030 of the state machine or part of the program that 25 00:01:15,030 --> 00:01:18,090 defines the state machine. 26 00:01:18,090 --> 00:01:20,960 So in the robot example, the actual states 27 00:01:20,960 --> 00:01:23,480 were composed of the two coordinates x and y, 28 00:01:23,480 --> 00:01:25,800 but the derived variable that we made up 29 00:01:25,800 --> 00:01:28,040 was their sum of signal. 30 00:01:28,040 --> 00:01:32,380 Another useful derived variable for that robot example 31 00:01:32,380 --> 00:01:38,270 was the parity of sigma, whether or not 32 00:01:38,270 --> 00:01:41,280 the number was even or odd. 33 00:01:41,280 --> 00:01:43,940 So sigma is a 0, 1 valued variable, 34 00:01:43,940 --> 00:01:48,730 which takes the value 0 if the sum is even 35 00:01:48,730 --> 00:01:52,550 and 1 if the sum is odd. 36 00:01:52,550 --> 00:01:55,270 So in the case of fast exponentiation, 37 00:01:55,270 --> 00:02:00,017 we looked at the actual variable z, 38 00:02:00,017 --> 00:02:02,100 which was part of the invariant and a crucial part 39 00:02:02,100 --> 00:02:02,725 of the program. 40 00:02:02,725 --> 00:02:06,920 And what we noticed about z was that z 41 00:02:06,920 --> 00:02:12,455 was a strictly decreasing and natural number valued variable. 42 00:02:12,455 --> 00:02:15,230 As a matter of fact, we noticed that it halved at each step. 43 00:02:15,230 --> 00:02:17,620 But its values were non-negative integers, 44 00:02:17,620 --> 00:02:22,570 and it's strictly decreasing at every step. 45 00:02:22,570 --> 00:02:26,050 So that implies by the Well Ordering Principle 46 00:02:26,050 --> 00:02:29,940 that it will take a minimum value. 47 00:02:29,940 --> 00:02:33,440 And what we know about the minimum value of a strictly 48 00:02:33,440 --> 00:02:37,000 decreasing variable is that the algorithm is stuck, 49 00:02:37,000 --> 00:02:41,330 because once z has reached its minimum value, 50 00:02:41,330 --> 00:02:45,240 if the machine took another step, 51 00:02:45,240 --> 00:02:46,760 then it would get smaller. 52 00:02:46,760 --> 00:02:51,360 So it means that the algorithm has to terminate. 53 00:02:51,360 --> 00:02:53,500 So this gives you a general methodology 54 00:02:53,500 --> 00:02:59,370 for proving termination-- find a non-negative integer valued 55 00:02:59,370 --> 00:03:03,430 strictly decreasing variable guarantees the program stops. 56 00:03:03,430 --> 00:03:05,920 As a matter of fact, we can say sometimes 57 00:03:05,920 --> 00:03:08,320 how long it will take for the program to stop. 58 00:03:08,320 --> 00:03:10,970 As we saw with fast exponentiation, 59 00:03:10,970 --> 00:03:13,760 it took not z, which was the obvious bound, 60 00:03:13,760 --> 00:03:16,910 but in fact, log of z, because z not only went down 61 00:03:16,910 --> 00:03:20,380 at every step, it got halved at every step. 62 00:03:20,380 --> 00:03:23,070 So in general, the concept of a strictly decreasing variable 63 00:03:23,070 --> 00:03:26,720 is one-- as shown here-- that at every step of the state 64 00:03:26,720 --> 00:03:31,720 machine, at each transition, it gets strictly smaller. 65 00:03:31,720 --> 00:03:35,660 A related idea is a weakly decreasing variable. 66 00:03:35,660 --> 00:03:39,530 These are not necessarily useful for proving termination, 67 00:03:39,530 --> 00:03:42,550 but they are often useful, as you'll 68 00:03:42,550 --> 00:03:46,540 see as we progress through the term-- examples 69 00:03:46,540 --> 00:03:49,660 where it helps you analyze the behavior of the algorithm. 70 00:03:49,660 --> 00:03:53,270 So a weakly decreasing variable is one which goes down 71 00:03:53,270 --> 00:03:55,290 or stays constant. 72 00:03:55,290 --> 00:03:59,160 It never gets larger. 73 00:03:59,160 --> 00:04:01,680 So if we looked at the example of sigma, 74 00:04:01,680 --> 00:04:04,520 the sum of the coordinates, that's 75 00:04:04,520 --> 00:04:06,060 up and down all over the place. 76 00:04:06,060 --> 00:04:08,570 It's neither increasing nor decreasing. 77 00:04:08,570 --> 00:04:12,020 The other extreme is the parity variable 78 00:04:12,020 --> 00:04:15,530 pi, which was the 0 or 1 according to whether 79 00:04:15,530 --> 00:04:18,990 or not the sum of the coordinates was even or odd. 80 00:04:18,990 --> 00:04:21,519 And pi is a constant, and that means 81 00:04:21,519 --> 00:04:24,330 that it's both weakly increasing and weakly 82 00:04:24,330 --> 00:04:26,280 decreasing in the degenerate sense 83 00:04:26,280 --> 00:04:29,660 that weakly increasing is allowed to stay the same. 84 00:04:29,660 --> 00:04:31,960 In fact, something is weakly increasing and weakly 85 00:04:31,960 --> 00:04:35,100 decreasing if and only if it's a constant. 86 00:04:35,100 --> 00:04:39,360 By the way, we used to call weakly decreasing variables 87 00:04:39,360 --> 00:04:41,800 "non-increasing," which is the standard terminology 88 00:04:41,800 --> 00:04:42,350 in the field. 89 00:04:42,350 --> 00:04:45,270 In calculus, you talk about non-increasing functions. 90 00:04:45,270 --> 00:04:48,240 And we just found that it caused a lot of confusion, 91 00:04:48,240 --> 00:04:54,620 because you have to remember that non-increasing is not 92 00:04:54,620 --> 00:04:57,080 the same as not increasing. 93 00:04:57,080 --> 00:04:59,430 So there's an example of a function that 94 00:04:59,430 --> 00:05:04,670 is not increasing, but it's certainly not non-increasing. 95 00:05:04,670 --> 00:05:07,880 And if that didn't register, I'll let you think about it. 96 00:05:07,880 --> 00:05:10,110 By the way, this method of proving termination 97 00:05:10,110 --> 00:05:14,850 by finding a strictly decreasing natural number 98 00:05:14,850 --> 00:05:17,420 valued variable generalizes straightforwardly 99 00:05:17,420 --> 00:05:21,680 to a variable which takes on values from a well-ordered set 100 00:05:21,680 --> 00:05:23,230 of real numbers. 101 00:05:23,230 --> 00:05:25,970 Remember, a well-ordered set of real numbers, 102 00:05:25,970 --> 00:05:28,390 one of the definitions of it is that it's 103 00:05:28,390 --> 00:05:31,390 a set of numbers where it's impossible to find 104 00:05:31,390 --> 00:05:33,940 an infinite decreasing sequence of values-- 105 00:05:33,940 --> 00:05:39,170 w0 less than w1 less than w2 less than w1 going on forever. 106 00:05:39,170 --> 00:05:44,530 If that can't happen, then the set is called well ordered. 107 00:05:44,530 --> 00:05:46,030 Of course, the non-negative integers 108 00:05:46,030 --> 00:05:48,410 are the most obvious basic case, but there 109 00:05:48,410 --> 00:05:52,480 are a bunch of others described in the notes. 110 00:05:52,480 --> 00:05:55,410 And in general, the termination principle 111 00:05:55,410 --> 00:06:00,400 is that if you can find a strictly decreasing 112 00:06:00,400 --> 00:06:05,140 variable of derived variable whose values always come 113 00:06:05,140 --> 00:06:07,670 from a well-ordered set, that also 114 00:06:07,670 --> 00:06:09,830 is a way to prove termination. 115 00:06:09,830 --> 00:06:12,700 That's going to guarantee termination for the same reason 116 00:06:12,700 --> 00:06:15,900 that the variable will have to take a minimum value. 117 00:06:15,900 --> 00:06:18,460 That's the other definition of well ordered. 118 00:06:18,460 --> 00:06:22,140 And when it does, the machine can't move anymore.