Introduction To Complexity Computational 2019

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 94

Introduction to Complexity and

Computability
Quan Thanh Tho
qttho@hcmut.edu.vn
Preliminary Concepts


•  Pseudocode
•  Abstract data type
•  Algorithm efficiency

2
Pseudocode
•  What is an algorithm?

3
Pseudocode
•  What is an algorithm?
–  The logical steps to solve a problem.

4
Pseudocode
•  What is a program?
–  Program = Data structures + Algorithms (Niklaus Wirth)

5
Pseudocode
•  The most common tool to define algorithms.
•  English-like representation of the code
required for an algorithm.

6
Pseudocode
•  Pseudocode = English + Code

relaxed syntax that extended version of the


is easy to read basic control structures
(sequential, conditional, iterative)

7
Pseudocode

Algorithm Header

Algorithm Body

8
Pseudocode
•  Algorithm Header:
–  Name
–  Parameters and their types
–  Purpose
•  what the algorithm does
–  Precondition
•  precursor requirements for the parameters
–  Postcondition
•  taken action and status of the parameters
–  Return condition
•  returned value
9
Pseudocode
•  Algorithm Body:
–  Statements
–  Statement numbers
•  decimal notation to express levels
–  Variables
•  important data
–  Algorithm analysis
•  comments to explain salient points
–  Statement constructs
•  sequence, selection, iteration

10
Example
Algorithm average
Pre nothing
Post numbers read and their average printed
1 i=0
2 loop (all data not read)
1 i=i+1
2 read number
3 sum = sum + number
3 average = sum / i
4 print average
5 return
End average
11
Abstract Data Type
•  What is a data type?
–  Class of data objects that have the same properties

12
Abstract Data Type
•  Development of programming concepts:
–  GOTO programming
•  control flow is like spaghetti on a plate
–  Modular programming
•  programs organized into subprograms
–  Structured programming
•  structured control statements (sequence, selection, iteration)
–  Object-oriented programming
•  encapsulation of data and operations

13
Abstract Data Type
•  ADT = Data structures + Operations

14
Abstract Data Type

Interface
User knows what a data
type can do.
Implementation of
data and operations
How it is done is hidden.

15
Abstract Data Type

data structure

internal
function
data
function A

data
function B

16
Example: Variable Access
•  Rectangle: r
–  length: x
–  width: y

•  Rectangle: r
–  length: x (hidden)
–  width: y (hidden)
–  get_length()
–  get_width()

17
Example: List
•  Interface:
–  Data:
•  sequence of components of a particular data type
–  Operations:
•  accessing
•  insertion
•  deletion

•  Implementation:
–  Array, or
–  Linked list

18
Algorithm Efficiency
•  How fast an algorithm is?
•  How much memory does it cost?
•  Computational complexity: measure of the
difficulty degree (time or space) of an
algorithm.

19
Algorithm Efficiency
•  General format:
f(n)
n is the size of a problem (the key number that determines
the size of input data)

20
Linear Loops
1 i=1 1 i=1
2 loop (i <= 1000) 2 loop (i <= 1000)
1 application code 1 application code
2 i=i+1 2 i=i+2

The number of times the body The number of times the body
of the loop is replicated is of the loop is replicated is
1000 500

21
Linear Loops
time
f(n) = n.T

f(n) = (n/2).T

n 22
Logarithmic Loops
Multiply loops
1 i=1
2 loop (i <= 1000)
1 application code
2 i=i×2

The number of times the body of the loop is replicated is


log2n

23
Logarithmic Loops
Multiply loops Divide loops
1 i=1 1 i = 1000
2 loop (i <= 1000) 2 loop (i >= 1)
1 application code 1 application code
2 i=i×2 2 i=i/2

The number of times the body of the loop is replicated is


log2n

24
Logarithmic Loops
time

f(n) = (log2n).T

n 25
Nested Loops

Iterations = Outer loop iterations × Inner loop iterations

26
Linear Logarithmic Loops
1 i=1
2 loop (i <= 10)
1 j=1
2 loop (j <= 10)
1 application code
2 j=j×2
3 i=i+1

The number of times the body of the loop is replicated is


nlog2n

27
Linear Logarithmic Loops
time f(n) = (nlog2n).T

n 28
Quadratic Loops
1 i=1
2 loop (i <= 10)
1 j=1
2 loop (j <= 10)
1 application code
2 j=j+1
3 i=i+1

The number of times the body of the loop is replicated is


