The algorithm is based on the following observation: , where i,j, and k are vertices and d(i,j) is the distance between i and j in the graph. Thus, by starting with a known set of edges, we can incrementally improve the shortest paths between any edges.
The pseudocode below assumes an input graph of N vertices. d(i,j) is if there is no edge from i to j, and the length of the appropriate edge otherwise.
for i = 1 to N for j = 1 to N dist[i][j] = d(i,j) for k = 1 to N for i = 1 to N for j = 1 to N dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])This will give the shortest distances between any two nodes, from which shortest paths may be constructed.
The algorithm takes O(N3) time and O(N2) space, and has the distinct advantage of hiding a small constant in its behavior, since very little work is done in the innermost loop.
The concept of the Turing machine is based on the idea of a person executing a well-defined procedure by changing the contents of an infinite number of ordered paper sheets that can contain one of a finite set of symbols. The person needs to remember one of a finite set of states and the procedure is formulated in very basic steps in the form of "If your state is 42 and the symbol you see is a '0' then replace this with a '1', remember the state 17, and go to the following sheet.".
More formally, a (one-tape) Turing machine is usually defined as a 6-tuple M = (Q,G,s,b,F,d)
The following Turing machine has an alphabet {'0', '1'}, with 0 being the blank symbol. It expects a series of 1s on the tape, with the head initially on the leftmost 1, and doubles the 1s with a 0 in between, i.e., "111" becomes "1110111". The set of states is {s1, s2, s3, s4, s5} and the start state is s1. The action table is as follows.
Old Read Wr. New Old Read Wr. New St. Sym. Sym. Mv. St. St. Sym. Sym. Mv. St. - - - - - - - - - - - - - - - - - - - - - - - - s1 1 -> 0 R s2 s4 1 -> 1 L s4 s2 1 -> 1 R s2 s4 0 -> 0 L s5 s2 0 -> 0 R s3 s5 1 -> 1 L s5 s3 0 -> 1 L s4 s5 0 -> 1 R s1 s3 1 -> 1 R s3
Step State Tape Step State Tape - - - - - - - - - - - - - - - - - 1 s1 11 9 s2 1001 2 s2 01 10 s3 1001 3 s2 010 11 s3 10010 4 s3 0100 12 s4 10011 5 s4 0101 13 s4 10011 6 s5 0101 14 s5 10011 7 s5 0101 15 s1 11011 8 s1 1101 -- halt --
A universal Turing machine is Turing-complete. It can calculate any recursive function, decide any recursive language, and accept any recursively enumerable language. According to the Church-Turing thesis, the problems solvable by a universal Turing machine are exactly those problems solvable by an algorithm or an effective method of computation, for any reasonable definition of those terms.
It's often said that Turing machines, unlike simpler automata, are as powerful as real machines, and are able to execute any operation that a real program can
Turing machines would actually only be equivalent to a machine that had an unlimited amount of storage space. We might ask, then, why Turing machines are useful models of real computers. There are a number of ways to answer this: