0% found this document useful (0 votes)
22 views39 pages

GRAPHS

The document provides an extensive overview of graph theory, including definitions of key terms such as graphs, vertices, edges, and various types of graphs. It discusses graph representation methods like adjacency matrices and lists, as well as traversal algorithms such as DFS and BFS. Additionally, it covers concepts like spanning trees, minimum spanning trees, and the Disjoint Set data structure, along with their applications in real-world scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views39 pages

GRAPHS

The document provides an extensive overview of graph theory, including definitions of key terms such as graphs, vertices, edges, and various types of graphs. It discusses graph representation methods like adjacency matrices and lists, as well as traversal algorithms such as DFS and BFS. Additionally, it covers concepts like spanning trees, minimum spanning trees, and the Disjoint Set data structure, along with their applications in real-world scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

GRAPHS

Graph Terminology
Basic Graph Terminology:

1. Graph

A Graph G is a non-empty set of vertices (or nodes) V and a set of edges E, where each edge connects a pair of
vertices. Formally, a graph can be represented as G= (V, E). Graphs can be classified based on various properties, such
as directedness of edges and connectivity.

2. Vertex (Node)

A Vertex, often referred to as a Node, is a fundamental unit of a graph. It represents an entity within the graph. In
applications like social networks, vertices can represent individuals, while in road networks, they can represent
intersections or locations.

3. Edge

An Edge is a connection between two vertices in a graph. It can be either directed or undirected. In a directed graph,
edges have a specific direction, indicating a one-way connection between vertices. In contrast, undirected graphs
have edges that do not have a direction and represent bidirectional connections.

4. Degree of a Vertex

The Degree of a Vertex in a graph is the number of edges incident to that vertex. In a directed graph, the degree is
further categorized into the in-degree (number of incoming edges) and out-degree (number of outgoing edges) of
the vertex.

5. Path

A Path in a graph is a sequence of vertices where each adjacent pair is connected by an edge. Paths can be of varying
lengths and may or may not visit the same vertex more than once. The shortest path between two vertices is of
particular interest in algorithms such as Dijkstra's algorithm for finding the shortest path in weighted graphs.

6. Cycle

A Cycle in a graph is a path that starts and ends at the same vertex, with no repetitions of vertices (except the
starting and ending vertex, which are the same).

Applications of Graph:

• Transportation Systems: Google Maps employs graphs to map roads, where intersections are vertices and
roads are edges. It calculates shortest paths for efficient navigation.

• Social Networks: Platforms like Facebook model users as vertices and friendships as edges, using graph
theory for friend suggestions.

• World Wide Web: Web pages are vertices, and links between them are directed edges, inspiring Google's
Page Ranking Algorithm.

• Resource Allocation and Deadlock Prevention: Operating systems use resource allocation graphs to prevent
deadlocks by detecting cycles.
• Mapping Systems and GPS Navigation: Graphs help in locating places and optimizing routes in mapping
systems and GPS navigation.

• Graph Algorithms and Measures: Graphs are analyzed for structural properties and measurable quantities,
including dynamic properties in networks.

Graph Representation
1. Adjacency Matrix

2. Adjacency List

Adjacency Matrix Representation


An adjacency matrix is a way of representing a graph as a matrix of Boolean (0’s and 1’s)

Let’s assume there are n vertices in the graph So, create a 2D matrix adjMat[n][n] having dimension n x n.

• If there is an edge from vertex i to j, mark adjMat[i][j] as 1.

• If there is no edge from vertex i to j, mark adjMat[i][j] as 0.

Representation of Undirected Graph as Adjacency Matrix:

Representation of Directed Graph as Adjacency Matrix:

The below figure shows a directed graph. Initially, the entire Matrix is initialized to 0. If there is an edge from source
to destination, we insert 1 for that particular adjMat[destination].
Adjacency List Representation
An array of Lists is used to store edges between two vertices. The size of array is equal to the number of vertices (i.e.,
n). Each index in this array represents a specific vertex in the graph. The entry at the index i of the array contains a
linked list containing the vertices that are adjacent to vertex i.

Let’s assume there are n vertices in the graph So, create an array of list of size n as adjList[n].

