Professional Documents
Culture Documents
CERTIFICATE
SUBMITTED BY
Place: Pune
Date:
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Department Dr. Geetanjali V. Kale and Principal Dr. S. T. Gandhe for their
Deshpande Department of Computer Engineering for her guidance and help. She has
continuously motivated us throughout the seminar project and thus was of great help to
make it successfully.
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Contents
1 INTRODUCTION 1
2 MOTIVATION 2
3 LITERATURE SURVEY 3
4 A SURVEY ON PAPERS 4
4
4.1 A BLOG - How to Solve It: Mathematical Approach to Programming
8 CONCLUSION 14
9 REFERENCES 16
P:F-SMR-UG/
Time and Space Complexity Analysis of Algorithms Using Mathematics
List of Tables
1 Literature survey 4
P:F-SMR-UG/
Time and Space Complexity Analysis of Algorithms Using Mathematics
Abstract
Many Times, in some problems it happens like Algorithmic approach or dynamic
programming approach can give the correct output but it’s difficult to implement or has
some large time complexity or space complexity. In such case Mathematical approach
is hard to think, but can give correct output in less time complexity (many times O(1)).
Different Algorithms can have other mathematical approach which can be used.
Mathematical approach can give output by only using a single line formula. Various
branch of Mathematics like combinatoric can be used for replacing DP approach in an
algorithm. Here we will find which algorithm has different mathematical approach.
P:F-SMR-UG/
Time and Space Complexity Analysis of Algorithms Using Mathematics
1 INTRODUCTION
1
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
2 MOTIVATION
2
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
3 LITERATURE SURVEY
The Following table shows the literature survey by comparing techniques pro-
pose in various references:
3
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
4 A SURVEY ON PAPERS
This Blog is written by Mr. Mateusz Czerwi´nski, In this blog the author gives a
approach to think a given problem in a mathematical way. The author read a
book called “How to Solve It” written by Prof. George P´olya and highlighted
some important points. The book is based on Heuristics discussed by prof.
George P´olya. The below steps were the main highlights from the Book.
2. Form an algorithm
Find the relation between the information given and what you want to find. In
some cases, it will be much easier to solve a slightly different problem if you
can’t find the solution directly. At the end of your work, you should have made
an algorithm to solve the problem.
4
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
The Case Study is written by Judith Gal-Ezer Orna Lichtenstein. The main aim
of this paper is to show, by means of a mathematical example, how algorithmic
thinking and mathematical thinking complement each other, and how an
algorithmic approach leads to questions that deepen the understanding of
mathematical material by students.
In this paper authors told how sets of mathematics can be helpful in different
algorithms. We can do different operations on sets. The list and the description
of the operations are given below.
5
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
2. Null Set: Null sets are those in which no element is present. Null sets are also
known as Empty set or Void set. Example - A =”
3. Finite Set: Finite sets have a finite number of elements, the number of
elements does not play an important role, as long as there are a finite number of
elements present in the set, the set will be called. Example - A = ’a’,’b’,’c’.
4. Infinite Set: Infinite sets are those in which an infinite number of elements
are present, it is possible to have the infinite number of elements in the set.
Example - set A contains all-natural numbers.
Above all types of sets and all types of operations can be very useful in various
algorithms and in competitive programming. The SET data-structure in C++ or
Python is inspired by the sets in mathematics and has many functions of this set.
6
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Problem Definition
Scope
The scope of time and space complexity optimization of algorithms is broad. It
encompasses a wide range of algorithms and problem domains, including:
Challenges
Time and space complexity optimization of algorithms can be a challenging
task. There is often a trade-off between time and space complexity. For
example, an algorithm that is optimized for time complexity may use more
space, and an algorithm that is optimized for space complexity may use more
time.
Another challenge is that it is not always easy to predict the time and space
complexity of an algorithm. The complexity of an algorithm can depend on the
specific input data, the size of the input data, and the implementation of the
algorithm.
7
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
In this section, we will list all algorithms which have another mathematical
approach and will give and compare the codes with both approaches.
Sum of Series
Sum of First N natural Numbers
Brute Force:
In Brute Force or Traditional Approach for taking sum of 1st N natural numbers
we can use a FOR loop and a ANS variable in which we will add N numbers.
Python Code:
n = int(input())
ans = 0
for i in range(1,n+1): ans+=i
print(ans)
Mathematical Approach:
For Mathematical Approach we can see that the natural numbers are in AP.
Hence a Simple mathematical formula can be derived.
The formula is (n*(n+1))//2.
Python Code:
n = int(input())
ans = (n*(n+1))//2
print(ans)
Comparison:
Time Complexity of Brute Force code is O(n)
Space Complexity of Brute Force code is O(1)
Time Complexity of Mathematical approach code is O(1)
Space Complexity of Mathematical approach code is O(1)
Observation:
The Space Complexity of Both code is SAME but the time complexity of Brute
force approach is O(n) and Mathematical approach code is O(1).
8
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Combinatorial Selection
Number of ways to distribute m chocolates to n students:
9
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Brute Force:
The Brute force approach contains a dynamic programming technique in which
the states of the dp array will be number of students left and number of
chocolates left.
Python Code:
def go(x,y):
if (y⩽ 0) : return0
if (x==n):
if (y⩾ 0) :
return 1
return 0
if (dp[x][y]!=-1):
return dp[x][y]
c1=0
c2=0
c1+=go(x+1,y-1)
c2+=go(x+1,y)
ans = c1+c2
dp[x][y]=ans
return ans
n,m = map(int,input().split())
dp = [ ]
for i in range(n+1):
x=[]
for j in range(m+1):
x.append(-1)
dp.append(x)
print(go(1,m))
In above code, c1 means we give 1 chocolate to the current student and c2
means we didn’t give the chocolate to the current student.
Mathematical Approach:
In Mathematical Approach we will use a concept known as nCr, which gives the
number of ways to distribute n things into r things.
The Formula of nCr is
(n!)/(r!*(n-r)!)
Here, n! reads as n Factorial i.e, the product of all number between 1 to n.
Python Code :
n,m = map(int,input().split())
ans = nCm
ans = math.fact(n)//(math.fact(r)*math.fact(n-r))
print(ans)
Comparison:
10
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Mathematical Approach:
In mathematical approach, the main idea is to see the path as the vertical and
horizontal lines. Therefore, the total lines in grid will be n+m-2 i.e, m-1 in right
direction and n-1 in left direction. So, if we select any m-1 or n-1 lines from
n+m-2 lines then we will get the desire number of ways.
The Formula of nCr is
(n+m-2!)/((m-1)!)
11
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Equation Solver:
Find Roots a Quadratic Equation:
Brute Force:
In Brute force approach, we will check value of equation for every possible
value of x. Let quadratic equation be ax**2 + bx + c = 0, so check value of this
equation for every possible value of x.
Python Code:
a,b,c = map(int,input().split())
for i in range(-10**6,10**6):
ans = a*(i**2) + b*i c
if (ans==0):
print(i)
Mathematical Approach:
In this approach, the formula is given for roots of the equation.
Formula is
ans = (-b +-(sqrt (b**2 - 4*a*c)))//2)
Here, sqrt is the square root.
Python Code:
a,b,c = map(int,input().split())
ans1 = (-b + math.sqrt(b**2 - 4*a*c))//2
ans2 = (-b - math.sqrt(b**2 - 4*a*c))//2
print(ans1, ans2)
Comparison:
TIME COMPLEXITY of Brute Force code is O(RANGE(2*(10**6))
SPACE COMPLEXITY of Brute Force code is O(1)
TIME COMPLEXITY of Mathematical approach code is O(1)
12
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
13
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
Limitations:
Mathematical models: Mathematical models of algorithms are often
simplified and may not accurately reflect the real-world performance of
the algorithms. This can make it difficult to use mathematical tools and
techniques to optimize algorithms for real-world use cases.
NP-hard problems: Many important problems in computer science are
NP-hard, meaning that there is no known polynomial-time algorithm for
solving them. This makes it difficult to find efficient algorithms for these
problems, even using mathematical tools and techniques.
Distributed and parallel algorithms: Mathematical tools and techniques
for optimizing time and space complexity are often designed for
sequential algorithms. It can be challenging to apply these techniques to
distributed and parallel algorithms, which are becoming increasingly
important in modern computing.
Future Work:
More accurate mathematical models: Future research could focus on
developing more accurate mathematical models of algorithms. This
would allow researchers to use mathematical tools and techniques to
optimize algorithms for real-world use cases more effectively.
New algorithms for NP-hard problems: Future research could focus on
developing new algorithms for NP-hard problems that are more efficient
than existing algorithms. This would be a significant breakthrough in
computer science, as it would make it possible to solve many important
problems more efficiently.
Mathematical tools and techniques for distributed and parallel
algorithms: Future research could focus on developing mathematical
tools and techniques for optimizing the time and space complexity of
distributed and parallel algorithms. This would make it possible to design
more efficient and scalable algorithms for modern computing systems.
In addition to the above, here are some other specific areas of future work in the
research on time and space complexity optimization of algorithms using
mathematics:
Developing new mathematical techniques for analyzing the time and
space complexity of algorithms.
Applying mathematical tools and techniques to optimize algorithms
for specific problem domains, such as machine learning and artificial
intelligence.
Developing tools and techniques to help programmers automatically
optimize their code for time and space complexity.
Overall, the research on time and space complexity optimization of algorithms
using mathematics is a very active and important area of computer science.
There is a lot of potential for future work in this area, and advances in this area
could lead to significant improvements in the performance of real-world
systems.
14
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
8 CONCLUSIONS
Conclusion:
Time and space complexity optimization of algorithms is an important area of
computer science. By optimizing algorithms, we can improve the performance
of real-world systems and make them more efficient and scalable.
Mathematical tools and techniques can be used to identify and implement
efficient algorithms for solving a variety of problems. However, there are also
some limitations to the use of mathematics for algorithm optimization. For
example, mathematical models of algorithms are often simplified and may not
accurately reflect the real-world performance of the algorithms. Additionally,
many important problems in computer science are NP-hard, meaning that there
is no known polynomial-time algorithm for solving them. This makes it difficult
to find efficient algorithms for these problems, even using mathematical tools
and techniques.
Despite these limitations, mathematical optimization of algorithms is a valuable
tool for computer scientists. By developing more accurate mathematical models
of algorithms, new algorithms for NP-hard problems, and mathematical tools
and techniques for distributed and parallel algorithms, researchers can continue
to improve the efficiency and scalability of real-world systems.
Here are some specific examples of the benefits of time and space complexity
optimization of algorithms:
Improved performance of web browsers and search engines: Web
browsers and search engines use a variety of algorithms to render web
pages and search the web. By optimizing these algorithms, researchers
have been able to improve the performance of these systems significantly.
Reduced memory usage of databases and compilers: Databases and
compilers use a variety of algorithms to store and retrieve data, and to
translate high-level programming languages into machine code. By
optimizing these algorithms, researchers have been able to reduce the
memory usage of these systems significantly.
Development of new machine learning algorithms: Machine learning
algorithms are often computationally expensive and require a lot of
memory. By optimizing the time and space complexity of machine
learning algorithms, researchers have been able to develop new
algorithms that can be used to solve larger and more complex problems.
Overall, time and space complexity optimization of algorithms is an important
area of research with a wide range of potential applications. By continuing to
develop new mathematical tools and techniques for algorithm optimization,
researchers can help to improve the performance and scalability of real-world
systems in a variety of domains.
15
P:F-SMR-UG/
08/R0
Time and Space Complexity Analysis of Algorithms Using Mathematics
9 References
[1] Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. A
“Mathematical Approach to Algorithms” to study of how the
behavior of an algorithm changes as the size of its input
grows.
[2] Martin Grötschel, László Lovász, and Alexander Schrijver
“Mathematical Foundations of Algorithm Design” provides a
comprehensive overview of the mathematical foundations of
algorithm design
[3] “How to Solve It: Mathematical Approach to Programming”,
Mateusz Czerwi´nski Oct 13, 2015
[4] “A Mathematical-Algorithmic Approach to Sets: A Case
Study”, Judith Gal-Ezer1 Orna Lichtenstein2 April 1996
[5] Papadimitriou, C. H., & Steiglitz, K. (1982). "Combinatorial
Optimization: Algorithms and Complexity." Dover
Publications.
[6] E. Polak, "Computational Methods in Optimization: A
Unified Approach," Springer, 2010.
[7] Rabin, M. O. (1980). "Probabilistic Algorithms."
Communications of the ACM, 21(9), 840-852.
[8] Mitzenmacher, M., & Upfal, E. (2017). "Probability and
Computing: Randomized Algorithms and Probabilistic
Analysis." Cambridge University Press.
[9] J. Nocedal and S. J. Wright, "Numerical Optimization,"
Springer, 2006.
16
P:F-SMR-UG/
08/R0