140 likes | 304 Views
Algorithm. Informally speaking, an algorithm is a collection of simple instructions for carrying out a task. Example: Elementary arithmetic algorithms for adding and multplying etc. Algorithms for computing students’ grades. Algortihms for classes scheduling.
E N D
Algorithm • Informally speaking, an algorithm is a collection of simple instructions for carrying out a task. • Example: • Elementary arithmetic algorithms for adding and multplying etc. • Algorithms for computing students’ grades. • Algortihms for classes scheduling. We need a strict mathematical definition of an algorithm. The above “definition” obviously does not qualify.
Algorithms should deal with strings The structure of an algorithm: Input(s) Output or Input(s) Yes (accept) or No (reject) The attempt to formalize the notion of algorithm should start from strictly specifying what kind of objects can be inputs/outputs. These are going to be only. strings No loss of generality: non-string objects can be easily represented with strings. A tuple of such objects can also be represented with a (one single) string. After all, computers exclusively deal with strings --- strings over the alphabet {0,1} !
Encoding Encoding--- representing objects as strings in some standard way. There are usually many ways to encode; as one encoding can be easily (algorithmically) converted into another, it does not matter which encoding we choose. Encoding numbers:0,1,2,3,…,34,…, or 0,1,10,11,…,100010,… Encoding pairs of numbers:(4,2), (1,3), …, or 4#2, 1#3,…, or aaaabb, abbb, … Encoding people:“David Stewart”, “Dongfei Wei”, …, or “Stewart, David”, “Wei, Dungfei”, or 315-21-5541, 326-11-1521… If O is an object, <O> will stand for a (the) encoding of O.
Encoding a graph • Consider the graph in figure 1 4 • The encoding <G> of a graph as a string is a list of nodes followed by a list of edges. • Each node is a decimal number, and each edge is a pair of decimal numbers that represent the nodes that edge connects. • Example encoding: the graph in Figure 1 is encoded by the string: • <G> = (1, 2, 3, 4) ((1, 2), (2, 3), (3, 1), (1, 4)) 1 2 3 Figure 1 a connected graph
Algorithmically solvable problems vs language recognition • Thus, • Every problem for which we are trying to design a yes/no algorithm • is of the type “Is string w in language L?”, --- the sort of problems • solved by TMs. • E.g., the problem of graph connectedness is the problem • w{u | u is the encoding of a graph G and G is connected}? • Every problem for which we are trying to design an algorithm that • produces an output is of the type “What string is the value of function • f for string w?”, --- the sort of problems solved by TMOs. • E.g., the problem of adding two numbers is the problem • f(w)=? • where f is the function defined by f(<a,b>)=<a+b>
The Church-Turing thesis The Church-Turing thesis: (the intuitive notion of) Algorithm = Turing machine It can never been mathematically proven! Importance: Without the Church-Turing thesis, you can never prove that a certain problem has no algorithmic solution. Example - Hilbert’s 10th problem (see page 142).
A turning point • We continue to speak of Turing machines, but our real focus is on algorithms. • Question: what is the right level of detail to give when describing a Turing machine algorithm? • The three possibilities are: • Formal description: Spell out in full all 7 components of a Turing machine. This is the lowest, most detailed level of description. • Implementation description: Use English prose to describe the way Turing machine moves its head and the way it stores data on its tape. No details of state transitions are given. • High-level description: Use English prose to describe the algorithm, ignoring the implementation model. No need to mention how machine manages its head and tape.
Encoding an object • Here we set up a format and notation for describing Turing machine while approaching decidability or computability theory. • Definition: An algorithm is a TM in the standard representation: • The input to a Turing machine is always a string ; • If we want an object, other than a string as input, we must first represent that object as a string. • If we have several objects O1, O2, …, Ok, we denote their encoding into a string by <O1, O2, …, Ok>.
Description of a TM • A Turing machine algorithm is described with an indented segment of text in quotes. • The algorithm is broken into stages, each stage involving many individual steps of computation. • The block structure of the algorithm is indicated by further indentation • The first line of the algorithm describes the input which is a string w • If the input is the encoding of an object such as <O> the Turing machine first implicitly test whether the input properly encodes O and rejects if it does not. • Example . A = {<G> | G is a connected undirected graph} • A TM deciding A is: • M = “on input <G>, the encoding of a graph G: • Select the first node of G and mark it; • Repeat the following stage until no new nodes are marked: • For each node in G, mark it if it is attached by an edge to a node that is already marked; • Scan all the nodes of G to determine whether they all are marked. If they are, accept; otherwise reject.”
M computes on G 1. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 2. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 3. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 4. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 4 1 2 3
M computes on G 1. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 2. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 4 1 2 3
M computes on G 1. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 2. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 3. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 4 1 2 3
M computes on G 1. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 2. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 3. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 4 1 0. (1,2,3,4)((1,2),(1,3),(1,4),(2,3)) 2 3 Accept
M computes on G 1. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 2. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 3. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 4. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 5. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 6. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 7. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 8. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 0. (1,2,3,4,5,6)((1,2),(1,3),(1,4),(2,3),(5,6)) 4 1 2 3 5 6 Reject