• adjList[0] will have all the nodes which are connected (neighbour) to vertex 0.

• adjList[1] will have all the nodes which are connected (neighbour) to vertex 1 and so on.

Representation of Undirected Graph as Adjacency list:

Types Of Graphs
1. Null Graph

A graph is known as a null graph if there are no edges in the graph.

2. Trivial Graph

Graph having only a single vertex, it is also the smallest graph possible.

3. Undirected Graph

A graph in which edges do not have any direction. That is the nodes are unordered pairs in the definition of every
edge.

4. Directed Graph
A graph in which edge has direction. That is the nodes are ordered pairs in the definition of every edge.

5. Connected Graph

The graph in which from one node we can visit any other node in the graph is known as a connected graph.

6. Disconnected Graph

The graph in which at least one node is not reachable from a node is known as a disconnected graph.

7. Regular Graph

The graph in which the degree of every vertex is equal to K is called K regular graph.

8. Complete Graph

The graph in which from each node there is an edge to each other node.

9. Cycle Graph

The graph in which the graph is a cycle in itself, the minimum value of degree of each vertex is 2.

10. Cyclic Graph

A graph containing at least one cycle is known as a Cyclic graph.

11. Directed Acyclic Graph

A Directed Graph that does not contain any cycle.

12. Bipartite Graph


A graph in which vertex can be divided into two sets such that vertex in each set does not contain any edge between
them.

13. Weighted Graph

• A graph in which the edges are already specified with suitable weight is known as a weighted graph.

• Weighted graphs can be further classified as directed weighted graphs and undirected weighted graphs.

Graph Traversal
1. DFS

1. Input: Graph G(V,E) with vertices V, edges E, and a starting vertex s.

2. Output: DFS traversal order of nodes.

Steps (Recursive):

1. Start at s and mark it as visited.

2. Process s (e.g., print or store it).

3. For each unvisited neighbour v of s:

o Recursively call DFS on v.


2. BFS

1. Input: Graph G(V,E) with vertices V, edges E, and a starting vertex s.

2. Output: BFS traversal order of nodes.

Steps:

1. Initialize:

o A queue Q.

o A set visited to track visited nodes.

o Enqueue s (starting node) into QQQ and mark it visited.

2. While QQQ is not empty:

o Dequeue a node u from QQQ.

o Process u (e.g., print or store it).

o For each unvisited neighbour v of u:

▪ Enqueue v into QQQ.

▪ Mark v as visited.

3. End.
Table: Comparison of Graph Traversal Algorithms

Space
Algorithm Type Uses Time Complexity Graph Type
Complexity

Topological
Works on both
Recursive or sort,
DFS O(V + E) O(V) (stack) directed and
Iterative Connected
undirected graphs.
components

Iterative Shortest path Works on both


BFS (Queue- in unweighted O(V+E) O(V) (queue) directed and
based) graphs, Levels undirected graphs.

DFS with Cycle Detection

1. Maintain a recStack to track nodes in the current recursion stack.

2. For each node:

o If it is not visited, perform a DFS:

▪ Mark it as visited and add it to the recStack.

▪ Check neighbours. If a neighbour is in the recStack, a cycle is detected.

o Remove the node from the recStack after recursion ends.

BFS for Weighted Graphs

1. Use a priority queue to store {distance, node} pairs.

2. Start with the source node, set its distance to 0.

3. For every node processed:

o Update distances to all its neighbours if a shorter path is found.

o Push the updated neighbour into the priority queue.

Comparison Table: Traversal with Enhancements

Time Space
Algorithm Purpose Special Condition
Complexity Complexity

DFS with Cycle Detect cycles in directed


O(V + E) O(V) (stack) Works for directed graphs.
Detection graphs

Shortest paths in weighted O((V + E) \log Requires non-negative edge


Weighted BFS O(V + E)
graphs V) weights.
Time Space
Algorithm Purpose Special Condition
Complexity Complexity

Spanning Trees
A spanning tree is a subset of Graph G, such that all the vertices are connected using minimum possible number of
edges. Hence, a spanning tree does not have cycles and a graph may have more than one spanning tree.

Properties of a Spanning Tree:

• A Spanning tree does not exist for a disconnected graph.

