Introduction(Updated)
Introduction(Updated)
Algorithm
Welcome to CS202
In this course we will look at the theory behind data structures and
algorithms. It will also give us a glimpse into the theory of
computers and computer science.
In this current day and age, every established university in the world
has people who work on quantum computers, even though most
people do not have access to such types of computers.
Similarly roughly 50 years back, most scientists did not have access
to personal laptops or computers. They also studied the capabilities
of computers based on some basic principles regarding their working.
Once you are assured about the correctness of an algorithm, you are
required to focus on the efficiency of the problem.
This is not the right way to go about algorithm design. One to first
be absolutely sure that logic of the code never fails and that there
are no black swan events. The task of ascertaining the correctness
of an algorithm is usually easier compared to the task of finding an
efficient algorithm.
Time
Mind Set RAM model of Computation
Algorithm Correctness
Efficiency
For example:- let’s consider the basic task of finding two factors of
an integer x with n digits. Please note that n denotes the size of
the input and not the value of the input.
Suppose, if the input integer is 10763, then its value is 10763, but
it’s size in decimal is 5 integers. By n, we are referring to 5 and not
10763.
So now we are sure that our algorithm always gives the factors of
the input if it is a composite number otherwise it outputs that the
input was a prime number.
Time
Mind Set RAM model of Computation
Algorithm Correctness
Efficiency
One might even go one step further and check only primes in the
√
range of 2 to x if they divide the input. But then we have to
encounter a whole new problem of trying to find prime numbers,
which make the matters more complicated so we will ignore this
tweak for the time being.
Right now we are happy with the fact that we have a procedure to
find a factor of an input of n digits if it is a composite or declare
that the integer given as the input is prime.
But how long will it take for our procedure to run and give the
output?
Time
Mind Set RAM model of Computation
Algorithm Correctness
Efficiency
Lets study the problem of finding the GCD of two integers x and y .
As students in the primary school, we were taught to factorize both
the integers x and y and then list out the common prime factor
while noting their multiplicity. As we noticed earlier, this is probably
not the efficient way to find GCD because we do not know how to
factorize an integer efficiently.
Time
Mind Set RAM model of Computation
Algorithm Correctness
Efficiency
For example
If we are trying to find GCD(1201, 88) then the next step would
be GCD(88, 1201 mod 88)= GCD(88, 57) which reduces the
number of digits in the input by 1.
Time
Mind Set RAM model of Computation
Algorithm Correctness
Efficiency
Suppose x and y have α digits each and their leading digits are
x` and y` respectively.
The goal is the figure out the number of steps needed to ensure
that one of the operands loses at least one digit.
1. If x` = y` = 1, then one step is enough and x mod y would
have less than α digits
2. If x` > 1 and y` = 1, then in one step,
I Either x mod y has less than α digits.
I Or x mod y also has α digits, but that would imply that the
leading digit on (x mod y ) is also 1. So in the next step, we
encounter Case 1.
...
In this way, one can analyze for all possible pairs of leading digits of
x and y , and figure out the number of steps needed to reduce the
number of digits in one of the operands.
Time
Mind Set RAM model of Computation
Algorithm Correctness
Efficiency
After checking for all the possible pairs of leading digits, one would
come to the conclusion that at most 5 steps are needed. For sake of
brevity, I have not included that analysis here.
Extending this logic, we can see that if the x and y had n digits in
total, then after at most 5n iterations of the GCD procedure we will
be able to find the GCD for two input integers and the algorithm
will terminate.
If the total size of the input is n digits, then we can find the GCD in
5n clock cycles. Here we can say that the algorithm is efficient
because it takes a linear number of clock cycles w.r.t the size of the
input to solve the problem.
Whether this algorithm is the best algorithm or not is a topic future
discussion, but this is an efficient algorithm.
Time
Mind Set RAM model of Computation
Algorithm Correctness
Efficiency