From c4858ec71546ada314c2a980ba894e94dce73ea3 Mon Sep 17 00:00:00 2001 From: Rehan Date: Mon, 15 Sep 2025 19:59:38 +0530 Subject: [PATCH 1/2] Add graphs codes --- java/dsa/Graph/BFS.java | 26 +++++++++++++++++ java/dsa/Graph/DFS.java | 29 +++++++++++++++++++ java/dsa/Graph/FloodFill.java | 30 ++++++++++++++++++++ java/dsa/Graph/NumberOfProvinces.java | 41 +++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 java/dsa/Graph/BFS.java create mode 100644 java/dsa/Graph/DFS.java create mode 100644 java/dsa/Graph/FloodFill.java create mode 100644 java/dsa/Graph/NumberOfProvinces.java diff --git a/java/dsa/Graph/BFS.java b/java/dsa/Graph/BFS.java new file mode 100644 index 0000000..7b0edd0 --- /dev/null +++ b/java/dsa/Graph/BFS.java @@ -0,0 +1,26 @@ +public class BFS { + public List bfsGraph(int v, List> adj){ + List bfs = new ArrayList<>(); + boolean [] vis = new boolean[v]; + Deque q = new ArrayDeque<>(); + + q.add(0); + vis[0] = true; + + while(!q.isEmpty()){ + Integer node = q.poll(); + bfs.add(node); + + for(Integer it: adj.get(node)){ + if(vis[it] == false){ + vis[it] = true; + q.add(it); + } + + } + } + + return bfs; + + } +} diff --git a/java/dsa/Graph/DFS.java b/java/dsa/Graph/DFS.java new file mode 100644 index 0000000..5685dca --- /dev/null +++ b/java/dsa/Graph/DFS.java @@ -0,0 +1,29 @@ +class Solution { + boolean[] vis; + List ans; + List> adj; + + void dfs(int node) { + vis[node] = true; + ans.add(node); + for (Integer nei : adj.get(node)) { + if (!vis[nei]) { + dfs(nei); + } + } + } + + public List dfsOfGraph(int v, List> adj) { + vis = new boolean[v]; // nodes are 0 to v-1 + ans = new ArrayList<>(); + this.adj = adj; + + for (int i = 0; i < v; i++) { + if (!vis[i]) { + dfs(i); + } + } + + return ans; + } +} diff --git a/java/dsa/Graph/FloodFill.java b/java/dsa/Graph/FloodFill.java new file mode 100644 index 0000000..4bbecc6 --- /dev/null +++ b/java/dsa/Graph/FloodFill.java @@ -0,0 +1,30 @@ +public class FloodFill { + class Solution { + + + void dfs(int[][] image, int sr, int sc, int color, int oldColor){ + + if(sr<0 || sc<0 || sr>=image.length || sc>=image[0].length || image[sr][sc]!=oldColor){ + return; + } + image[sr][sc] = color; + + dfs(image, sr+1, sc, color, oldColor); + dfs(image, sr-1, sc, color, oldColor); + dfs(image, sr, sc+1, color, oldColor); + dfs(image, sr, sc-1, color, oldColor); + + } + + public int[][] floodFill(int[][] image, int sr, int sc, int color) { + + int oldColor = image[sr][sc]; + int newColor = color; + if(oldColor == newColor) return image; + + dfs(image, sr, sc, color, oldColor); + + return image; + } + } +} diff --git a/java/dsa/Graph/NumberOfProvinces.java b/java/dsa/Graph/NumberOfProvinces.java new file mode 100644 index 0000000..9817ded --- /dev/null +++ b/java/dsa/Graph/NumberOfProvinces.java @@ -0,0 +1,41 @@ +public class NumberOfProvinces { + //https://leetcode.com/problems/number-of-provinces/ + class Solution { + + + int[][] adjMatrix; + int vis[]; + int cnt; + + void dfs(int node){ + + vis[node] = 1; + + for(int i = 0; i < adjMatrix.length; i++){ + if(adjMatrix[node][i] == 1){ + if(vis[i]==0){ + dfs(i); + } + } + } + } + + + public int findCircleNum(int[][] isConnected) { + adjMatrix = isConnected; + int v = isConnected.length; + vis = new int[v]; + cnt = 0; + for(int i = 0; i < v; i++){ + if(vis[i] == 0){ + cnt++; + dfs(i); + } + } + + + return cnt; + + } + } +} From 22a0f1a6ae7d8e211dc11b0f618e6328abaf88b0 Mon Sep 17 00:00:00 2001 From: Rehan Date: Wed, 17 Sep 2025 17:13:13 +0530 Subject: [PATCH 2/2] Add graph solutions --- java/dsa/Graph/DetectCycleBFS.java | 39 ++++++++++++++++ java/dsa/Graph/DetectCycleDFS.java | 39 ++++++++++++++++ java/dsa/Graph/MarkOs.java | 65 ++++++++++++++++++++++++++ java/dsa/Graph/NearestCellHaving1.java | 57 ++++++++++++++++++++++ java/dsa/Graph/NumberOfEnclaves.java | 61 ++++++++++++++++++++++++ 5 files changed, 261 insertions(+) create mode 100644 java/dsa/Graph/DetectCycleBFS.java create mode 100644 java/dsa/Graph/DetectCycleDFS.java create mode 100644 java/dsa/Graph/MarkOs.java create mode 100644 java/dsa/Graph/NearestCellHaving1.java create mode 100644 java/dsa/Graph/NumberOfEnclaves.java diff --git a/java/dsa/Graph/DetectCycleBFS.java b/java/dsa/Graph/DetectCycleBFS.java new file mode 100644 index 0000000..97c25b4 --- /dev/null +++ b/java/dsa/Graph/DetectCycleBFS.java @@ -0,0 +1,39 @@ +public class DetectCycleBFS { + public boolean checkForCycle(int src, List> adj, boolean[] vis){ + vis[src] = true; + Queue q = new LinkedList<>(); + q.add(new Pair(src,-1)); + + while(!q.isEmpty()){ + int node = q.peek().first; + int parent = q.peek().second; + q.poll(); + + for(int v: adj.get(node)){ + if(!vis[v]){ + vis[v] = true; + q.offer(new Pair(v, node)); + } + else if(parent != v){ + return true; + } + } + } + return false; + } + + + public boolean isCycle(int V, List> adj){ + boolean vis[] = new boolean[V]; + + for(int i =0; i < V; i++){ + if(vis[i] == false){ + if(vis[i] == false && checkForCycle(i, adj, vis)) + return true; + } + } + return false; + + + } +} diff --git a/java/dsa/Graph/DetectCycleDFS.java b/java/dsa/Graph/DetectCycleDFS.java new file mode 100644 index 0000000..8524e0e --- /dev/null +++ b/java/dsa/Graph/DetectCycleDFS.java @@ -0,0 +1,39 @@ +public class DetectCycleDFS { + public boolean checkForCycle(int src, List> adj, boolean[] vis){ + vis[src] = true; + Queue q = new LinkedList<>(); + q.add(new Pair(src,-1)); + + while(!q.isEmpty()){ + int node = q.peek().first; + int parent = q.peek().second; + q.poll(); + + for(int v: adj.get(node)){ + if(!vis[v]){ + vis[v] = true; + q.offer(new Pair(v, node)); + } + else if(parent != v){ + return true; + } + } + } + return false; + } + + + public boolean isCycle(int V, List> adj){ + boolean vis[] = new boolean[V]; + + for(int i =0; i < V; i++){ + if(vis[i] == false){ + if(vis[i] == false && checkForCycle(i, adj, vis)) + return true; + } + } + return false; + + + } +} diff --git a/java/dsa/Graph/MarkOs.java b/java/dsa/Graph/MarkOs.java new file mode 100644 index 0000000..5d31f5d --- /dev/null +++ b/java/dsa/Graph/MarkOs.java @@ -0,0 +1,65 @@ +public class MarkOs { + void dfs(int r, int c, int[][] vis, char[][] matrix){ + if(vis[r][c] == 1) return; + + vis[r][c] = 1; + + int[] dRow = {-1, 0, 1, 0}; + int[] dCol = {0, 1, 0, -1}; + + for(int i = 0; i < 4; i++){ + int row = r+ dRow[i]; + int col = c + dCol[i]; + + if(row>=0&&col>=0&&row queue = new ArrayDeque<>(); + + // Initialize queue with all cells that have value 1 + for (int r = 0; r < numRows; r++) { + for (int c = 0; c < numCols; c++) { + if (grid[r][c] == 1) { + visited[r][c] = true; + queue.offer(new Cell(r, c, 0)); + } + } + } + + // Direction vectors for up, right, down, left + int[] dRow = {-1, 0, 1, 0}; + int[] dCol = {0, 1, 0, -1}; + + while (!queue.isEmpty()) { + Cell current = queue.poll(); + distances[current.row][current.col] = current.distance; + + // Explore neighbors + for (int i = 0; i < 4; i++) { + int newRow = current.row + dRow[i]; + int newCol = current.col + dCol[i]; + + if (newRow >= 0 && newRow < numRows && newCol >= 0 && newCol < numCols + && !visited[newRow][newCol]) { + + visited[newRow][newCol] = true; + queue.offer(new Cell(newRow, newCol, current.distance + 1)); + } + } + } + + return distances; + } + +} diff --git a/java/dsa/Graph/NumberOfEnclaves.java b/java/dsa/Graph/NumberOfEnclaves.java new file mode 100644 index 0000000..e6ef8f4 --- /dev/null +++ b/java/dsa/Graph/NumberOfEnclaves.java @@ -0,0 +1,61 @@ +public class NumberOfEnclaves { + class Solution { + void dfs(int r, int c, int[][] vis, int[][] matrix){ + if(vis[r][c] == 1) return; + + vis[r][c] = 1; + + int[] dRow = {-1, 0, 1, 0}; + int[] dCol = {0, 1, 0, -1}; + + for(int i = 0; i < 4; i++){ + int row = r+ dRow[i]; + int col = c + dCol[i]; + + if(row>=0&&col>=0&&row