0% found this document useful (0 votes)
76 views

Exercises Algoritms

The document describes the halting problem and shows that it is unsolvable. It introduces a hypothetical function H that takes a program P and input I and returns whether P halts on input I. It then shows that if such a function H existed, it would lead to a contradiction by considering what H would return for the program P and input P. Therefore, there is no algorithm that can solve the halting problem for all programs and inputs.

Uploaded by

Alena Mansika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Exercises Algoritms

The document describes the halting problem and shows that it is unsolvable. It introduces a hypothetical function H that takes a program P and input I and returns whether P halts on input I. It then shows that if such a function H existed, it would lead to a contradiction by considering what H would return for the program P and input P. Therefore, there is no algorithm that can solve the halting problem for all programs and inputs.

Uploaded by

Alena Mansika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

202 3 / Algorithms

If H(P, P) = “halts,”
P as program then loop forever
Program Program
Input H(P, I) Output K(P)
Program P H(P, P)

P as input If H(P, P) = “loops forever,”


then halt

FIGURE 2 Showing that the Halting Problem is Unsolvable.

is “halt,” then by the definition of K we see that K(K) loops forever, in violation of what H
tells us. In both cases, we have a contradiction.
Thus, H cannot always give the correct answers. Consequently, there is no procedure that
solves the halting problem.

Exercises

1. List all the steps used byAlgorithm 1 to find the maximum 8. Describe an algorithm that takes as input a list of n dis-
of the list 1, 8, 12, 9, 11, 2, 14, 5, 10, 4. tinct integers and finds the location of the largest even
integer in the list or returns 0 if there are no even integers
2. Determine which characteristics of an algorithm de-
in the list.
scribed in the text (after Algorithm 1) the following pro-
cedures have and which they lack. 9. A palindrome is a string that reads the same forward
and backward. Describe an algorithm for determining
a) procedure double(n: positive integer)
whether a string of n characters is a palindrome.
while n > 0
n := 2n 10. Devise an algorithm to compute x n , where x is a real
number and n is an integer. [Hint: First give a procedure
b) procedure divide(n: positive integer) for computing x n when n is nonnegative by successive
while n ≥ 0 multiplication by x, starting with 1. Then extend this pro-
m := 1/n cedure, and use the fact that x −n = 1/x n to compute x n
n := n − 1 when n is negative.]
c) procedure sum(n: positive integer) 11. Describe an algorithm that interchanges the values of the
sum := 0 variables x and y, using only assignments. What is the
while i < 10 minimum number of assignment statements needed to do
sum := sum + i this?
d) procedure choose(a, b: integers) 12. Describe an algorithm that uses only assignment state-
x := either a or b ments that replaces the triple (x, y, z) with (y, z, x).
3. Devise an algorithm that finds the sum of all the integers What is the minimum number of assignment statements
in a list. needed?
13. List all the steps used to search for 9 in the sequence 1,
4. Describe an algorithm that takes as input a list of n in-
3, 4, 5, 6, 8, 9, 11 using
tegers and produces as output the largest difference ob-
tained by subtracting an integer in the list from the one a) a linear search. b) a binary search.
following it. 14. List all the steps used to search for 7 in the sequence given
in Exercise 13 for both a linear search and a binary search.
5. Describe an algorithm that takes as input a list of n inte-
15. Describe an algorithm that inserts an integer x in the ap-
gers in nondecreasing order and produces the list of all
propriate position into the list a1 , a2 , . . . , an of integers
values that occur more than once. (Recall that a list of
that are in increasing order.
integers is nondecreasing if each integer in the list is at
least as large as the previous integer in the list.) 16. Describe an algorithm for finding the smallest integer in
a finite sequence of natural numbers.
6. Describe an algorithm that takes as input a list of n in- 17. Describe an algorithm that locates the first occurrence of
tegers and finds the number of negative integers in the the largest element in a finite list of integers, where the
list. integers in the list are not necessarily distinct.
7. Describe an algorithm that takes as input a list of n inte- 18. Describe an algorithm that locates the last occurrence of
gers and finds the location of the last even integer in the the smallest element in a finite list of integers, where the
list or returns 0 if there are no even integers in the list. integers in the list are not necessarily distinct.
3.1 Algorithms 203

