Algorithm to find connected tiles (percolation)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












I wrote a little program in JavaFX which displays a path of neighbouring open tiles going from top to bottom. It should simulate a percolation.



The algorithm iterates through the whole grid (every tile) populating a list with tiles that have a continuous connection to the top. It all works somewhat. My concern however is that it probably is highly inefficient and I don't really know how to improve said efficiency as we didn't learn anything of that sort in my school.



burnt is just another ArrayList initialised as a member variable.



GitHub link



public void searchCluster(Grid grid)
Node array = grid.getNodeArray();
ArrayList<Node> tmpBurnt = new ArrayList();
LinkedList<Node> tmpSave = new LinkedList();
Node left;
Node right;
Node below;

for(Node n : array[0])
if(n.isSet())
tmpBurnt.add(n);



for(;;)
if(tmpBurnt.isEmpty())
return;

for(Node n : tmpBurnt)
//-----check left--------
if(n.getX() > 0)
left = array[n.getY()][n.getX() - 1];
if(left.isSet() && !tmpBurnt.contains(left) && !this.burnt.contains(left))
tmpSave.add(left);


//-----check right--------
if(n.getX() < array[n.getY()].length - 1)
right = array[n.getY()][n.getX() + 1];
if(right.isSet() && !tmpBurnt.contains(right) && !this.burnt.contains(right))
tmpSave.add(right);


//-----check below--------
if(n.getY() < array.length - 1)
below = array[n.getY() + 1][n.getX()];
if(below.isSet() && !tmpBurnt.contains(below) && !this.burnt.contains(below))
tmpSave.add(below);



for(Node n : tmpBurnt)
this.burnt.add(n);

tmpBurnt.clear();
for(Node n : tmpSave)
tmpBurnt.add(n);

tmpSave.clear();








share|improve this question





















  • why do you scan left and right but not up and down?
    – Martin Frank
    Feb 14 at 13:05






  • 1




    @MartinFrank I actually do scan downwards, see the "check below" comment. I didn't scan upwards at the time which you correctly stated was a mistake.
    – Equiphract
    Apr 7 at 16:14
















up vote
3
down vote

favorite












I wrote a little program in JavaFX which displays a path of neighbouring open tiles going from top to bottom. It should simulate a percolation.



The algorithm iterates through the whole grid (every tile) populating a list with tiles that have a continuous connection to the top. It all works somewhat. My concern however is that it probably is highly inefficient and I don't really know how to improve said efficiency as we didn't learn anything of that sort in my school.



burnt is just another ArrayList initialised as a member variable.



GitHub link



public void searchCluster(Grid grid)
Node array = grid.getNodeArray();
ArrayList<Node> tmpBurnt = new ArrayList();
LinkedList<Node> tmpSave = new LinkedList();
Node left;
Node right;
Node below;

for(Node n : array[0])
if(n.isSet())
tmpBurnt.add(n);



for(;;)
if(tmpBurnt.isEmpty())
return;

for(Node n : tmpBurnt)
//-----check left--------
if(n.getX() > 0)
left = array[n.getY()][n.getX() - 1];
if(left.isSet() && !tmpBurnt.contains(left) && !this.burnt.contains(left))
tmpSave.add(left);


//-----check right--------
if(n.getX() < array[n.getY()].length - 1)
right = array[n.getY()][n.getX() + 1];
if(right.isSet() && !tmpBurnt.contains(right) && !this.burnt.contains(right))
tmpSave.add(right);


//-----check below--------
if(n.getY() < array.length - 1)
below = array[n.getY() + 1][n.getX()];
if(below.isSet() && !tmpBurnt.contains(below) && !this.burnt.contains(below))
tmpSave.add(below);



for(Node n : tmpBurnt)
this.burnt.add(n);

tmpBurnt.clear();
for(Node n : tmpSave)
tmpBurnt.add(n);

tmpSave.clear();








share|improve this question





















  • why do you scan left and right but not up and down?
    – Martin Frank
    Feb 14 at 13:05






  • 1




    @MartinFrank I actually do scan downwards, see the "check below" comment. I didn't scan upwards at the time which you correctly stated was a mistake.
    – Equiphract
    Apr 7 at 16:14












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I wrote a little program in JavaFX which displays a path of neighbouring open tiles going from top to bottom. It should simulate a percolation.



The algorithm iterates through the whole grid (every tile) populating a list with tiles that have a continuous connection to the top. It all works somewhat. My concern however is that it probably is highly inefficient and I don't really know how to improve said efficiency as we didn't learn anything of that sort in my school.



burnt is just another ArrayList initialised as a member variable.