n2

29
Dependent Quadratic Loops
1 i=1
2 loop (i <= 10)
1 j=1
2 loop (j <= i)
1 application code
2 j=j+1
3 i=i+1

The number of times the body of the loop is replicated is


1 + 2 + … + n = n(n + 1)/2

30
Quadratic Loops
time f(n) = n2.T

n 31
Asymtotic Complexity
•  Algorithm efficiency is considered with only
big problem sizes.
•  We are not concerned with an exact
measurement of an algorithm's efficiency.
•  Terms that do not substabtially change the
function’s magnitude are eliminated.

32
Big-O Notation
•  f(n) = c.n ⇒ f(n) = O(n).
•  f(n) = n(n + 1)/2 = n2/2 + n/2 ⇒ f(n) = O(n2).

33
Big-O Notation
•  Set the coefficient of the term to one.
•  Keep the largest term and discard the
others.
log2n n nlog2n n2 n3 ... nk ... 2n n!

34
Standard Measures of Efficiency
Efficiency Big-O Iterations Est. Time
logarithmic O(log2n) 14 microseconds
linear O(n) 10,000 .1 seconds
linear logarithmic O(nlog2n) 140,000 2 seconds
quadratic O(n2) 10,0002 15-20 min.
polynomial O(nk) 10,000k hours
exponential O(2n) 210,000 intractable
factorial O(n!) 10,000! intractable

Assume instruction speed of 1 microsecond and 10 instructions in loop.


n = 10,000

35
Standard Measures of Efficiency
O(n) n2 nlog2n n

log2n

n 36
Big-O Analysis Examples
Algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>,
val size <integer>, ref matrix3 <matrix>)
Add matrix1 to matrix2 and place results in matrix3
Pre matrix1 and matrix2 have data
size is number of columns and rows in matrix
Post matrices added - result in matrix3
1 r=1
2 loop (r <= size)
1 c=1
2 loop (c <= size)
1 matrix3[r, c] = matrix1[r, c] + matrix2[r, c]
2 c=c+1
3 r=r+1
3 return
End addMatrix
37
Big-O Analysis Examples
Algorithm addMatrix (val matrix1 <matrix>, val matrix2 <matrix>,
val size <integer>, ref matrix3 <matrix>)
Add matrix1 to matrix2 and place results in matrix3
Pre matrix1 and matrix2 have data
size is number of columns and rows in matrix
Post matrices added - result in matrix3
1 r=1
2 loop (r <= size)
1 c=1
2 loop (c <= size)
1 matrix3[r, c] = matrix1[r, c] + matrix2[r, c]
2 c=c+1
3 r=r+1
3 return Nested linear loop: f(size) = O(size 2)

End addMatrix
38
Time Costing Operations
•  The most time consuming: data movement
to/from memory/storage.
•  Operations under consideration:
–  Comparions
–  Arithmetic operations
–  Assignments

39
Recurrence Equation
•  An equation or inequality that describes a
function in terms of its value on smaller
input.

40
Recurrence Equation
•  Example: binary search.

a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] a[12]

4 7 8 10 14 21 22 36 62 77 81 91

41
Recurrence Equation
•  Example: binary search.

a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] a[12]

4 7 8 10 14 21 22 36 62 77 81 91

f(n) = 1 + f(n/2) ⇒ f(n) = O(log2n)

42
Best, Average, Worst Cases
•  Best case: when the number of steps is
smallest.
•  Worst case: when the number of steps is
largest.
•  Average case: in between.

43
Best, Average, Worst Cases
•  Example: sequential search.

a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] a[12]

4 8 7 10 21 14 22 36 62 91 77 81

Best case: f(n) = O(1)


Worst case: f(n) = O(n)

44
Best, Average, Worst Cases
•  Example: sequential search.

a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] a[12]

4 8 7 10 21 14 22 36 62 91 77 81

Average case: f(n) = ∑i.pi


pi: probability for the target being at a[i]
pi = 1/n ⇒ f(n) = (∑i)/n = O(n)

45
P and NP Problems
•  P: Polynomial (can be solved in polynomial
time on a deterministic machine).
•  NP: Nondeterministic Polynomial (can be
solved in polynomial time on a non-
deterministic machine).

