The document discusses Temporal and Spatial Complexity in algorithms, explaining how time and space requirements affect algorithm design. It introduces concepts like Asymptotic Behavior and Big O Notation, which are used to describe the efficiency of algorithms in terms of their runtime and space usage. Real-world examples illustrate the trade-offs between time efficiency and space efficiency in algorithm design.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views
Lecture 10 Spatial and Temporal Complexity
The document discusses Temporal and Spatial Complexity in algorithms, explaining how time and space requirements affect algorithm design. It introduces concepts like Asymptotic Behavior and Big O Notation, which are used to describe the efficiency of algorithms in terms of their runtime and space usage. Real-world examples illustrate the trade-offs between time efficiency and space efficiency in algorithm design.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24
ICT 143-2 Data Structures and
Algorithms
Mrs. W.B.M.S.C. Wijayakoon
Lecturer(Probationary) Department of Information and Communication Technology Faculty of Technological Studies Uva Wellassa University Lecture 10 – Temporal and Spatial Complexity Temporal and Spatial Complexity Complexity Theory •A function can be computed by a Machine in the next hour, day, year, millennium, or, indeed, before the heat-death of the Universe. Very roughly, this is the problem of Temporal Complexity. •The theory of Temporal Complexity asks a very simple question: how much time does a Machine require? •Spatial Complexity involves the amount of space a Machine requires to compute its outputs from input.
DATA STRUCTURES AND ALGORITHMS 2
Time Complexity and Space Complexity Designing algorithms is about making decisions and decisions have tradeoffs. •in the shortest time •using the least amount of space The complexity can be break down to •Time Complexity -> How long does the algorithm take •Space Complexity -> How much space does the algorithm use
DATA STRUCTURES AND ALGORITHMS 3
Real world example An example of an algorithm that may be more time efficient: Time Efficient Grocery Shopping • Drive a large car to the grocery store. • Buy everything that you need. This algorithm saves time but requires you to have a large car. This means lower time complexity and higher space complexity An example of an algorithm that may be more space efficient: Space Efficient Grocery Shopping • Walk to the grocery store with a tote bag. • Buy only one item. • Walk home and drop off the item. • Repeat until done. This algorithm takes a long time but does not require a car. This means higher time complexity and lower space complexity.
DATA STRUCTURES AND ALGORITHMS 4
What is Asymptotic and Unbounded Behaviour? •Asymptote: a straight line that continually approaches a given curve but does not meet it at any finite distance. •Asymptotic behavior describes a function or expression with a defined limit or asymptote. Your function may approach this limit, getting closer and closer as you change the function's input, but will never reach it. •Unbounded behavior is when you have a function or expression without any limits. As you increase your function's input, the output also increases
DATA STRUCTURES AND ALGORITHMS 5
Asymptotic and Unbounded Behaviour- E.g., Example for Asymptotic expression is division, such as 1/x. No matter how big x gets, you will never quite make it to having exactly nothing. Cutting a piece of Pizza Unbounded behavior is when you increase your function's input, the output also increases. A good example of this is multiplication, or 1*x. If you have 5 ovens, you can cook 5 pizzas at once; if you had an infinite number of ovens, you could cook an infinite number of pizzas.
DATA STRUCTURES AND ALGORITHMS 6
Asymptotic and Unbounded Behaviour- E.g., •f(x) = 1/x •Here, if you plug in infinity for x, you get 1 over a really huge number (infinity). Now, this doesn’t mean that 1/infinity=0, but we can say that there's an asymptote at 0, meaning the values of •f(x) get REALLY close to 0, but never actually touch 0. This is an asymptote. A function is unbounded when the limit of the function tends to infinity or negative infinity. A function that fluctuates is also unbounded. •Many functions have interesting behavior as their input grows to infinity, or as they themselves grow without a bound as they approach a certain value. •While infinity, or numbers growing without end, seems hard to study, the concepts of limits and asymptotes enables to describe the behavior of functions as they approach the unreachable infinity.
DATA STRUCTURES AND ALGORITHMS 7
What is the Big O Notation? •Big O Notation is the language we use to describe the complexity of an algorithm. •In other words, Big O Notation is the language we use for talking about how long an algorithm takes to run. •It is how we compare the efficiency of different approaches to a problem. •With Big O Notation we express the runtime in terms of — how quickly it grows relative to the input, as the input gets larger.
DATA STRUCTURES AND ALGORITHMS 8
What is the Big O Notation? Let’s break down the last line: •how quickly the runtime grows — It is difficult to determine the exact runtime of an algorithm. It depends on the speed of the computer processor. So instead of talking about the runtime directly, we use Big O Notation to talk about how quickly the runtime grows. •relative to the input — With Big O Notation, we use the size of the input, which we call “n”. So we can say things like the runtime grows “on the order of the size of the input” (O(n)) or “on the order of the square of the size of the input” (O(n²)). •as the input gets larger — Our algorithm may have steps that seem expensive when n is small but are eclipsed eventually by other steps as n gets larger. For Big O Notation analysis, we care more about the stuff that grows fastest as the input grows, because everything else is quickly eclipsed as n gets very large.
DATA STRUCTURES AND ALGORITHMS 9
Asymptotic Analysis •Ignoring constants in T(n) •Analyzing T(n) as n "gets large“
Example: T (n)=13n3+42n2+2nlog n+4 n
As n grows larger, n3 is MUCH larger than n2 , n log n , and n , so it dominates T (n)
The running time grows roughly on the order of
T (n) = O(n3 ) The big-oh (O) Notation
DATA STRUCTURES AND ALGORITHMS 10
Asymptotic Analysis •The O symbol was introduced in 1927 to indicate relative growth of two functions based on asymptotic behavior of the functions now used to classify functions and families of functions •The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. For example, consider the case of Insertion Sort. It takes linear time in best case and quadratic time in worst case. We can safely say that the time complexity of Insertion sort is O(n^2). Note that O(n^2) also covers linear time. T(n) = O(f(n)) if there are constants c and n0 such that T(n) < c*f(n) when n ≥ n0
DATA STRUCTURES AND ALGORITHMS 11
Three Major Notations •Ο(g(n)), Big-Oh of g of n, the Asymptotic Upper Bound. •Ω(g(n)), Big-Omega of g of n, the Asymptotic Lower Bound. •Θ(g(n)), Big-Theta of g of n, the Asymptotic Tight Bound.
DATA STRUCTURES AND ALGORITHMS 12
Can we justify Big O notation? Big O notation is a huge simplification; can we justify it? It only makes sense for large problem sizes For sufficiently large problem sizes, the highest-order term swamps all the rest! Consider R = x2 + 3x + 5 as x varies: x=0 x2 = 0 3x = 10 5=5 R=5 x = 10 x2 = 100 3x = 30 5=5 R = 135 x = 100 x2 = 10000 3x = 300 5=5 R = 10,305 x = 1000 x2 = 1000000 3x = 3000 5=5 R = 1,003,005 x = 10,000 R = 100,030,005 x = 100,000 R = 10,000,300,005
DATA STRUCTURES AND ALGORITHMS 13
Different Big O Curves
DATA STRUCTURES AND ALGORITHMS 14
Rules for Calculating Big-Oh Base of Logs ignored logan = O(logbn) Power inside logs ignored log(n2) = O(log n) Base and powers in exponents not ignored 3n is not O(2n) a(n )2 is not O(an) If T(x) is a polynomial of degree n, then T(x) = O(xn)
DATA STRUCTURES AND ALGORITHMS 15
Some Examples of Big O Notation This function runs in O(1) time (or “constant time”) relative to its input. This means that the input array could be 1 item or 1,000 items, but this function would still just require one “step.”
DATA STRUCTURES AND ALGORITHMS 16
Some Examples of Big O Notation This function runs in O(n) time (or “linear time”), where n is the number of items in the array. This means that if the array has 10 items, I have to print 10 times. If it has 1,000 items, I have to print 1,000 times.
DATA STRUCTURES AND ALGORITHMS 17
Some Examples of Big O Notation Both of these functions have O(n) runtime, even though one takes an integer as its input and the other takes an array:
DATA STRUCTURES AND ALGORITHMS 18
Some Examples of Big O Notation In this example, I might have 100 items in the haystack, but the first item might be the needle, in which case I would return in just 1 iteration of the loop. I can say this is O(n) runtime and the worst- case scenario would be implied. But to be more specific I could say this is worst case O(n) and best-case O(1) runtime.
DATA STRUCTURES AND ALGORITHMS 19
Some Examples of Big O Notation When you have two nested for loops O(n2) Binary tree search O(log n)
DATA STRUCTURES AND ALGORITHMS 20
Some Examples of Big O Notation – Space Complexity This function takes O(1) space (not allocating any new variables)
DATA STRUCTURES AND ALGORITHMS 21
Some Examples of Big O Notation – Space Complexity This function takes O(n) space (the size of helloArray scales with the size of the input):