|
I have tried similar problem using Genetic Algorithm and the results were astonishing. I think trying Genetic Algorithm may be wonderful for you too in speeding the search time.
|
|
|
|
|
I'm confused about recursion in mergesort. I've tried putting comments in the code that track the variables so its easier to understand what's going on, but I'm still struggling.
For an array of size 4, I understand the initial process -
mergesort(o,3).
0 is less than 3, so we find the middle value = 1. Low = 0, Middle,1 and High = 3
mergesort(0,1)
0 is less than 1, so we find the middle value = 0. Low = 0, Middle = 0, High 1
0 is not < 0, so the function is called again but with (middle +1 and high) as its parameters.
1 is not < 1, so the function merge is called with the parameters (0,0,1).
So far so good !
But when the recursive function kicks in again, this time it has the values (0,2,3) which has been passed to the second recursive function (middle +1, high) How did this happen ?? Is it because the second recursive function is using the same parameters as the first function on its second iteration? (0,1,3).
I've been looking all over for this, and lots of people just seem to skim over this step. I'm new, so would really appreciate any advice anyone has to give. I've tried experimenting with recursive functions, like finding the nth term in fib series, and sum of triangular numbers, which I can do now, but it doesnt seem to be helping in solving this problem.
Big thanks
public void mergesort (int low, int high) {
sum ++;
System.out.println("Round " + sum + ". Parameters being passed in = " + low + " " + middle + " " +high);
if (low<high) {
int middle = low + (high - low)/2;
System.out.println("Checking left ---- if " + low + " is < " + high);
System.out.println("Current value of high = " + high);
System.out.println("Current value of mid = " + middle);
System.out.println("low, mid and high =" + low + "" + middle + "" + high);
System.out.println();
mergesort(low,middle);
System.out.println("low, mid and high =" + low + "" + middle + "" + high);
mergesort(middle +1,high);
System.out.println("Checking right ---- if " + (middle +1) + " is < " + high);
System.out.println("low, mid and high =" + low + "" + middle + "" + high);
merge(low,middle,high);
}
|
|
|
|
|
If helps to view recursion in terms of a stack of work items. For example, you start with a work item:
mergesort(0, 3)
This is popped off the stack and executed, giving:
mergesort(0, 1)
mergesort(2, 3)
The top entry is popped off the stack, and executed, giving
mergesort(0, 0)
mergesort(1, 1)
mergesort(2, 3)
No work needs be performed for the first item (0, 0)
No work needs be performed for the second item (1, 1)
==> merge(0, 0, 1)
We now have mergesort(2, 3) left on the stack, which is precisely what you see.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Hi Daniel,
I don't understand your third call of mergesort. The first time it calls (0,3) which is the first and last index of the array.
The second time it calls (0,1), which is the first half, or first two indexes in the list.
But then why is it going to (2,3) ?
Surely as 0<1, the function would give (0,0) at which point it would exit finding the left half as the base condition has been met ?
I've read it then retains the value of (0,1), which is then passed into the second instance of mergesort (mid +1,high), to give (1,1), which means the left and right sides are both ready to be merged ?
I'll try to think of it more in terms of a stack, thanks for the pointer on that. I know how to program stacks, so I might try something this afternoon to see if I can get a full picture of whats going on underneath the recursive process,
Thanks for your help 
|
|
|
|
|
If you move your print statements out of the if (low < high) { … } block, you will see the extra calls to mergesort() that I added. In the cases of mergesort(0, 0) and mergesort(1, 1) you don't ender the if() block, so you don't see them at present.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Thank you, I will try this today
|
|
|
|
|
How do I convert ANY number to negative representation? I'm trying this in Excel.
To converting a positive number X, I just multiply with -1 & it gets converted to X * -1 = -X.
But what if I a number that is negative already, and the same formula should work seamlessly.
For example if it's -X & I multiply with -1 it gets converted to +X. Like -X * -1 = X.
which I don't want.
On a list containing both negative & positive values, I want this formula to have an impact only with the negative numbers.
modified 19-Feb-20 9:34am.
|
|
|
|
|
=IF(X>0,-X,X) ' where X is the cell reference
|
|
|
|
|
Richard MacCutchan wrote: =IF(X>0,-X,X)
Wow, that's sweet. 
|
|
|
|
|
Excel functions generally are.
|
|
|
|
|
Another option would be to use the ABS function:
=-ABS(X) ABS function - Office Support[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Terrific. 
|
|
|
|
|
Okay, so I know the table generation algorithms are slightly different in that LALR merges entries and a canonical LR parser will not.
My question is, is about the actual *use* of those entries - the final parse table.
Is there a difference between how a LALR parser parses and an LR parser *parses*?
Specifically, can I use the same parser code regardless of where I got the parse table? (whether from an LR algo or the LALR ago)
Real programmers use butterflies
|
|
|
|
|
shift/reduce parsing: the difference between LALR (1) and LR (1) is that LALR is based
on an LR (0) automaton.
Operationally they are the same: shift when you have to shift, reduce when you have to
reduce and give an error message when there is a mismatch between the set of expected tokens
and the received token
|
|
|
|
|
anyway thanks for your answer, i figured it out eventually the day i asked it but i didn't think to close the question (and i don't know if that's expected)
Real programmers use butterflies
|
|
|
|
|
Hi all, just seeking some advice please on how to approach this problem.
I need to create an algorithm for a small courier company.
The basic parameters would be x amount of drivers and a list of deliveries, pickup and drop-off postcodes with delivery time slots.
My thinking is to first create a matrix of all the pick up and delivery job postcodes and populate it with distance and journey times using Google’s API, then somehow loop through the jobs and match journey time’s with the delivery slots but not really sure where to start or even if this is a viable approach.
Any suggestions much appreciated !!!
Many thanks in advance 😊
|
|
|
|
|
Sounds like a nightmare version of the "travelling salesman" problem to me.
Traveling Salesman Problem | Solve the Traveling Salesman Problem[^]
All I can offer is that you should check whether the API returns the actual driving distance, or the "as the crow flies" straight-line distance. Once you start taking things like rivers and one-way systems into account, someone who is geographically closer to a destination might take considerably longer to reach it than someone who is technically further away.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hey, yep definitely is that, the journey time’s returned are actual driving times based on Google’s distance API 😊
|
|
|
|
|
|
Get a UPS, USPS or FedEx account and use their costing algorithms (and maybe their services, all told).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Kindly help me out to solve this question fro the book "Foundation of Algorithm"
There are two algorithms called Alg1 and Alg2 for a problem of size n. Alg1 runs in n2 microseconds and Alg2 runs in 100n log n microseconds. Alg1 can be implemented using 4 hours of programmer time and needs 2 minutes of CPU time. On the other hand, Alg2 requires 15 hours of programmer time and 6 minutes of CPU time. If programmers are paid 20 dollars per hour and CPU time costs 50 dollars per minute, how many times must a problem instance of size 500 be solved using Alg2 in order to justify its development cost?
thanks
|
|
|
|
|
This is a mathematics question, nothing really to do with algorithms.
|
|
|
|
|
If we would do this exercise for you, it would defeat its purpose which is to teach you how to develop and apply your analytical/mathematical skills.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
I've come very close a couple of times, but I just can't get it right.
I know one person on Code Project that knows how, but I don't think he's telling. Fair enough.
Does anyone know of any resources on this? Something not math heavy, or at least not math exclusive. I'd prefer sample code or a high level description at least. Any programming language, though I prefer C family languages except java. I'll take java though, as it's what most of academia uses, and this is an academic exercise primarily.
I really would like to solve this for the parser generators I've built.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I am looking for the stat-of-the-art solver for the 0-1 knapsack problem, Do any one know how to find such a solver? better if it will be written in python or matlab.
Do you know what is limitation of current solver?
Thanks Tomer
|
|
|
|
|