GitHub link



public void searchCluster(Grid grid)
Node array = grid.getNodeArray();
ArrayList<Node> tmpBurnt = new ArrayList();
LinkedList<Node> tmpSave = new LinkedList();
Node left;
Node right;
Node below;

for(Node n : array[0])
if(n.isSet())
tmpBurnt.add(n);



for(;;)
if(tmpBurnt.isEmpty())
return;

for(Node n : tmpBurnt)
//-----check left--------
if(n.getX() > 0)
left = array[n.getY()][n.getX() - 1];
if(left.isSet() && !tmpBurnt.contains(left) && !this.burnt.contains(left))
tmpSave.add(left);


//-----check right--------
if(n.getX() < array[n.getY()].length - 1)
right = array[n.getY()][n.getX() + 1];
if(right.isSet() && !tmpBurnt.contains(right) && !this.burnt.contains(right))
tmpSave.add(right);


//-----check below--------
if(n.getY() < array.length - 1)
below = array[n.getY() + 1][n.getX()];
if(below.isSet() && !tmpBurnt.contains(below) && !this.burnt.contains(below))
tmpSave.add(below);



for(Node n : tmpBurnt)
this.burnt.add(n);

tmpBurnt.clear();
for(Node n : tmpSave)
tmpBurnt.add(n);

tmpSave.clear();








share|improve this question













I wrote a little program in JavaFX which displays a path of neighbouring open tiles going from top to bottom. It should simulate a percolation.



The algorithm iterates through the whole grid (every tile) populating a list with tiles that have a continuous connection to the top. It all works somewhat. My concern however is that it probably is highly inefficient and I don't really know how to improve said efficiency as we didn't learn anything of that sort in my school.



burnt is just another ArrayList initialised as a member variable.



GitHub link



public void searchCluster(Grid grid)
Node array = grid.getNodeArray();
ArrayList<Node> tmpBurnt = new ArrayList();
LinkedList<Node> tmpSave = new LinkedList();
Node left;
Node right;
Node below;

for(Node n : array[0])
if(n.isSet())
tmpBurnt.add(n);



for(;;)
if(tmpBurnt.isEmpty())
return;

for(Node n : tmpBurnt)
//-----check left--------
if(n.getX() > 0)
left = array[n.getY()][n.getX() - 1];
if(left.isSet() && !tmpBurnt.contains(left) && !this.burnt.contains(left))
tmpSave.add(left);


//-----check right--------
if(n.getX() < array[n.getY()].length - 1)
right = array[n.getY()][n.getX() + 1];
if(right.isSet() && !tmpBurnt.contains(right) && !this.burnt.contains(right))
tmpSave.add(right);


//-----check below--------
if(n.getY() < array.length - 1)
below = array[n.getY() + 1][n.getX()];
if(below.isSet() && !tmpBurnt.contains(below) && !this.burnt.contains(below))
tmpSave.add(below);



for(Node n : tmpBurnt)
this.burnt.add(n);

tmpBurnt.clear();
for(Node n : tmpSave)
tmpBurnt.add(n);

tmpSave.clear();










share|improve this question












share|improve this question




share|improve this question








edited Jan 16 at 23:20









Jamal♦

30.1k11114225




30.1k11114225









asked Jan 16 at 23:13









Equiphract

161




161











  • why do you scan left and right but not up and down?
    – Martin Frank
    Feb 14 at 13:05






  • 1




    @MartinFrank I actually do scan downwards, see the "check below" comment. I didn't scan upwards at the time which you correctly stated was a mistake.
    – Equiphract
    Apr 7 at 16:14
















  • why do you scan left and right but not up and down?
    – Martin Frank
    Feb 14 at 13:05






  • 1




    @MartinFrank I actually do scan downwards, see the "check below" comment. I didn't scan upwards at the time which you correctly stated was a mistake.
    – Equiphract
    Apr 7 at 16:14















why do you scan left and right but not up and down?
– Martin Frank
Feb 14 at 13:05




why do you scan left and right but not up and down?
– Martin Frank
Feb 14 at 13:05




1




1




@MartinFrank I actually do scan downwards, see the "check below" comment. I didn't scan upwards at the time which you correctly stated was a mistake.
– Equiphract
Apr 7 at 16:14




@MartinFrank I actually do scan downwards, see the "check below" comment. I didn't scan upwards at the time which you correctly stated was a mistake.
– Equiphract
Apr 7 at 16:14















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

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: "196"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185265%2falgorithm-to-find-connected-tiles-percolation%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185265%2falgorithm-to-find-connected-tiles-percolation%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?