Comparing two binary matrix grids for matching islands(connected regions of '1's)Compare two dates with JavaScriptHow to cycle through matrix blocks?Pattern matching for people who dont know algorithms - finding adjacent X's in a gridNumber of tours through m x n grid?Empty intersection countingCluster numbers in big matrixMaximum Area of rectangle without any monstersPHP combination of pairs for a 2D array as part of the solution for Christofides algorithmR: Extract values from matrix relative to row/column positionIn 2D binary matrix find the number of islands
Fedora boot screen shows both Fedora logo and Lenovo logo. Why and How?
Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback
What's currently blocking the construction of the wall between Mexico and the US?
What are the penalties for overstaying in USA?
What was the Shuttle Carrier Aircraft escape tunnel?
Is it damaging to turn off a small fridge for two days every week?
Find the diameter of a word graph
How risky is real estate?
Can Ogre clerics use Purify Food and Drink on humanoid characters?
Why do some games show lights shine thorugh walls?
Suggested order for Amazon Prime Doctor Who series
Long term BTC investing
C-152 carb heat on before landing in hot weather?
Except duplicates from duplicates based on columns
Java TreeMap.floorKey() equivalent for std::map
Hot coffee brewing solutions for deep woods camping
How does a monk's Martial Arts feature modify damage done by magical monk weapons?
How to remove this component from PCB
Should developer taking test phones home or put in office?
STM Microcontroller burns every time
How was Hillel permitted to go to the skylight to hear the shiur
Is this one of the engines from the 9/11 aircraft?
Apply brace expansion in "reverse order"
What is the origin of Scooby-Doo's name?
Comparing two binary matrix grids for matching islands(connected regions of '1's)
Compare two dates with JavaScriptHow to cycle through matrix blocks?Pattern matching for people who dont know algorithms - finding adjacent X's in a gridNumber of tours through m x n grid?Empty intersection countingCluster numbers in big matrixMaximum Area of rectangle without any monstersPHP combination of pairs for a 2D array as part of the solution for Christofides algorithmR: Extract values from matrix relative to row/column positionIn 2D binary matrix find the number of islands
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
It is very similar to this Number of Islands JavaScript challenge https://codereview.stackexchange.com/questions/201530/count-number-of-islands-2d-grid.
Given two grids where each cell of the grids contains either a '0' or a '1'. If two cells share a side then they are adjacent. Cells that contain '1' form a connected region(or island as commonly called) if any cell of that region can be reached by moving through the adjacent cells that contain '1'. Overlay the first grid onto the second and if a region of the first grid completely matches a region of the second grid, the regions are matched. Count total number of such matched regions in the second grid
Here's an example:
Grid 1
0 0 1
0 1 1
1 0 0
Grid 2
0 0 1
0 1 1
1 0 1
Grid 1 forms two regions at point (0,2), (1,1), (1,2) and (2,0)
Grid 2 forms two regions at point (0, 2), (1,1),(1,2),(2,2) and (2,0)
Region (2,0) is the only matching region so the answer here is 1.
This below is what I have done. My function countMatches() returns the number of connected regions in each grid, but what I want is to compare any connected region in grid 1 with that of grid 2 and return the number of matches i.e number of exact connected regions present in both. This result will be a single integer.
Also here is a codepen link to it - https://codepen.io/Baystef/pen/OqBJzG
JavaScript
function countMatches(grid1, grid2)
let gridOne = grid1.filter(n => typeof(n) !== 'number');
let gridTwo = grid2.filter(n => typeof(n) !== 'number');
//this only gives number of connected region in each grid, but what i want to get is number of islands that match perfectly when the grids are overlayed.
return `We have $countIslands(gridOne) island(s) in grid 1 and $countIslands(gridTwo) island(s) in grid 2`
function countIslands(grid)
let markIsland = function (grid, x, y, visited) ;
let visited = [];
for (let i = 0; i < grid.length; i++)
visited[i] = [];
let count = 0;
for (let x = 0; x < grid.length; x++)
for (let y = 0; y < grid[x].length; y++)
if (!visited[x][y] && grid[x][y] === '1')
count++;
markIsland(grid, x, y, visited);
visited[x][y] = true;
return count;
Sample grid input:-
const grid1_1 = [
4,
['0', '1', '0', '0'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid2_1 = [
4,
['0', '1', '0', '1'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid1_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '0']
]
const grid2_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '1']
]
The first line of each grid contains an integer which is the size of the grid array i.e 4 means it's a 4 X 4 grid.
If I run the function countMatches(grid1_1, grid2_1) it should return 2.
Reason is grid1_1 has 3 connected regions(islands) which are positions (0,1) , (1,0) and (1,3),(2,2),(2,3),(3,2),(3,3)
grid2_1 also has 3 connected regions(islands) at positions (0,1), (1,0) and (0,3),(1,3),(2,2),(2,3),(3,2),(3,3)
Regions (0,1), (1,0) are the only two regions that exist in grid1_1 and grid2_1 exclusively and that's why the function should return 2.
javascript algorithm matrix array-algorithms
|
show 1 more comment
It is very similar to this Number of Islands JavaScript challenge https://codereview.stackexchange.com/questions/201530/count-number-of-islands-2d-grid.
Given two grids where each cell of the grids contains either a '0' or a '1'. If two cells share a side then they are adjacent. Cells that contain '1' form a connected region(or island as commonly called) if any cell of that region can be reached by moving through the adjacent cells that contain '1'. Overlay the first grid onto the second and if a region of the first grid completely matches a region of the second grid, the regions are matched. Count total number of such matched regions in the second grid
Here's an example:
Grid 1
0 0 1
0 1 1
1 0 0
Grid 2
0 0 1
0 1 1
1 0 1
Grid 1 forms two regions at point (0,2), (1,1), (1,2) and (2,0)
Grid 2 forms two regions at point (0, 2), (1,1),(1,2),(2,2) and (2,0)
Region (2,0) is the only matching region so the answer here is 1.
This below is what I have done. My function countMatches() returns the number of connected regions in each grid, but what I want is to compare any connected region in grid 1 with that of grid 2 and return the number of matches i.e number of exact connected regions present in both. This result will be a single integer.
Also here is a codepen link to it - https://codepen.io/Baystef/pen/OqBJzG
JavaScript
function countMatches(grid1, grid2)
let gridOne = grid1.filter(n => typeof(n) !== 'number');
let gridTwo = grid2.filter(n => typeof(n) !== 'number');
//this only gives number of connected region in each grid, but what i want to get is number of islands that match perfectly when the grids are overlayed.
return `We have $countIslands(gridOne) island(s) in grid 1 and $countIslands(gridTwo) island(s) in grid 2`
function countIslands(grid)
let markIsland = function (grid, x, y, visited) ;
let visited = [];
for (let i = 0; i < grid.length; i++)
visited[i] = [];
let count = 0;
for (let x = 0; x < grid.length; x++)
for (let y = 0; y < grid[x].length; y++)
if (!visited[x][y] && grid[x][y] === '1')
count++;
markIsland(grid, x, y, visited);
visited[x][y] = true;
return count;
Sample grid input:-
const grid1_1 = [
4,
['0', '1', '0', '0'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid2_1 = [
4,
['0', '1', '0', '1'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid1_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '0']
]
const grid2_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '1']
]
The first line of each grid contains an integer which is the size of the grid array i.e 4 means it's a 4 X 4 grid.
If I run the function countMatches(grid1_1, grid2_1) it should return 2.
Reason is grid1_1 has 3 connected regions(islands) which are positions (0,1) , (1,0) and (1,3),(2,2),(2,3),(3,2),(3,3)
grid2_1 also has 3 connected regions(islands) at positions (0,1), (1,0) and (0,3),(1,3),(2,2),(2,3),(3,2),(3,3)
Regions (0,1), (1,0) are the only two regions that exist in grid1_1 and grid2_1 exclusively and that's why the function should return 2.
javascript algorithm matrix array-algorithms
in the first example you have the same count of cells. maybe you add some more information how do you count a region and how do you get a result. is the first number the row and second a col? btw, what have you tried?
– Nina Scholz
Mar 25 at 9:32
1
//Write code here--- good idea. Why don't you try it? The point of Stack Overflow is not to do it for you. If you have written code and are stuck at a specific point, show the code that you have written and ask a focused question about that code. Otherwise, you haven't done enough of the needed preliminary work to be able to ask a good question.
– John Coleman
Mar 25 at 9:56
@JohnColeman First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:14
@NinaScholz First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:15
Thank you for updating. I have upvoted your question as edited and removed by vote that the question be closed. The problem is that on Stack Overflow, many students copy-paste homework assignments (or things from online challenge sites), supply no code, and seem to want working to code which solves the problem (I say "seems" because such questions often don't even ask a question, so you have to guess their intentions). With the edit, it is now a much better question, (though it would be even better if you describe the problem with your code -- how is it failing to solve the problem?)
– John Coleman
Mar 25 at 12:44
|
show 1 more comment
It is very similar to this Number of Islands JavaScript challenge https://codereview.stackexchange.com/questions/201530/count-number-of-islands-2d-grid.
Given two grids where each cell of the grids contains either a '0' or a '1'. If two cells share a side then they are adjacent. Cells that contain '1' form a connected region(or island as commonly called) if any cell of that region can be reached by moving through the adjacent cells that contain '1'. Overlay the first grid onto the second and if a region of the first grid completely matches a region of the second grid, the regions are matched. Count total number of such matched regions in the second grid
Here's an example:
Grid 1
0 0 1
0 1 1
1 0 0
Grid 2
0 0 1
0 1 1
1 0 1
Grid 1 forms two regions at point (0,2), (1,1), (1,2) and (2,0)
Grid 2 forms two regions at point (0, 2), (1,1),(1,2),(2,2) and (2,0)
Region (2,0) is the only matching region so the answer here is 1.
This below is what I have done. My function countMatches() returns the number of connected regions in each grid, but what I want is to compare any connected region in grid 1 with that of grid 2 and return the number of matches i.e number of exact connected regions present in both. This result will be a single integer.
Also here is a codepen link to it - https://codepen.io/Baystef/pen/OqBJzG
JavaScript
function countMatches(grid1, grid2)
let gridOne = grid1.filter(n => typeof(n) !== 'number');
let gridTwo = grid2.filter(n => typeof(n) !== 'number');
//this only gives number of connected region in each grid, but what i want to get is number of islands that match perfectly when the grids are overlayed.
return `We have $countIslands(gridOne) island(s) in grid 1 and $countIslands(gridTwo) island(s) in grid 2`
function countIslands(grid)
let markIsland = function (grid, x, y, visited) ;
let visited = [];
for (let i = 0; i < grid.length; i++)
visited[i] = [];
let count = 0;
for (let x = 0; x < grid.length; x++)
for (let y = 0; y < grid[x].length; y++)
if (!visited[x][y] && grid[x][y] === '1')
count++;
markIsland(grid, x, y, visited);
visited[x][y] = true;
return count;
Sample grid input:-
const grid1_1 = [
4,
['0', '1', '0', '0'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid2_1 = [
4,
['0', '1', '0', '1'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid1_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '0']
]
const grid2_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '1']
]
The first line of each grid contains an integer which is the size of the grid array i.e 4 means it's a 4 X 4 grid.
If I run the function countMatches(grid1_1, grid2_1) it should return 2.
Reason is grid1_1 has 3 connected regions(islands) which are positions (0,1) , (1,0) and (1,3),(2,2),(2,3),(3,2),(3,3)
grid2_1 also has 3 connected regions(islands) at positions (0,1), (1,0) and (0,3),(1,3),(2,2),(2,3),(3,2),(3,3)
Regions (0,1), (1,0) are the only two regions that exist in grid1_1 and grid2_1 exclusively and that's why the function should return 2.
javascript algorithm matrix array-algorithms
It is very similar to this Number of Islands JavaScript challenge https://codereview.stackexchange.com/questions/201530/count-number-of-islands-2d-grid.
Given two grids where each cell of the grids contains either a '0' or a '1'. If two cells share a side then they are adjacent. Cells that contain '1' form a connected region(or island as commonly called) if any cell of that region can be reached by moving through the adjacent cells that contain '1'. Overlay the first grid onto the second and if a region of the first grid completely matches a region of the second grid, the regions are matched. Count total number of such matched regions in the second grid
Here's an example:
Grid 1
0 0 1
0 1 1
1 0 0
Grid 2
0 0 1
0 1 1
1 0 1
Grid 1 forms two regions at point (0,2), (1,1), (1,2) and (2,0)
Grid 2 forms two regions at point (0, 2), (1,1),(1,2),(2,2) and (2,0)
Region (2,0) is the only matching region so the answer here is 1.
This below is what I have done. My function countMatches() returns the number of connected regions in each grid, but what I want is to compare any connected region in grid 1 with that of grid 2 and return the number of matches i.e number of exact connected regions present in both. This result will be a single integer.
Also here is a codepen link to it - https://codepen.io/Baystef/pen/OqBJzG
JavaScript
function countMatches(grid1, grid2)
let gridOne = grid1.filter(n => typeof(n) !== 'number');
let gridTwo = grid2.filter(n => typeof(n) !== 'number');
//this only gives number of connected region in each grid, but what i want to get is number of islands that match perfectly when the grids are overlayed.
return `We have $countIslands(gridOne) island(s) in grid 1 and $countIslands(gridTwo) island(s) in grid 2`
function countIslands(grid)
let markIsland = function (grid, x, y, visited) ;
let visited = [];
for (let i = 0; i < grid.length; i++)
visited[i] = [];
let count = 0;
for (let x = 0; x < grid.length; x++)
for (let y = 0; y < grid[x].length; y++)
if (!visited[x][y] && grid[x][y] === '1')
count++;
markIsland(grid, x, y, visited);
visited[x][y] = true;
return count;
Sample grid input:-
const grid1_1 = [
4,
['0', '1', '0', '0'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid2_1 = [
4,
['0', '1', '0', '1'],
['1', '0', '0', '1'],
['0', '0', '1', '1'],
['0', '0', '1', '1']
]
const grid1_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '0']
]
const grid2_2 = [
3,
['0', '0', '1'],
['0', '1', '1'],
['1', '0', '1']
]
The first line of each grid contains an integer which is the size of the grid array i.e 4 means it's a 4 X 4 grid.
If I run the function countMatches(grid1_1, grid2_1) it should return 2.
Reason is grid1_1 has 3 connected regions(islands) which are positions (0,1) , (1,0) and (1,3),(2,2),(2,3),(3,2),(3,3)
grid2_1 also has 3 connected regions(islands) at positions (0,1), (1,0) and (0,3),(1,3),(2,2),(2,3),(3,2),(3,3)
Regions (0,1), (1,0) are the only two regions that exist in grid1_1 and grid2_1 exclusively and that's why the function should return 2.
javascript algorithm matrix array-algorithms
javascript algorithm matrix array-algorithms
edited Mar 25 at 17:52
Adebayo
asked Mar 25 at 9:27
Adebayo Adebayo
215 bronze badges
215 bronze badges
in the first example you have the same count of cells. maybe you add some more information how do you count a region and how do you get a result. is the first number the row and second a col? btw, what have you tried?
– Nina Scholz
Mar 25 at 9:32
1
//Write code here--- good idea. Why don't you try it? The point of Stack Overflow is not to do it for you. If you have written code and are stuck at a specific point, show the code that you have written and ask a focused question about that code. Otherwise, you haven't done enough of the needed preliminary work to be able to ask a good question.
– John Coleman
Mar 25 at 9:56
@JohnColeman First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:14
@NinaScholz First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:15
Thank you for updating. I have upvoted your question as edited and removed by vote that the question be closed. The problem is that on Stack Overflow, many students copy-paste homework assignments (or things from online challenge sites), supply no code, and seem to want working to code which solves the problem (I say "seems" because such questions often don't even ask a question, so you have to guess their intentions). With the edit, it is now a much better question, (though it would be even better if you describe the problem with your code -- how is it failing to solve the problem?)
– John Coleman
Mar 25 at 12:44
|
show 1 more comment
in the first example you have the same count of cells. maybe you add some more information how do you count a region and how do you get a result. is the first number the row and second a col? btw, what have you tried?
– Nina Scholz
Mar 25 at 9:32
1
//Write code here--- good idea. Why don't you try it? The point of Stack Overflow is not to do it for you. If you have written code and are stuck at a specific point, show the code that you have written and ask a focused question about that code. Otherwise, you haven't done enough of the needed preliminary work to be able to ask a good question.
– John Coleman
Mar 25 at 9:56
@JohnColeman First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:14
@NinaScholz First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:15
Thank you for updating. I have upvoted your question as edited and removed by vote that the question be closed. The problem is that on Stack Overflow, many students copy-paste homework assignments (or things from online challenge sites), supply no code, and seem to want working to code which solves the problem (I say "seems" because such questions often don't even ask a question, so you have to guess their intentions). With the edit, it is now a much better question, (though it would be even better if you describe the problem with your code -- how is it failing to solve the problem?)
– John Coleman
Mar 25 at 12:44
in the first example you have the same count of cells. maybe you add some more information how do you count a region and how do you get a result. is the first number the row and second a col? btw, what have you tried?
– Nina Scholz
Mar 25 at 9:32
in the first example you have the same count of cells. maybe you add some more information how do you count a region and how do you get a result. is the first number the row and second a col? btw, what have you tried?
– Nina Scholz
Mar 25 at 9:32
1
1
//Write code here --- good idea. Why don't you try it? The point of Stack Overflow is not to do it for you. If you have written code and are stuck at a specific point, show the code that you have written and ask a focused question about that code. Otherwise, you haven't done enough of the needed preliminary work to be able to ask a good question.– John Coleman
Mar 25 at 9:56
//Write code here --- good idea. Why don't you try it? The point of Stack Overflow is not to do it for you. If you have written code and are stuck at a specific point, show the code that you have written and ask a focused question about that code. Otherwise, you haven't done enough of the needed preliminary work to be able to ask a good question.– John Coleman
Mar 25 at 9:56
@JohnColeman First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:14
@JohnColeman First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:14
@NinaScholz First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:15
@NinaScholz First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:15
Thank you for updating. I have upvoted your question as edited and removed by vote that the question be closed. The problem is that on Stack Overflow, many students copy-paste homework assignments (or things from online challenge sites), supply no code, and seem to want working to code which solves the problem (I say "seems" because such questions often don't even ask a question, so you have to guess their intentions). With the edit, it is now a much better question, (though it would be even better if you describe the problem with your code -- how is it failing to solve the problem?)
– John Coleman
Mar 25 at 12:44
Thank you for updating. I have upvoted your question as edited and removed by vote that the question be closed. The problem is that on Stack Overflow, many students copy-paste homework assignments (or things from online challenge sites), supply no code, and seem to want working to code which solves the problem (I say "seems" because such questions often don't even ask a question, so you have to guess their intentions). With the edit, it is now a much better question, (though it would be even better if you describe the problem with your code -- how is it failing to solve the problem?)
– John Coleman
Mar 25 at 12:44
|
show 1 more comment
1 Answer
1
active
oldest
votes
You could get the count of islands first and then check the second array against the array with numbered islands.
This approach uses a simplified data set and an array with the length of the island count.
As result all truthy values of the matches array are counted and retuned.
Examples:
a b congruent islands/matching areas
------- ------- --------------------------------
0 1 0 0 0 1 0 1
2 0 0 3 1 0 0 1
0 0 3 3 0 0 1 1
0 0 3 3 0 0 1 1 3/islands 1 2 3
0 1 0 2 0 1 0 0
3 0 0 2 1 0 0 1
0 0 2 2 0 0 1 1
0 0 2 2 0 0 1 1 2/islands 1 3
0 0 1 0 0 1
0 1 1 0 1 1
2 0 0 1 0 1 2/islands 1 2
0 0 1 0 0 1
0 1 1 0 1 1
2 0 1 1 0 0 1/islands 2
function getIslands(array)
function test(array, i, j, count)
if (array[i] && array[i][j] === -1)
array[i][j] = count;
test(array, i - 1, j, count);
test(array, i + 1, j, count);
test(array, i, j - 1, count);
test(array, i, j + 1, count);
return true;
var count = 1,
islands = array.map(a => a.map(v => -v));
islands.forEach((a, i, aa) => a.forEach((_, j) => test(aa, i, j, count) && count++));
return islands, count: count - 1 ;
function countMatches(array1, array2)
var islands = getIslands(array1),
matches = Array.from( length: islands.count + 1 ).fill(true);
islands.islands.forEach((a, i) => a.forEach((v, j) => matches[v] = matches[v] && array2[i][j] && v));
islands.islands.forEach(a => console.log(...a));
console.log('');
array2.forEach(a => console.log(...a));
console.log('islands', ...matches.filter(Boolean));
return matches.reduce((s, b) => s + !!b, 0);
console.log(countMatches([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 0, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[0, 0, 1], [0, 1, 1], [1, 0, 0]]));.as-console-wrapper max-height: 100% !important; top: 0;
I changed var count = 1 to var count = 0 and it works perfectly, thanks.
– Adebayo
Mar 26 at 13:45
you need this value to be one, because this is the value where the islands are marked and counted. if you have a look toislandsbeforereturntakes place, the array has this values and it is important to distinguish between a value/island or zero/no island. if you start with zero, then you mark the first found island as zero and it vanished.
– Nina Scholz
Mar 26 at 13:50
Yes, you're correct it vanished, but leaving as 1 doesn't work too, the first test case should return 2 and not 3 because only 2 out of the 3 islands on grid 1 match exactly with that of grid 2
– Adebayo
Mar 26 at 17:48
i have still the problem, which array do you mean, even the question lacks the exact arrays and the wanted result.
– Nina Scholz
Mar 26 at 17:54
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%2f55334724%2fcomparing-two-binary-matrix-grids-for-matching-islandsconnected-regions-of-1s%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could get the count of islands first and then check the second array against the array with numbered islands.
This approach uses a simplified data set and an array with the length of the island count.
As result all truthy values of the matches array are counted and retuned.
Examples:
a b congruent islands/matching areas
------- ------- --------------------------------
0 1 0 0 0 1 0 1
2 0 0 3 1 0 0 1
0 0 3 3 0 0 1 1
0 0 3 3 0 0 1 1 3/islands 1 2 3
0 1 0 2 0 1 0 0
3 0 0 2 1 0 0 1
0 0 2 2 0 0 1 1
0 0 2 2 0 0 1 1 2/islands 1 3
0 0 1 0 0 1
0 1 1 0 1 1
2 0 0 1 0 1 2/islands 1 2
0 0 1 0 0 1
0 1 1 0 1 1
2 0 1 1 0 0 1/islands 2
function getIslands(array)
function test(array, i, j, count)
if (array[i] && array[i][j] === -1)
array[i][j] = count;
test(array, i - 1, j, count);
test(array, i + 1, j, count);
test(array, i, j - 1, count);
test(array, i, j + 1, count);
return true;
var count = 1,
islands = array.map(a => a.map(v => -v));
islands.forEach((a, i, aa) => a.forEach((_, j) => test(aa, i, j, count) && count++));
return islands, count: count - 1 ;
function countMatches(array1, array2)
var islands = getIslands(array1),
matches = Array.from( length: islands.count + 1 ).fill(true);
islands.islands.forEach((a, i) => a.forEach((v, j) => matches[v] = matches[v] && array2[i][j] && v));
islands.islands.forEach(a => console.log(...a));
console.log('');
array2.forEach(a => console.log(...a));
console.log('islands', ...matches.filter(Boolean));
return matches.reduce((s, b) => s + !!b, 0);
console.log(countMatches([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 0, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[0, 0, 1], [0, 1, 1], [1, 0, 0]]));.as-console-wrapper max-height: 100% !important; top: 0;
I changed var count = 1 to var count = 0 and it works perfectly, thanks.
– Adebayo
Mar 26 at 13:45
you need this value to be one, because this is the value where the islands are marked and counted. if you have a look toislandsbeforereturntakes place, the array has this values and it is important to distinguish between a value/island or zero/no island. if you start with zero, then you mark the first found island as zero and it vanished.
– Nina Scholz
Mar 26 at 13:50
Yes, you're correct it vanished, but leaving as 1 doesn't work too, the first test case should return 2 and not 3 because only 2 out of the 3 islands on grid 1 match exactly with that of grid 2
– Adebayo
Mar 26 at 17:48
i have still the problem, which array do you mean, even the question lacks the exact arrays and the wanted result.
– Nina Scholz
Mar 26 at 17:54
add a comment |
You could get the count of islands first and then check the second array against the array with numbered islands.
This approach uses a simplified data set and an array with the length of the island count.
As result all truthy values of the matches array are counted and retuned.
Examples:
a b congruent islands/matching areas
------- ------- --------------------------------
0 1 0 0 0 1 0 1
2 0 0 3 1 0 0 1
0 0 3 3 0 0 1 1
0 0 3 3 0 0 1 1 3/islands 1 2 3
0 1 0 2 0 1 0 0
3 0 0 2 1 0 0 1
0 0 2 2 0 0 1 1
0 0 2 2 0 0 1 1 2/islands 1 3
0 0 1 0 0 1
0 1 1 0 1 1
2 0 0 1 0 1 2/islands 1 2
0 0 1 0 0 1
0 1 1 0 1 1
2 0 1 1 0 0 1/islands 2
function getIslands(array)
function test(array, i, j, count)
if (array[i] && array[i][j] === -1)
array[i][j] = count;
test(array, i - 1, j, count);
test(array, i + 1, j, count);
test(array, i, j - 1, count);
test(array, i, j + 1, count);
return true;
var count = 1,
islands = array.map(a => a.map(v => -v));
islands.forEach((a, i, aa) => a.forEach((_, j) => test(aa, i, j, count) && count++));
return islands, count: count - 1 ;
function countMatches(array1, array2)
var islands = getIslands(array1),
matches = Array.from( length: islands.count + 1 ).fill(true);
islands.islands.forEach((a, i) => a.forEach((v, j) => matches[v] = matches[v] && array2[i][j] && v));
islands.islands.forEach(a => console.log(...a));
console.log('');
array2.forEach(a => console.log(...a));
console.log('islands', ...matches.filter(Boolean));
return matches.reduce((s, b) => s + !!b, 0);
console.log(countMatches([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 0, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[0, 0, 1], [0, 1, 1], [1, 0, 0]]));.as-console-wrapper max-height: 100% !important; top: 0;
I changed var count = 1 to var count = 0 and it works perfectly, thanks.
– Adebayo
Mar 26 at 13:45
you need this value to be one, because this is the value where the islands are marked and counted. if you have a look toislandsbeforereturntakes place, the array has this values and it is important to distinguish between a value/island or zero/no island. if you start with zero, then you mark the first found island as zero and it vanished.
– Nina Scholz
Mar 26 at 13:50
Yes, you're correct it vanished, but leaving as 1 doesn't work too, the first test case should return 2 and not 3 because only 2 out of the 3 islands on grid 1 match exactly with that of grid 2
– Adebayo
Mar 26 at 17:48
i have still the problem, which array do you mean, even the question lacks the exact arrays and the wanted result.
– Nina Scholz
Mar 26 at 17:54
add a comment |
You could get the count of islands first and then check the second array against the array with numbered islands.
This approach uses a simplified data set and an array with the length of the island count.
As result all truthy values of the matches array are counted and retuned.
Examples:
a b congruent islands/matching areas
------- ------- --------------------------------
0 1 0 0 0 1 0 1
2 0 0 3 1 0 0 1
0 0 3 3 0 0 1 1
0 0 3 3 0 0 1 1 3/islands 1 2 3
0 1 0 2 0 1 0 0
3 0 0 2 1 0 0 1
0 0 2 2 0 0 1 1
0 0 2 2 0 0 1 1 2/islands 1 3
0 0 1 0 0 1
0 1 1 0 1 1
2 0 0 1 0 1 2/islands 1 2
0 0 1 0 0 1
0 1 1 0 1 1
2 0 1 1 0 0 1/islands 2
function getIslands(array)
function test(array, i, j, count)
if (array[i] && array[i][j] === -1)
array[i][j] = count;
test(array, i - 1, j, count);
test(array, i + 1, j, count);
test(array, i, j - 1, count);
test(array, i, j + 1, count);
return true;
var count = 1,
islands = array.map(a => a.map(v => -v));
islands.forEach((a, i, aa) => a.forEach((_, j) => test(aa, i, j, count) && count++));
return islands, count: count - 1 ;
function countMatches(array1, array2)
var islands = getIslands(array1),
matches = Array.from( length: islands.count + 1 ).fill(true);
islands.islands.forEach((a, i) => a.forEach((v, j) => matches[v] = matches[v] && array2[i][j] && v));
islands.islands.forEach(a => console.log(...a));
console.log('');
array2.forEach(a => console.log(...a));
console.log('islands', ...matches.filter(Boolean));
return matches.reduce((s, b) => s + !!b, 0);
console.log(countMatches([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 0, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[0, 0, 1], [0, 1, 1], [1, 0, 0]]));.as-console-wrapper max-height: 100% !important; top: 0; You could get the count of islands first and then check the second array against the array with numbered islands.
This approach uses a simplified data set and an array with the length of the island count.
As result all truthy values of the matches array are counted and retuned.
Examples:
a b congruent islands/matching areas
------- ------- --------------------------------
0 1 0 0 0 1 0 1
2 0 0 3 1 0 0 1
0 0 3 3 0 0 1 1
0 0 3 3 0 0 1 1 3/islands 1 2 3
0 1 0 2 0 1 0 0
3 0 0 2 1 0 0 1
0 0 2 2 0 0 1 1
0 0 2 2 0 0 1 1 2/islands 1 3
0 0 1 0 0 1
0 1 1 0 1 1
2 0 0 1 0 1 2/islands 1 2
0 0 1 0 0 1
0 1 1 0 1 1
2 0 1 1 0 0 1/islands 2
function getIslands(array)
function test(array, i, j, count)
if (array[i] && array[i][j] === -1)
array[i][j] = count;
test(array, i - 1, j, count);
test(array, i + 1, j, count);
test(array, i, j - 1, count);
test(array, i, j + 1, count);
return true;
var count = 1,
islands = array.map(a => a.map(v => -v));
islands.forEach((a, i, aa) => a.forEach((_, j) => test(aa, i, j, count) && count++));
return islands, count: count - 1 ;
function countMatches(array1, array2)
var islands = getIslands(array1),
matches = Array.from( length: islands.count + 1 ).fill(true);
islands.islands.forEach((a, i) => a.forEach((v, j) => matches[v] = matches[v] && array2[i][j] && v));
islands.islands.forEach(a => console.log(...a));
console.log('');
array2.forEach(a => console.log(...a));
console.log('islands', ...matches.filter(Boolean));
return matches.reduce((s, b) => s + !!b, 0);
console.log(countMatches([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 0, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[0, 0, 1], [0, 1, 1], [1, 0, 0]]));.as-console-wrapper max-height: 100% !important; top: 0; function getIslands(array)
function test(array, i, j, count)
if (array[i] && array[i][j] === -1)
array[i][j] = count;
test(array, i - 1, j, count);
test(array, i + 1, j, count);
test(array, i, j - 1, count);
test(array, i, j + 1, count);
return true;
var count = 1,
islands = array.map(a => a.map(v => -v));
islands.forEach((a, i, aa) => a.forEach((_, j) => test(aa, i, j, count) && count++));
return islands, count: count - 1 ;
function countMatches(array1, array2)
var islands = getIslands(array1),
matches = Array.from( length: islands.count + 1 ).fill(true);
islands.islands.forEach((a, i) => a.forEach((v, j) => matches[v] = matches[v] && array2[i][j] && v));
islands.islands.forEach(a => console.log(...a));
console.log('');
array2.forEach(a => console.log(...a));
console.log('islands', ...matches.filter(Boolean));
return matches.reduce((s, b) => s + !!b, 0);
console.log(countMatches([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 0, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[0, 0, 1], [0, 1, 1], [1, 0, 0]]));.as-console-wrapper max-height: 100% !important; top: 0; function getIslands(array)
function test(array, i, j, count)
if (array[i] && array[i][j] === -1)
array[i][j] = count;
test(array, i - 1, j, count);
test(array, i + 1, j, count);
test(array, i, j - 1, count);
test(array, i, j + 1, count);
return true;
var count = 1,
islands = array.map(a => a.map(v => -v));
islands.forEach((a, i, aa) => a.forEach((_, j) => test(aa, i, j, count) && count++));
return islands, count: count - 1 ;
function countMatches(array1, array2)
var islands = getIslands(array1),
matches = Array.from( length: islands.count + 1 ).fill(true);
islands.islands.forEach((a, i) => a.forEach((v, j) => matches[v] = matches[v] && array2[i][j] && v));
islands.islands.forEach(a => console.log(...a));
console.log('');
array2.forEach(a => console.log(...a));
console.log('islands', ...matches.filter(Boolean));
return matches.reduce((s, b) => s + !!b, 0);
console.log(countMatches([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]], [[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 0, 1]]));
console.log(countMatches([[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[0, 0, 1], [0, 1, 1], [1, 0, 0]]));.as-console-wrapper max-height: 100% !important; top: 0; edited Mar 26 at 18:14
answered Mar 26 at 9:57
Nina ScholzNina Scholz
212k16 gold badges124 silver badges192 bronze badges
212k16 gold badges124 silver badges192 bronze badges
I changed var count = 1 to var count = 0 and it works perfectly, thanks.
– Adebayo
Mar 26 at 13:45
you need this value to be one, because this is the value where the islands are marked and counted. if you have a look toislandsbeforereturntakes place, the array has this values and it is important to distinguish between a value/island or zero/no island. if you start with zero, then you mark the first found island as zero and it vanished.
– Nina Scholz
Mar 26 at 13:50
Yes, you're correct it vanished, but leaving as 1 doesn't work too, the first test case should return 2 and not 3 because only 2 out of the 3 islands on grid 1 match exactly with that of grid 2
– Adebayo
Mar 26 at 17:48
i have still the problem, which array do you mean, even the question lacks the exact arrays and the wanted result.
– Nina Scholz
Mar 26 at 17:54
add a comment |
I changed var count = 1 to var count = 0 and it works perfectly, thanks.
– Adebayo
Mar 26 at 13:45
you need this value to be one, because this is the value where the islands are marked and counted. if you have a look toislandsbeforereturntakes place, the array has this values and it is important to distinguish between a value/island or zero/no island. if you start with zero, then you mark the first found island as zero and it vanished.
– Nina Scholz
Mar 26 at 13:50
Yes, you're correct it vanished, but leaving as 1 doesn't work too, the first test case should return 2 and not 3 because only 2 out of the 3 islands on grid 1 match exactly with that of grid 2
– Adebayo
Mar 26 at 17:48
i have still the problem, which array do you mean, even the question lacks the exact arrays and the wanted result.
– Nina Scholz
Mar 26 at 17:54
I changed var count = 1 to var count = 0 and it works perfectly, thanks.
– Adebayo
Mar 26 at 13:45
I changed var count = 1 to var count = 0 and it works perfectly, thanks.
– Adebayo
Mar 26 at 13:45
you need this value to be one, because this is the value where the islands are marked and counted. if you have a look to
islands before return takes place, the array has this values and it is important to distinguish between a value/island or zero/no island. if you start with zero, then you mark the first found island as zero and it vanished.– Nina Scholz
Mar 26 at 13:50
you need this value to be one, because this is the value where the islands are marked and counted. if you have a look to
islands before return takes place, the array has this values and it is important to distinguish between a value/island or zero/no island. if you start with zero, then you mark the first found island as zero and it vanished.– Nina Scholz
Mar 26 at 13:50
Yes, you're correct it vanished, but leaving as 1 doesn't work too, the first test case should return 2 and not 3 because only 2 out of the 3 islands on grid 1 match exactly with that of grid 2
– Adebayo
Mar 26 at 17:48
Yes, you're correct it vanished, but leaving as 1 doesn't work too, the first test case should return 2 and not 3 because only 2 out of the 3 islands on grid 1 match exactly with that of grid 2
– Adebayo
Mar 26 at 17:48
i have still the problem, which array do you mean, even the question lacks the exact arrays and the wanted result.
– Nina Scholz
Mar 26 at 17:54
i have still the problem, which array do you mean, even the question lacks the exact arrays and the wanted result.
– Nina Scholz
Mar 26 at 17:54
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%2f55334724%2fcomparing-two-binary-matrix-grids-for-matching-islandsconnected-regions-of-1s%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
in the first example you have the same count of cells. maybe you add some more information how do you count a region and how do you get a result. is the first number the row and second a col? btw, what have you tried?
– Nina Scholz
Mar 25 at 9:32
1
//Write code here--- good idea. Why don't you try it? The point of Stack Overflow is not to do it for you. If you have written code and are stuck at a specific point, show the code that you have written and ask a focused question about that code. Otherwise, you haven't done enough of the needed preliminary work to be able to ask a good question.– John Coleman
Mar 25 at 9:56
@JohnColeman First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:14
@NinaScholz First of all, forgive my ignorance, this is my first ever Stack overflow question and Secondly, I have updated the question and added my own code. Thanks
– Adebayo
Mar 25 at 12:15
Thank you for updating. I have upvoted your question as edited and removed by vote that the question be closed. The problem is that on Stack Overflow, many students copy-paste homework assignments (or things from online challenge sites), supply no code, and seem to want working to code which solves the problem (I say "seems" because such questions often don't even ask a question, so you have to guess their intentions). With the edit, it is now a much better question, (though it would be even better if you describe the problem with your code -- how is it failing to solve the problem?)
– John Coleman
Mar 25 at 12:44