Old Exams Practice (topics may be different)
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
These questions are for study purposes. They are from past quizzes, so the content/topics may not necessarily match up. We do not provide solutions, but encourage you to come to office hours or post on piazza if you have any questions.
Topic: Graphs
1-1. If the heuristic, h(n), used to estimate distance in A* underestimates the distance to the destination, A* will still always find an optimal answer. 1-2. If the heuristic, h(n), used to estimate distance in A* overestimates the distance to the destination, A* will still always find an optimal answer. 1-3. Dijkstra's algorithm is a variant of depth-first search. 1-4. In a tree, the depth-first search and breadth-first search algorithms both find the same path between a source and destination node.
Which of the following are true?
Check all that apply.
2-1. This graph is a digraph. 2-2. This graph is a tree. 2-3. This graph is unweighted. 2-4. On this graph, breadth-first search visits fewer distinct nodes than depth-first search when finding the shortest path from node A to node D. 2-5. There is exactly one path between any pair of nodes.
Which of the following are true?
Check all that apply.
3-1. This graph has exactly 1 cycle. 3-2. Breadth-first search can be used to find a path between any pair of nodes in this graph.
4-1. A shortest path problem. 4-2. A minimum spanning tree problem. 4-3. A graph flow problem. 4-4. A graph partition problem.
def dfs_graph_search(graph, source, target, verbose=False):
def dfs_internal(path, shortest):
if verbose:
print('Current DFS path:', path_to_string(path))
last_node = path[-1]
if last_node == target:
if verbose:
print('Path found')
return path
if shortest and len(path) + 1 >= len(shortest):
return None
best_path = None
for next_node in graph.children_of(last_node):
if next_node in path:
continue
new_path = dfs_internal(path + [next_node], shortest)
if new_path:
if not best_path or len(new_path) < len(best_path):
best_path = new_path
if not shortest or len(new_path) < len(shortest):
shortest = new_path
return best_path
return dfs_internal([source], None)