Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Breadth-First Search (BFS) Searching .java #2

Merged
merged 1 commit into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import java.util.*;

public class BFS {
public class Graph {
private int V; // Number of vertices
private LinkedList<Integer> adj[]; // Adjacency list representation of the graph
private LinkedList<Integer> adj[]; // Adjacency List

public BFS(int v) {
// Constructor
Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}

// Function to add an edge into the graph
// Add an edge to the graph
void addEdge(int v, int w) {
adj[v].add(w);
if (v >= 0 && v < V && w >= 0 && w < V) {
adj[v].add(w);
} else {
throw new IllegalArgumentException("Invalid edge: " + v + " -> " + w);
}
}

// BFS traversal starting from a given source vertex
// BFS traversal from a given source s
void BFS(int s) {
// Mark all the vertices as not visited
boolean visited[] = new boolean[V];

// Create a queue for BFS
LinkedList<Integer> queue = new LinkedList<>();
LinkedList<Integer> queue = new LinkedList<Integer>();

// Mark the current node as visited and enqueue it
visited[s] = true;
queue.add(s);

while (queue.size() != 0) {
// Dequeue a vertex from the queue and print it
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s + " ");

// Get all adjacent vertices of the dequeued vertex s. If an adjacent vertex has not been visited,
// then mark it visited and enqueue it
// Get all adjacent vertices of the dequeued vertex s.
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext()) {
int n = i.next();
Expand All @@ -46,26 +50,46 @@ void BFS(int s) {
}
}

// Driver method to
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of vertices: ");
int V = scanner.nextInt();
try {
System.out.print("Enter the number of vertices: ");
int V = scanner.nextInt();
if (V <= 0) {
throw new IllegalArgumentException("Number of vertices must be positive.");
}

BFS graph = new BFS(V);
Graph g = new Graph(V);

System.out.println("Enter the edges (vertex pairs, -1 to stop): ");
while (true) {
int v1 = scanner.nextInt();
if (v1 == -1) break;
int v2 = scanner.nextInt();
graph.addEdge(v1, v2);
}
System.out.println("Enter the number of edges: ");
int E = scanner.nextInt();
if (E < 0) {
throw new IllegalArgumentException("Number of edges must be non-negative.");
}

System.out.print("Enter the starting vertex for BFS: ");
int startVertex = scanner.nextInt();
System.out.println("Enter the edges (source destination): ");
for (int i = 0; i < E; i++) {
int source = scanner.nextInt();
int destination = scanner.nextInt();
g.addEdge(source, destination);
}

System.out.print("Enter the starting vertex for BFS: ");
int startVertex = scanner.nextInt();
if (startVertex < 0 || startVertex >= V) {
throw new IllegalArgumentException("Starting vertex is out of bounds.");
}

System.out.println("Breadth First Traversal starting from vertex " + startVertex + ":");
graph.BFS(startVertex);
System.out.println("BFS Traversal starting from vertex " + startVertex + ":");
g.BFS(startVertex);
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter integers only.");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} finally {
scanner.close();
}
}
}