计算学科中的经典问题

advertisement
HPM&S
计算学科中的经典问题(三)
Great Ideas in Computer Science(3)
李波 boblee@xjtu.edu.cn
weibo.com/bobbleee 13709218618
计算机教学实验中心 高效能建模与仿真研究小组
西安交通大学 2012年11月
HPM&S
1.
2.
3.
4.
5.
6.
7.
8.
9.
尺规作图-三等分角
希尔伯特的第十个问题
考拉兹猜想
王氏铺砖问题
Bare Bones 语言
Godel编码
通用图灵机
停机问题
冯.诺伊曼结构
HPM&S
Edsger Dijkstra
“我们所使用的工具深刻地影响
我们的思考习惯,从而也影响了我
们的思考能力”
不可解问题 ,算法,停机问题
HPM&S
尺规作图-三等分角
三等分角是古希腊几何尺规作图当中的名题,和化圆为方、
倍立方问题被并列为古代数学的三大难题之一。
在只用圆规及一把没有刻度的直尺将一个给定角三等分
现在已经证明,这个问题是没有办法在给定的条件之下完成的。
如果放宽限制,使用有刻度的直尺,则三等分角是可能的。
用有刻度的直尺(二刻尺)
HPM&S
希尔伯特的第十个问题
 不定方程(又称为丢番图方程)的可解性。这是希尔伯特于1900年在巴黎
的国际数学家大会演说中,所提出的23个重要数学问题的第十题。这
个问题是“对于任意多个未知数的整系数不定方程,要求给出一个可
行的方法(verfahren),使得借助于它,通过有限次运算,可以判定该方
程有无整数解”。
 在整数域求解以下方程
2x-2y=1
3x=6
7x-17y=1
 一般而言,上述的“整数域上的代数方程”定义为,P=0,其中P是系
统为整数的多项式,包含一个,两个或多个未知数。例如7x2- 5xy- 3y2
+ 2x + 4y- 11 = 0和x3 + y3 = z3。需要解决的问题是:给定方程P(x, y, ...)
= 0,如何判定方程在整数域内是否有解,如果有,如何高效找到所有
解?这类问题称为丢番图方程求解问题。
HPM&S
Verfahren- algorithm
 希尔伯特在定义第十问题时用的德语verfahren(方法),就是英文所
谓的算法 algorithm。
 对于算法的概念人们是不陌生的,例如远在古希腊时代,人们就知道
可以使用辗转相除法,求两个自然数的最大公约数。还有,任给一个
自然数,也存在着一个方法,在有限步骤内,可以判定这个数是不是
质数。
 虽然人们很早就有了算法的朴素概念,但对于到底什么是可行的计算,
仍没有精确的概念。
 一个问题的可解与不可解究竟是什么含意,当时的人们还不得而知。
 为了研究第十问题,必须给予算法精确化的观念。
 这点还有赖于数理逻辑学对可计算性理论的发展,才得以实现。
HPM&S
考拉兹猜想-Collatz conjecture
任何一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循
环,最终都能够得到1。
取一个数字如n = 6,根据上述数式,得出 6→3→10→5→16→8→4→2→1
步骤中最高的数是16,共有8个步骤
如n = 11,根据上述数式,得出
11→34→17→52→26→13→40→20→10→5→16→8→4→2→1
步骤中最高的数是52,共有14个步骤
如n = 27,根据上述数式,得出 :
27→82→41→124→62→31→94→47→142→71→214→107→322→161→484→242→121→364→182
→91→274→137→412→206→103→310→155→466→233
→700→350→175→526→263→790→395→1186→593→1780→890→445→1336→668→334→167→
502→251→754→377→1132→566→283→850→425→1276
→638→319→958→479→1438→719→2158→1079→3238→1619→4858→2429→7288→3644→1822
→911→2734→1367→4102→2051→6154→3077→9232
→4616→2308→1154→577→1732→866→433→1300→650→325→976→488→244→122→61→184
→92→46→23→70→35→106→53→160→80→40→20→10
→5→16→8→4→2→1。(步骤中最高的数是9232,共有111个步骤)
HPM&S
def collatz(n)
print n
if n.odd? and n > 1
collatz(3n + 1)
else if n.even?
collatz(n / 2)
 n小于1万,步骤中最高的数是6171
 n小于1亿,步骤中最高的数是63728127,共有949个步骤;
 n小于10亿,步骤中最高的数是670617279,共有986个步骤。
目前已经有分布式计算在进行验证。到2009年1月18日,已验证正整数到
20 × 258 = 5,764,607,523,034,234,880,也仍未有找到例外的情况。
但是这并不能够证明对于任何大小的数,这猜想都能成立。
HPM&S
考拉兹猜想-与停机问题
 在程序中使用Loop语句不当,可能导致程序不能结束(停机),例如
