I'm trying to implement a breadthFirstSearch function that takes in a graph, a start vertex, and an end vertex to search for, and locates the given vertex using a breadth-first search algorithm.
below is my code only using start, how would I add the end value so that if I do:
const list = {
'A': ['B', 'C'],
'B': ['A', 'F', 'G'],
'C': ['A', 'H'],
'D': [],
'E': ['F'],
'F': ['B', 'E', 'H', 'I'],
'G': ['B'],
'H': ['C', 'F'],
'I': ['F'],
}
const graph = new Graph(list);
breadthFirstSearch(graph, 'A', 'I') // true
const breadthFirstSearch = (graph, start, end) => {
const queue = [start];
const result = [];
const visited = {};
let currentVertex;
visited[start] = true;
while(queue.length){
currentVertex = queue.shift();
result.push(currentVertex);
this.adjacencyList[currentVertex].forEach(neighbor => {
if(!visited[neighbor]){
visited[neighbor] = true;
queue.push(neighbor);
}
});
}
return result;
}