Skip to content

Commit ce8934b

Browse files
committed
Graph ques and Trie added
1 parent d5eff93 commit ce8934b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsa-Graph/src/AdjListGraph.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.util.LinkedList;
22
import java.util.Queue;
3+
import java.util.Stack;
34

45
public class AdjListGraph {
56
// Undirected graph implementation using list.
@@ -43,13 +44,32 @@ public void bfs(int s){
4344
while (!q.isEmpty()){
4445
int u=q.poll();
4546
System.out.print(u+" ");
47+
4648
for(int v:adj[u]){
4749
if(!visited[v]){
4850
visited[v]=true;
4951
q.offer(v);
5052
}
5153
}
5254
}
55+
System.out.println();
56+
}
57+
public void dfs(int s){
58+
boolean[]visited=new boolean[V];
59+
Stack<Integer>stack=new Stack<>();
60+
stack.push(s);
61+
while (!stack.isEmpty()){
62+
int u=stack.pop();
63+
if(!visited[u]){
64+
visited[u]=true;
65+
System.out.print(u+" ");
66+
for (int v:adj[u]){
67+
if (!visited[v]){
68+
stack.push(v);
69+
}
70+
}
71+
}
72+
}
5373
}
5474

5575
public static void main(String[] args) {
@@ -60,6 +80,7 @@ public static void main(String[] args) {
6080
g.addEdge(3, 0);
6181
// 4
6282
g.bfs(0);
83+
g.dfs(0);
6384

6485

6586
}

dsa-Graph/src/NumberOfIslands.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class NumberOfIslands {
2+
3+
public static int numIslands(char[][]grid){
4+
int m=grid.length;
5+
int n=grid[0].length;
6+
boolean[][]visited=new boolean[m][n];
7+
int numOfIslands=0;
8+
for(int i=0;i<m;i++){
9+
for(int j=0;j<n;j++){
10+
if(!visited[i][j]&&grid[i][j]=='1'){
11+
dfs(grid,i,j,visited);
12+
numOfIslands++;
13+
}
14+
}
15+
}
16+
return numOfIslands;
17+
}
18+
public static void dfs(char[][] grid, int row, int col, boolean[][] visited){
19+
if(row<0||col<0||row> grid.length||col>= grid[0].length|| visited[row][col]||grid[row][col]=='0' ){
20+
return;
21+
}
22+
visited[row][col]=true;
23+
dfs(grid,row,col-1,visited);
24+
dfs(grid,row-1,col,visited);
25+
dfs(grid,row,col+1,visited);
26+
dfs(grid,row+1,col,visited);
27+
}
28+
29+
30+
public static void main(String[] args) {
31+
char [][]a={
32+
{1,1,0,0},
33+
{1,0,0,0},
34+
{0,0,1,0},
35+
{0,0,0,1}
36+
};
37+
int c=numIslands(a);
38+
System.out.println(c);
39+
}
40+
}

dsa-Trie/dsa-Trie.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

0 commit comments

Comments
 (0)