• For a connected graph having N vertices then the number of edges in the spanning tree for that graph will
be N-1.

• A Spanning tree does not have any cycle.

• We can construct a spanning tree for a complete graph by removing E-N+1 edges, where E is the number of
Edges and N is the number of vertices.

• Cayley's Formula: It states that the number of spanning trees in a complete graph with N vertices is N^{N-2}

o For example: N=4, then maximum number of spanning tree possible =4^{4-2} = 16 (shown in the
above image).

Real World Applications of A Spanning Tree:

• Several path finding algorithms, such as Dijkstra's algorithm and A* search algorithm, internally build a
spanning tree as an intermediate step.

• Building Telecommunication Network.

• Image Segmentation to break an image into distinguishable components.

• Computer Network Routing Protocol


Minimum Spanning Tree (MST)
The weight of a spanning tree is determined by the sum of weight of all the edge involved in it.

A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among all the possible
spanning trees.

Properties of Minimum Spanning Tree:

• A minimum spanning tree connects all the vertices in the graph, ensuring that there is a path between any
pair of nodes.

• An MST is acyclic, meaning it contains no cycles. This property ensures that it remains a tree and not a graph
with loops.

• An MST with V vertices (where V is the number of vertices in the original graph) will have exactly V - 1 edges,
where V is the number of vertices.

• An MST is optimal for minimizing the total edge weight, but it may not necessarily be unique.

• The cut property states that if you take any cut (a partition of the vertices into two sets) in the original graph
and consider the minimum-weight edge that crosses the cut, that edge is part of the MST.

Minimum Spanning Tree of a Graph may not be Unique:

Like a spanning tree, there can also be many possible MSTs for a graph as shown in the below image:
Disjoint Set Data Structure
The Disjoint Set Union (DSU), also called Union-Find, is a data structure that keeps track of a partition of a set into
disjoint subsets. It is particularly useful in graph problems, such as finding connected components or implementing
Kruskal's algorithm for minimum spanning trees.

Core Operations

1. Find:

o Determines the representative (or "parent") of the subset to which an element belongs.

o Implements path compression for efficient lookups.

2. Union:

o Combines two subsets into one.

o Optimized by either:

▪ Union by Rank: Keeps the tree shallow by attaching the smaller tree under the root of the
larger tree.

▪ Union by Size: Similar to rank, but uses the number of nodes instead of the height of the
tree.

Disjoint Set [Union by Rank]

• Rank: Represents the approximate height of a tree.

• Key Idea: Attach the tree with a smaller rank under the root of the tree with a larger rank to keep the tree
flat.

Algorithm Steps

1. Initialization:

o Create an array parent[] where each element points to itself.

o Create a rank[] array initialized to 0.

2. Find with Path Compression:

o Ensures that each node points directly to the root.


3. Union by Rank:

o Merge sets based on their ranks.

Disjoint Set [Union by Size]

• Size: Represents the number of elements in the subset.

• Key Idea: Attach the smaller set to the root of the larger set to minimize the depth.

Algorithm Steps

1. Initialization:

o Create an array parent[] where each element points to itself.

o Create a size[] array initialized to 1.

2. Find with Path Compression:

o Same as the find function in Union by Rank.

3. Union by Size:

o Merge sets based on their sizes.


Comparison: Union by Rank vs Union by Size

Criterion Union by Rank Union by Size

Metric Used Tree height (rank) Number of elements in the subset

Data Structure Requires a rank[] array Requires a size[] array

Efficiency Works efficiently in all scenarios Works well but can be slightly slower

Implementation Slightly more complex Simpler logic for union operation

Time Complexity

• Find with Path Compression: O(α(N)), where α is the inverse Ackermann function, which grows extremely
slowly.

• Union: O(α(N))

Applications

1. Connected Components in Graphs.

2. Kruskal’s Algorithm for Minimum Spanning Tree.

3. Dynamic Connectivity Problems.

4. Cycle Detection in Graphs.

By combining path compression and union optimizations, DSU achieves near-constant time operations for practical
purposes.
Algorithms to Find Minimum Spanning Tree of a Graph:

1. Kruskal's Minimum Spanning Tree:


