MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / Graph

Class Graph

Programs/BFS.java:10–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import java.util.Queue;
9
10class Graph {
11 private int v; // total number of vertices
12 private LinkedList<Integer> adj[]; // adjacency list
13 private boolean visited[];
14
15 Graph(int v) {
16 this.v = v;
17 this.adj = new LinkedList[v];
18 this.visited = new boolean[v];
19 for (int i = 0; i < this.v; i++) {
20 adj[i] = new LinkedList<Integer>();
21 }
22
23 }
24
25 public void addEdge(int u, int v) { // edge from u -> v
26 this.adj[u].add(v);
27 }
28
29 public void BFS(int source) {
30 Queue<Integer> queue = new LinkedList<Integer>();
31
32 queue.add(source);
33 visited[source] = true;
34
35 while (!queue.isEmpty()) {
36
37 int currNode = queue.poll();
38 System.out.print(currNode + " ");
39
40 for (int next : this.adj[currNode]) {
41 if (!visited[next]) {
42 visited[next] = true;
43 queue.add(next);
44 }
45 }
46 }
47 }
48
49 public void BFS_end() {
50
51 }
52
53}
54
55public class Main {
56 public static void main(String args[]) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected