{"id":2021,"date":"2020-07-01T09:51:08","date_gmt":"2020-07-01T09:51:08","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=2021"},"modified":"2022-03-22T08:57:52","modified_gmt":"2022-03-22T08:57:52","slug":"number-of-islands","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/number-of-islands\/","title":{"rendered":"Number of islands"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png\" alt=\"\" \/><\/p>\n<h3>Concepts Used<\/h3>\n<blockquote>\n<p>Depth First Search, Disjoint Set<\/p>\n<\/blockquote>\n<h3>Difficulty Level<\/h3>\n<blockquote>\n<p>Medium<\/p>\n<\/blockquote>\n<h3>Problem Statement :<\/h3>\n<blockquote>\n<p>Given a 2D matrix, which contains only two numbers 0 or 1. In the map group of connected 0 represents, the water and group of connected 1 represent the island. Find the number of islands.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/graphs\/NUMOFILAND\" title=\"Go to mycode.prepbytes.com\" target=\"_blank\" rel=\"noopener noreferrer\"><u><strong><\/strong><\/u><\/a><\/p>\n<h3>Solution Approach :<\/h3>\n<h4>Introduction :<\/h4>\n<blockquote>\n<p>A group of connected 1&#8217;s represents an island.<br \/>\nIdea is to consider the 1&#8217;s and look for their neightbours if they form a component together it will be counted as 1 island.<\/p>\n<p>This problem can be solved by many ways like <strong>Breadth First Search<\/strong>, <strong>Depth First Search<\/strong> or <strong>Disjoint Set<\/strong>. Below we are going to discuss two of the above mentioned methods to solve this problem.<\/p>\n<\/blockquote>\n<h4>EXAMPLE:<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2020\/06\/numofinland.png\" alt=\"\" \/><\/p>\n<h4>Method 1 (Using DFS) :<\/h4>\n<blockquote>\n<p>In this method we are going to use <strong>Depth First Search<\/strong> or <strong>DFS<\/strong> to find total number of connected components. In <strong>dfs()<\/strong>, we are going to traverse all the adjacent vertices(cells) which are not yet visited and mark them as visited so we won&#8217;t traverse them again. Once we encounter a cell which has value <code>1<\/code>, then we will look for all its neighbour cells for the <code>1&#039;s<\/code> by calling <strong>dfs()<\/strong> and count them as a single component, then increment count by <code>1<\/code>.<br \/>\nWe can keep track if the node is visited or not, using a boolean <strong>visited[ ]<\/strong> array of size <code>n<\/code> which is initially set false (why <code>?<\/code>).<\/p>\n<p>Now the question arise, why are we counting the different calls made to <strong>dfs()<\/strong> <code>?<\/code> The answer lies in the fact that each time we call our dfs() function with some vertex <code>v<\/code>, it marks all vertices(cells) which are connected to <code>v<\/code> as visited, which means the we are visiting the vertices which are directly or indirectly connected to the <code>v<\/code> and has value <code>1<\/code>. This is considered as one island. So each call made up to dfs() with some unvisited vertex <code>v<\/code>, gives us the different connected islands. <\/p>\n<\/blockquote>\n<h4>Method 2 (Using Disjoint Set) :<\/h4>\n<blockquote>\n<p><strong>Disjoint Set Union<\/strong> or <strong>DSU<\/strong>  data structure is also sometimes called <strong>Union-Find Set<\/strong>. This data structure is used to keep track of the different disjoint sets.<br \/>\nIt basically performs two operations :<\/p>\n<ol>\n<li><strong>Union :<\/strong> It takes two vertices and join them in a single set (if it is not already there).<\/li>\n<li><strong>Find :<\/strong> Determines in which subset the element is.<\/li>\n<\/ol>\n<p>We will use a <code>parent[]<\/code> array to keep track of the parents of each vertex. <code>ith<\/code> element will store the root\/parent of <code>ith<\/code> vertex. Now, iterate through each cell or vertex of the matrix, if any cell has <code>1<\/code> then we will look for its neighbours if it has a value <code>1<\/code>. If we find any such neighbour we will perform <strong>union()<\/strong> for the cell and its neighbour (both having <code>1<\/code>) and update the <code>parent[]<\/code> array.<br \/>\nNow, iterate each value of the matrix again, this time if we will find <code>1<\/code>, we will look for its root (or its set).<br \/>\nNow, iterate for all elements of matrix again, for each vertex\/cell <code>c<\/code>, we will find root of <code>c<\/code> using <strong>find()<\/strong> and keep track of all distinct roots. Every time we encounter a distinct root increment the counter by <code>1<\/code>.<\/p>\n<p>Each disjoint set stores the vertices which are connected (directly or indirectly) to each other and has no relation\/connection with any other vertex (as it is non overlapping set), which means each set itself represents a connected islands and since each set contains exactly one vertex as its root, the sum of distinct roots of all sets gives us the total number of connected islands.<\/p>\n<\/blockquote>\n<h3>Algorithms :<\/h3>\n<blockquote>\n<h3><strong>dfs()<\/strong> :<\/h3>\n<ol>\n<li>For each call, for some vertex <code>v<\/code> ( dfs(<code>v<\/code>) ), we will mark the vertex <code>v<\/code> as visited (<code>visited[v]= true<\/code>).<\/li>\n<li>Iterate for all the adjacent vertices of <code>v<\/code> and for every adjacent vertex <code>a<\/code>, do following :\n<ul>\n<li>if <code>a<\/code> is not visited i.e <code>visited[a]= false<\/code>,<br \/>\nand if <code>a<\/code> has value <code>1<\/code>. <\/li>\n<li>recursively call <strong>dfs (<code>a<\/code>)<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><strong>union()<\/strong> :<\/h3>\n<ol>\n<li>For two vertices <code>u<\/code> &amp; <code>v<\/code>, find the root for both vertices using <strong>find()<\/strong> (<code>a=find(u)<\/code> &amp; <code>b = find(v)<\/code>).<\/li>\n<li>If the root of <code>u<\/code> &amp; <code>v<\/code> are not similar, check the level of <code>a<\/code> &amp; <code>b<\/code> as follows:\n<ul>\n<li>if (<code>level[a]&gt;* else if (<\/code>level[a]&gt;level[b]<code>), update <\/code>parent[b] = a`.<\/li>\n<li>else , update <code>parent[b]=a<\/code> &amp; update <code>level[a] = level[a]+1<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><strong>find()<\/strong> :<\/h3>\n<ol>\n<li>If <code>(parent[v]==v)<\/code>, returns the vertex <code>v<\/code> (which is root).<\/li>\n<li>Else, update <code>parent[v]<\/code> by recursively looking up the tree for the root. <code>(find(parent[v]))<\/code>.<\/li>\n<\/ol>\n<\/blockquote>\n<h3>Complexity Analysis:<\/h3>\n<blockquote>\n<p>The <strong>time complexity<\/strong> of the first method is represented in the form of <code>O(R*C)<\/code>, where <code>R<\/code> is the number of rows and <code>c<\/code> is the number of columns.<\/p>\n<p>Talking about method 2 using <strong>Union-Find<\/strong> data structure, since we have used path compression &amp; union by rank (refer to <strong>Disjoint Set<\/strong> article for more detailed explanation), we will reach nearly constant time <code>O(1)<\/code> for each query. But we are traversing the matrix which gives us the time complexity <code>O(R*C)<\/code> for this method as well.<\/p>\n<\/blockquote>\n<h3>Solutions:<\/h3>\n<p>\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_2031 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_2031 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_2031 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_2031 .wpsm_nav-tabs > li.active > a, #tab_container_2031 .wpsm_nav-tabs > li.active > a:hover, #tab_container_2031 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_2031 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_2031 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_2031 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_2031 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_2031 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_2031 .wpsm_nav-tabs > li > a:hover , #tab_container_2031 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_2031 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_2031 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_2031 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_2031 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_2031 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_2031 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_2031 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_2031 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_2031 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_2031 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_2031 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_2031\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_2031\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_2031_1\" aria-controls=\"tabs_desc_2031_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_2031_2\" aria-controls=\"tabs_desc_2031_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_2031_3\" aria-controls=\"tabs_desc_2031_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_2031\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_2031_1\">\r\n\t\t\t\t\t\t\t\t\r\n\r\n<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n#include &lt;stdbool.h&gt; \r\n    #include &lt;stdio.h&gt; \r\n    #include &lt;string.h&gt; \r\n\r\n    int ROW,COL;\r\n\r\n\r\n    int isSafe(int M[][COL], int row, int col, bool visited[][COL]) \r\n    { \r\n        return (row &gt;= 0) &amp;&amp; (row &lt; ROW) &amp;&amp; (col &gt;= 0) &amp;&amp; (col &lt; COL) &amp;&amp; (M[row][col] &amp;&amp; !visited[row][col]); \r\n    } \r\n\r\n    void DFS(int M[][COL], int row, int col, bool visited[][COL]) \r\n    { \r\n\r\n        static int rowNbr[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; \r\n        static int colNbr[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; \r\n\r\n        \/\/ Mark this cell as visited \r\n        visited[row][col] = true; \r\n\r\n        \/\/ Recur for all connected neighbours \r\n        for (int k = 0; k &lt; 8; ++k) \r\n            if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited)) \r\n                DFS(M, row + rowNbr[k], col + colNbr[k], visited); \r\n    } \r\n\r\n    int countIslands(int M[][COL]) \r\n    { \r\n\r\n        bool visited[ROW][COL]; \r\n        memset(visited, 0, sizeof(visited)); \r\n\r\n        int count = 0; \r\n        for (int i = 0; i &lt; ROW; ++i) \r\n            for (int j = 0; j &lt; COL; ++j) \r\n                if (M[i][j] &amp;&amp; !visited[i][j]) \r\n                { \r\n                    DFS(M, i, j, visited); \r\n                    ++count;  \r\n                } \r\n\r\n        return count; \r\n    } \r\n\r\n\r\n    int main() \r\n    { \r\n    int t;\r\n    scanf(\"%d\",&amp;t);\r\n    while(t--)\r\n    {\r\n    scanf(\"%d %d\",&amp;ROW,&amp;COL);\r\n        int M[ROW][COL];\r\n        for(int i=0;i&lt;ROW;i++)\r\n        {\r\n        for(int j=0;j&lt;COL;j++)\r\n        {\r\n            scanf(\"%d\",&amp;M[i][j]);\r\n        }\r\n        }\r\n\r\n        printf(\"%d&#92;n\", countIslands(M)); \r\n    }\r\n\r\n        return 0; \r\n    } \r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_2031_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n#include &lt;bits\/stdc++.h&gt; \r\n    using namespace std; \r\n\r\n    class  DisjointUnionSets \r\n    { \r\n\r\n    vector&lt;int&gt; rank, parent; \r\n    int n; \r\n\r\n    public: \r\n    DisjointUnionSets(int n) \r\n    { \r\n        rank.resize(n); \r\n        parent.resize(n); \r\n        this-&gt;n = n; \r\n        makeSet(); \r\n    } \r\n\r\n    void makeSet() \r\n    { \r\n        for(int i = 0; i &lt; n; i++) \r\n            parent[i] = i; \r\n    } \r\n\r\n        int find(int x) \r\n        { \r\n            if(parent[x] != x) \r\n            { \r\n\r\n                return parent[x] = find(parent[x]); \r\n            } \r\n\r\n        return x; \r\n    } \r\n\r\n        void Union(int x, int y) \r\n        { \r\n            int xRoot = find(x); \r\n            int yRoot = find(y); \r\n\r\n            if(xRoot == yRoot) \r\n                    return; \r\n\r\n            if(rank[xRoot] &lt; rank[yRoot]) \r\n                    parent[xRoot] = yRoot; \r\n\r\n            else if (rank[yRoot] &lt; rank[xRoot]) \r\n                    parent[yRoot] = xRoot; \r\n\r\n            else     \r\n            { \r\n                    parent[yRoot] = xRoot; \r\n\r\n                    \/\/ And increment the result tree's \r\n                    \/\/ rank by 1 \r\n                    rank[xRoot] = rank[xRoot] + 1; \r\n            } \r\n        } \r\n    }; \r\n\r\n    int countIslands(vector&lt;vector&lt;int&gt;&gt; a,int n,int m) \r\n    { \r\n\r\n\r\n        DisjointUnionSets *dus = new DisjointUnionSets(n * m); \r\n\r\n        for(int j = 0; j &lt; n; j++) \r\n        { \r\n            for(int k = 0; k &lt; m; k++) \r\n            { \r\n                    \/\/ If cell is 0, nothing to do \r\n                    if (a[j][k] == 0) \r\n                    continue; \r\n\r\n                    \/\/ Check all 8 neighbours and do a Union \r\n                    \/\/ with neighbour's set if neighbour is \r\n                    \/\/ also 1 \r\n                    if (j + 1 &lt; n &amp;&amp; a[j + 1][k] == 1) \r\n                    dus-&gt;Union(j * (m) + k,(j + 1) * (m) + k); \r\n                    if (j - 1 &gt;= 0 &amp;&amp; a[j - 1][k] == 1) \r\n                    dus-&gt;Union(j * (m) + k, \r\n                        (j - 1) * (m) + k); \r\n                    if (k + 1 &lt; m &amp;&amp; a[j][k + 1] == 1) \r\n                    dus-&gt;Union(j * (m) + k, \r\n                        (j) * (m) + k + 1); \r\n                    if (k - 1 &gt;= 0 &amp;&amp; a[j][k - 1] == 1) \r\n                    dus-&gt;Union(j * (m) + k, \r\n                        (j) * (m) + k - 1); \r\n                    if (j + 1 &lt; n &amp;&amp; k + 1 &lt; m &amp;&amp; \r\n                    a[j + 1][k + 1] == 1) \r\n                    dus-&gt;Union(j * (m) + k, \r\n                        (j + 1) * (m) + k + 1); \r\n                    if (j + 1 &lt; n &amp;&amp; k - 1 &gt;= 0 &amp;&amp; \r\n                    a[j + 1][k - 1] == 1) \r\n                    dus-&gt;Union(j * m + k, \r\n                        (j + 1) * (m) + k - 1); \r\n                    if (j - 1 &gt;= 0 &amp;&amp; k + 1 &lt; m &amp;&amp; \r\n                    a[j - 1][k + 1] == 1) \r\n                    dus-&gt;Union(j * m + k, \r\n                        (j - 1) * m + k + 1); \r\n                    if (j - 1 &gt;= 0 &amp;&amp; k - 1 &gt;= 0 &amp;&amp; \r\n                    a[j - 1][k - 1] == 1) \r\n                    dus-&gt;Union(j * m + k, \r\n                        (j - 1) * m + k - 1); \r\n        } \r\n    } \r\n\r\n\r\n    set&lt;int&gt; s;\r\n    for(int j = 0; j &lt; n; j++) \r\n        { \r\n        for(int k = 0; k &lt; m; k++) \r\n            { \r\n\r\n                if (a[j][k] == 1) \r\n                 s.insert(dus-&gt;find(j * m + k));\r\n            } \r\n        } \r\n        return s.size(); \r\n    } \r\n\r\n        int main(void) \r\n        { \r\n            int t;\r\n            cin&gt;&gt;t;\r\n            while(t--)\r\n            {\r\n                 int r,c;\r\n                 cin&gt;&gt;r&gt;&gt;c;\r\n                 vector&lt;vector&lt;int&gt;&gt; a(r);\r\n                 for(int i=0;i&lt;r;i++)\r\n                 {\r\n                 for(int j=0;j&lt;c;j++)\r\n                 {\r\n                 int t;\r\n                 cin&gt;&gt;t;\r\n                 a[i].push_back(t);\r\n                 }\r\n                 }\r\n\r\n                 cout&lt;&lt; countIslands(a,r,c) &lt;&lt; endl; \r\n            }\r\n        } \r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_2031_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n import java.io.*; \r\n    import java.util.*; \r\n\r\n    public class Main \r\n    { \r\n    public static void main(String[] args)throws IOException \r\n    { \r\n      Scanner sc = new Scanner(System.in);\r\n      int t = sc.nextInt();\r\n      while(t--&gt;0)\r\n      {\r\n       int r = sc.nextInt();\r\n       int c = sc.nextInt();\r\n       int [][]a = new int[r][c];\r\n\r\n       for(int i =0;i&lt;r;i++)\r\n       {\r\n         for(int j=0;j&lt;c;j++)\r\n         {\r\n           a[i][j] = sc.nextInt();\r\n         }\r\n       }\r\n\r\n       System.out.println(countIslands(a)); \r\n      }\r\n    } \r\n\r\n    \/\/ Returns number of islands in a[][] \r\n    static int countIslands(int a[][]) \r\n    { \r\n        int n = a.length; \r\n        int m = a[0].length; \r\n\r\n        DisjointUnionSets dus = new DisjointUnionSets(n*m); \r\n\r\n        \/* The following loop checks for its neighbours \r\n        and unites the indexes if both are 1. *\/\r\n        for (int j=0; j&lt;n; j++) \r\n        { \r\n            for (int k=0; k&lt;m; k++) \r\n            { \r\n                \/\/ If cell is 0, nothing to do \r\n                if (a[j][k] == 0) \r\n                    continue; \r\n\r\n                \/\/ Check all 8 neighbours and do a union \r\n                \/\/ with neighbour's set if neighbour is \r\n                \/\/ also 1 \r\n                if (j+1 &lt; n &amp;&amp; a[j+1][k]==1) \r\n                    dus.union(j*(m)+k, (j+1)*(m)+k); \r\n                if (j-1 &gt;= 0 &amp;&amp; a[j-1][k]==1) \r\n                    dus.union(j*(m)+k, (j-1)*(m)+k); \r\n                if (k+1 &lt; m &amp;&amp; a[j][k+1]==1) \r\n                    dus.union(j*(m)+k, (j)*(m)+k+1); \r\n                if (k-1 &gt;= 0 &amp;&amp; a[j][k-1]==1) \r\n                    dus.union(j*(m)+k, (j)*(m)+k-1); \r\n                if (j+1&lt;n &amp;&amp; k+1&lt;m &amp;&amp; a[j+1][k+1]==1) \r\n                    dus.union(j*(m)+k, (j+1)*(m)+k+1); \r\n                if (j+1&lt;n &amp;&amp; k-1&gt;=0 &amp;&amp; a[j+1][k-1]==1) \r\n                    dus.union(j*m+k, (j+1)*(m)+k-1); \r\n                if (j-1&gt;=0 &amp;&amp; k+1&lt;m &amp;&amp; a[j-1][k+1]==1) \r\n                    dus.union(j*m+k, (j-1)*m+k+1); \r\n                if (j-1&gt;=0 &amp;&amp; k-1&gt;=0 &amp;&amp; a[j-1][k-1]==1) \r\n                    dus.union(j*m+k, (j-1)*m+k-1); \r\n            } \r\n        } \r\n\r\n        \/\/ Array to note down frequency of each set \r\n        int[] c = new int[n*m]; \r\n        int numberOfIslands = 0; \r\n        for (int j=0; j&lt;n; j++) \r\n        { \r\n            for (int k=0; k&lt;m; k++) \r\n            { \r\n                if (a[j][k]==1) \r\n                { \r\n\r\n                    int x = dus.find(j*m+k); \r\n\r\n                    \/\/ If frequency of set is 0, \r\n                    \/\/ increment numberOfIslands \r\n                    if (c[x]==0) \r\n                    { \r\n                        numberOfIslands++; \r\n                        c[x]++; \r\n                    } \r\n\r\n                    else\r\n                        c[x]++; \r\n                } \r\n            } \r\n        } \r\n        return numberOfIslands; \r\n     } \r\n    } \r\n\r\n\r\n    class DisjointUnionSets \r\n    { \r\n    int[] rank, parent; \r\n    int n; \r\n\r\n    public DisjointUnionSets(int n) \r\n    { \r\n        rank = new int[n]; \r\n        parent = new int[n]; \r\n        this.n = n; \r\n        makeSet(); \r\n    } \r\n\r\n    void makeSet() \r\n    { \r\n        \/\/ Initially, all elements are in their \r\n        \/\/ own set. \r\n        for (int i=0; i&lt;n; i++) \r\n            parent[i] = i; \r\n    } \r\n\r\n    int find(int x) \r\n    { \r\n        if (parent[x] != x) \r\n        { \r\n            return parent[x] = find(parent[x]); \r\n        } \r\n\r\n        return x; \r\n    } \r\n\r\n    void union(int x, int y) \r\n    { \r\n        int xRoot = find(x); \r\n        int yRoot = find(y); \r\n        if (xRoot == yRoot) \r\n            return; \r\n\r\n        if (rank[xRoot] &lt; rank[yRoot]) \r\n            parent[xRoot] = yRoot; \r\n\r\n        else if(rank[yRoot]&lt;rank[xRoot]) \r\n            parent[yRoot] = xRoot; \r\n\r\n        else \r\n        { \r\n            parent[yRoot] = xRoot; \r\n            rank[xRoot] = rank[xRoot] + 1; \r\n        } \r\n      } \r\n    } \r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_2031 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_2031 a\"),jQuery(\"#tab-content_2031\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<br \/>\n[forminator_quiz id=&quot;2121&quot;]<\/p>\n<p>So, in this blog, we have tried to explain the concept of Depth First Search, Disjoint Set. If you want to solve more questions on Graphs, which are curated by our expert mentors at PrepBytes, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\">Graphs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Concepts Used Depth First Search, Disjoint Set Difficulty Level Medium Problem Statement : Given a 2D matrix, which contains only two numbers 0 or 1. In the map group of connected 0 represents, the water and group of connected 1 represent the island. Find the number of islands. Solution Approach : Introduction : A group [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[114],"tags":[],"class_list":["post-2021","post","type-post","status-publish","format-standard","hentry","category-graphs-interview-questions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Graphs Interview Questions | Number of Islands | Prepbytes<\/title>\n<meta name=\"description\" content=\"Disjoint Set Union or Dsu Data Structure Is Also Sometimes Called Union-find Set. This Data Structure Is Used to Keep Track of the Different Disjoint Sets.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/number-of-islands\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Graphs Interview Questions | Number of Islands | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Disjoint Set Union or Dsu Data Structure Is Also Sometimes Called Union-find Set. This Data Structure Is Used to Keep Track of the Different Disjoint Sets.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/number-of-islands\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-01T09:51:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-22T08:57:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Number of islands\",\"datePublished\":\"2020-07-01T09:51:08+00:00\",\"dateModified\":\"2022-03-22T08:57:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/\"},\"wordCount\":798,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png\",\"articleSection\":[\"Graphs Interview Questions\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/\",\"name\":\"Graphs Interview Questions | Number of Islands | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png\",\"datePublished\":\"2020-07-01T09:51:08+00:00\",\"dateModified\":\"2022-03-22T08:57:52+00:00\",\"description\":\"Disjoint Set Union or Dsu Data Structure Is Also Sometimes Called Union-find Set. This Data Structure Is Used to Keep Track of the Different Disjoint Sets.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/number-of-islands\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/number-of-islands\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Graphs Interview Questions\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/graphs-interview-questions\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Number of islands\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Graphs Interview Questions | Number of Islands | Prepbytes","description":"Disjoint Set Union or Dsu Data Structure Is Also Sometimes Called Union-find Set. This Data Structure Is Used to Keep Track of the Different Disjoint Sets.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/number-of-islands\/","og_locale":"en_US","og_type":"article","og_title":"Graphs Interview Questions | Number of Islands | Prepbytes","og_description":"Disjoint Set Union or Dsu Data Structure Is Also Sometimes Called Union-find Set. This Data Structure Is Used to Keep Track of the Different Disjoint Sets.","og_url":"https:\/\/prepbytes.com\/blog\/number-of-islands\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-07-01T09:51:08+00:00","article_modified_time":"2022-03-22T08:57:52+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Number of islands","datePublished":"2020-07-01T09:51:08+00:00","dateModified":"2022-03-22T08:57:52+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/"},"wordCount":798,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png","articleSection":["Graphs Interview Questions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/number-of-islands\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/","url":"https:\/\/prepbytes.com\/blog\/number-of-islands\/","name":"Graphs Interview Questions | Number of Islands | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png","datePublished":"2020-07-01T09:51:08+00:00","dateModified":"2022-03-22T08:57:52+00:00","description":"Disjoint Set Union or Dsu Data Structure Is Also Sometimes Called Union-find Set. This Data Structure Is Used to Keep Track of the Different Disjoint Sets.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/number-of-islands\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645180697277-Article_425.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/number-of-islands\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Graphs Interview Questions","item":"https:\/\/prepbytes.com\/blog\/category\/graphs-interview-questions\/"},{"@type":"ListItem","position":3,"name":"Number of islands"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/2021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=2021"}],"version-history":[{"count":12,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/2021\/revisions"}],"predecessor-version":[{"id":8152,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/2021\/revisions\/8152"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=2021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=2021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=2021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}