In the first question, you will demonstrate your understanding of the behavior of the breadth-first search algorithm. There will be a problem similar to that from Homework 17 (reproduced here):
Suppose you have the following directed graph.
If you were to perform a breadth-first exploration, starting at S, in what order would the vertices be added to the frontier? Use the BFS algorithm in the lecture notes (findPath
). Assume the graph is fully explored (that is, that neither G1 nor G2 trigger the goal condition).
Assume that the neighbors of each vertex are returned in alphabetical order. For example, the only neighbors of S are A and B, in that order. But neither B nor A are neighbors of S. And, assume the graph is fully explored (that is, that neither G1 nor G2 trigger the goal condition).
S, B, D, A, C, G1, E, G2
In the second questions, you will show you understand the LIFO behavior of stacks and/or the FIFO behavior of queues (be prepared for either). For example, a question might state:
Suppose you have an empty stack, and you execute the following operations:
push(1);
push(2);
push(3);
peek()
pop();
push(1);
pop();
pop();
What are the contents of the stack? Be sure to clearly indicate the top and bottom of the stack.
The stack contains a single value:
bottom -> [1] <- top
The final question will ask you to write one of the stack or queue operations that we covered in class.
For example, a question might state:
Suppose we have a singly linked-list-based implementation of a Stack<E>
, built of Node<E>
elements that link to one another.
The Node<E>
class has a public Node<E> next
and a public E data
instance variable.
The Stack<E>
has a head
instance variable, pointing to the first Node
s of the list (or null
if the list is empty), as well as an int size
.
In other words, it is the implementation we wrote in lecture.
Write the void push(E e)
method.
public void push(E e) {
Node<E> node = new Node<>();
node.data = e;
node.next = head;
head = node;
size++;
}