Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

In general, m is the # of edges and n is the # of vertices

Finding path with the fewest edges and weight: Either add a small Max. flow / bottleneck has at most E values; sometimes simply trying all of
epsilon to each edge and run Dijkstra's as usual, or run Dijkstra's and them is the best solution - subgraphs in this case can be useful. (e.g.
break ties (of which node to process next) by path length. minimizing latency at different bottleneck capacities for data transmission)
Sometimes doing a binary search on all possible values is also a good
Finding distances to t from multiple different nodes in a directed graph: idea.
Reverse the graph (switch edge directions but retain the same weight),
then run Dijkstra's from node t to all other nodes. No three consecutive colors: New graph with (u, c_1, c_2), where c_1 is
the previous color, and c_2 is the one before that. With C as the color of u,
Vertices with weights instead of edges: Create an in-vertex and an add an edge between (u, c_1, c_2) and (v, C, c_1) if (u, v) is in the original
out-vertex for each original vertex, with an edge weighted by the original graph and not (c_1 = c_2 = C). We can also apply a similar approach to
vertex weight; all other edges have weight 0. find paths with length divisible by 3. Adding states to vertices in a new
graph can be helpful.
After running Dijkstra's, c(u) for each vertex u on the path is
non-decreasing from source to sink. Project selection profit = (sum of all projects) - cost of cut = sum of all
projects not cut - sum of all tools cut
Finding the longest path: Negate all edge weights, then running
Dijkstra's to find the path
Linear Programming
Replacement paths problem for a directed graph where the SS passes
through every vertex: Find shortest path if edge e is removed, for every e
in G. Run Dijkstra's to find c, sort all vertices by c(u). For every edge (u, u
+ 1) that's removed, the best replacement edge (x, y) has the lowest c(t) +
w(e') - (c(y) - c(x)) where x <= u and y >= u + 1. We can use a min. heap
to keep track of the current best replacement edge by iterating through
every edge (not in the shortest path) sorted by their starting position and
removing edges as they become invalid. Can also use a segment tree for
the last step.

Vertex disjoint paths: Create in- and out- vertices for each original vertex
connected by edge with weight 1. Convert all undirected edges to two
directed edges with weight 1. Then, max flow = # vertex-disjoint paths.
Edge disjoint paths: Same as above, but without adding the additional
vertices.

Maximum value closure: Positive nodes (that depend on negative


nodes) on the left, negative nodes (that are depended upon) on the right,
project selection formulation; Take all positive nodes that don’t depend on
negative nodes

Baseball elimination: Distribute remaining wins to either team involved in


each matchup; nodes on the left are each matchup, nodes on the right are
the teams, source -> left nodes edges have # games in that matchup as
weights, right nodes -> target edges have max. # games the team can win
without team i not being champion as weights. Team i is not eliminated if
all edges from source can be saturated in max. Flow.
NP Hardness

“To prove that problem A is NP-hard, reduce a known NP-hard


problem to A.”
Examples of reductions:

Larger Complexity Classes & Computability

● However, a Turing machine can be constructed to


compute a specific instance of a busy beaver function

DP components:
1. Prose description of subproblem
2. Base case & recurrence of subproblem
3. How to compute final formula
4. How to “fill in” table & runtime analysis

You might also like