More on Inductive Reasoning

5 downloads 0 Views 335KB Size Report
Inductive Reasoning. Peter B. Henderson elcome to the Math CountS “2003 December” column. In this column, I would like to hear from the readers about their ...
Featured Columns Association of America Professional Enhancement Program workshop organized by Bill Marion was held June 2-6, 2003 at Valparaiso University primarily for mathematics faculty teaching introductory discrete mathematics courses for computer science and software engineering majors. Fifteen participants heard presentations by Susanna Epp, Peter Henderson and Henry Walker, and developed new material, curricula, etc. for introductory discrete math courses for our majors. 2. Computing Curriculum Software Engineering – the first public draft of the CCSE volume was released on July 17, 2003 ( see http://sites.computer.org/ccse/ ) with reviews by the public requested by September 19, 2003. The core mathematical foundations consists of 58 hours (see page 24 of the volume) with two recommended courses CS 105 (Discrete Structures I) and CS 106 ((Discrete Structures II) to be taken as early as possible in the curriculum (Quote from Page 53 “It is highly recommended that the discrete mathematics courses be taught starting in first year in lieu of any other mathematic course requirements since it is more important that a strong discrete mathematic foundation is made than, for example, calculus.”). The ‘missing piece’ is integrating this mathematics into the foundational computer science and software engineering courses. However, this will come as computer science software engineering education mature. 3. SIGCSE Committee on the Implementation of a Discrete Mathematics Course – this is the first such SIGCSE committee and was approved by the SIGCSE board in July 2003. I am thrilled that the topic of mathematics in our computer science curricula was selected to spearhead this effort. Doug Baldwin and Bill Marion are the co-facilitators for this committee. The charge of the committee can be found at http://www.sigcse.org/topics/committees.shtml . I encourage you to join the committees discussion listserver by sending the message ‘SUBSCRIBE SIGCSE-MATH-COMM your name’ to LISTSERV @ACM.ORG . 4. Mathematics in CS Education - The September 2003 issue of the Communications of the ACM, with guest editor Keith Devlin, will be devoted to the topic of the role of mathematics in computer science education. There will even be an article by yours truly entitled “Mathematical Reasoning in Software Engineering Education.” I anticipate that this issue will further increase the awareness of the relevance of mathematics in the education of computer science and software engineering students, and will stimulate valuable discussions.

have provided extensive support and leadership for studentrelated activities in the computing sciences”. This year’s recipient was announced at the UPE International Convention, held in Hollywood, California in conjunction with the ACM International Collegiate Programming Contest (http://icpc.baylor.edu/icpc), sponsored by IBM. Across town during the same March weekend, the Academy of Motion Picture Arts and Sciences tried to upstage us by having their own event (the Oscars – you may have seen this in the press) and yet no star shone more brightly in our universe than Marsha Poucher, our awardee. Marsha has been Manager of the ACM International Collegiate Programming Contest since 1997, maintaining a steady flow of communication between program staff, regional contest directors, programming team coaches, and contestants. As I have seen time and again, no detail is too small for her to know and attend to, no task so large as to be overwhelming. Throughout it all, she maintains a calm and poise that belie the madness swirling around her. Believe me, trying to coordinate sixty-eight teams from six continents contributes to a good deal of swirling madness. In presenting the award, UPE’s Executive Council member James Comer said, “Students from all corners of the world owe their very presence at this competition because of her hard work and dedication.” Hear, hear, James!! In addition to the award, Marsha received a check for $500 and – what else? – a wooden abacus. Conclusion In closing, it is always a pleasure to reward great scholarship and service by outstanding individuals. You can find more information about UPE scholarships and awards – including application forms – at the UPE website of . I hope to be reading about your own great works in the next round!

Math CountS

More on Inductive Reasoning Peter B. Henderson

W

elcome to the Math CountS “2003 December” column. In this column, I would like to hear from the readers about their view of why they think math counts in CS education. Email me and I will publish a summary in the next column. Here is a summary of some of the recent events and activities relevant to mathematics in CS and SE education. 1. Discrete Mathematics: An Early Foundation for the Study of Computer Science, a Mathematical inroads – The SIGCSE Bulletin

Again, as you can see there is significant activity relating to the topic of mathematics in the education of our 23

Volume 35, Number 4, 2003 December

Featured Columns students. However, there is still much to be done – specifically, integrating the mathematics into computer science and software engineering courses so students understand its importance and relevance, introducing discrete mathematics and logic early in the curricula and improving the quality of the instruction of the basic mathematics courses for computer science and software engineering students. Continuing the topic of inductive reasoning, recall from the previous column that ‘inductive reasoning’ is “reasoning from detailed facts to general principles” Inductive reasoning plays a very important role in understanding, specifying, designing, implementing and validating software. Below is the illustrative problem considered in the previous column.

It is interesting to observe that many novice students identify only the first universal quantification, not realizing the second existential one is required. A simple example such as the sequence 1,2 with maxValue = 3 suffices to jog their thinking. A potential iteration invariant for an algorithmic solution can be discovered from the previous columns generalized inductive reasoning and the above generalized patterns. Basically, in English, the maximum value of the first k values in the sequence has been correctly computed. More formally, the proposed iteration invariant is a logical predicate

Problem Consider for n > 0, a sequence of integers i1, i2, i3, …, in for which we wish to find the maximum value.

Below is a skeleton of an algorithmic solution illustrating key logical assertions based upon this iteration invariant.

I(maxValue, { i1, i2, i3, …, ik } ) ≡ maxValue = maxOfSeq( i1, i2, i3, …, ik )

{ precondition }

Here are generalized patterns discovered using the inductive reasoning from the previous column - assume one has computed the maximum of the first k integers in the sequence (i1, i2, i3, …, ik) and wishes to compute the maximum of the first k+1 integers in the sequence (i1, i2, i3, …, ik, ik+1). Let maxOfSeq : integer sequence Æ int;

k Å 1 maxValue Å i1 { maxValue = maxOfSeq( i1, i2, i3, …, ik ) } while do { maxValue = maxOfSeq( i1, i2, i3, …, ik ) } k Å k + 1 maxValue Å max ( maxValue, ik )

(1)

now, for each k ≥ 1

{ maxValue = maxOfSeq( i1, i2, i3, …, ik ) } { maxValue = maxOfSeq( i1, i2, i3, …, ik ) and not => post condition }

maxOfSeq( i1, i2, i3, …, ik, ik+1 ) = max ( maxOfSeq( i1, i2, i3, …, ik ), ik+1 ) maxValue = max ( maxOfSeq( i1, i2, i3, …, ik ), maxValue = max ( maxValue′,

Upon termination, the invariant and negation of must imply the truth of the post condition. Note that the invariant must be true at the four locations shown. Students can see the truth of the invariant after the initialization. Using forward logical reasoning, if the invariant is true before the body of the iteration, students can see (with your guidance) that it is true after the body. Backward reasoning can also be used. If has no side effects (2), then it is easy to argue, using forward and backward logical reasoning, that if the invariant is true at one location then it must be true at another ‘adjacent’ location (e.g., if the invariant is true before the while do and the value of all variables remain the same, then the invariant must be true immediately after the while do ) (3). Also, it is easy to see that the must be designed to ensure that k = n upon termination. This example was designed to illustrate the power of inductive thinking when discovering and/or reasoning about algorithms, and the important connections with mathematical logic. I hope you find it useful.

ik+1 )

ik+1 )

where function max : int X int Æ int returns the maximum of its two argument values, and maxValue′ represents the previous value of the variable maxValue in an iterative algorithm. Once we have gained a good understanding of the problem, we can identify appropriate pre and post conditions. Some may argue that we should have done this first; but without ‘playing’ with the problem, it is hard to ensure we understand it completely as there are often unclear or conflicting aspects and/or hidden nuances, Precondition: { a sequence of n > 0 integers i1, i2, i3, …, in } Postcondition: ∀ j ∈ {1,2,3,…,n}, maxValue ≥ ij and ∃ j ∈ {1,2,3,…,n} such that maxValue = ij

inroads – The SIGCSE Bulletin

24

Volume 35, Number 4, 2003 December

Featured Columns Previous Challenge Solution

Endnotes (1) ‘maxOfSeq’ is a function representing a solution to the problem we are trying to solve. (2) Creates no changes to the value of any variables. (3) In general, this is a mathematical induction based argument.

Parity of Permutation Disorders We define the number of disorders of a permutation of the integers 1..N as the number of unordered pairs in the permutation. A pair is considered unordered if j>i, but j is to the left of i in the permutation. For example, the number of disorders in the permutation 4 1 3 2 is 4, due to the unordered pairs: , , , . Develop an O(N) algorithm for which the input is a given permutation of the integers 1..N, and the output is the parity of the number of disorders in the permutation.

Colorful Challenges

Board Reconstruction

Solution Knuth calls an unordered pair of elements (not necessarily adjacent) an inversion, and regards a permutation odd or even according to the parity of the number of inversions [1]. We will use this terminology. Simple counting of the number of inversions (“disorders”) in a given permutation may be performed in O(N2) time, by examining the relative ordering of each pair of elements. We are asked to do better. A better solution should effectively capitalize on primary characteristics of the given task. The task involves the notion of ordering. This may “call for” some sortingrelated scheme. In addition, the task involves a permutation of the particular values 1..N. This may yield a simplified sorting-based scheme. The basic operation repeatedly invoked in many sorting schemes is that of “exchanging”. Some schemes involve exchanges of adjacent elements (Bubble-Sort, Insertion-Sort), and some involve exchanges of elements that are not necessarily adjacent (Selection-Sort, QuickSort). It may be beneficial to perform a short analysis of a single exchange, with respect to its effect on the number of inversions. An exchange of two adjacent elements changes the number of inversions by -1 or +1. An exchange of two non-adjacent elements requires further insight. Let a permutation be ..X..Y..Z.. and the two exchanged elements – X and Z. The change in the number of inversions due to reordering the pairs and is: +2, 0, or -2. This is true for any Y between X and Z. The change due to reordering the pair is +1 or -1. These observations imply an illuminating invariant.

David Ginat

I

n December 2000 we posed a challenge that involved the reconstruction of a 3-D box placement from two given 2-D profiles. The new challenge in this issue involves a simpler reconstruction, of a 2-D Black/White board. Haim Averbuch, from the Open University in Israel, offered the challenge. In our experience with students, the challenge may seem rather simple, at first glance, but hasty ideas may be misleading. An extension of this challenge, which we will display with its solution, is relevant in the domain of graph algorithms. The June 2003 challenge and its solution is displayed next. The solution involves unfolded characteristics of basic sub-components of sorting. Board Reconstruction The description of an N×N board, of Black/White cells, has to be transmitted. Unfortunately, it is impossible to transmit quadratic-size information. Only linear-size information may be transmitted. Thus, the number of Black cells in each row and each column are transmitted, rather than the (Black/White) description of each board cell. The receiver of this transmission has to reconstruct a corresponding N×N board description (there may be more than one board that will suit the transmitted information). Your task is to develop the receiver’s algorithm of reconstructing a suitable board. Develop an efficient algorithm for which the input is the board size N, followed by two lines of N elements each – the first line describes the number of Black squares in each of the N rows and the second line describes the number of Black squares in each of the N columns. The output is a suitable Black/White matrix description.

inroads – The SIGCSE Bulletin

Exchange Invariant: Each exchange of two (not necessarily adjacent) elements modifies the parity (0 to 1, and 1 to 0) of the number of inversions in the permutation. The above invariant expresses an essential property on which we may elegantly capitalize in our task. Parity Principle: Execute a Sorting algorithm that is based on exchanges, and count the number of exchanges. If this number is odd, then the given permutation is odd; otherwise, it is even. 25

Volume 35, Number 4, 2003 December