46
P and NP Problems
Travelling Salesman Problem:
A salesman has a list of cities, each of which he must visit
exactly once. There are direct roads between each pair of
cities on the list.
Find the route the salesman should follow for the shortest
possible round trip that both starts and finishes at any one of
the cities.
A
1 10
B
D E
5 5
15 5
C
47
P and NP Problems
Travelling Salesman Problem:
Deterministic machine: f(n) = n(n-1)(n-2) … 1 = O(n!)

⇒ NP problem
A
1 10
B
D E
5 5
15 5
C

48
P and NP Problems
•  NP-complete: NP and every other problem
in NP is polynomially reducible to it.
•  Open question: P = NP?

P
NP
NP-complete

49
Class P

input x output F(x)


Problem F

F is in Class P if there exists an algorithm to compute F(x) in


time polynomial w.r.t. |x|

Example :
- input : integers a and b, in binary notation
- output : a * b

January 2008 Advanced Algorithmics - Complexity 50


Decision Problems

input x output : F(x) ∈ { true,false }


Problem F

Examples :

- input : integers a, b, c
- decide whether a * b = c

- input : a directed graph G


- decide whether G is acyclic
January 2008 Advanced Algorithmics - Complexity 51
BIPARTITE
- input : an undirected graph G
- decide whether the vertices of G can be split into two parts, so that every
edge goes from one part to the other

F B
A
C C

A H
H D
D
B E
F
E

G G

January 2008 Advanced Algorithmics - Complexity 52


BIPARTITE is in class P
int Bipartite(graph G) {
in every connected component of G {
choose any vertex v, and colour v in BLACK ;
enqueue(v);
while (queue is not empty) {
s = dequeue() ;
for every successor t of s
if (t has never been enqueued) {
colour t differently from s ;
enqueue(t) ;
}
else if (s and t have the same colour)
return 0 ;
}
}
return 1 ;
}
January 2008 Advanced Algorithmics - Complexity
Exam Session
- input :a list of m courses, a list of n students, and for each student, the
two courses he takes
- decide whether an exam session may be organized in two days, so that
every student takes an exam a day

The problem reduces to BIPARTITE :


- build a graph G with vertices the m courses, and with edges C - C'
whenever there is a student taking both courses C and C'
- solve BIPARTITE(G)

The reduction can be done in polynomial time :


- size of the input : Θ(m+n)
- running time : an obvious algorithm O(m² n)

January 2008 Advanced Algorithmics - Complexity 54


Polynomial Reductions

input x output F(x)


Problem F
reduction R such that R(x) can be computed in
polynomial time (w.r.t. |x|)

input y output G(y)


Problem G
and that if y = R(x) then G(y) = F(x)

F polynomially reduces to G : if G is in class P, so is F


Advanced Algorithmics - Complexity
January 2008 55
k-COLOUR
- input : an undirected graph G
- decide whether the vertices of G can be coloured, using at most k
different colours, so that adjacent vertices never have the same colour

January 2008 Advanced Algorithmics - Complexity 56


A greedy algorithm to colour a graph
Perform a breadth-first search of every connected component,
colouring each vertex with the smallest possible colour.

is not optimal !

no polynomial algorithm is known to solve 3-COLOUR

January 2008 Advanced Algorithmics - Complexity 57


Class NP
input x output F(x)
Decision Problem F

x
Verifier V if V(x,p) = true
p
then F(x) = true

F is in Class NP if there is a verifier V and an algorithm to compute V(x,p) in


time polynomial w.r.t. |x|
(the proof p must be of polynomial size, too)

NP : “non-deterministic polynomial time”


(intuitively : “guessing” a proper proof yields the answer true)
January 2008 Advanced Algorithmics - Complexity 58
k-COLOUR is NP

G, k G is k-colourable
k-COLOUR
G,k
Verifier
a colour associated at most k different colours
with each vertex of G are used, and the ends of
every edge are of different
colours

January 2008 Advanced Algorithmics - Complexity 59


SAT
- input : a boolean function in conjunctive normal form (CNF),
i.e. product of sums of literals (either a variable or its complement)

ex. f(u,v,x,y,z) = (u+v+x) (v+y+z)(u+y) (v+y) (u+x+y+z) (u+z) (x+y+z)

- decide whether the function is satisfiable, i.e. whether there are values
of the variables such that f=1

SAT is NP
(proof : values of variables)

January 2008 Advanced Algorithmics - Complexity 60


3-COLOUR polynomially reduces to SAT
With each vertex s, associate three variables sr , sg , sb

Build up the boolean function in CNF :


