1
TIME COMPLEXITY
2
Pages 247 - 256
3
Introduction
• Even when a problem:- is decidable and- computationally solvable
• it may not be solvable in practice if the solution requires an inordinate amount of time or memory.
4
Objectives
• investigation of the:- time, - memory, or - Other resources required for solving
computational problems.
• to present the basics of time complexity theory.
5
Objectives (cont.)
• First - introduce a way of measuring the time used to solve a problem.
• Then - show how to classify problems according to the amount of time required.
• After - discuss the possibility that certain decidable problems require enormous amounts of time and how to determine when you are faced with such a problem.
6
MEASURING COMPLEXITY
The language
7
MEASURING COMPLEXITY (cont.)
• How much time does a single-tape Turing machine need to decide A?
8
• The number of steps that an algorithm uses on a particular input may depend on several parameters: (if the input is a graph)
- the number of steps may depend on :- the number of nodes, - the number of edges, and - the maximum degree of the graph, or - some combination of these and/or other
factors.
MEASURING COMPLEXITY (cont.)
9
Analysis
• worst-case analysis - consider the longest running time of all inputs of a particular length.
• average-case analysis - consider the average of all the running times of inputs of a particular length.
10
11
BIG-O AND SMALL-O NOTATION
• Exact running time of an algorithm often is a complex expression. (estimation)
• Asymptotic analysis - seek to understand the running time of the algorithm when it is run on large inputs.
The asymptotic notation or big-O notation fordescribing this relationship is
12
Self study
• EXAMPLE 7.3
• EXAMPLE 7.4
13
BIG-O AND SMALL-O NOTATION (cont.)
• Performing this scan uses n steps. • Typically use n to represent the length of the
input. • Repositioning the head at the left-hand end of
the tape uses another n steps. • The total used in this stage is 2n steps.
In stage 1
14
BIG-O AND SMALL-O NOTATION (cont.)
In stage 4 the machine makes a single scan to decide whether to accept or reject. The time taken in this stage is at most O(n).
15
• Thus the total time of M1 on an input of length n is
O(n) + O(n2 ) + O(n) or O(n2 ).
In other words, it's running time is O(n2 ), which completes the time analysis of this machine.
BIG-O AND SMALL-O NOTATION (cont.)
16
17
Is there a machine that decides A asymptotically more quickly?
18
Executed Time
• Stages 1 and 5 are executed once, taking a total of O(n) time.
• Stage 4 crosses off at least half the 0s and 1s is each time it is executed, so at most 1+log2 n.
• the total time of stages 2, 3, and 4 is (1 + log2 n)O(n), or O(n log n).
• The running time of M2 is O(n) + O (n logn) = O(n log n).
19
COMPLEXITY RELATIONSHIPS AMONG MODELS
• We consider three models: - the single-tape Turing machine;- the multi-tape Turing machine; and - the nondeterministic Turing machine
20
• convert any multi-tape TM into a single-tape TM that simulates it.
• Analyze that simulation to determine how much additional time it requires.
• simulating each step of the multi-tape machine uses at most O(t(n)) steps on the single-tape machine.
• the total time used is O(t2 (n)) steps.O(n) + O(t2 (n)) running time O(t2 (n))
COMPLEXITY RELATIONSHIPS AMONG MODELS (cont.)
21
SPACE COMPLEXITY I N T R A C T A B I L I T Y
22
Pages 303 – 308
23
Objective
• Consider the complexity of computational problems in terms of the amount of space, or memory, that they require.
• Time and space are two of the most important considerations when we seek practical solutions to many computational problems.
• Space complexity shares many of the features of time complexity and serves as a further way of classifying problems according to their computational difficulty.
24
Introduction
• select a model for measuring the space used by an algorithm.
• Turing machines are mathematically simple and close enough to real computers to give meaningful results.
25
26
Estimation the space complexity
• We typically estimate the space complexity of Turing machines by using asymptotic notation.
• EXAMPLE 8.3
27
EXAMPLE 8.4
28
SAVITCH'S THEOREM read only
29
Pages 335 - 338
I N T R A C T A B I L I T Y
30
• Certain computational problems are solvable in principle, but the solutions require so much time or space that they can't be used in practice. Such problems are called intractable.
• Turing machines should be able to decide more languages in time n3 than they can in time n2. The hierarchy theorems prove that .