forked from hussien89aa/DataStructureAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSGraph.java
More file actions
46 lines (42 loc) · 964 Bytes
/
BFSGraph.java
File metadata and controls
46 lines (42 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.graph;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BFSGraph {
int size;
adjList[] array;
public BFSGraph(int size) {
this.size=size;
array= new adjList[ this.size];
for(int i=0;i<size;i++){
array[i]=new adjList();
array[i].head=null;
}
}
public void add(int src, int dest){
Node n= new Node(dest, null);
n.next= array[src].head;
array[src].head=n;
}
public void BFSExplore( int StartVertex){
Boolean[] visited= new Boolean[size];
for(int i=0;i<size;i++)
visited[i]=false;
Queue<Integer> q= new LinkedList<Integer>();
q.add(StartVertex);
while( !q.isEmpty()){
int n= q.poll();
System.out.println("Visted node: "+ n);
visited[n]=true;
Node head= array[n].head;
while(head!=null){
if(visited[head.value]==false){
q.add(head.value);
visited[head.value]=true;
}else{
head=head.next;
}
}
}
}
}