Sherlock and The Beast
All topics
Div Mod
The modulo operation is one of the most primitive things along with arithmetic operations on integers.
or is the remainder when is divided by .
if it means m divides a such that for some multiple k,
Some of the properties are
This operation is very useful when computation involves very large numbers and to check correctness we usually perform computation under modulo operation, hence keeping variables in standard integer size limits.
Modulo operation is also useful useful in the following:
- Chinese Remainder Theorm
- Fast Modulo exponentiation
- Inverse modulo operation
Greedy Technique
A greedy algorithm is an algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum.
A very common problem which gives good insight into this is the Job Scheduling Problem.
You have jobs numbered and you have the start times and the end times for the job. Which jobs should you choose such that you get the maximal set of non-overlapping jobs?
The correct solution to this problem is to sort all the jobs on the basis of their end times and then keep on selecting the job with the minimal index in this list which does not overlap with currently selected jobs.
Sounds intuitive, but why does it work?
Well, since each job has equal weight, selecting the one which makes way for newer jobs sooner is optimal. Although a more formal argument can be made for a rigorous proof, the intuition behind it is similar.
Now, let's consider another problem. You again have jobs. Each job can be completed at any time and takes time to complete and has a point value of . But with each second, the point value of the job decreases by . If you have to complete all the jobs, what is the maximum points that you can get?
The problem basically asks us for an order of accomplishing the jobs.
Here, doing the job with higher first makes sense. At the same time, doing the job with lower also sounds good. So how do we decide?
Assume you have just jobs which, without loss of generality, can be numbered as and .
Now, if you do job before job , your net score is:
Otherwise,
If then:
Notice that this argument can be applied to jobs as a sorting rule. The job with maximum value should be done first and so on.
This gives us the optimal ordering and is also in line with our intuition.
Greedy doesn't always work
Greedy solutions are usually good whenever they exist, because they are usually easy to implement and usually have a fast running time. However, greedy algorithms don't always work! By this, we don't mean that the greedy algorithm fails to return the correct answer on all inputs. Instead, we mean that the algorithm fails on at least one input.
For example, consider the following problem: You again have jobs, and the job takes time to complete and has a point value of . This time, the point values do not decrease over time, and you don't have to finish all jobs. Unfortunately you only have a total of time to spend. What is the maximum points you can get?
One greedy algorithm that comes to mind is the following: while there is still time remaining, take the job with the largest point value that can be finished within the remaining time. Intuitively, this can be seen to work in some cases. However, this fails in the following set of jobs:
Assuming , the greedy algorithm mentioned above first takes job , then job , for a total of points. However, this is not the global optimum, because you can take jobs and for a total of points, which is much higher.
The next greedy algorithm we can try is to always take the job which takes the shortest amount of time to finish. However, this also fails the set of jobs above (where you only get points).
You can probably try crafting a more sophisticated greedy algorithm than the ones we described, but it probably won't work, i.e. it will probably fail on some input. This is because the problem we described to you is equivalent to the knapsack problem which currently has no known efficient algorithm!