forked from Burnout-Devil/HacktoberFest-DSA2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Burnout-Devil#21 from Premjeet1804/main
BFS
- Loading branch information
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Graph | ||
{ | ||
private int V; //number of nodes in the graph | ||
private LinkedList<Integer> adj[]; //adjacency list | ||
private Queue<Integer> queue; //maintaining a queue | ||
|
||
Graph(int v) | ||
{ | ||
V = v; | ||
adj = new LinkedList[v]; | ||
for (int i=0; i<v; i++) | ||
{ | ||
adj[i] = new LinkedList<>(); | ||
} | ||
queue = new LinkedList<Integer>(); | ||
} | ||
|
||
|
||
void addEdge(int v,int w) | ||
{ | ||
adj[v].add(w); //adding an edge to the adjacency list (edges are bidirectional in this example) | ||
} | ||
|
||
void BFS(int n) | ||
{ | ||
|
||
boolean nodes[] = new boolean[V]; //initialize boolean array for holding the data | ||
int a = 0; | ||
|
||
nodes[n]=true; | ||
queue.add(n); //root node is added to the top of the queue | ||
|
||
while (queue.size() != 0) | ||
{ | ||
n = queue.poll(); //remove the top element of the queue | ||
System.out.print(n+" "); //print the top element of the queue | ||
|
||
for (int i = 0; i < adj[n].size(); i++) //iterate through the linked list and push all neighbors into queue | ||
{ | ||
a = adj[n].get(i); | ||
if (!nodes[a]) //only insert nodes into queue if they have not been explored already | ||
{ | ||
nodes[a] = true; | ||
queue.add(a); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void main(String args[]) | ||
{ | ||
Graph graph = new Graph(6); | ||
|
||
graph.addEdge(0, 1); | ||
graph.addEdge(0, 3); | ||
graph.addEdge(0, 4); | ||
graph.addEdge(4, 5); | ||
graph.addEdge(3, 5); | ||
graph.addEdge(1, 2); | ||
graph.addEdge(1, 0); | ||
graph.addEdge(2, 1); | ||
graph.addEdge(4, 1); | ||
graph.addEdge(3, 1); | ||
graph.addEdge(5, 4); | ||
graph.addEdge(5, 3); | ||
|
||
System.out.println("The Breadth First Traversal of the graph is as follows:-"); | ||
|
||
graph.BFS(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ void SieveOfEratosthenes(int n) | |
if (prime[p]) | ||
cout << p << " "; | ||
} | ||
|
||
int main() | ||
{ | ||
int n = 30; | ||
|