How to solve a problem with ArrayLists recursivelyWhat is tail recursion?How do I efficiently iterate over each entry in a Java Map?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Initialization of an ArrayList in one lineHow do I convert a String to an int in Java?How to pair socks from a pile efficiently?
Which star / galaxy is moving away from us the fastest?
Does Lufthansa weigh your carry on luggage?
For a hashing function like MD5, how similar can two plaintext strings be and still generate the same hash?
Optimization terminology: "Exact" v. "Approximate"
Sharing shapefile collection
What happens to unproductive professors?
Why did Harry Potter get a bedroom?
Why isn't pressure filtration popular compared to vacuum filtration?
Is English unusual in having no second person plural form?
Why does wrapping aluminium foil around my food help it keep warm, even though aluminium is a good conductor?
Why would non-kinetic weapons be used for orbital bombardment?
Why do we need common sense in AI?
Fast validation of time windows in a routing problem
How can I effectively communicate to recruiters that a phone call is not possible?
Are there any sports for which the world's best player is female?
Misspelling my name on my mathematical publications
This one's for Matthew:
Swapping "Good" and "Bad"
Can you cast a blanket Invisibility and let the targets see each other?
LED glows slightly during soldering
Why didn't Thanos kill all the Dwarves on Nidavellir?
How to tell someone I'd like to become friends without letting them think I'm romantically interested in them?
When an electron changes its spin, or any other intrinsic property, is it still the same electron?
When I press the space bar it deletes the letters after it
How to solve a problem with ArrayLists recursively
What is tail recursion?How do I efficiently iterate over each entry in a Java Map?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Initialization of an ArrayList in one lineHow do I convert a String to an int in Java?How to pair socks from a pile efficiently?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to write a program that "solves a circle". The Circle class contains an ArrayList of Triangle Objects and an ArrayList of Integers. The Triangle objects each have three int instance fields that represent three numbers at the vertices of each triangle. There is also a Pairs class (you can see all of the code I have in the "code" section)
Here is an example of the setup using four triangles that is not solved:
And here is the same circle after it has been "solved":
The Circle in the second picture is a solved Circle because the number on any arc of the circle is equal to the sum of the two vertex numbers next to it: 6 = 1+5, 15 = 6+9, 11 = 7+4, and 9 = 5+4. Note that this was obtained by rotating the given triangles. This is analagous in the code to simply changing the Pair that is present in the solution for each triangle (where a "Pair" is an object of two ints, where those ints are the values on the circle for each triangle)
A Circle isn't always given in a "solved" state. If this is the case, the triangles can be rotated so that the circle will be in the solved state. The precondition of any given circle is that there is a solved state, so the numbers will always line up.
A circle will always have at least two triangles and there is no (practical) maximum number. Every given circle will always be solvable, meaning there is a way to rotate each triangle so that the number on the circle is the result of the sum of the two adjacent vertices from the two different triangles.
The point of the program is not to alter any of the given instance fields; instead, I just want to create a method called solveCircle that returns an ArrayList of Pairs that represents the solution to the Circle. In the above example, the solveCircle method would return an ArrayList containing the following pairs: (4,1), (5,6), (9,7), (4,5). These pairs are in the solution because they are all pairs of numbers on a triangle, and each pair is also on the circle. Note that the solution goes counter-clockwise around the circle.
My gut is telling me that this process should involve some type of recursion, since a loop would be tricky due to the circular nature of the circle; in other words, I could loop through each pair of triangles finding the proper solution, but there could easily be more than one, and comparing each one with the solution to the next sum seems like it will be inefficient; recursion seems like a better option but I'm not sure what to apply the recursion to...what alrgorithm should I use and what is even the base case?
public class Triangle
private int num1;
private int num2;
private int num3;
public Triangle(int n1, int n2, int n3)
num1 = n1;
num2 = n2;
num3 = n3;
public ArrayList<Pair> getPairs()
ArrayList<Pair> pairs = new ArrayList<Pair>();
pairs.add(new Pair(num1, num2));
pairs.add(new Pair(num2, num3));
pairs.add(new Pair(num3, num1));
return pairs;
class Pair
private int p1;
private int p2;
public Pair(int x, int y)
p1 = x;
p2 = y;
public class Circle
private ArrayList<Triangle> triangles;
private ArrayList<Integer> sums;
public Wheel(ArrayList<Integer> s, ArrayList<Triangle> t)
triangles = t;
sums = s;
public ArrayList<Pair> solveCircle()
//need help here
java algorithm recursion
|
show 1 more comment
I'm trying to write a program that "solves a circle". The Circle class contains an ArrayList of Triangle Objects and an ArrayList of Integers. The Triangle objects each have three int instance fields that represent three numbers at the vertices of each triangle. There is also a Pairs class (you can see all of the code I have in the "code" section)
Here is an example of the setup using four triangles that is not solved:
And here is the same circle after it has been "solved":
The Circle in the second picture is a solved Circle because the number on any arc of the circle is equal to the sum of the two vertex numbers next to it: 6 = 1+5, 15 = 6+9, 11 = 7+4, and 9 = 5+4. Note that this was obtained by rotating the given triangles. This is analagous in the code to simply changing the Pair that is present in the solution for each triangle (where a "Pair" is an object of two ints, where those ints are the values on the circle for each triangle)
A Circle isn't always given in a "solved" state. If this is the case, the triangles can be rotated so that the circle will be in the solved state. The precondition of any given circle is that there is a solved state, so the numbers will always line up.
A circle will always have at least two triangles and there is no (practical) maximum number. Every given circle will always be solvable, meaning there is a way to rotate each triangle so that the number on the circle is the result of the sum of the two adjacent vertices from the two different triangles.
The point of the program is not to alter any of the given instance fields; instead, I just want to create a method called solveCircle that returns an ArrayList of Pairs that represents the solution to the Circle. In the above example, the solveCircle method would return an ArrayList containing the following pairs: (4,1), (5,6), (9,7), (4,5). These pairs are in the solution because they are all pairs of numbers on a triangle, and each pair is also on the circle. Note that the solution goes counter-clockwise around the circle.
My gut is telling me that this process should involve some type of recursion, since a loop would be tricky due to the circular nature of the circle; in other words, I could loop through each pair of triangles finding the proper solution, but there could easily be more than one, and comparing each one with the solution to the next sum seems like it will be inefficient; recursion seems like a better option but I'm not sure what to apply the recursion to...what alrgorithm should I use and what is even the base case?
public class Triangle
private int num1;
private int num2;
private int num3;
public Triangle(int n1, int n2, int n3)
num1 = n1;
num2 = n2;
num3 = n3;
public ArrayList<Pair> getPairs()
ArrayList<Pair> pairs = new ArrayList<Pair>();
pairs.add(new Pair(num1, num2));
pairs.add(new Pair(num2, num3));
pairs.add(new Pair(num3, num1));
return pairs;
class Pair
private int p1;
private int p2;
public Pair(int x, int y)
p1 = x;
p2 = y;
public class Circle
private ArrayList<Triangle> triangles;
private ArrayList<Integer> sums;
public Wheel(ArrayList<Integer> s, ArrayList<Triangle> t)
triangles = t;
sums = s;
public ArrayList<Pair> solveCircle()
//need help here
java algorithm recursion
"counterclockwise," but which comes first: triangle 0 or sum 0?
– Patrick Parker
Mar 26 at 2:34
Triangle 0 would come first, so I would use triangle 0 and triangle 1 to find sum 1, and so on, until I get to Triangle "size()-1". Then I would use that and Triangle 0 with sum 0. The size of the sum ArrayList will be one less than the size of the Triangle ArrayList.
– analysischallenged
Mar 26 at 3:13
I would use the Sudoku approach. Go around the circle and find the triangle that has the minimum number of valid orientations. If there's only one valid orientation, choose that orientation, and lock the triangle. If there are two or three valid orientations, then choose one, and recurse.
– user3386109
Mar 26 at 3:32
I think my problem is that I'm confused about the concept of recursion when it isn't used in sequences of numbers or situations like a factorial or a sum. In this case, I'm not really sure what the recursion is. How would I write the base case? I think I just need an example of how this is working like, for example, in the 4-triangle Circle that I gave as an example.
– analysischallenged
Mar 26 at 3:35
I would add astate
variable to theTriangle
class. There are two states:Undecided
andLocked
. At each level of recursion, one triangle is assigned an orientation, and locked. Which means that at each level of recursion there is one less undecided triangle. Successful base case: all of the triangles are locked. Otherwise, find an undecided triangle with the least number of valid orientations. Choose an orientation for that triangle, lock it, and recurse. Note that there may be an undecided triangle with no valid orientations. That's the failed base case.
– user3386109
Mar 26 at 4:45
|
show 1 more comment
I'm trying to write a program that "solves a circle". The Circle class contains an ArrayList of Triangle Objects and an ArrayList of Integers. The Triangle objects each have three int instance fields that represent three numbers at the vertices of each triangle. There is also a Pairs class (you can see all of the code I have in the "code" section)
Here is an example of the setup using four triangles that is not solved:
And here is the same circle after it has been "solved":
The Circle in the second picture is a solved Circle because the number on any arc of the circle is equal to the sum of the two vertex numbers next to it: 6 = 1+5, 15 = 6+9, 11 = 7+4, and 9 = 5+4. Note that this was obtained by rotating the given triangles. This is analagous in the code to simply changing the Pair that is present in the solution for each triangle (where a "Pair" is an object of two ints, where those ints are the values on the circle for each triangle)
A Circle isn't always given in a "solved" state. If this is the case, the triangles can be rotated so that the circle will be in the solved state. The precondition of any given circle is that there is a solved state, so the numbers will always line up.
A circle will always have at least two triangles and there is no (practical) maximum number. Every given circle will always be solvable, meaning there is a way to rotate each triangle so that the number on the circle is the result of the sum of the two adjacent vertices from the two different triangles.
The point of the program is not to alter any of the given instance fields; instead, I just want to create a method called solveCircle that returns an ArrayList of Pairs that represents the solution to the Circle. In the above example, the solveCircle method would return an ArrayList containing the following pairs: (4,1), (5,6), (9,7), (4,5). These pairs are in the solution because they are all pairs of numbers on a triangle, and each pair is also on the circle. Note that the solution goes counter-clockwise around the circle.
My gut is telling me that this process should involve some type of recursion, since a loop would be tricky due to the circular nature of the circle; in other words, I could loop through each pair of triangles finding the proper solution, but there could easily be more than one, and comparing each one with the solution to the next sum seems like it will be inefficient; recursion seems like a better option but I'm not sure what to apply the recursion to...what alrgorithm should I use and what is even the base case?
public class Triangle
private int num1;
private int num2;
private int num3;
public Triangle(int n1, int n2, int n3)
num1 = n1;
num2 = n2;
num3 = n3;
public ArrayList<Pair> getPairs()
ArrayList<Pair> pairs = new ArrayList<Pair>();
pairs.add(new Pair(num1, num2));
pairs.add(new Pair(num2, num3));
pairs.add(new Pair(num3, num1));
return pairs;
class Pair
private int p1;
private int p2;
public Pair(int x, int y)
p1 = x;
p2 = y;
public class Circle
private ArrayList<Triangle> triangles;
private ArrayList<Integer> sums;
public Wheel(ArrayList<Integer> s, ArrayList<Triangle> t)
triangles = t;
sums = s;
public ArrayList<Pair> solveCircle()
//need help here
java algorithm recursion
I'm trying to write a program that "solves a circle". The Circle class contains an ArrayList of Triangle Objects and an ArrayList of Integers. The Triangle objects each have three int instance fields that represent three numbers at the vertices of each triangle. There is also a Pairs class (you can see all of the code I have in the "code" section)
Here is an example of the setup using four triangles that is not solved:
And here is the same circle after it has been "solved":
The Circle in the second picture is a solved Circle because the number on any arc of the circle is equal to the sum of the two vertex numbers next to it: 6 = 1+5, 15 = 6+9, 11 = 7+4, and 9 = 5+4. Note that this was obtained by rotating the given triangles. This is analagous in the code to simply changing the Pair that is present in the solution for each triangle (where a "Pair" is an object of two ints, where those ints are the values on the circle for each triangle)
A Circle isn't always given in a "solved" state. If this is the case, the triangles can be rotated so that the circle will be in the solved state. The precondition of any given circle is that there is a solved state, so the numbers will always line up.
A circle will always have at least two triangles and there is no (practical) maximum number. Every given circle will always be solvable, meaning there is a way to rotate each triangle so that the number on the circle is the result of the sum of the two adjacent vertices from the two different triangles.
The point of the program is not to alter any of the given instance fields; instead, I just want to create a method called solveCircle that returns an ArrayList of Pairs that represents the solution to the Circle. In the above example, the solveCircle method would return an ArrayList containing the following pairs: (4,1), (5,6), (9,7), (4,5). These pairs are in the solution because they are all pairs of numbers on a triangle, and each pair is also on the circle. Note that the solution goes counter-clockwise around the circle.
My gut is telling me that this process should involve some type of recursion, since a loop would be tricky due to the circular nature of the circle; in other words, I could loop through each pair of triangles finding the proper solution, but there could easily be more than one, and comparing each one with the solution to the next sum seems like it will be inefficient; recursion seems like a better option but I'm not sure what to apply the recursion to...what alrgorithm should I use and what is even the base case?
public class Triangle
private int num1;
private int num2;
private int num3;
public Triangle(int n1, int n2, int n3)
num1 = n1;
num2 = n2;
num3 = n3;
public ArrayList<Pair> getPairs()
ArrayList<Pair> pairs = new ArrayList<Pair>();
pairs.add(new Pair(num1, num2));
pairs.add(new Pair(num2, num3));
pairs.add(new Pair(num3, num1));
return pairs;
class Pair
private int p1;
private int p2;
public Pair(int x, int y)
p1 = x;
p2 = y;
public class Circle
private ArrayList<Triangle> triangles;
private ArrayList<Integer> sums;
public Wheel(ArrayList<Integer> s, ArrayList<Triangle> t)
triangles = t;
sums = s;
public ArrayList<Pair> solveCircle()
//need help here
java algorithm recursion
java algorithm recursion
edited Mar 26 at 2:13
analysischallenged
asked Mar 26 at 1:35
analysischallengedanalysischallenged
162 bronze badges
162 bronze badges
"counterclockwise," but which comes first: triangle 0 or sum 0?
– Patrick Parker
Mar 26 at 2:34
Triangle 0 would come first, so I would use triangle 0 and triangle 1 to find sum 1, and so on, until I get to Triangle "size()-1". Then I would use that and Triangle 0 with sum 0. The size of the sum ArrayList will be one less than the size of the Triangle ArrayList.
– analysischallenged
Mar 26 at 3:13
I would use the Sudoku approach. Go around the circle and find the triangle that has the minimum number of valid orientations. If there's only one valid orientation, choose that orientation, and lock the triangle. If there are two or three valid orientations, then choose one, and recurse.
– user3386109
Mar 26 at 3:32
I think my problem is that I'm confused about the concept of recursion when it isn't used in sequences of numbers or situations like a factorial or a sum. In this case, I'm not really sure what the recursion is. How would I write the base case? I think I just need an example of how this is working like, for example, in the 4-triangle Circle that I gave as an example.
– analysischallenged
Mar 26 at 3:35
I would add astate
variable to theTriangle
class. There are two states:Undecided
andLocked
. At each level of recursion, one triangle is assigned an orientation, and locked. Which means that at each level of recursion there is one less undecided triangle. Successful base case: all of the triangles are locked. Otherwise, find an undecided triangle with the least number of valid orientations. Choose an orientation for that triangle, lock it, and recurse. Note that there may be an undecided triangle with no valid orientations. That's the failed base case.
– user3386109
Mar 26 at 4:45
|
show 1 more comment
"counterclockwise," but which comes first: triangle 0 or sum 0?
– Patrick Parker
Mar 26 at 2:34
Triangle 0 would come first, so I would use triangle 0 and triangle 1 to find sum 1, and so on, until I get to Triangle "size()-1". Then I would use that and Triangle 0 with sum 0. The size of the sum ArrayList will be one less than the size of the Triangle ArrayList.
– analysischallenged
Mar 26 at 3:13
I would use the Sudoku approach. Go around the circle and find the triangle that has the minimum number of valid orientations. If there's only one valid orientation, choose that orientation, and lock the triangle. If there are two or three valid orientations, then choose one, and recurse.
– user3386109
Mar 26 at 3:32
I think my problem is that I'm confused about the concept of recursion when it isn't used in sequences of numbers or situations like a factorial or a sum. In this case, I'm not really sure what the recursion is. How would I write the base case? I think I just need an example of how this is working like, for example, in the 4-triangle Circle that I gave as an example.
– analysischallenged
Mar 26 at 3:35
I would add astate
variable to theTriangle
class. There are two states:Undecided
andLocked
. At each level of recursion, one triangle is assigned an orientation, and locked. Which means that at each level of recursion there is one less undecided triangle. Successful base case: all of the triangles are locked. Otherwise, find an undecided triangle with the least number of valid orientations. Choose an orientation for that triangle, lock it, and recurse. Note that there may be an undecided triangle with no valid orientations. That's the failed base case.
– user3386109
Mar 26 at 4:45
"counterclockwise," but which comes first: triangle 0 or sum 0?
– Patrick Parker
Mar 26 at 2:34
"counterclockwise," but which comes first: triangle 0 or sum 0?
– Patrick Parker
Mar 26 at 2:34
Triangle 0 would come first, so I would use triangle 0 and triangle 1 to find sum 1, and so on, until I get to Triangle "size()-1". Then I would use that and Triangle 0 with sum 0. The size of the sum ArrayList will be one less than the size of the Triangle ArrayList.
– analysischallenged
Mar 26 at 3:13
Triangle 0 would come first, so I would use triangle 0 and triangle 1 to find sum 1, and so on, until I get to Triangle "size()-1". Then I would use that and Triangle 0 with sum 0. The size of the sum ArrayList will be one less than the size of the Triangle ArrayList.
– analysischallenged
Mar 26 at 3:13
I would use the Sudoku approach. Go around the circle and find the triangle that has the minimum number of valid orientations. If there's only one valid orientation, choose that orientation, and lock the triangle. If there are two or three valid orientations, then choose one, and recurse.
– user3386109
Mar 26 at 3:32
I would use the Sudoku approach. Go around the circle and find the triangle that has the minimum number of valid orientations. If there's only one valid orientation, choose that orientation, and lock the triangle. If there are two or three valid orientations, then choose one, and recurse.
– user3386109
Mar 26 at 3:32
I think my problem is that I'm confused about the concept of recursion when it isn't used in sequences of numbers or situations like a factorial or a sum. In this case, I'm not really sure what the recursion is. How would I write the base case? I think I just need an example of how this is working like, for example, in the 4-triangle Circle that I gave as an example.
– analysischallenged
Mar 26 at 3:35
I think my problem is that I'm confused about the concept of recursion when it isn't used in sequences of numbers or situations like a factorial or a sum. In this case, I'm not really sure what the recursion is. How would I write the base case? I think I just need an example of how this is working like, for example, in the 4-triangle Circle that I gave as an example.
– analysischallenged
Mar 26 at 3:35
I would add a
state
variable to the Triangle
class. There are two states: Undecided
and Locked
. At each level of recursion, one triangle is assigned an orientation, and locked. Which means that at each level of recursion there is one less undecided triangle. Successful base case: all of the triangles are locked. Otherwise, find an undecided triangle with the least number of valid orientations. Choose an orientation for that triangle, lock it, and recurse. Note that there may be an undecided triangle with no valid orientations. That's the failed base case.– user3386109
Mar 26 at 4:45
I would add a
state
variable to the Triangle
class. There are two states: Undecided
and Locked
. At each level of recursion, one triangle is assigned an orientation, and locked. Which means that at each level of recursion there is one less undecided triangle. Successful base case: all of the triangles are locked. Otherwise, find an undecided triangle with the least number of valid orientations. Choose an orientation for that triangle, lock it, and recurse. Note that there may be an undecided triangle with no valid orientations. That's the failed base case.– user3386109
Mar 26 at 4:45
|
show 1 more comment
2 Answers
2
active
oldest
votes
You can use a tree to separate the triangles that are solved from the ones that are unsolved. The same for the circles that are solved and that are unsolved. This way, you would make it a log n
search function that would ignore the solved ones, thus avoiding unnecessary comparisons.
if (solved)
add to left side of the tree
else
add to right side of the tree
The complexity as well for this could be an extreme overkill depending on the use case.
I haven't learned about trees yet in my class and I can't use anything that hasn't been used. I don't understand what you mean by "triangles that are solved". I won't know if a triangle is correct unless the entire circle is solved correctly. For example, if I have a sum of 10, and I have two adjacent triangles with vertices 3,4,8 and 7,6,2, there would be three different ways that they could be arranged that would be correct; but only one would work based on the other information in the circle, namely the other sums and other triangles.
– analysischallenged
Mar 26 at 1:51
Edit your question with a little more example on how one would receive the problem. For example, from this, I can't understand the maximum size of the circle, and what's the domain/range of the values existent for the problem.
– Luke Perez
Mar 26 at 1:53
This is why I think it has something to do with being recurvsive, where I would check if two triangles are correct, then if they are check if the third one works and, if it does move on to the fourth but if it doesn't, go back and revisit the first two triangles. I'm just not certain how to implement that.
– analysischallenged
Mar 26 at 1:55
Edited with an example of a "given" Circle and then the solved version. I need to write the code that will take that given Circle and then produce an ArrayList of Pairs that represent the pairs of vertices of each triangle that are part of the solution.
– analysischallenged
Mar 26 at 2:04
I don't think this answer is correct. A triangle is not "solved" in isolation. It may need to be rotated again depending on what comes later.
– Patrick Parker
Mar 26 at 2:31
|
show 1 more comment
for the initial step, you call the helper three times: once for each pair, until a success is returned (boolean could be used to indicate success.)
the helper performs the recursive step.
for the recursive step, you have a sum extending behind you, an integer which you must combine with to meet that sum, and three possible ways to acheive it... however you do not return success unless your recursive call also returns success
for the terminal step, there is no rotating allowed, just a final true or false for did I complete the sum behind me.
So my helper function is a boolean function that returns true if the sum of the two vertices is equal to the sum I want? I'm a little lost with your explanation (maybe because I'm having trouble picturing this as recursive in the same way that the fibonacci numbers are recursive). I apologize for the lapse in time for responses; I was attempting to do this on my own without recursion. I came up with something but it is messy and, unfortunately, it does not output the correct answer.
– analysischallenged
Mar 26 at 3:11
the helper function returns true IF its current rotation matches the required sum behind it AND the recursive call is also true ELSE it tries the next rotation UNTIL it has tried all three THEN it returns false
– Patrick Parker
Mar 26 at 6:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348653%2fhow-to-solve-a-problem-with-arraylists-recursively%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a tree to separate the triangles that are solved from the ones that are unsolved. The same for the circles that are solved and that are unsolved. This way, you would make it a log n
search function that would ignore the solved ones, thus avoiding unnecessary comparisons.
if (solved)
add to left side of the tree
else
add to right side of the tree
The complexity as well for this could be an extreme overkill depending on the use case.
I haven't learned about trees yet in my class and I can't use anything that hasn't been used. I don't understand what you mean by "triangles that are solved". I won't know if a triangle is correct unless the entire circle is solved correctly. For example, if I have a sum of 10, and I have two adjacent triangles with vertices 3,4,8 and 7,6,2, there would be three different ways that they could be arranged that would be correct; but only one would work based on the other information in the circle, namely the other sums and other triangles.
– analysischallenged
Mar 26 at 1:51
Edit your question with a little more example on how one would receive the problem. For example, from this, I can't understand the maximum size of the circle, and what's the domain/range of the values existent for the problem.
– Luke Perez
Mar 26 at 1:53
This is why I think it has something to do with being recurvsive, where I would check if two triangles are correct, then if they are check if the third one works and, if it does move on to the fourth but if it doesn't, go back and revisit the first two triangles. I'm just not certain how to implement that.
– analysischallenged
Mar 26 at 1:55
Edited with an example of a "given" Circle and then the solved version. I need to write the code that will take that given Circle and then produce an ArrayList of Pairs that represent the pairs of vertices of each triangle that are part of the solution.
– analysischallenged
Mar 26 at 2:04
I don't think this answer is correct. A triangle is not "solved" in isolation. It may need to be rotated again depending on what comes later.
– Patrick Parker
Mar 26 at 2:31
|
show 1 more comment
You can use a tree to separate the triangles that are solved from the ones that are unsolved. The same for the circles that are solved and that are unsolved. This way, you would make it a log n
search function that would ignore the solved ones, thus avoiding unnecessary comparisons.
if (solved)
add to left side of the tree
else
add to right side of the tree
The complexity as well for this could be an extreme overkill depending on the use case.
I haven't learned about trees yet in my class and I can't use anything that hasn't been used. I don't understand what you mean by "triangles that are solved". I won't know if a triangle is correct unless the entire circle is solved correctly. For example, if I have a sum of 10, and I have two adjacent triangles with vertices 3,4,8 and 7,6,2, there would be three different ways that they could be arranged that would be correct; but only one would work based on the other information in the circle, namely the other sums and other triangles.
– analysischallenged
Mar 26 at 1:51
Edit your question with a little more example on how one would receive the problem. For example, from this, I can't understand the maximum size of the circle, and what's the domain/range of the values existent for the problem.
– Luke Perez
Mar 26 at 1:53
This is why I think it has something to do with being recurvsive, where I would check if two triangles are correct, then if they are check if the third one works and, if it does move on to the fourth but if it doesn't, go back and revisit the first two triangles. I'm just not certain how to implement that.
– analysischallenged
Mar 26 at 1:55
Edited with an example of a "given" Circle and then the solved version. I need to write the code that will take that given Circle and then produce an ArrayList of Pairs that represent the pairs of vertices of each triangle that are part of the solution.
– analysischallenged
Mar 26 at 2:04
I don't think this answer is correct. A triangle is not "solved" in isolation. It may need to be rotated again depending on what comes later.
– Patrick Parker
Mar 26 at 2:31
|
show 1 more comment
You can use a tree to separate the triangles that are solved from the ones that are unsolved. The same for the circles that are solved and that are unsolved. This way, you would make it a log n
search function that would ignore the solved ones, thus avoiding unnecessary comparisons.
if (solved)
add to left side of the tree
else
add to right side of the tree
The complexity as well for this could be an extreme overkill depending on the use case.
You can use a tree to separate the triangles that are solved from the ones that are unsolved. The same for the circles that are solved and that are unsolved. This way, you would make it a log n
search function that would ignore the solved ones, thus avoiding unnecessary comparisons.
if (solved)
add to left side of the tree
else
add to right side of the tree
The complexity as well for this could be an extreme overkill depending on the use case.
answered Mar 26 at 1:48
Luke PerezLuke Perez
768 bronze badges
768 bronze badges
I haven't learned about trees yet in my class and I can't use anything that hasn't been used. I don't understand what you mean by "triangles that are solved". I won't know if a triangle is correct unless the entire circle is solved correctly. For example, if I have a sum of 10, and I have two adjacent triangles with vertices 3,4,8 and 7,6,2, there would be three different ways that they could be arranged that would be correct; but only one would work based on the other information in the circle, namely the other sums and other triangles.
– analysischallenged
Mar 26 at 1:51
Edit your question with a little more example on how one would receive the problem. For example, from this, I can't understand the maximum size of the circle, and what's the domain/range of the values existent for the problem.
– Luke Perez
Mar 26 at 1:53
This is why I think it has something to do with being recurvsive, where I would check if two triangles are correct, then if they are check if the third one works and, if it does move on to the fourth but if it doesn't, go back and revisit the first two triangles. I'm just not certain how to implement that.
– analysischallenged
Mar 26 at 1:55
Edited with an example of a "given" Circle and then the solved version. I need to write the code that will take that given Circle and then produce an ArrayList of Pairs that represent the pairs of vertices of each triangle that are part of the solution.
– analysischallenged
Mar 26 at 2:04
I don't think this answer is correct. A triangle is not "solved" in isolation. It may need to be rotated again depending on what comes later.
– Patrick Parker
Mar 26 at 2:31
|
show 1 more comment
I haven't learned about trees yet in my class and I can't use anything that hasn't been used. I don't understand what you mean by "triangles that are solved". I won't know if a triangle is correct unless the entire circle is solved correctly. For example, if I have a sum of 10, and I have two adjacent triangles with vertices 3,4,8 and 7,6,2, there would be three different ways that they could be arranged that would be correct; but only one would work based on the other information in the circle, namely the other sums and other triangles.
– analysischallenged
Mar 26 at 1:51
Edit your question with a little more example on how one would receive the problem. For example, from this, I can't understand the maximum size of the circle, and what's the domain/range of the values existent for the problem.
– Luke Perez
Mar 26 at 1:53
This is why I think it has something to do with being recurvsive, where I would check if two triangles are correct, then if they are check if the third one works and, if it does move on to the fourth but if it doesn't, go back and revisit the first two triangles. I'm just not certain how to implement that.
– analysischallenged
Mar 26 at 1:55
Edited with an example of a "given" Circle and then the solved version. I need to write the code that will take that given Circle and then produce an ArrayList of Pairs that represent the pairs of vertices of each triangle that are part of the solution.
– analysischallenged
Mar 26 at 2:04
I don't think this answer is correct. A triangle is not "solved" in isolation. It may need to be rotated again depending on what comes later.
– Patrick Parker
Mar 26 at 2:31
I haven't learned about trees yet in my class and I can't use anything that hasn't been used. I don't understand what you mean by "triangles that are solved". I won't know if a triangle is correct unless the entire circle is solved correctly. For example, if I have a sum of 10, and I have two adjacent triangles with vertices 3,4,8 and 7,6,2, there would be three different ways that they could be arranged that would be correct; but only one would work based on the other information in the circle, namely the other sums and other triangles.
– analysischallenged
Mar 26 at 1:51
I haven't learned about trees yet in my class and I can't use anything that hasn't been used. I don't understand what you mean by "triangles that are solved". I won't know if a triangle is correct unless the entire circle is solved correctly. For example, if I have a sum of 10, and I have two adjacent triangles with vertices 3,4,8 and 7,6,2, there would be three different ways that they could be arranged that would be correct; but only one would work based on the other information in the circle, namely the other sums and other triangles.
– analysischallenged
Mar 26 at 1:51
Edit your question with a little more example on how one would receive the problem. For example, from this, I can't understand the maximum size of the circle, and what's the domain/range of the values existent for the problem.
– Luke Perez
Mar 26 at 1:53
Edit your question with a little more example on how one would receive the problem. For example, from this, I can't understand the maximum size of the circle, and what's the domain/range of the values existent for the problem.
– Luke Perez
Mar 26 at 1:53
This is why I think it has something to do with being recurvsive, where I would check if two triangles are correct, then if they are check if the third one works and, if it does move on to the fourth but if it doesn't, go back and revisit the first two triangles. I'm just not certain how to implement that.
– analysischallenged
Mar 26 at 1:55
This is why I think it has something to do with being recurvsive, where I would check if two triangles are correct, then if they are check if the third one works and, if it does move on to the fourth but if it doesn't, go back and revisit the first two triangles. I'm just not certain how to implement that.
– analysischallenged
Mar 26 at 1:55
Edited with an example of a "given" Circle and then the solved version. I need to write the code that will take that given Circle and then produce an ArrayList of Pairs that represent the pairs of vertices of each triangle that are part of the solution.
– analysischallenged
Mar 26 at 2:04
Edited with an example of a "given" Circle and then the solved version. I need to write the code that will take that given Circle and then produce an ArrayList of Pairs that represent the pairs of vertices of each triangle that are part of the solution.
– analysischallenged
Mar 26 at 2:04
I don't think this answer is correct. A triangle is not "solved" in isolation. It may need to be rotated again depending on what comes later.
– Patrick Parker
Mar 26 at 2:31
I don't think this answer is correct. A triangle is not "solved" in isolation. It may need to be rotated again depending on what comes later.
– Patrick Parker
Mar 26 at 2:31
|
show 1 more comment
for the initial step, you call the helper three times: once for each pair, until a success is returned (boolean could be used to indicate success.)
the helper performs the recursive step.
for the recursive step, you have a sum extending behind you, an integer which you must combine with to meet that sum, and three possible ways to acheive it... however you do not return success unless your recursive call also returns success
for the terminal step, there is no rotating allowed, just a final true or false for did I complete the sum behind me.
So my helper function is a boolean function that returns true if the sum of the two vertices is equal to the sum I want? I'm a little lost with your explanation (maybe because I'm having trouble picturing this as recursive in the same way that the fibonacci numbers are recursive). I apologize for the lapse in time for responses; I was attempting to do this on my own without recursion. I came up with something but it is messy and, unfortunately, it does not output the correct answer.
– analysischallenged
Mar 26 at 3:11
the helper function returns true IF its current rotation matches the required sum behind it AND the recursive call is also true ELSE it tries the next rotation UNTIL it has tried all three THEN it returns false
– Patrick Parker
Mar 26 at 6:15
add a comment |
for the initial step, you call the helper three times: once for each pair, until a success is returned (boolean could be used to indicate success.)
the helper performs the recursive step.
for the recursive step, you have a sum extending behind you, an integer which you must combine with to meet that sum, and three possible ways to acheive it... however you do not return success unless your recursive call also returns success
for the terminal step, there is no rotating allowed, just a final true or false for did I complete the sum behind me.
So my helper function is a boolean function that returns true if the sum of the two vertices is equal to the sum I want? I'm a little lost with your explanation (maybe because I'm having trouble picturing this as recursive in the same way that the fibonacci numbers are recursive). I apologize for the lapse in time for responses; I was attempting to do this on my own without recursion. I came up with something but it is messy and, unfortunately, it does not output the correct answer.
– analysischallenged
Mar 26 at 3:11
the helper function returns true IF its current rotation matches the required sum behind it AND the recursive call is also true ELSE it tries the next rotation UNTIL it has tried all three THEN it returns false
– Patrick Parker
Mar 26 at 6:15
add a comment |
for the initial step, you call the helper three times: once for each pair, until a success is returned (boolean could be used to indicate success.)
the helper performs the recursive step.
for the recursive step, you have a sum extending behind you, an integer which you must combine with to meet that sum, and three possible ways to acheive it... however you do not return success unless your recursive call also returns success
for the terminal step, there is no rotating allowed, just a final true or false for did I complete the sum behind me.
for the initial step, you call the helper three times: once for each pair, until a success is returned (boolean could be used to indicate success.)
the helper performs the recursive step.
for the recursive step, you have a sum extending behind you, an integer which you must combine with to meet that sum, and three possible ways to acheive it... however you do not return success unless your recursive call also returns success
for the terminal step, there is no rotating allowed, just a final true or false for did I complete the sum behind me.
answered Mar 26 at 2:47
Patrick ParkerPatrick Parker
3,5582 gold badges10 silver badges35 bronze badges
3,5582 gold badges10 silver badges35 bronze badges
So my helper function is a boolean function that returns true if the sum of the two vertices is equal to the sum I want? I'm a little lost with your explanation (maybe because I'm having trouble picturing this as recursive in the same way that the fibonacci numbers are recursive). I apologize for the lapse in time for responses; I was attempting to do this on my own without recursion. I came up with something but it is messy and, unfortunately, it does not output the correct answer.
– analysischallenged
Mar 26 at 3:11
the helper function returns true IF its current rotation matches the required sum behind it AND the recursive call is also true ELSE it tries the next rotation UNTIL it has tried all three THEN it returns false
– Patrick Parker
Mar 26 at 6:15
add a comment |
So my helper function is a boolean function that returns true if the sum of the two vertices is equal to the sum I want? I'm a little lost with your explanation (maybe because I'm having trouble picturing this as recursive in the same way that the fibonacci numbers are recursive). I apologize for the lapse in time for responses; I was attempting to do this on my own without recursion. I came up with something but it is messy and, unfortunately, it does not output the correct answer.
– analysischallenged
Mar 26 at 3:11
the helper function returns true IF its current rotation matches the required sum behind it AND the recursive call is also true ELSE it tries the next rotation UNTIL it has tried all three THEN it returns false
– Patrick Parker
Mar 26 at 6:15
So my helper function is a boolean function that returns true if the sum of the two vertices is equal to the sum I want? I'm a little lost with your explanation (maybe because I'm having trouble picturing this as recursive in the same way that the fibonacci numbers are recursive). I apologize for the lapse in time for responses; I was attempting to do this on my own without recursion. I came up with something but it is messy and, unfortunately, it does not output the correct answer.
– analysischallenged
Mar 26 at 3:11
So my helper function is a boolean function that returns true if the sum of the two vertices is equal to the sum I want? I'm a little lost with your explanation (maybe because I'm having trouble picturing this as recursive in the same way that the fibonacci numbers are recursive). I apologize for the lapse in time for responses; I was attempting to do this on my own without recursion. I came up with something but it is messy and, unfortunately, it does not output the correct answer.
– analysischallenged
Mar 26 at 3:11
the helper function returns true IF its current rotation matches the required sum behind it AND the recursive call is also true ELSE it tries the next rotation UNTIL it has tried all three THEN it returns false
– Patrick Parker
Mar 26 at 6:15
the helper function returns true IF its current rotation matches the required sum behind it AND the recursive call is also true ELSE it tries the next rotation UNTIL it has tried all three THEN it returns false
– Patrick Parker
Mar 26 at 6:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348653%2fhow-to-solve-a-problem-with-arraylists-recursively%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"counterclockwise," but which comes first: triangle 0 or sum 0?
– Patrick Parker
Mar 26 at 2:34
Triangle 0 would come first, so I would use triangle 0 and triangle 1 to find sum 1, and so on, until I get to Triangle "size()-1". Then I would use that and Triangle 0 with sum 0. The size of the sum ArrayList will be one less than the size of the Triangle ArrayList.
– analysischallenged
Mar 26 at 3:13
I would use the Sudoku approach. Go around the circle and find the triangle that has the minimum number of valid orientations. If there's only one valid orientation, choose that orientation, and lock the triangle. If there are two or three valid orientations, then choose one, and recurse.
– user3386109
Mar 26 at 3:32
I think my problem is that I'm confused about the concept of recursion when it isn't used in sequences of numbers or situations like a factorial or a sum. In this case, I'm not really sure what the recursion is. How would I write the base case? I think I just need an example of how this is working like, for example, in the 4-triangle Circle that I gave as an example.
– analysischallenged
Mar 26 at 3:35
I would add a
state
variable to theTriangle
class. There are two states:Undecided
andLocked
. At each level of recursion, one triangle is assigned an orientation, and locked. Which means that at each level of recursion there is one less undecided triangle. Successful base case: all of the triangles are locked. Otherwise, find an undecided triangle with the least number of valid orientations. Choose an orientation for that triangle, lock it, and recurse. Note that there may be an undecided triangle with no valid orientations. That's the failed base case.– user3386109
Mar 26 at 4:45