-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
84 lines (67 loc) · 1.66 KB
/
Copy pathBFS.java
File metadata and controls
84 lines (67 loc) · 1.66 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//STUDENT: Rose Lin
//TEACHER: Mr. Radulovic
//DATE: May 7th, 2019
//DESCRIPTION: Mini ADT Assignment | Breadth First Search Class
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class BFS {
String st;
String end;
int size;
String[] posGenes;
AdjacencyList adjList;
public BFS(String st, String end, int size, String[] posGenes, AdjacencyList adjList) {
this.st = st;
this.end = end;
this.size = size;
this.posGenes = posGenes;
this.adjList = adjList;
}
public int compute() {
//distance array: provides distance between nodes (operates as a visited array and to count the number of mutations)
int[] distArray = new int [size];
Arrays.fill(distArray, -1);
Queue<String> que = new LinkedList<>();
que.add(st);
int index = 0;
for (int i = 0; i < size; i++) {
if (posGenes[i].equals(st)) {
index = i;
}
}
distArray[index] = 0;
if (st == end) {
return 0;
}
while (!que.isEmpty()) {
String x = que.poll();
//provides index for the string that is removed from the queue
int u = 0;
for (int i = 0; i < size; i++) {
if (posGenes[i].equals(x)) {
u = i;
}
}
//traverses through the nodes of the adj list
for (Edge e : adjList.G[u]) {
//provides an index for the start gene
int w = 0;
for (int i = 0; i < size; i++) {
if (posGenes[i].equals(e.gene)) {
w = i;
}
}
if (distArray[w]==-1) { //if false (ie. not visited)
distArray[w] = distArray[u]+1;
if (e.gene.equals(end)) {
return distArray[w] ;
}
que.add(e.gene);
}
}
}
return -1;
}
}