19. Describe an algorithm that produces the maximum, me- 33. Devise an algorithm that finds the first term of a sequence
dian, mean, and minimum of a set of three integers. (The of positive integers that is less than the immediately pre-
median of a set of integers is the middle element in the list ceding term of the sequence.
when these integers are listed in order of increasing size. 34. Use the bubble sort to sort 6, 2, 3, 1, 5, 4, showing the
The mean of a set of integers is the sum of the integers lists obtained at each step.
divided by the number of integers in the set.) 35. Use the bubble sort to sort 3, 1, 5, 7, 4, showing the lists
20. Describe an algorithm for finding both the largest and the obtained at each step.
smallest integers in a finite sequence of integers. 36. Use the bubble sort to sort d, f, k, m, a, b, showing the
21. Describe an algorithm that puts the first three terms of lists obtained at each step.
a sequence of integers of arbitrary length in increasing ∗ 37. Adapt the bubble sort algorithm so that it stops when
order. no interchanges are required. Express this more efficient
22. Describe an algorithm to find the longest word in an En- version of the algorithm in pseudocode.
glish sentence (where a sentence is a sequence of symbols, 38. Use the insertion sort to sort the list in Exercise 34, show-
either a letter or a blank, which can then be broken into ing the lists obtained at each step.
alternating words and blanks). 39. Use the insertion sort to sort the list in Exercise 35, show-
23. Describe an algorithm that determines whether a function ing the lists obtained at each step.
from a finite set of integers to another finite set of integers 40. Use the insertion sort to sort the list in Exercise 36, show-
is onto. ing the lists obtained at each step.
24. Describe an algorithm that determines whether a function The selection sort begins by finding the least element in the
from a finite set to another finite set is one-to-one. list. This element is moved to the front. Then the least element
25. Describe an algorithm that will count the number of 1s in among the remaining elements is found and put into the sec-
a bit string by examining each bit of the string to deter- ond position. This procedure is repeated until the entire list
mine whether it is a 1 bit. has been sorted.
26. Change Algorithm 3 so that the binary search procedure 41. Sort these lists using the selection sort.
compares x to am at each stage of the algorithm, with the a) 3, 5, 4, 1, 2 b) 5, 4, 3, 2, 1
algorithm terminating if x = am . What advantage does c) 1, 2, 3, 4, 5
this version of the algorithm have? 42. Write the selection sort algorithm in pseudocode.
27. The ternary search algorithm locates an element in a list 43. Describe an algorithm based on the linear search for de-
of increasing integers by successively splitting the list into termining the correct position in which to insert a new
three sublists of equal (or as close to equal as possible) element in an already sorted list.
size, and restricting the search to the appropriate piece. 44. Describe an algorithm based on the binary search for de-
Specify the steps of this algorithm. termining the correct position in which to insert a new
28. Specify the steps of an algorithm that locates an element element in an already sorted list.
in a list of increasing integers by successively splitting 45. How many comparisons does the insertion sort use to sort
the list into four sublists of equal (or as close to equal as the list 1, 2, . . . , n?
possible) size, and restricting the search to the appropriate 46. How many comparisons does the insertion sort use to sort
piece. the list n, n − 1, . . . , 2, 1?
In a list of elements the same element may appear several The binary insertion sort is a variation of the insertion sort
times. A mode of such a list is an element that occurs at least that uses a binary search technique (see Exercise 44) rather
as often as each of the other elements; a list has more than than a linear search technique to insert the ith element in the
one mode when more than one element appears the maximum correct place among the previously sorted elements.
number of times.
47. Show all the steps used by the binary insertion sort to sort
29. Devise an algorithm that finds a mode in a list of nonde- the list 3, 2, 4, 5, 1, 6.
creasing integers. (Recall that a list of integers is nonde- 48. Compare the number of comparisons used by the inser-
creasing if each term is at least as large as the preceding tion sort and the binary insertion sort to sort the list 7, 4,
term.) 3, 8, 1, 5, 4, 2.
30. Devise an algorithm that finds all modes. (Recall that a ∗ 49. Express the binary insertion sort in pseudocode.
list of integers is nondecreasing if each term of the list is
50. a) Devise a variation of the insertion sort that uses a lin-
at least as large as the preceding term.)
ear search technique that inserts the j th element in the
31. Devise an algorithm that finds the first term of a se- correct place by first comparing it with the (j − 1)st
quence of integers that equals some previous term in the element, then the (j − 2)th element if necessary, and
sequence. so on.
32. Devise an algorithm that finds all terms of a finite se- b) Use your algorithm to sort 3, 2, 4, 5, 1, 6.
quence of integers that are greater than the sum of all c) Answer Exercise 45 using this algorithm.
previous terms of the sequence. d) Answer Exercise 46 using this algorithm.
204 3 / Algorithms

