Doug’s MATLAB Video Tutorials
June 16th, 2008
Puzzler: Difficult algorithm development challenge
This week’s puzzler is significantly harder than others we have done in the past, I would be surprised (and delighted) to see an answer before I go home tonight…
The challenge is shown below.
Given a group of trees, find the rectangular bounding box in the x-y direction that holds all the trees. Once you have that bounding box, look for the largest rectangle (by area) that can be made inside the bounding box. Of course, this rectangle can have no trees in the interior.
Since this is a more difficult problem, you may want to look at the first video to see the outline for an algorithm to solve this:
If you do not want to attempt this challenge on your own, then feel free to watch the code review of my solution in the video below:
This video walks through the described algorithm line by line.
If you come up with a solution, please post it in the comments below. I have already posted the code from the video as the first comment.
Here is the code that you can use to test and visualize your solution with. Simply make main.m in this form:
function [bigArea, boundBox] = main(trees)
% bigArea and boundBox are structures that describe a rectangle and
% contain the fields:
% bigArea.x X of lower left corner
% bigArea.y Y of lower left corner
% bigArea.width Width of rectangle
% bigArea.height Height of rectangle
% bigArea is the largest open area
% boundBox is the bounding box of the entire set of trees
Here is the code to get you started:
clc
n = 5;
trees.x = rand(1,n);
trees.y = rand(1,n);
[bigArea, boundBox] = main(trees);
clf
rectangle('position', [ bigArea.x bigArea.y bigArea.width bigArea.height], ...
'facecolor', [1 1 0])
rectangle('position', [boundBox.x boundBox.y boundBox.width boundBox.height])
axis([0 1 0 1])
line(trees.x,trees.y, ...
'color',[0 1 0], ...
'marker','^' , ...
'linestyle','none' , ...
'markersize',5 , ...
'linewidth',5)
Remember to use the <pre><code> and </code></pre> tags around your code in the comments.
08:59 UTC |
Posted in Format: Video, Level: Advanced, Topic: Puzzler |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
 |
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
|
 |
|
Here’s the formatted code.
Hi.
Unfortunately, this problem is a bit on the lengthy side for me to give a go at during office hours, but I realized somthing when I was thinking through how I would solve it and while watching the solution.
The proposed algorithm will find the largest area twice using the scenario from the video. Had there been three trees inside the bounding box that enclosed the largesty area I believe it would be found 3 times. This sounds somewhat inefficient. Could one device a check inside the algoritm that stops once the largest area is found the first time?
A little white-board scetching tells me that once you start subdividing around the second tree, if an area contains the first tree than you can immediately stop because if there is an interesting area there, then it has allready been found. This should trivially extend to N trees.
Anyone have a comment on my reasoning here?
–DA
Daniel,
Yes, I had an inkling of the same conclusion, but did not pursue it. My original outline on paper proposed something like that, but I wanted to keep it simple. I often find that I will compromise: taking simplicity over efficiency and then continue to optimize from there. Since this algorithm works, I stopped. If the inefficiency started to matter, that would be the lowest hanging fruit to go after.
What I like about MATLAB is that it is pretty easy to prototype ideas. I can afford to throw this together, see how it works and then iterate on my code because it is fast to program.
-Thanks,
Doug
Joel,
I tried the above code. It does not work for me. I looks to me like the logic test:
Is implemented incorrectly. You are only checking for top and right, but not bottom and left. Also, the single equals sign is an assignment, I think you wanted a greater than. Once I added the other checks, I found that this finds a valid rectangle, but never the best one. I think the reason for that is the “indexing into something that indexes into something else” is done incorrectly.
I think these changes can be fixed, but a simpler scheme might make it easier to avoid such errors.
-Doug
Doug, thanks for this interesting problem. However, the proposed solution is wrong. I can think of at least one tree configuration (not a boundary condition) that will make the algorithm fail. In the sense that it will not find the largest rectangle.
Do you want me to tell you or do you like me to keep this as an added problem to solve?
I think that if you try to prove in a rigorous way that your algorithm always finds the largest rectangle you will probably realize that it doesn’t.
Hi all,
I am a biologist working on a project where the location of the trees are already known on a plot.
I have to set some boxes of 25 by 4 randomly on this plot.
Then find which trees have fallen in the box.
Seems I can easily do it using Matlab but am new and Alien to theis malab wrold.
Grateful if someone can help or guide me please
Thx
Dav
Dav,
I am sorry, I do not understand your question. Please show more details of code you have tried and where it is not working.
thanks,
Doug