java- Big O Notations - algorithm

So, I have tried to solve some big o questions and I had some troubles with some of them. I don't quite understand them.
like for eg the dominant term of 10MlogM + (N/2) log (N/2) + N/4
and M log (N) + M log (M ). I have trouble understanding big o expressions with 'log' in them. Any help would be appreciated and thanks in advance.

I have trouble understanding big o expressions with 'log' in them
It's quite easy to understand Big-O of log (or any function) if we draw its graph:
Now since you are facing an equation having multiple terms (and one of them is log), so maybe it would confuse you out. If we plot first term (I assume its NlogN and discard the constant (10)):
Similarly for other term (N/2 * log(N/2)):
I am sure you will already know, but this graph easily demonstrates that only major term (N logN) defines the Big O (as its just to show the upper bound of function) as I plot the graph of whole function:
What does N log N mean
This link provides very good explanation with this example:
O(n log n): There was a mix-up at the printer's office, and our phone book had all its pages inserted in a random order. Fix the ordering so that it's correct by looking at the first name on each page and then putting that page in the appropriate spot in a new, empty phone book.
For the below examples, we're now at the printer's office. Phone books
are waiting to be mailed to each resident or business, and there's a
sticker on each phone book identifying where it should be mailed to.
Every person or business gets one phone book.
O(n log n): We want to personalize the phone book, so we're going to
find each person or business's name in their designated copy, then
circle their name in the book and write a short thank-you note for
their patronage.

Related

Big-o complexity problem - Linear cumulative power

Background
I am trying to work through some problems I found from Stanfords "Design & Analysis of Algorithms" course from 2013. In particular, problem 3 from problem set 1 here.
In summary it states:
You are stuck on a desert island with a radio that can transmit a distress signal at integer power levels (i.e. 1W, 2W, 3W, etc.).
If you transmit a signal with high enough power, you will receive a response and be rescued.
Unfortunately you do not know how much power, n, is needed.
The problem requests you design an algorithm that uses Θ(n)W total power.
Being a 5pt question from problem set 1, I presume this is easier than I am finding it.
My question is
...what is this algorithm?....or how can I change my thinking to find one?
Where I'm stuck
The question states that the strategy "just increase power by 1 watt each time" will result in an Θ(n^2)W total power. Indeed, this is true, as the total power used by any n is n * (n+1) / 2.
However, I can't think of any strategy that isn't:
greater than linear; or
a strategy where I cheat by "not doing anything for a few consecutive n's".
Also, if I ignore the discreteness of the radio for a minute and analyse the problem as a continuous linear function, the total power should be generalisable to a function g(n) of the form g(n) = Kn + B (where K and B are constants). This linear function would represent the integral of the function we need to use for controlling the radio.
Then, if I take the derivative of this function, dg(n)/dn, I'm left with K. I.e. If I want to have linear total power, I should just drive the radio at a constant power for n times...but this would only result in a rescue if I happened to guess K correctly the first time.
EDIT
Yes, I had already thought of doubling etc....but the answers here pointed out the error in my thinking. I had been trying to solve the question "design an algorithm that has linear cumulative power consumption"...which I think is impossible. As pointed out by the answers, I should have thought about it as "for a given n, design an algorithm that will consume Kn"...i.e. what the question posed.
I've read the assignment...
It states that the radio is capable of transmitting in integers, but it doesn't mean you should try it one by one and go over all the integers until n.
Well, I could give you the answer but I'll try just to lead you to think about it on your own:
Please notice that you need to transmit a signal equal or greater than n, so there is no way you are "going to far". Now, with the concepts of complexity, if you go over all the signals you'll get a series of (1+2+3+...+n) which equals to Θ(n^2), try to think of a pattern which you can skip some of those and getting a series that results a sum of Θ(n).
This task is similar to searching algorithms which naively goes for Θ(n^2), but there are algorithms reduced to less than that - you should go and explore how they work :)
If you want an approach for an answer:
You can start with 1W and each step double it for the next transmit. That way you will do log(n) attempts, and each attempt costs i which i is the power of that attempt. So the cumulative power will be something like that: (1+2+4+8+16+...+n) which equals to 2n-1 and fits the requirement of Θ(n)
Well here is a simple algorithm and complexity analysis:
Initialy try with power=1W
If it wasn't received try with power=2*previous_power until it is received
Complexity:
So basically we stop when power p>= n, where n is the desired threshold.
We know that:
p>=n and p/2<n => n<=p<2n
In order to reach the pW (i.e the desired level in order to be received) this means you tried previously with p/2, prior to that p/4... and initially with 1, so let's sum up all the steps:
1+2+4+...+p/2+p -> 2*p ~ Θ(p) = Θ(n)

