From d2e9a960ce94dd2e4384f361828f08e73b50ad6b Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 19:26:50 +1200 Subject: [PATCH 1/8] Update README.md --- ch02/README.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ch02/README.md b/ch02/README.md index a7d7396..507ba47 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -315,11 +315,20 @@ I.M.T -> LI holds. * Time complexity : theta(n) * pseudocode ```python -Naive-Polynomial-Evaluation(arr_of_terms, x) - y = 0; - for term in arr_of_terms +Naive-Polynomial-Evaluation(arr, x) + y = 0 + for i = 0 to arr.length - 1 X = 1 - for i = 1 to term.k + for k = i downto 1 X *= x - y += term.ak * X + y += X * arr[i] + return y +``` + + * LI and proof +```cpp +(To avoid Greek letter Sigma, here is using sum(head, tail)() function instead) +LI: + At the start of each iteration of the for loop of lines 2–3, + y = sum(k = 0, k = n - (i + 1)) () ``` From 6a4d35dc698f87b91603477cfe86a4d0c748afad Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 20:58:14 +1200 Subject: [PATCH 2/8] add p2.3.c --- ch02/README.md | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/ch02/README.md b/ch02/README.md index 507ba47..36b71fc 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -328,7 +328,40 @@ Naive-Polynomial-Evaluation(arr, x) * LI and proof ```cpp (To avoid Greek letter Sigma, here is using sum(head, tail)() function instead) -LI: +L.I.: At the start of each iteration of the for loop of lines 2–3, - y = sum(k = 0, k = n - (i + 1)) () + y = sum(k = 0, k = n - (i + 1)) (a(k + i + 1) * x ^ k) + +I.: + Prior to the first iteration, + y = sum(k = 0, k = -1) (a(k + i + 1) * x ^ k) + upper limit is lower than lower limit, this equation is meaningless, y = 0 + Hence, I holds. + +M.: + Suppose L.I. holds before i = i + y = sum(k = 0, k = n - (i + 1)) (a(k + i + 1) * x ^ k) + + by line 2 - 3, LHS becomes: + y = a(i) + x * y + = a(i) + x * ( a(i + 1) + a(i + 2) * x + ... + a(n) * x^(n - i - 1) + = a(i) + a(i + 1) * x + a(i + 2) * x^2 + ... + a(n) * x^(n - i) + + when i = i - 1 RHS becomes + sum(k = 0, k = n - i) (a(k + i) * x ^ k) + = a(i) + a(i + 1) * x + a(i + 2) * x^2 + ... + a(n) * x^(n - i) + + LHS = RHS, hence M holds for next iteration. + +T.: + The termination condition is i = -1 + y = sum(k = 0, k = n - (-1 + 1)) (a(k - 1 + 1) * x ^ k) + = sum(k = 0, k = n) (a(k) * x ^ k) + Hence, T holds. + +L.I. holds. + + + + ``` From 15261f5ef985368e69f22df2712a6ef9b7940cc6 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 21:05:22 +1200 Subject: [PATCH 3/8] Update README.md --- ch02/README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ch02/README.md b/ch02/README.md index 36b71fc..9081d04 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -360,8 +360,6 @@ T.: Hence, T holds. L.I. holds. - - - - ``` + + * As shown above, this code fragment correctly evaluates a polynomial. From a3727a773a99631d31252c03db018a44a405e4bb Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 21:11:57 +1200 Subject: [PATCH 4/8] Update README.md --- ch02/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ch02/README.md b/ch02/README.md index 9081d04..94944fb 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -363,3 +363,9 @@ L.I. holds. ``` * As shown above, this code fragment correctly evaluates a polynomial. + +##Problem 2-3 Inversions + * Five inversions: +```cpp +{2,1}, {3,1}, {8,1}, {6,1}, {8,6} +``` From 1f9e1499c5ecc504494569946f2ad969755d6ced Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 21:33:09 +1200 Subject: [PATCH 5/8] Update README.md --- ch02/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ch02/README.md b/ch02/README.md index 94944fb..e47052a 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -369,3 +369,13 @@ L.I. holds. ```cpp {2,1}, {3,1}, {8,1}, {6,1}, {8,6} ``` + * set {n, n-1, n-2, ...,2, 1}, i.e. numbers in descending order has most inversions. + * As shown below, the expression `A[i] > key` is in essence checking for an inversion. So a function can be made to describe the relationship between the running time and number of inversions: +```cpp + T(n) = theta(n * number_of_inversions + n) +``` + +```cpp +//from Insertion-Sort +5 while i > 0 and A[i] > key +``` From 0731bac3dd27c3c9e6966000b1da574344eb7ed8 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 21:34:43 +1200 Subject: [PATCH 6/8] Update README.md --- ch02/README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ch02/README.md b/ch02/README.md index e47052a..87cecee 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -370,12 +370,7 @@ L.I. holds. {2,1}, {3,1}, {8,1}, {6,1}, {8,6} ``` * set {n, n-1, n-2, ...,2, 1}, i.e. numbers in descending order has most inversions. - * As shown below, the expression `A[i] > key` is in essence checking for an inversion. So a function can be made to describe the relationship between the running time and number of inversions: + * As shown below, the expression `A[i] > key` in line 5 Insertion-Sort is in essence checking for an inversion. So a function can be made to describe the relationship between the running time and number of inversions: ```cpp T(n) = theta(n * number_of_inversions + n) ``` - -```cpp -//from Insertion-Sort -5 while i > 0 and A[i] > key -``` From f3fc2c2d154f1133a6ccad62e22dee0eda4a85a3 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 21:56:22 +1200 Subject: [PATCH 7/8] Update README.md --- ch02/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ch02/README.md b/ch02/README.md index 87cecee..55df9f3 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -364,13 +364,16 @@ L.I. holds. * As shown above, this code fragment correctly evaluates a polynomial. -##Problem 2-3 Inversions +##Problem 2-4 Inversions * Five inversions: ```cpp {2,1}, {3,1}, {8,1}, {6,1}, {8,6} ``` * set {n, n-1, n-2, ...,2, 1}, i.e. numbers in descending order has most inversions. +``` +number of inversions = n(n - 1)/2 +``` * As shown below, the expression `A[i] > key` in line 5 Insertion-Sort is in essence checking for an inversion. So a function can be made to describe the relationship between the running time and number of inversions: ```cpp - T(n) = theta(n * number_of_inversions + n) + T(n) = theta(number_of_inversions + n) ``` From 111e598e62b497d7234f7e7bcc321e0d53458272 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 6 Apr 2015 21:56:58 +1200 Subject: [PATCH 8/8] Update README.md --- ch02/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch02/README.md b/ch02/README.md index 55df9f3..e66b946 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -375,5 +375,5 @@ number of inversions = n(n - 1)/2 ``` * As shown below, the expression `A[i] > key` in line 5 Insertion-Sort is in essence checking for an inversion. So a function can be made to describe the relationship between the running time and number of inversions: ```cpp - T(n) = theta(number_of_inversions + n) +T(n) = theta(number_of_inversions + n) ```