- for every vertex s, add factors (sr + sg + sb ) (sg + sb ) (sb + sr ) (sr + sg )
- for every edge s ─ s', add factors (sr + s'r ) (sg + s'g ) (sb + s'b )

The reduction is linear in the size of the graph.

(we don't know, but the answer is worth $1,000,000)

January 2008 Advanced Algorithmics - Complexity 61


Polynomial Reductions of NP problems
input x output F(x)
Problem F
x
polynomial reduction R Verifier for F
y = R(x)
Problem G
input y output G(y)

p
Verifier V V(y,p)

If G is in class NP, so is F
Advanced Algorithmics - Complexity
January 2008 62
NP-hardness and NP-completeness
Cook’s theorem : SAT is NP-hard,
i.e. ANY NP problem polynomially reduces to SAT
To prove that a problem F is NP-hard, it is sufficient to show that SAT (or any other
known NP-hard problem) polynomially reduces to F :
any NP problem G SAT F

A problem both NP and NP-hard is said to be NP-complete.


If a polynomial algorithm is found to solve ONE NP-complete problem, then P = NP.

January 2008 Advanced Algorithmics - Complexity 63


Some NP-complete problems

3-COLOUR

HAMILTONIAN-PATH
- input : a graph G (directed or undirected)
- decide whether there is a path in G going exactly once through each vertex

SUBSET-SUM
- input : a set I of integers, and a number S
- decide whether there is a subset of I whose sum is exactly S

All known algorithms run in exponential time

January 2008 Advanced Algorithmics - Complexity 64


Solving difficult problems
Find classes of instances for which there is an efficient algorithm
By the 4-colour theorem, all planar graphs are 4-colourable.
This (trivially) solves the 4-COLOUR decision problem for any geographical map (but
it does not provide the colouring !)

Heuristics + Backtracking
Apply empirical means to guess a solution : e.g. use a greedy algorithm to 4-colour a
planar graph. If the result is not satisfactory (more than 4 colours), change your last
choice. The algorithm is exponential, but it could be efficient in most cases.

Optimization
Solve a weaker problem (e.g. use more than 4 colours to colour a map), and modify the
solution so that it fits the constraints of the original problem.

January 2008 Advanced Algorithmics - Complexity 65


Primality Testing
- input : an integer n
- COMPOSITE : decide whether n has a non-trivial divisor d, 1 < d < n
- PRIME : decide whether n is a prime number

COMPOSITE is NP
Proof : a non-trivial divisor

2002 : PRIME is P
AKS primality test (Agrawal, Kayal and Saxena)

Therefore, COMPOSITE (the opposite of PRIME) is also P

January 2008 Advanced Algorithmics - Complexity


66
Decision Problems

input x output : F(x) ∈ { true,false }


Problem F

Examples :

- input : integers a, b, c
- decide whether a * b = c

- input : a directed graph G


- decide whether G is acyclic
January 2008 Advanced Algorithmics - Complexity 67
BIPARTITE
- input : an undirected graph G
- decide whether the vertices of G can be split into two parts, so that every
edge goes from one part to the other

F B
A
C C

A H
H D
D
B E
F
E

G G

January 2008 Advanced Algorithmics - Complexity 68


BIPARTITE is in class P
int Bipartite(graph G) {
in every connected component of G {
choose any vertex v, and colour v in BLACK ;
enqueue(v);
while (queue is not empty) {
s = dequeue() ;
for every successor t of s
if (t has never been enqueued) {
colour t differently from s ;
enqueue(t) ;
}
else if (s and t have the same colour)
return 0 ;
}
}
return 1 ;
}
January 2008 Advanced Algorithmics - Complexity
Exam Session
- input :a list of m courses, a list of n students, and for each student, the
two courses he takes
- decide whether an exam session may be organized in two days, so that
every student takes an exam a day

The problem reduces to BIPARTITE :


- build a graph G with vertices the m courses, and with edges C - C'
whenever there is a student taking both courses C and C'
- solve BIPARTITE(G)

The reduction can be done in polynomial time :


- size of the input : Θ(m+n)
- running time : an obvious algorithm O(m² n)

January 2008 Advanced Algorithmics - Complexity 70


Polynomial Reductions

input x output F(x)


Problem F
reduction R such that R(x) can be computed in
polynomial time (w.r.t. |x|)

input y output G(y)


Problem G
and that if y = R(x) then G(y) = F(x)

F polynomially reduces to G : if G is in class P, so is F


Advanced Algorithmics - Complexity
January 2008 71
k-COLOUR
- input : an undirected graph G
- decide whether the vertices of G can be coloured, using at most k
different colours, so that adjacent vertices never have the same colour

January 2008 Advanced Algorithmics - Complexity 72


A greedy algorithm to colour a graph
Perform a breadth-first search of every connected component,
colouring each vertex with the smallest possible colour.

is not optimal !

no polynomial algorithm is known to solve 3-COLOUR

January 2008 Advanced Algorithmics - Complexity 73


Class NP
input x output F(x)
Decision Problem F

x
Verifier V if V(x,p) = true
p
then F(x) = true

F is in Class NP if there is a verifier V and an algorithm to compute V(x,p) in


time polynomial w.r.t. |x|
(the proof p must be of polynomial size, too)

NP : “non-deterministic polynomial time”


(intuitively : “guessing” a proper proof yields the answer true)
January 2008 Advanced Algorithmics - Complexity 74
k-COLOUR is NP

G, k G is k-colourable
k-COLOUR
G,k
Verifier
a colour associated at most k different colours
with each vertex of G are used, and the ends of
every edge are of different
colours

January 2008 Advanced Algorithmics - Complexity 75


SAT
- input : a boolean function in conjunctive normal form (CNF),
i.e. product of sums of literals (either a variable or its complement)

ex. f(u,v,x,y,z) = (u+v+x) (v+y+z)(u+y) (v+y) (u+x+y+z) (u+z) (x+y+z)

- decide whether the function is satisfiable, i.e. whether there are values
of the variables such that f=1

SAT is NP
(proof : values of variables)

January 2008 Advanced Algorithmics - Complexity 76


3-COLOUR polynomially reduces to SAT
With each vertex s, associate three variables sr , sg , sb

Build up the boolean function in CNF :


- for every vertex s, add factors (sr + sg + sb ) (sg + sb ) (sb + sr ) (sr + sg )
- for every edge s ─ s', add factors (sr + s'r ) (sg + s'g ) (sb + s'b )

