Skip to content

Commit a637925

Browse files
committed
BFS(BREADTH-FIRST-SEARCH) traversal in graph
0 parents  commit a637925

17 files changed

+399
-0
lines changed

Graph$Node.class

378 Bytes
Binary file not shown.

Graph.class

2.2 KB
Binary file not shown.

L

Whitespace-only changes.

LCS.class

907 Bytes
Binary file not shown.

LCS.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
class LCS{
3+
4+
static int solve(String a,String b){
5+
int n=a.length(),m=b.length();
6+
int[][] dp=new int[n+1][m+1];
7+
8+
for(int i=0;i<=n;i++){
9+
for(int j=0;j<=m;j++){
10+
if(i==0 || j==0){
11+
dp[i][j]=0;
12+
}
13+
else if(a.charAt(i-1)==b.charAt(j-1)){
14+
dp[i][j]= 1 + dp[i-1][j-1];
15+
}
16+
else{
17+
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
18+
}
19+
}
20+
}
21+
22+
return dp[n][m];
23+
}
24+
public static void main(String[] args){
25+
String a="abccdbba";
26+
String b="jksjccdbaisd";
27+
28+
//given two strings , need to find out the longest Common Subsequence
29+
30+
System.out.println(solve(a,b));
31+
}
32+
}

Main.class

658 Bytes
Binary file not shown.

Main.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.util.*;
2+
class Graph{
3+
4+
static HashMap<String,List<Node>> adj; //creating a adjacency List {source ->destinations}
5+
6+
public Graph(){
7+
this.adj=new HashMap<>(); //constructor initializes the adjacency list
8+
}
9+
10+
static class Node{ //Node class to store the destination place and the distance frome source to destination
11+
String dest;
12+
int dist;
13+
14+
Node(String dest,int dist){
15+
this.dest=dest;
16+
this.dist=dist;
17+
}
18+
}
19+
20+
public static void insert(String src,String dest,int dist){
21+
insert(src,new Node(dest,dist)); //Undirected weighted graph
22+
insert(dest,new Node(src,dist));
23+
}
24+
25+
private static void insert(String src,Node n){
26+
if(adj.isEmpty() || !adj.containsKey(src)){
27+
adj.put(src,new ArrayList<>());
28+
}
29+
adj.get(src).add(n);
30+
}
31+
32+
public static void BFS(String src){ // BREADTH-FIRST-SEARCH
33+
if(adj.isEmpty()) return;
34+
35+
HashSet<String> set=new HashSet<>();
36+
Queue<String> q=new LinkedList<>();
37+
q.offer(src);
38+
set.add(src);
39+
40+
while(!q.isEmpty()){
41+
String node=q.poll();
42+
System.out.print(node+" ");
43+
List<Node> neighbours=adj.get(src);
44+
if(neighbours!=null){
45+
for(Node neighbour:neighbours){
46+
if(neighbour!=null && !set.contains(neighbour.dest)){
47+
q.offer(neighbour.dest);
48+
set.add(neighbour.dest);
49+
}
50+
}
51+
}
52+
}
53+
}
54+
55+
}
56+
57+
class Main{
58+
public static void main(String[] args){
59+
Graph graph=new Graph(); //instantiating Graph by making an object graph of GRAPH
60+
graph.insert("DELHI","MUMBAI",990);
61+
graph.insert("MUMBAI","PUNE",100);
62+
graph.insert("PUNE","GOA",140);
63+
graph.insert("BANGALORE","PUNE",453);
64+
graph.insert("DELHI","PUNE",1056);
65+
graph.insert("DELHI","HYDERABAD",1099);
66+
graph.insert("VISHAKHAPATNAM","HYDERABAD",230);
67+
graph.insert("BANGALORE","DELHI",1000);
68+
graph.insert("CHENNAI","BANGALORE",600);
69+
graph.insert("CHENNAI","DELHI",1300);
70+
71+
graph.BFS("DELHI");
72+
}
73+
}

TREE$Node.class

428 Bytes
Binary file not shown.

TREE.class

2.7 KB
Binary file not shown.

customTree.class

771 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)