Cheat Sheet (New) For Algorithms and Complexity
Cheat Sheet (New) For Algorithms and Complexity
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.
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