Kruskal's Minimum Spanning Tree (MST) algorithm is to connect all the vertices of a graph with the minimum total
edge weight while avoiding cycles. This algorithm employs a greedy approach, meaning it makes locally optimal
choices at each step to achieve a globally optimal solution.

Algorithm:

Step 1: Determine an arbitrary vertex as the starting vertex of the MST.


Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit

This algorithm can be implemented efficiently using a DSU (Disjoint-Set) data structure to keep track of the
connected components of the graph. This is used in a variety of practical applications such as network design,
clustering, and data analysis.
2. Prim's Minimum Spanning Tree:
Minimum Spanning Tree (MST) is to build the MST incrementally by starting with a single vertex and gradually
extending the tree by adding the closest neighbouring vertex at each step.
Algorithm:

• Create a set mstSet that keeps track of vertices already included in MST.

• Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE. Assign the key value
as 0 for the first vertex so that it is picked first.

• While mstSet doesn’t include all vertices

o Pick a vertex u that is not there in mstSet and has a minimum key value.

o Include u in the mstSet.

o Update the key value of all adjacent vertices of u. To update the key values, iterate through all
adjacent vertices.

o For every adjacent vertex v, if the weight of edge u-v is less than the previous key value of v,
update the key value as the weight of u-v.

The idea of using key values is to pick the minimum weight edge from the cut. The key values are used only for
vertices that are not yet included in MST, the key value for these vertices indicates the minimum weight edges
connecting them to the set of vertices included in MST.
Transitive Closure
Activity Networks
Critical Path
+
Shortest Path Algorithms
1. Single-Source Shortest Path (SSSP)

Breadth-First Search (BFS) for Unweighted Graphs

• Initialize a distance array with INT_MAX for all nodes.

• Set the source node’s distance to 0 and push it into a queue.

• While the queue is not empty:

o Pop the front node and update its neighbours' distances to current_distance + 1 if smaller.

o Push updated neighbours into the queue.


2. Dijkstra’s Algorithm (Weighted, Non-Negative)

• Initialize a distance array with INT_MAX and set source node distance to 0.

• Use a min-priority queue to store {distance, node} pairs.

• While the queue is not empty:

o Extract the node with the smallest distance.

o For each neighbour, update its distance if a shorter path is found and push it into the queue.
[All-Pairs Shortest Path]

3. Floyd-Warshall Algorithm (All-Pairs, Weighted, Negative Weights Allowed)

• Use a distance matrix, initialized with edge weights or INF for no edge.

• For each node k, update every pair i, j as:

o dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

• Repeat for all nodes as the intermediate node.


4. Bellman-Ford Algorithm (SSSP, Weighted, Negative Weights Allowed)

• Initialize a distance array with INT_MAX and set the source node distance to 0.

• For n−1n-1n−1 iterations:

o Relax all edges by checking if dist[u] + weight < dist[v].

• For cycle detection, repeat edge relaxation; if any distance improves, there's a negative cycle.
5. Shortest Path in a DAG

Algorithm: Topological Sort + Relaxation

• Perform a topological sort of the graph.


• Initialize distances with INT_MAX and set the source node distance to 0.
• For each node in topological order:

• Relax all its outgoing edges (update neighbour distances if shorter paths are found).
Topological Sort
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge
u-v, vertex u comes before v in the ordering.

Note: Topological Sorting for a graph is not possible if the graph is not a DAG.

Algorithm for Topological Sorting using DFS:

• Create a graph with n vertices and m-directed edges.

• Initialize a stack and a visited array of size n.

• For each unvisited vertex in the graph, do the following:

o Call the DFS function with the vertex as the parameter.

o In the DFS function, mark the vertex as visited and recursively call the DFS function for all unvisited
neighbours of the vertex.

o Once all the neighbours have been visited, push the vertex onto the stack.

• After all, vertices have been visited, pop elements from the stack and append them to the output list until
the stack is empty.
• The resulting list is the topologically sorted order of the graph.

Advantages of Topological Sort:

• Helps in scheduling tasks or events based on dependencies.

• Detects cycles in a directed graph.

• Efficient for solving problems with precedence constraints.

Disadvantages of Topological Sort:

• Only applicable to directed acyclic graphs (DAGs), not suitable for cyclic graphs.