51. When a list of elements is in close to the correct order, of the opposite gender. Furthermore, suppose that each person
would it be better to use an insertion sort or its variation ranks, in order of preference, with no ties, the people of the
described in Exercise 50? opposite gender. We say that a matching of people of opposite
52. Use the greedy algorithm to make change using quarters, genders to form couples is stable if we cannot find a man m
dimes, nickels, and pennies for and a woman w who are not assigned to each other such that
a) 87 cents. b) 49 cents. m prefers w over his assigned partner and w prefers m to her
c) 99 cents. d) 33 cents. assigned partner.
53. Use the greedy algorithm to make change using quarters, 60. Suppose we have three men m1 , m2 , and m3 and three
dimes, nickels, and pennies for women w1 , w2 , and w3 . Furthermore, suppose that the
a) 51 cents. b) 69 cents. preference rankings of the men for the three women, from
c) 76 cents. d) 60 cents. highest to lowest, are m1 : w3 , w1 , w2 ; m2 : w1 , w2 , w3 ; m3 :
w2 , w3 , w1 ; and the preference rankings of the women for
54. Use the greedy algorithm to make change using quar-
the three men, from highest to lowest, are w1 : m1 , m2 ,
ters, dimes, and pennies (but no nickels) for each of the
m3 ; w2 : m2 , m1 , m3 ; w3 : m3 , m2 , m1 . For each of the
amounts given in Exercise 52. For which of these amounts
six possible matchings of men and women to form three
does the greedy algorithm use the fewest coins of these
couples, determine whether this matching is stable.
denominations possible?
The deferred acceptance algorithm, also known as the Gale-
55. Use the greedy algorithm to make change using quar-
Shapley algorithm, can be used to construct a stable matching
ters, dimes, and pennies (but no nickels) for each of the
of men and women. In this algorithm, members of one gender
amounts given in Exercise 53. For which of these amounts
are the suitors and members of the other gender the suitees.
does the greedy algorithm use the fewest coins of these
The algorithm uses a sequence of rounds; in each round every
denominations possible?
suitor whose proposal was rejected in the previous round pro-
56. Show that if there were a coin worth 12 cents, the greedy poses to his or her highest ranking suitee who has not already
algorithm using quarters, 12-cent coins, dimes, nickels, rejected a proposal from this suitor. A suitee rejects all pro-
and pennies would not always produce change using the posals except that from the suitor that this suitee ranks highest
fewest coins possible. among all the suitors who have proposed to this suitee in this
57. Use Algorithm 7 to schedule the largest number of talks round or previous rounds. The proposal of this highest ranking
in a lecture hall from a proposed set of talks, if the starting suitor remains pending and is rejected in a later round if a more
and ending times of the talks are 9:00 a.m. and 9:45 a.m.; appealing suitor proposes in that round. The series of rounds
9:30 a.m. and 10:00 a.m.; 9:50 a.m. and 10:15 a.m.; ends when every suitor has exactly one pending proposal. All
10:00 a.m. and 10:30 a.m.; 10:10 a.m. and 10:25 a.m.; pending proposals are then accepted.
10:30 a.m. and 10:55 a.m.; 10:15 a.m. and 10:45 a.m.;
61. Write the deferred acceptance algorithm in pseudocode.
10:30 a.m. and 11:00 a.m.; 10:45 a.m. and 11:30 a.m.;
10:55 a.m. and 11:25 a.m.; 11:00 a.m. and 11:15 a.m. 62. Show that the deferred acceptance algorithm terminates.
58. Show that a greedy algorithm that schedules talks in a lec- ∗ 63. Show that the deferred acceptance always terminates with
ture hall, as described in Example 7, by selecting at each a stable assignment.
step the talk that overlaps the fewest other talks, does not
64. Show that the problem of determining whether a program
always produce an optimal schedule.
∗ 59. a) Devise a greedy algorithm that determines the fewest with a given input ever prints the digit 1 is unsolvable.
lecture halls needed to accommodate n talks given the 65. Show that the following problem is solvable. Given two
starting and ending time for each talk. programs with their inputs and the knowledge that exactly
b) Prove that your algorithm is optimal. one of them halts, determine which halts.
Suppose we have s men m1 , m2 , . . . , ms and s women 66. Show that the problem of deciding whether a specific
w1 , w2 , . . . , ws . We wish to match each person with a member program with a specific input halts is solvable.

3.2 The Growth of Functions


Introduction
In Section 3.1 we discussed the concept of an algorithm. We introduced algorithms that solve a
variety of problems, including searching for an element in a list and sorting a list. In Section 3.3
we will study the number of operations used by these algorithms. In particular, we will estimate
the number of comparisons used by the linear and binary search algorithms to find an element
in a sequence of n elements. We will also estimate the number of comparisons used by the

You might also like