The reduction is linear in the size of the graph.

(we don't know, but the answer is worth $1,000,000)

January 2008 Advanced Algorithmics - Complexity 77


Polynomial Reductions of NP problems
input x output F(x)
Problem F
x
polynomial reduction R Verifier for F
y = R(x)
Problem G
input y output G(y)

p
Verifier V V(y,p)

If G is in class NP, so is F
Advanced Algorithmics - Complexity
January 2008 78
NP-hardness and NP-completeness
Cook’s theorem : SAT is NP-hard,
i.e. ANY NP problem polynomially reduces to SAT
To prove that a problem F is NP-hard, it is sufficient to show that SAT (or any other
known NP-hard problem) polynomially reduces to F :
any NP problem G SAT F

A problem both NP and NP-hard is said to be NP-complete.


If a polynomial algorithm is found to solve ONE NP-complete problem, then P = NP.

January 2008 Advanced Algorithmics - Complexity 79


Some NP-complete problems

3-COLOUR

HAMILTONIAN-PATH
- input : a graph G (directed or undirected)
- decide whether there is a path in G going exactly once through each vertex

SUBSET-SUM
- input : a set I of integers, and a number S
- decide whether there is a subset of I whose sum is exactly S

All known algorithms run in exponential time

January 2008 Advanced Algorithmics - Complexity 80


Solving difficult problems
Find classes of instances for which there is an efficient algorithm
By the 4-colour theorem, all planar graphs are 4-colourable.
This (trivially) solves the 4-COLOUR decision problem for any geographical map (but
it does not provide the colouring !)

Heuristics + Backtracking
Apply empirical means to guess a solution : e.g. use a greedy algorithm to 4-colour a
planar graph. If the result is not satisfactory (more than 4 colours), change your last
choice. The algorithm is exponential, but it could be efficient in most cases.

Optimization
Solve a weaker problem (e.g. use more than 4 colours to colour a map), and modify the
solution so that it fits the constraints of the original problem.

January 2008 Advanced Algorithmics - Complexity 81


Primality Testing
- input : an integer n
- COMPOSITE : decide whether n has a non-trivial divisor d, 1 < d < n
- PRIME : decide whether n is a prime number

COMPOSITE is NP
Proof : a non-trivial divisor

2002 : PRIME is P
AKS primality test (Agrawal, Kayal and Saxena)

Therefore, COMPOSITE (the opposite of PRIME) is also P

January 2008 Advanced Algorithmics - Complexity


82
The Halting Problem
int halt(void (*f)()) ;
// returns 1 if f() terminates and 0 otherwise
void self(){
if (halt(self))
while (1)
;
}
If self() terminates, then halt(self)returns 1, and self()
loops.

If self() loops, then halt(self) returns 0, and self()


terminates.
Contradiction : halt() cannot be implemented !

January  2008   Advanced  Algorithmics  -­‐  Computability   83  


Partial resolution of the Halting Problem

int halt(void (*f)()) ;


// returns 1 if f() terminates
void self(){
if (halt(self))
while (1)
;
}
int halt(void (*f)()) {
f() ;
return 1 ;
}

No contradiction : self() loops (endless recursions).


January  2008   Advanced  Algorithmics  -­‐  Computability   84  
Effective Computability

A function F is computable if it could be implemented on


an ideal computer :
- unbounded memory
- no time limits

The only requirement is that given input x, the computer


should output F(x), whenever it is defined, in a finite
amount of time.

January  2008   Advanced  Algorithmics  -­‐  Computability   85  


Decidability and Semi-Decidability

A decidable problem is a computable decision problem.

A decision problem F is semi-decidable if the partial function

SF(x) = { true if F(x) = true


undefined otherwise

is computable.

January  2008   Advanced  Algorithmics  -­‐  Computability   86  


God's existence is "decidable"

A function is computable if there exists an algorithm to compute its values.


It does not mean that we know such an algorithm.

int f(int n) ;
// returns 1 if there is life on some other
planet
// and 0 otherwise
f is computable :
int f(int n) { int f(int n) {
return 1 ; return 0 ;
either } or }

January  2008   Advanced  Algorithmics  -­‐  Computability   87  


HALT
- input : the code of a computable function

- output : true if the execution terminates,


false otherwise

HALT is semi-decidable
but is not decidable

January  2008   Advanced  Algorithmics  -­‐  Computability   88  


A variant of HALT
int iszero(int (*f)(int),int n) ;
int self(int n){
if (iszero(self,n))
return 42 ;
else
return 0 ;
}
If self(n)=0, then iszero(self,n)returns 1, and
self(n)=42.

If self(n)≠0, then iszero(self) returns 0, and


self(n)=0.

January  2008   Advanced  Algorithmics  -­‐  Computability   89  


Rice's Theorem
int isSomething(int (*f)()) ;
// decides whether f() has some property
// for which there is an example int fExample()
// and a counterexample int fCounterExample()
int self(){
if (isSomething(self))
return fCounterExample() ;
else
return fExample() ;
}

Rice's Theorem : all problems with


- input : the code of a computable function
- output : whether the function has some non-trivial property
are undecidable.
January  2008   Advanced  Algorithmics  -­‐  Computability   90  
Unreachable Code
$ cat -n unreachable.c
1 int f(int n) {
2 if (n > 0)
3 return 42 ;
4 else {
5 while(1)
6 ;
7 }
8 return 0 ;
9 }
$ gcc -c -Wunreachable-code unreachable.c
unreachable.c: In function 'f':
unreachable.c:8: warning: will never be
executed
January  2008   Advanced  Algorithmics  -­‐  Computability   91  
UNREACHABLE
- input : the text of a C function void f()
- decide whether the last line is unreachable

If UNREACHABLE were a decidable problem,


then HALT would be decidable, by the following algorithm :

Step 1. Modify the text of the function :


- replace every return statement with goto LAST
- add a last line LAST: return;

Step 2. Compute UNREACHABLE with the modified function as


input. If the answer is true, the original function loops. If the
answer is false, the original function terminates.
But we know that HALT is undecidable.

Conclusion : UNREACHABLE is undecidable.


January  2008   Advanced  Algorithmics  -­‐  Computability   92  
Reduction of a decision problem

input x output
Problem F
F(x)
computable reduction R

input output
y
Problem G
such that if y = R(x) G(y) G(y) = F(x)
then

F reduces to G : if G is decidable, so is F
if F is undecidable, so is G
January  2008   Advanced  Algorithmics  -­‐  Computability   93  
Unreachable Code II
$ cat -n unreachable.c
1 int f(int n) {
2 if (n > 0)
3 return 42 ;
4 else {
5 while(n <= 0)
6 ;
7 }
8 return 0 ;
9 }
$ gcc -c -Wunreachable-code unreachable.c

no warning !
January  2008   Advanced  Algorithmics  -­‐  Computability   94  

You might also like