• May not be unique, multiple valid topological orderings can exist.

Applications of Topological Sort:

• Task scheduling and project management.

• Dependency resolution in package management systems.

• Determining the order of compilation in software build systems.

• Deadlock detection in operating systems.

• Course scheduling in universities.

• It is used to find shortest paths in weighted directed acyclic graphs


Topological Sorting Using BFS:

Kahn’s Algorithm for Topological Sorting is a method used to order the vertices of a directed graph in a linear order
such that for every directed edge from vertex A to vertex B, A comes before B in the order. The algorithm works by
repeatedly finding vertices with no incoming edges, removing them from the graph, and updating the incoming edges
of the remaining vertices. This process continues until all vertices have been ordered.

Algorithm:
• Add all nodes with in-degree 0 to a queue.
• While the queue is not empty:
o Remove a node from the queue.
o For each outgoing edge from the removed node, decrement the in-degree of the destination node
by 1.
o If the in-degree of a destination node becomes 0, add it to the queue.
• If the queue is empty and there are still nodes in the graph, the graph contains a cycle and cannot be
topologically sorted.
• The nodes in the queue represent the topological ordering of the graph.

To find the in-degree of each node by initially calculating the number of incoming edges to each node. Iterate through
all the edges in the graph and increment the in-degree of the destination node for each edge. This way, you can
determine the in-degree of each node before starting the sorting process.
Bridges in Graph
A bridge in a graph is an edge that, if removed, increases the number of connected components. In other words, a
bridge is a critical edge whose removal disconnects parts of the graph.

Key Points
• Applicable to undirected graphs.
• Useful in network design and reliability analysis.
• Found using DFS in O(V+E), where V is the number of vertices and E is the number of edges.

Algorithm to Find Bridges


1. DFS Tree:
o Traverse the graph using Depth First Search (DFS).
o Record the discovery time (disc[]) and the lowest point (low[]) reachable from each node.
2. Bridge Detection:
o An edge (u, v) is a bridge if:
low[v] > disc[u]

Code for Finding Bridges


Articulation Points

An articulation point in a graph is a vertex that, if removed, increases the number of connected components. These
are critical nodes for connectivity.

Key Points

• Found using DFS.

• A vertex u is an articulation point if:

o It is the root of the DFS tree and has more than one child.

o Or, for a non-root vertex:

low[v] ≥ disc[u]

for any child v of u.

Code for Finding Articulation Points


Kosaraju's Algorithm

Kosaraju's algorithm finds Strongly Connected Components (SCCs) in a directed graph. An SCC is a maximal subgraph
where every vertex is reachable from every other vertex.

Steps

1. Perform DFS to record the finish times of vertices.

2. Transpose the graph (reverse all edges).

3. Perform DFS on the transposed graph, processing vertices in the decreasing order of finish times from Step
1. Each DFS call identifies one SCC.

Code for Kosaraju's Algorithm

Summary

• Bridges: Critical edges; DFS-based detection using low and disc arrays.

• Articulation Points: Critical vertices; DFS-based detection with similar logic to bridges.

• Kosaraju's Algorithm: Efficient O(V+E)O(V + E)O(V+E) method for finding SCCs in directed graphs.
Theoretical Questions

1. Terminology:

o Define the following terms with examples: graph, vertex, edge, degree, adjacency, path, and cycle.

o What is the difference between directed and undirected graphs?

2. Graph Types:

o Explain bipartite graphs and provide an example.

o What is a weighted graph, and how does it differ from an unweighted graph?

3. Representation:

o Compare and contrast adjacency list and adjacency matrix representations. Which one is more
efficient for sparse graphs?

4. Graph Traversal:

o Explain Breadth-First Search (BFS) and Depth-First Search (DFS) with real-life use cases.

5. Spanning Trees:

o What is a spanning tree? How does it differ from a minimum spanning tree?

6. Shortest Path:

o Explain Dijkstra's algorithm. How does it handle negative edge weights?


o What are the advantages and limitations of Bellman-Ford compared to Dijkstra's algorithm?

7. Topological Sorting:

o What is topological sorting? Why is it applicable only to Directed Acyclic Graphs (DAGs)?

8. Strongly Connected Components:

