Checkpoint_Week_2
Checkpoint_Week_2
Graph representation
Adjacency matrix : An adjacency matrix of a graph is a two-
dimensional array of size n x n, where n is the number of nodes in the
graph, with the property that a[ i ][ j ] = 1 if the edge (vᵢ, vⱼ) is in the set
of edges, and a[ i ][ j ] = 0 if there is no such edge
Adjacency list : We use an array of lists, where we have n lists, where n
is the number of vertices in the graph. The indices of the lists are the
respective nodes, and each list will store the vertices which are direct
neighbours of the respective vertex.
Graph traversals
Time complexity of BFS is O(n + m), where n is the number of vertices and
m is the number of edges.
The DFS tree is one of the most useful techniques for solving graph
structure related questions. It classifies the edges of the graph.
BFS starts at a chosen node (usually the root or any arbitrary node in a
graph) and explores its neighbors. Then it proceeds to the neighbors of
those neighbors, and so on, in a level-by-level manner. BFS uses a
queue data structure to manage the nodes to be explored. This queue
ensures that nodes are processed in the order they are discovered
Start from the root (or any arbitrary node in the case of a
graph)
Visit the node, mark it as visited, and enqueue its neighbors
Dequeue a node, process it, and enqueue its unvisited
neighbors
Repeat the process until all nodes are visited or the target
node is found.
Multisource BFS
DJIKSTRA’S ALGORITHM
shortest path from a single source node to all other nodes in a weighted
graph. It ensures that the total distance (sum of edge weights) from the
Key Characteristic
Finds the shortest path from a single source to all other nodes
efficiency.
These resources will help you to get a better idea on this algorithm :
Dijkstra's algorith
Problems
D jikstra
Planet
BELLMAN-FORD ALGORITHM
Key Characteristic
Works on graphs with negative weights, but not on graphs with
negative weight cycles
Uses relaxation to update distances iteratively
Can detect negative weight cycles in a graph
Has a higher time complexity than Dijkstra’s Algorithm, making it less
efficient for large graphs.
RESOURCES
PROBLEMS
to find the shortest paths between all pairs of nodes in a weighted graph.
Unlike Dijkstra’s and Bellman-Ford, which find the shortest path from a
single source, Floyd-Warshall finds the shortest path between every pair of
Key Characteristic
These resources will help you to get a better idea on this algorithm with
Practice Problem
Shortest Routes I
TREE ALGORITHMS
A tree is a connected, acyclic graph that consists of n nodes and n-1
edges. Moreover, the path between any two nodes of a tree is unique.
Note that since a tree is also a graph, all previous traversal algorithms also
work on it.
Tree Terminology
Root: An arbitrarily chosen node of the tree, considered the "top" of
the structure
Leaf: A node with a degree of 1, meaning it is connected only to one
other node (except in the case of a tree having only 1 node)
Parent: The immediate predecessor of a node along the path from the
node to the root
Ancestors: All the nodes on the path from a given node to the root,
including its parent and its parents ancestors
Subtree: The set of all nodes having the given node as an ancestor
Depth/Level: The distance of a given node from the root of the tre
Diameter: The longest path between any two nodes in a tree. There
can be multiple diameters in a tree
Centre: The middle vertex (or two middle vertices) in the longest path
(diameter) of the tree. Could be different for each diameter in a tree.
Practice Problems
Tree Diameter CSE
Tree Distances I CSE
Dynamic Diameter (CF Edu)
Prim’s Algorithm:
RESOURCES
Kruskal's Algorithm:
algorithm goes through the edges sorted by the edge weight going lower to
higher, and adds an edge to the tree if it does not create a cycle which can
edge is added to the tree, two components are merged into a single
RESOURCES
Each disjoint set is structured as a tree, with one element designated as the
representative (root) of the set. This root serves as the identifier for the set.
Note that if two elements are in the same set, they have the same root.
Union_Set: Optimally combines the two sets of the given two elements
into a single set or does nothing if both the elements are in the same set
Find_Set: Finds the representative (root) of the set containing the given
element.
RESOURCES
Implementation
Practice Problems:
DP on Trees
Dynamic Programming (DP) on trees is a technique used to efficiently solve
problems involving tree structures by breaking them down into smaller
subproblems, typically in the form of subtrees, and storing intermediate
results to avoid redundant computations.
The most popular idea in tree DP is to store subtree properties and analyze
how a child influences its parent or vice versa. Developing this intuition is
essential for effectively applying DP to tree-based problems.
RESOURCES
CF Blog on DP on Trees
Classic Problems
Subordinates CSE
Tree Matching CSE
Tree Diameter CSE
Tree Distances II CSES
Practice Problems
Game on Tree (easy
Game on Tree (medium
Gardening Friend
s s a and Chinchill
K yu h
PermuTree (easy version
Game on Tree (hard) (HARD
Sasha and a Walk in the City(HARD
Vlad and Trouble at MIT (HARD
TREE XOR (HARD
Score of a Tree (HARD)