Graph Handout
Graph Handout
Aditya Mishra
IICPC (Inter IIT competitive Programming Conclave)
Organisers:
Session Holders:
We are thankful to the session holders for the Graph Lectures without which
the camp would not be possible:
1
Contents
1 Graph Traversals 3
1.1 Breadth First Search . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Depth First Search . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Topological Sort 7
5 Spanning Trees 9
5.1 Kruskal’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.2 Prim’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
6 DP on trees 11
7 IICPC Resources 12
2
Chapter 1
Graph Traversals
A lot of graph problems boil down to simply traversing the graph in a certain
manner, and using the information obtained at each step in the traversal process.
3
• Path Sum
• Maximum area of island
• House Robber
• Contain Virus
Visualization: DFS
4
Chapter 2
5
• CSES-Negative cycle detection
Termination of the edge-iterations: Terminate when after a full traversal through
edges, no edge is relaxed. If there is no termination after (Num Nodes) iter-
ations, it means there is a negative edge cycle! Since the maximum number of
traversal is N (Num Nodes), the time complexity is
O(E*N).
6
Chapter 3
Topological Sort
In essence this method is the chopping of leaves sequentially, till you have no leaf
left. This is the soul of numerous interesting problem types, including diameter
finding and course scheduling. How it is done is basically you store all the leaf
nodes in a data structure (Queue or stack or anything) and then decrease the
number of neighbours of their neighbour. If the neighbour now has number of
neighbours==1, then it is a leaf and it is pushed in the data structure storing
leaves! This way you sequentially chop leaves and head towards to the center!
Implement it:
• CSES- Subordinates
• CSES- Tree Diameter
• CSES- Course Schedule
7
Chapter 4
The broad problem statement is: you have 2 sets of connected nodes A and
B, and you add an edge between an element of A and an element of B, thus
creating a single connected component C. This process is called a disjoint set
union. DSU involves mainly 2 kinds of operations:
• Find: Finding which set a given node belongs to.
• Union: Uniting the sets of 2 different nodes.
For carrying out DSU, we have a special kind of DSU data structure that
consists of a forest of trees. Each tree has a representative element that has no
parent, and each element in the tree except for the representative element has
a parent.
Find: Keep going to the parent as long as you don’t reach the representa-
tive element of the tree. Once you reach the representative element, its parent
attribute will be the size of the tree instead of a node’s address.
Union: To unite the trees of 2 nodes, first find their parents, and then make
the one with the larger tree size, as the parent of the other one.
• Connect Manhattan
• Regions cuts and slashes
8
Chapter 5
Spanning Trees
Cycle detection:
Maintain a DSU data stricture throughout the process, and for each node, see
if it is being added within a connected component or not. If it is, it will lead to
a cycle and hence it is to be skipped.
9
Practice MST!
• Repairing Roads
• Inverse the problem
10
Chapter 6
DP on trees
• Red-Black
• CSES-Tree Matching
• Distance in Tree
• CSES-Tree Distances I
• CSES-Tree Distances II
11
Chapter 7
IICPC Resources
• Lectures
• POTD list
12