Skip to content

Commit edea301

Browse files
Merge branch 'main' into newyash
2 parents e86aa57 + c2a1293 commit edea301

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

Programs/BreathFirstSearch.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// BFS algorithm in Java
2+
3+
import java.util.*;
4+
5+
public class Graph {
6+
private int V;
7+
private LinkedList<Integer> adj[];
8+
9+
// Create a graph
10+
Graph(int v) {
11+
V = v;
12+
adj = new LinkedList[v];
13+
for (int i = 0; i < v; ++i)
14+
adj[i] = new LinkedList();
15+
}
16+
17+
// Add edges to the graph
18+
void addEdge(int v, int w) {
19+
adj[v].add(w);
20+
}
21+
22+
// BFS algorithm
23+
void BFS(int s) {
24+
25+
boolean visited[] = new boolean[V];
26+
27+
LinkedList<Integer> queue = new LinkedList();
28+
29+
visited[s] = true;
30+
queue.add(s);
31+
32+
while (queue.size() != 0) {
33+
s = queue.poll();
34+
System.out.print(s + " ");
35+
36+
Iterator<Integer> i = adj[s].listIterator();
37+
while (i.hasNext()) {
38+
int n = i.next();
39+
if (!visited[n]) {
40+
visited[n] = true;
41+
queue.add(n);
42+
}
43+
}
44+
}
45+
}
46+
47+
public static void main(String args[]) {
48+
Graph g = new Graph(4);
49+
50+
g.addEdge(0, 1);
51+
g.addEdge(0, 2);
52+
g.addEdge(1, 2);
53+
g.addEdge(2, 0);
54+
g.addEdge(2, 3);
55+
g.addEdge(3, 3);
56+
57+
System.out.println("Following is Breadth First Traversal " + "(starting from vertex 2)");
58+
59+
g.BFS(2);
60+
}
61+
}

Programs/DepthFirstSearch.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// DFS algorithm in Java
2+
3+
import java.util.*;
4+
5+
class Graph {
6+
private LinkedList<Integer> adjLists[];
7+
private boolean visited[];
8+
9+
// Graph creation
10+
Graph(int vertices) {
11+
adjLists = new LinkedList[vertices];
12+
visited = new boolean[vertices];
13+
14+
for (int i = 0; i < vertices; i++)
15+
adjLists[i] = new LinkedList<Integer>();
16+
}
17+
18+
// Add edges
19+
void addEdge(int src, int dest) {
20+
adjLists[src].add(dest);
21+
}
22+
23+
// DFS algorithm
24+
void DFS(int vertex) {
25+
visited[vertex] = true;
26+
System.out.print(vertex + " ");
27+
28+
Iterator<Integer> ite = adjLists[vertex].listIterator();
29+
while (ite.hasNext()) {
30+
int adj = ite.next();
31+
if (!visited[adj])
32+
DFS(adj);
33+
}
34+
}
35+
36+
public static void main(String args[]) {
37+
Graph g = new Graph(4);
38+
39+
g.addEdge(0, 1);
40+
g.addEdge(0, 2);
41+
g.addEdge(1, 2);
42+
g.addEdge(2, 3);
43+
44+
System.out.println("Following is Depth First Traversal");
45+
46+
g.DFS(2);
47+
}
48+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ It is very easy to contribute, you may follow these steps -
101101
74. [Huffman Coding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/HuffmanCoding.java) - Program that demonstrates Huffman Coding for data compression.
102102
75. [ADT Fraction Program](https://github.com/PrajaktaSathe/Java/blob/main/Programs/ADTFractionApp.java) - Program to demontrate how to deal with numarators and denomenator
103103
76. [Number Array without duplicate elements](https://github.com/PrajaktaSathe/Java/blob/main/Programs/NoDupArray.java) - Program to demontrate Number array without duplicate elements
104+
77. [Depth First Search](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Gssarray.java) - Program demonstrates Depth First Search algorithm
105+
78. [Breath First Search](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Gssarray.java) - Program demonstrates Breath First Search algorithm
106+
104107

105108
# Contributors -
106109
## A big thank you to all our contributors!!!

0 commit comments

Comments
 (0)