Algorithm to find connected tiles (percolation)
Clash 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();
java performance algorithm graph javafx
add a comment |Â
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();
java performance algorithm graph javafx
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
add a comment |Â
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();
java performance algorithm graph javafx
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();
java performance algorithm graph javafx
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
add a comment |Â
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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
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
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
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
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