Show whether f is Big Oh of g with logarithmic equations

Hello, how would you do whether f is Big Oh of g. I know how to do this for simple exponential functions but when it comes to logs, I have no idea what to do. Can anyone help?
You should use the properties of the logarithm. For example, for your second case, you can use the fact that:
So in big O notation:
Regarding the first case, my advice is to read this document, specially the section 4, labeled "Growth Rate of Standard Functions", where you will find a bullet titled "Any polylog grows slower than any polynomial".

Telephone book algorithm

This is an exam practice question i've been working on, i know of methods to do this but as the question states i don't know which would be the most efficient.
You
are
given
a
telephone
book
listing
the
surnames
of
people
in
alphabetical
order.
Describe
the
fastest
method
(clearly
explain
what
you
have
to
do)
you
can
use
to
find
a
given
surname.
If
there
are
n
people
listed
in
the
telephone
book,
what
is
the
Big
O
complexity
of
your
fastest
method
(and
explain
why)?
In this case you know the phone book entries are in order already. This means that a binary search is probably your best bet. This search works by cutting the number of entries to search in half on each iteration. It only works if your data is already sorted however. Check out this website for time complexity in Big O notation: http://bigocheatsheet.com
Edit wording

Algorithm used in the game Ruzzle

I have been thinking about the algorithm used in the game Ruzzle for a while. The aim in the game is to find the presence of any word in a given grid, word can be matched in any direction up-down, down-up, left-right, right-left, both diagonals up and down etc.
Let's make it simple. Find a given word in a 2-D grid with the same matching constraints (directions). What is the algorithm with the best time-complexity possible ?
For example, FOREVER can be found in this grid.
H O F E R
L R E T O
S N V O R
P Q T E N
You can further optimise it by using a Trie data structure. Once you fill-in the structure with all the English words (or a different language), than you can check in O(1) if a particular neighbour character needs to be explored or not.
Please, notice that at this point you are trading storage for time: you presumably will need more RAM to store the entire Trie but you will query it faster than checking in the ordered list of words.
In terms of architecture behind the game, I think they use a dedicated server, which works full time to store in a DB new games (matrixes) and their list of admissible words. During the game, your device receives an ID, it downloads the matrix and the list of admissible words and this is enough to allow you to play. At the end of each game everything is deleted and the final score (just an integer) is submitted to the server, which will update your profile. In the actual game there's something more because they also have badges and statistics (but that's trivial stuff to collect). Bear in mind that this is just the way I would design and develop it.
What do you think? Can we do even better?
I have created a course about making ruzzle game mechanics in Unity. Please check it out and you will find exact answers:
https://www.udemy.com/word-game-unity/?couponCode=wordgamecourse

What's the best reserved seat sorting algorithm?

I'm trying to find the best algorithm for the following sorting problem.
There are N = K × M seats in an auditorium with one aisle, K rows, and M seats per aisle. The assumption is made that K is a bigger than M, but I don't think that's very important. There are N people that are in
bijection with the seats (assigned seats). Assuming that people don't
like waiting, what's the fastest way to line them up to get them all
in their seats as quickly as possible?
I ran some simple experiements (using random permutations) and it
seemed that letting them line up randomly is faster than having the
people in the front third (further down the aisle) line up first, then
the middle third, then the back third. That seems wrong to me.
I'm writing this in MatLab if that matters at all. Any ideas or answers?
There is a very nice article by Bachmat, Berend, Sapir, Skiena and Stolyarov entitled Analysis of airplane boarding via space-time geometry and random matrix theory that models this exact problem for airplane boarding. From their abstract:
We show that airplane boarding can be
asymptotically modeled by
2-dimensional Lorentzian geometry.
Boarding time is given by the maximal
proper time among curves in the model.
Discrepancies between the model and
simulation results are closely related
to random matrix theory. We then show
how such models can be used to explain
why some commonly practiced airline
boarding policies are ineffective and
even detrimental.
The conclusions of the paper are:
BEST: Window-Middle-Aisle
NEAR OPTIMAL: Random Boarding
REALLY BAD: Back-to-Front
For your set-up, I think this means you should ignore how far down the aisle the people are and instead focus on how far from the aisle they are. This model also accounts for time to store luggage, so you may need to adjust that somewhat for your situation. In any event, I think this confirms what you are finding through your model.

Resources