o Explain Kosaraju's algorithm and its time complexity.

9. Critical Connections:

o What are bridges and articulation points in a graph? How are they useful in network design?

10. Disjoint Sets:

• How do "Union by Rank" and "Union by Size" improve the efficiency of the Union-Find algorithm

11. Graph Representation:

• Write a program to represent a graph using an adjacency list and an adjacency matrix.

12. Graph Traversal:

• Implement BFS and DFS for a given graph.

13. Cycle Detection:

• Write a function to detect a cycle in a directed graph.

• Extend the above to detect a cycle in an undirected graph.

14. Minimum Spanning Tree:

• Implement Prim's algorithm to find the minimum spanning tree of a weighted graph.

• Write a program to find the minimum spanning tree using Kruskal’s algorithm.

15. Shortest Path:

• Write code to implement Dijkstra's algorithm for finding the shortest path in a weighted graph.

• Implement the Floyd-Warshall algorithm to find the shortest paths between all pairs of nodes.

16. Topological Sorting:

• Implement topological sorting using both DFS and Kahn's algorithm.

17. Strongly Connected Components:

• Write a program to find all Strongly Connected Components (SCCs) in a directed graph using Kosaraju's
algorithm.

18. Bridges and Articulation Points:

• Write a program to find all bridges in an undirected graph using DFS.

• Implement a function to find all articulation points in a graph.

19. Transitive Closure:

• Write a program to find the transitive closure of a directed graph using the Floyd-Warshall algorithm.

20. Graph Coloring:


• Implement a graph coloring algorithm to find the minimum number of colors required for a given graph.

LeetCode-Type Graph Questions


Easy-Level Questions

1. Graph Representation:

o Represent a graph using an adjacency list. [Example Problem: Convert Edge List to Adjacency List]

2. Graph Traversal:

o Implement BFS to find all nodes reachable from a given node. [Example Problem: Number of
Connected Components in an Undirected Graph]

3. Connected Components:

o Count the number of islands in a 2D grid. [Example Problem: Number of Islands]

4. Cycle Detection:

o Detect a cycle in an undirected graph. [Example Problem: Find if Path Exists in Graph]

5. Topological Sorting:

o Find the order of tasks given prerequisites. [Example Problem: Course Schedule II]

Medium-Level Questions

6. Shortest Path:

o Find the shortest path in a graph using BFS (unweighted graph). [Example Problem: Shortest Path in
Binary Matrix]

7. Minimum Spanning Tree:

o Find the MST of a weighted graph. [Example Problem: Connecting Cities with Minimum Cost]

8. Graph Coloring:

o Determine if a graph is bipartite. [Example Problem: Is Graph Bipartite?]

9. Critical Connections:

o Find all bridges in a graph. [Example Problem: Critical Connections in a Network]

10. Strongly Connected Components:

• Find the number of SCCs in a graph. [Example Problem: Kosaraju’s Algorithm Variant]

11. Shortest Path in Weighted Graph:

• Use Dijkstra's algorithm to find the shortest path. [Example Problem: Network Delay Time]

12. Union-Find:

• Find the number of connected components in an undirected graph. [Example Problem: Number of Provinces]
Hard-Level Questions

13. Articulation Points:

• Find all articulation points in a graph. [Example Problem: Critical Nodes in a Network]

14. Cycle Detection in Directed Graph:

• Detect a cycle in a directed graph. [Example Problem: Redundant Connection II]

15. Maximum Flow Problem:

• Solve the max flow problem using the Ford-Fulkerson algorithm. [Example Problem: Max Flow Variants]

16. Transitive Closure:

• Determine all reachable nodes from every node in a graph. [Example Problem: Transitive Closure Matrix]

17. Pathfinding in Weighted Graph:

• Find the shortest path in a weighted graph with negative weights. [Example Problem: Cheapest Flights Within
K Stops]

18. Topological Sorting:

• Find whether a topological sort exists for a graph. [Example Problem: Course Schedule]

19. Graph Reconstruction:

• Reconstruct a graph given certain conditions. [Example Problem: Reconstruct Itinerary]

20. 2D Grid Graph Problems:

• Find the shortest path in a maze. [Example Problem: The Maze II]

You might also like