Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
37 views3 pages

MIT Quiz Mar

The document is a review for Quiz 2 of the MIT course 'Introduction to Algorithms', focusing on graph algorithms and problem-solving strategies. It covers topics such as graph reachability, shortest path algorithms, and common mistakes in graph problem definitions. Additionally, it presents specific problems for students to solve, emphasizing the importance of clearly defining graphs and connecting solutions back to original problems.

Uploaded by

shiva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

MIT Quiz Mar

The document is a review for Quiz 2 of the MIT course 'Introduction to Algorithms', focusing on graph algorithms and problem-solving strategies. It covers topics such as graph reachability, shortest path algorithms, and common mistakes in graph problem definitions. Additionally, it presents specific problems for students to solve, emphasizing the importance of clearly defining graphs and connecting solutions back to original problems.

Uploaded by

shiva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Algorithms: 6.

006
Massachusetts Institute of Technology
Instructors: Erik Demaine, Jason Ku, and Justin Solomon Quiz 2 Review

Quiz 2 Review

Scope
• Quiz 1 material fair game but explicitly not emphasized
• 6 lectures on graphs, L09-L14, 2 Problem Sets, PS5-PS6

Graph Problems
• Graph reachability by BFS or DFS in O(|E|) time
• Graph exploration/connected components via Full-BFS or Full-DFS
• Topological sort / Cycle detection via DFS
• Negative-weight cycle detection via Bellman-Ford
• Single Source Shortest Paths (SSSP)

Restrictions SSSP Algorithm


Graph Weights Name Running Time O(·)
DAG Any DAG Relaxation |V | + |E|
General Unweighted BFS |V | + |E|
General Non-negative Dijkstra |V | log |V | + |E|
General Any Bellman-Ford |V | · |E|
• All Pairs Shortest Paths (APSP)
– Run a SSSP algorithm |V | times
– Johnson’s solves APSP with negative weights in O(|V |2 log |V | + |V ||E|)

Graph Problem Strategies


• Be sure to explicitly describe a graph in terms of problem parameters
• Convert problem into finding a shortest path, cycle, topo. sort, conn. comps., etc.
• May help to duplicate graph vertices to encode additional information
• May help to add auxiliary vertices/edges to graph
• May help to pre-process the graph (e.g., to remove part of the graph)
Quiz 2 Review 2

Graph Problem Common Mistakes


• Define your graphs! Specify vertices, edges, and weights clearly (and count them!)

– (e.g., construct graph G = (V, E) with a vertex for each...


and a directed edge (u, v) with weight w for each...)

• State the problem you are solving, not just the algorithm you use to solve it

– (e.g., solve SSSP from s by running DAG Relaxation...)

• Connect the graph problem you solve back to the original problem

– (e.g., the weight of a path from s to t in G corresponds to the sum of tolls paid along a
driving route, so a path of minimum weight corresponds to a route minimizing tolls)

Problem 1. Counting Blobs (S18 Quiz 2)


An image is a 2D grid of black and white square pixels where each white pixel is contained in a
blob. Two white pixels are in the same blob if they share an edge of the grid. Black pixels are
not contained in blobs. Given an n × m array representing an image, describe an O(nm)-time
algorithm to count the number of blobs in the image.

Problem 2. Unicycles (S18 Quiz 2)


Given a connected undirected graph G = (V, E) with strictly positive weights w : E → Z+ where
|E| = |V |, describe an O(|V |)-time algorithm to determine a path from vertex s to vertex t of
minimum weight.

Problem 3. Doh!-nut (S18 Quiz 2)


Momer has just finished work at the FingSprield power plant at location p, and needs to drive to
his home at location h. But along the way, if his driving route ever comes within driving distance
k of a doughnut shop, he will stop and eat doughnuts, and his wife, Harge, will be angry. Momer
knows the layout of FingSprield, which can be modeled as a set of n locations, with two-way roads
of known driving distance connecting some pairs of locations (you may assume that no location
is incident to more than five roads), as well as the locations of the d doughnut shops in the city.
Describe an O(n log n)-time algorithm to find the shortest driving route from the power plant back
home that avoids driving within driving distance k of a doughnut shop (or determine no such route
exists).

Problem 4. Long Shortest Paths


Given directed graph G = (V, E) having arbitrary edge weights w : E → Z and two vertices
s, t ∈ V , describe an O(|V |3 )-time algorithm to find the minimum weight of any path from s to t
containing at least |V | edges.
MIT OpenCourseWare
https://ocw.mit.edu

6.006 Introduction to Algorithms


Spring 2020

For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms

You might also like