下面程序永远是不会终止的。
X =1; while X;
 例如考虑以下程序,请问当
n>=1,程序是否会终止。
while (n > 1)
if even(n)
n = n/2;
else
n = n *3 + 1;
 可以简单地给出几个测试用例,当n=1、2、3、4、5、6、8、10时终止
problem)是图灵巧妙构思出来的一个问题。问题
具体表示为:“你能写出一个程序吗?该程序可以测试任意一个由哥
德尔数表达的程序是否会终止”。停机问题是不可解的(Halting
problem is not solvable)。该结论可采用反证法得证。
 停机问题(Halting
HPM&S
王氏铺砖问题
HPM&S
Wang Tiles或 Wang dominoes
 王氏铺砖问题是美籍华人逻辑学家王浩(Hao Wang)在1961年提出来
的。有13种彩色地砖如图所示,铺设规则是地砖不能旋转或镜像,并
且共享边的两块地砖颜色相同。
 王氏铺砖问题是能否将一个平面用上述13种地砖按照铺设规则铺满
(Can you tile the entire plane with copies of the following?)。
 1966年 Robert Berger证明了这个算法是不存在的,故这个问题是不可
判定的。
HPM&S
通用Bare Bones语言 Statements in simple language
Increment
statement:
Decrement statement:
Loop statement:
incr X
decr X
while X
{
Actions
}
HPM&S
Power of the Simple Language
The
simple language with only three statements is as
powerful as any sophisticated language in use today,
such as C.
Here
we show how we can simulate several statements
found in some popular languages.
We
call each simulation a macro and use it in other
simulations without the need to repeat code.
HPM&S
Macros in the Simple Language
First
Macro: X  0
while X
{
decr X
}
Second Macro: X  n
X0
incr X
incr X
…
incr X
HPM&S
Macros in the Simple Language
Third
Macro:
YX
0
Y
TEMP  0
while X
{
incr
Y
decr X
incr
TEMP
}
while TEMP
{
decr TEMP
incr
}
X
HPM&S
Macros in the Simple Language
Fourth
Z
Macro: Z  X + Y
X
TEMP  Y
while TEMP
{
incr Z
decr TEMP
}
HPM&S
Macros in the Simple Language
Fifth Macro: Z  X * Y
Z
0
TEMP  Y
while TEMP
{
Z Z +X
decr TEMP
}
HPM&S
Macros in the Simple Language
Sixth
Macro:
Z
Z  X ** Y
1
TEMP  Y
while TEMP
{
ZZ*X
decr TEMP
}
HPM&S
Macros in the Simple Language
Seventh
Macro:
comp ( X )
complements
TEMP  1
while X
{
If
the value of X is 0, change
it to 1.
If
X0
TEMP  0
it is not 0, change it to 0.
}
while TEMP
{
incr X
decr TEMP
}
HPM&S
Macros in the Simple Language
Eighth
Macro:
if X then A1 else A2
TEMP  X
while TEMP
{
A1
TEMP  0
}
TEMP  X
comp (TEMP)
while TEMP
{
A2
TEMP  0
}
HPM&S
Simulation of Simple Language
Write
programs (create transition tables) that
implement the statements of the Simple Language.
Increment
statement
Decrement
statement
Loop
statement
Figure 17-4
HPM&S
Transition state
Four
states : A, B, C, D.
HPM&S
Increment statement
Transition diagram for incr X
HPM&S
Transitional table for incr X statement
Current
State
--------StartIncr
Forward
Forward
Added
Backward
Backward
Read
Write
Move
------------#
1
&
any
not #
#
---------------#
1
1
&
same as read
#
-------




New
State
---------Forward
Forward
Added
Backward
Backward
StopIncr
HPM&S
Steps in incr X statement
HPM&S
Decrement statement
Transition diagram for decr X
HPM&S
Transitional table for decr X statement
Current
State
--------StartDecr
Forward
Forward
Delete
Backward
Backward
Read
Write
Move
------------#
1
&
1
not #
#
---------------#
1
blank
&
same as read
#
-------




New
State
---------Forward
Forward
Delete
Backward
Backward
StopDecr
HPM&S
Transition diagram
for the loop statement
HPM&S
Transitional table for the loop statement
Current
State
--------StartLoop
Check
Check
Forward
Forward
…
…
EndProcess
Backward
Backward
Read
Write
Move
------------#
not 1
1
not &
&
…
…
any
not #
#
------------#
same as read
1
same as read
&
…
…
same as read
same as read
#
-------



none
…
…


none
New
State
--------Check
StopLoop
Forward
Forward
StartProcess
…
…
Backward
Backward
Check
HPM&S
Conclusion
Any
problem that can be solved by the Simple
Language can also be solved by the Turing machine.
Is
there a problem solvable by the Turing machine that
is not solvable by the Simple Language? We can not
prove it now.
Church
thesis: the Simple Language and the Turing
machine are equivalent. We believe it.
HPM&S
Godel numbers
In
theoretical computer science, an unsigned
number is assigned to every program that can
be written in a specific language.
Programs
can be used as a single data item as input
to another program.
Programs
can be referred to by just their integer
representations.
The
numbering can be used to prove that some
problems can not be solved by a computer.
HPM&S
Code for symbols used in the Simple Language
Symbol
--------1
2
3
4
5
6
7
8
Hex Code
------------1
2
3
4
5
6
7
8
Symbol
--------9
incr
decr
while
{
}
x
Hex Code
------------9
A
B
C
D
E
F
HPM&S
Representing a program
Example
1
What is the Godel number for the
program incr X?
Solution
incr
X
A
F
175 (in decimal)
HPM&S
Interpreting a number
Example
1
Interpret 3058 as a program.
Solution
3058
B
F
2
decr X
2
HPM&S
Universal Turing machine -Wikipedia
 In
computer science, a universal Turing machine (UTM) is a
Turing machine that can simulate an arbitrary Turing machine on
arbitrary input.
 The
universal machine essentially achieves this by reading both
the description of the machine to be simulated as well as the
input thereof from its own tape.
 Alan
Turing introduced this machine in 1936–1937. This model is
considered by some (for example, Martin Davis (2000)) to be the
origin of the stored program computer—used by John von
Neumann (1946) for the "Electronic Computing Instrument" that
now bears von Neumann's name: the von Neumann architecture.
It is also known as universal computing machine, universal machine
(UM), machine U, U.
HPM&S
Halting problem
In
programs, a
terminate (halt).
For
repetition
construct
may
never
example, the following program never terminates.
X1
while X
{
}
HPM&S
Halting problem
A Classical
Programming Question:
Can you write a program that tests
whether or not any program,
represented by its Godel number,
will terminate? No.
HPM&S
Halting problem is not solvable
Proof
by contradiction (矛盾証法)
Step 1: Assume that a program, called Test, exists.
HPM&S
Step 2: Create another program called Strange that
is made of two parts:
a
copy of Test at the beginning
an empty loop at the end
HPM&S
Step3:Now having made the program Strange, we test
this program with itself as input.
If we assume that Test exists, we
have the following contradictions:
停机问题本质是一阶逻辑的不自恰性和
不完备性。
类似的命题有理发师悖论、全能悖论等。
This proves that the Test program
cannot exist.
The halting problem is unsolvable.
HPM&S
Complexity of solvable problems
One
way to measure the complexity of a solvable
problem is to find the number of operations executed
by the computer when it runs the program.
Big-O notation
For example:
n : the number of input data
Polynomial
O(nk),…
problems: O(1), O(logn), O(n), O(nlogn), O(n2),…,
Non-polynomial
problems: O(2n), O(n!),…
HPM&S
Key terms
Controller
Decrement
Godel
Non-polynomial
statement
Increment
Loop
macro
Polynomial
number
Hexadecimal
problem
digit
statement
statement
Solvable
problem
problem
Tape
Turing
machine
Unsolvable
problem
HPM&S
冯·诺依曼体系结构
几十年来,计算机的制造技术都是基于科学家冯·诺依曼
1946年提出的“存储程序”概念(1946年发表论文“电子计算机
装置逻辑结构初探”)。这样的计算机称为冯·诺依曼体系结构
计算机。
冯·诺依曼体系结构的思想可以概括为以下几点:
(1)由运算器、存储器、控制器、输入设备和输出设备等五
大基本部分组成计算机系统,并规定了这五部分的基本功能。
(2)计算机内部采用二进制来表示数据和指令。
(3)将程序数据存入内部存储器中,计算机在
工作时可以自动逐条取出指令并加以执行。
HPM&S
计算机的基本工作原理为存储程序和执行
指令。示意如下:
输入设备
存储器
取存
数数
据据
输出设备
控制指令
运
控
控制指令
算
制
器
器
CPU
HPM&S
讨论-冯.诺伊曼的伟大之处
认识
基于通用图灵机建造的计算机都是在存储器中存储数据。
在1944-1945年期间,冯.诺伊曼指出:鉴于程序和数据
在逻辑上是相同的,因此程序也能存储在计算机的存储
器中。
工程
Data as code, Code as data
简化:指令的顺序执行。指令一条接一条按顺序执行。
即使跳转指令可以请求控制器跳转之前或之后的指令,
这并不意味着指令没有按照顺序执行。
HPM&S
谢谢,请批评
祝您 一切顺利
Download