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

Lecture 4_Programs and Problems

The document discusses the structure and types of programs, defining a program as a finite sequence of instructions over a domain of variables. It distinguishes between deterministic and nondeterministic programs, detailing various instruction types and their execution semantics. Additionally, it introduces the concept of problems in programming, explaining how problems can be defined by a domain and a question, and the criteria for partial solvability and solvability of problems by programs.

Uploaded by

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

Lecture 4_Programs and Problems

The document discusses the structure and types of programs, defining a program as a finite sequence of instructions over a domain of variables. It distinguishes between deterministic and nondeterministic programs, detailing various instruction types and their execution semantics. Additionally, it introduces the concept of problems in programming, explaining how problems can be defined by a domain and a question, and the criteria for partial solvability and solvability of problems by programs.

Uploaded by

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

Lecture 4

Programs and Problems


Programs
• A program can be defined as a finite sequence of
instructions over some domain D.
• The domain D, called the domain of the variables,
is assumed to be a set of elements with a
distinguished element, called the initial value of
the variables.
• Each of the elements in D is assumed to be a
possible assignment of a value to the variables of
the program.
Types of instructions contained in programs
• The sequence of instructions is assumed to consist of instructions of the following form:
 Read instructions of the form
read x
where x is a variable.

 Write instructions of the form


write x
where x is a variable.

 Deterministic assignment instructions of the form


y := f(x1, . . . , xm)
where x1, . . . , xm, and y are variables, and f is a function from D m to D.

 Conditional if instructions of the form


if Q(x1, . . . , xm) then I
where I is an instruction, x1, . . . , xm are variables, and Q is a predicate from D m to {false, true}.

 Deterministic looping instructions of the form


do
α
until Q(x1, . . . , xm)
where is a nonempty sequence of instructions, x1, . . . , xm are variables, and Q is a predicate from
Dm to {false, true}.
Types of instructions contained in programs
 Conditional accept instructions of the form
if eof then accept

 Reject instructions of the form


reject

 Nondeterministic assignment instructions of the form


x := ?
where x is a variable.

 Nondeterministic looping instructions of the form


do
α1
or
α2
or
.
.
.
or
αk
until Q(x1, . . . , xm)
where k ≥ 2, each of α1, . . . , αk is a nonempty sequence of
instructions, x 1, . . . , xm are variables, and Q is a predicate from D m to {false, true}.
• In each program the domain D of the variables is assumed to
have a representation over some alphabet.
• For instance, D can be the set of natural numbers, the set of
integers, and any finite set of elements.
• The functions f and predicates Q are assumed to be from a
given "built-in" set of computable functions and predicates.
• An input of a given program is a sequence of elements from
the domain of the variables of the program.
• Each element in an input of a program is called an input value.
• An execution sequence of a given program is an execution on a
given input of the instructions according to their semantics.
• The instructions are executed consecutively, starting with the
first instruction.
• The variables initially hold the initial value of the variables.
• Programs without nondeterministic instructions are
called deterministic programs, and programs with
nondeterministic instructions are
called nondeterministic programs.
Deterministic programs
• These have the property that no matter how many
times they are executed on a given input, the
executions are always in exactly the same manner.
• Each instruction of a deterministic program fully
specifies the operations to be performed.
• In contrast, nondeterministic instructions provide
only partial specifications for the actions.
• An execution of a read instruction read x reads the
next input value to x. An execution of a
write instruction write x writes the value of x.
• The deterministic assignment instructions and the
conditional if instructions have the conventional
semantics.
Deterministic programs cont...
• An execution of a deterministic looping
instruction do α until Q(x1, . . . , xm) consists of repeatedly
executing and checking the value of Q(x1, . . . , xm).
• The execution of the looping instruction is terminated upon
detecting that the predicate Q(x1, . . . , xm) has the value true.
• If Q(x1, . . . , xm) is the constant true, then only one iteration is
executed.
• On the other hand, if Q(x1, . . . , xm) is the constant false, then the
looping goes on forever, unless the execution terminates in stated
halt state .
• A conditional accept instruction causes an execution sequence to
halt if executed after all the input is consumed, that is, after
reaching the end of input file (eof for short).
• Otherwise the execution of the instruction causes the execution
sequence to continue at the code following the instruction.
• Similarly, an execution sequence also halts upon executing a
reject instruction, trying to read beyond the end of the input, trying
to transfer the control beyond the end of the program, or trying to
compute a value not in the domain of the variables (e.g., trying to
divide by 0).
Examples of Deterministic programs
Assume that the programs have the set of integers for the
domains of their variables, with 0 as initial value

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

• If I1 is an instance of K1, then


a. I2 = f(I1) is an instance of K2.
b. K1 has a solution S1 at I1 if and only if K2 has a solution S2
at I2 = f(I1) such that S1 = g(S2).

You might also like