Shortest Path and MST
This page needs to be organised and partitioned down to multiple pages
- A graph is bipartite if it is possible to color it using two colors. It turns out that a graph is bipartite exactly when it does not contain a cycle with an odd number of edges.
Krushkal’s algorithm
- Minimum Spanning Tree
- Use dsu and priority queue
Rerooting the Tree
void change_root(int old_root, int new_root) {
// Update data structures as if `new_root` was being removed as a child from `old_root`
// Update data structures as if `old_root` was being added as a child to `new_root`
}
void walk(int vertex, int parent) {
// Data structures now reflect the tree as if `vertex` were the root
for (child in adjacency_list[vertex]) {
if (child == parent) continue;
change_root(vertex, child);
walk(child, vertex);
change_root(child, vertex);
}
}
// Initialize data structures with respect to the initial root `root`
walk(root, -1);
Resources
- Shortest Path Modelling Tutorial
- How to use Dijkstra on state graph
- Algorithm Live : Tree’s Diameter, Center and Centroid
- Good approach towards theoratical proofs of tree’s properties
- Algorithm Thread : Tree Basics video and Gym Contest
- Covers Binary lifting for LCA, diameter and Euler tour to answer subtree queries using segment tree
- Gym has a good question to practice your basics
Problems
- Silly Sort
- Sweet
- Road Decoration
- FCAR
- Special Shortest Walk
- Two Closest
- Check out the random solution
- Civilization
- KOICOST
- Good MST like solution