Lecture 4_Programs and Problems
Lecture 4_Programs and Problems
a). b).
do do
if eof then accept read value
read value write value
write value until value < 0
until false if eof then accept
• For each input the program in a). has one execution sequence.
• In each execution sequence the program provides an output that is equal
to the input.
• All the execution sequences of the program terminate due to the
execution of the conditional accept instruction.
• On input "1, 2" the execution sequence repeatedly executes for three
times the body of the deterministic looping instruction.
• During the first iteration, the execution sequence determines that the
predicate eof has the value false.
• Consequently, the execution sequence ignores the accept command and
continues by reading the value 1 and writing it out.
• During the second iteration the execution sequence verifies again that the
end of the input has not been reached yet, and then the execution
sequence reads the input value 2 and writes it out.
• During the third iteration, the execution sequence terminates due to the
accept command, after determining a true value for the predicate eof .
• The execution sequences of the program in b) halt due to the
conditional accept instruction, only on inputs that end with a
negative value and have no negative values elsewhere (e.g., the
input "1, 2, -3").
• On inputs that contain no negative values at all, the execution
sequences of the program halt due to trying to read beyond the
end of the input (e.g., on input "1, 2, 3").
• On inputs that have negative values before their end, the execution
sequences of the program halt due to the transfer of control
beyond the end of the program (e.g., on input "-1, 2, -3").
• An accept can be viewed as a halt command that signals a
successful completion of a program execution, where
the accept can be executed only after the end of the input is
reached.
• A reject can be viewed as a halt instruction that signals an
unsuccessful completion of a program execution.
Computations
• An execution sequence is said to be
an accepting computation if it terminates due to an
accept command.
• An execution sequence is said to be
nonaccepting computation or
a rejecting computation if it is on input that has no
accepting computations.
• An execution sequence is said to be a computation if it
is an accepting computation or a nonaccepting
computation.
• A computation is said to be a halting computation if it
is finite.
Computations cont...
Example: Consider the program below,
read value • On an input that consists of a single, even,
do positive integer, the program has an
execution sequence that is an accepting
write value computation (e.g., on input "4").
value := value - 2 • On an input that consists of more than
until value = 0 one value and that starts with an even
positive integer, the program has a halting
if eof then accept execution sequence that is a nonaccepting
computation (e.g., on input "4, 3, 2").
• Assume that the • On the rest of the inputs the program has
domain of the nonhalting execution sequences that are
variables is the set nonaccepting computations (e.g., on input
of integers, with 0 "1").
as initial value.
Computations cont...
• An input is said to be accepted , or recognized , by a program if the program
has an accepting computation on such an input.
• Otherwise the input is said to be not accepted , or rejected , by the program.
• A program is said to have an output y on input x if it has an accepting
computation on x with output y.
• The outputs of the nonaccepting computations are considered to be
undefined , even though such computations may execute write instructions.
• The program in the example above accepts the inputs "2", "4", "6", . . . On
input "6" the program has the output "6, 4, 2", and on input "2" the program
has the output "2".
• The program does not accept the inputs "0", "1", and "4, 2". For these inputs
the program has no output, that is, the output is undefined.
• A computation is said to be a nondeterministic computation if it involves the
execution of a nondeterministic instruction.
• Otherwise the computation is said to be a deterministic computation.
Nondeterministic Programs
• Nondeterministic instructions are essentially instructions
that can choose between some given options
• Different objectives create the need for nondeterministic
instructions in programming languages.
• One of the objectives is to allow the programs to deal
with problems that may have more than one solution.
• In such a case, nondeterministic instructions provide a
natural method of selection (see, e.g., Example below).
• Another objective is to simplify the task of programming.
• Still another objective is to provide tools for identifying
difficult problems and for studying restricted classes of
programs
Example: A nondeterministic program that chooses five input values
• The set of natural numbers is assumed to be the domain of
counter := 0 the variables, with 0 as initial value.
/* Choose five input • The program on input "1, 2, 3, 4, 5, 6" has an execution
values. */ sequence of the following form:
• The execution sequence starts with an iteration of the
do nondeterministic looping instruction in which the first code segment
read value is chosen.
or • The execution of the code segment consists of reading the input
value 1, while writing nothing and leaving counter with the value
read value of 0.
write value • Then the execution sequence continues with five additional
iterations of the nondeterministic looping instruction.
counter := counter + 1 •
In each of the additional iterations, the second code segment is
until counter = 5 chosen.
/* Read the remainder • Each execution of the second code segment reads an input value,
outputs the value that has been read, and increases the value
of the input. */ of counter by 1.
do • When counter reaches the value of 5, the execution sequence exits
if eof then accept the first looping instruction.
• During the first iteration of the second looping instruction, the
read value execution sequence halts due to the execution of the conditional
until false accept instruction.
• The execution sequence is an accepting computation with output "2,
• The program on input "1, 2, 3, 4, 5, 6" has four additional execution sequences
similar to the one above.
• The only difference is that the additional execution sequences, instead of ignoring
the input value 1, ignore the input values 2, 3, 4, and 5, respectively.
• An execution sequence ignores an input value i by choosing to read the value in
the first code segment of the nondeterministic looping instruction.
• The additional execution sequences are accepting computations with outputs "1,
3, 4, 5, 6", "1, 2, 4, 5, 6", "1, 2, 3, 5, 6", and "1, 2, 3, 4, 6", respectively.
• The program on input "1, 2, 3, 4, 5, 6" also has an accepting computation of the
following form.
– The computation starts with five iterations of the first looping instruction. In each of these
iterations the second code segment of the nondeterministic looping instruction is executed.
• During each iteration an input value is read, that value is written into the output,
and the value of counter is increased by 1.
• After five iterations of the nondeterministic looping instruction, counter reaches
the value of 5, and the computation transfers to the deterministic looping
instruction.
• The computation reads the input value 6 during the first iteration of the
deterministic looping instruction, and terminates during the second iteration. The
output of the computation is "1, 2, 3, 4, 5".
Problems
• The motivation for writing programs is to
manipulate information, thereby solving
problems.
• Each problem K is a pair consisting of a set and
a question, where the question can be applied
to each element in the set.
• The set is called the domain of the problem,
and its elements are called the instances of
the problem.
• Example: Consider the problem K1 defined by
the following domain and question.
Domain:
{ <a, b> | a and b are natural numbers }.
Question:
What is the integer part y of a divided by b
for the given instance x = <a, b>?
• The domain of the problem contains the
instances <0, 0>, <5, 0>, <3, 8>, <24, 6>, and
<27, 8>. On the other hand, <-5, 3> is not an
instance of the problem.
• For the instance <27, 8> the problem asks
what is the integer part of 27 divided by 8.
Similarly, for the instance <0, 0> the problem
asks what is the integer part of 0 divided by 0.
• An answer to the question that the problem K
poses for a given instance is said to be a
solution for the problem at the given instance.
• The relation induced by the problem, denoted
R(K), is the set { (x, y) | x is an instance of the
problem, and y is a solution for the problem at
x }.
• Consider the problem K1 in Example above
– The problem has the solution 3 at instance <27,
8>, and an undefined solution at instance <0, 0>.
– K1 induces the relation R(K1) = {(<0, 1>, 0), (<0,
2>, 0), (<1, 1>, 1), (<0, 3>, 0), (<1, 2>, 0), (<2, 1>,
2), (<0, 3>, 0), . . . }.
• The problem is said to be a decision problem if
for each instance the problem has either a yes
or a no solution.
• The problem K1 is not a decision problem. But
the problem K2 defined by the following pair
is.
Domain:
{ <a, b> | a and b are natural numbers }.
Question:
Does b divide a, for the given instance <a, b>?
Partial Solvability and Solvability
• A program P is said to partially solve a given
problem K if it provides the answer for each
instance of the problem, that is, if R(P) = R(K).
• If, in addition, all the computations of the
program are halting computations, then the
program is said to solve the problem.
• Example: Consider the program P1 below that
partially solves the problem of dividing natural
numbers (example above). The domain of the
variables is assumed to equal the set of
natural numbers.
• On input "27, 8" the program halts in an accepting
configuration with the answer 3 in the output.
• On input "0, 0" the program never halts, and so the
program has undefined output on such an input.
• On input "27" and input "27, 8, 2" the program halts in
rejecting configurations and does not define an output.
• The program P1 does not solve K1 because it does not
halt when the input value for b is 0.
• P1 can be modified to a program P that solves K1 by
letting P check for a 0 assignment to b.
Partially decidability and decidability
• A program is said to partially decide a problem
if the following two conditions are satisfied.
a. The problem is a decision problem; and
b. The program accepts a given input if and only if
the problem has an answer yes for the input, that
is, the program accepts the language { x | x is an
instance of the problem, and the problem has the
answer yes at x }.
• A program is said to decide a problem if it
partially decides the problem and all its
computations are halting computations.
• Consider the program P2 below that partially
decides the problem of divisibility of natural
numbers (problem K2 above).
• A problem is said to be unsolvable if no
algorithm can solve it.
• The problem is said to be undecidable if it is a
decision problem and no algorithm can decide
it.
• The relationship between the different classes
of problems is illustrated in the Venn diagram of
below.
Reducibility among Problems
• A common approach in solving problems is to
transform them to different problems, solve the
new ones, and derive the solutions for the
original problems from those for the new ones.
• This approach is useful when the new problems
are simpler to solve, or when they already have
known algorithms for solving them.
• A similar approach is also very useful in the
classification of problems according to their
complexity.
• A problem K1, which can be transformed to another
problem K2, is said to be reducible to the new
problem.
• Specifically, a problem K1 is said to be reducible to a
problem K2 if there exist computable total functions f
and g with the following properties