GRAPHS
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
Let’s assume there are n vertices in the graph So, create a 2D matrix adjMat[n][n] having dimension n x n.
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.
Types Of Graphs
1. Null 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.
• 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
Steps (Recursive):
Steps:
1. Initialize:
o A queue Q.
▪ 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
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.
• For a connected graph having N vertices then the number of edges in the spanning tree for that graph will
be N-1.
• 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).
• Several path finding algorithms, such as Dijkstra's algorithm and A* search algorithm, internally build a
spanning tree as an intermediate step.
A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among all the possible
spanning trees.
• 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.
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.
2. Union:
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.
• 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:
• Key Idea: Attach the smaller set to the root of the larger set to minimize the depth.
Algorithm Steps
1. Initialization:
3. Union by Size:
Efficiency Works efficiently in all scenarios Works well but can be slightly slower
Time Complexity
• Find with Path Compression: O(α(N)), where α is the inverse Ackermann function, which grows extremely
slowly.
• Union: O(α(N))
Applications
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:
Algorithm:
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.
o Pick a vertex u that is not there in mstSet and has a minimum key value.
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)
o Pop the front node and update its neighbours' distances to current_distance + 1 if smaller.
• Initialize a distance array with INT_MAX and set source node distance to 0.
o For each neighbour, update its distance if a shorter path is found and push it into the queue.
[All-Pairs Shortest Path]
• Use a distance matrix, initialized with edge weights or INF for no edge.
• Initialize a distance array with INT_MAX and set the source node distance to 0.
• For cycle detection, repeat edge relaxation; if any distance improves, there's a negative cycle.
5. Shortest Path in a DAG
• 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.
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.
• Only applicable to directed acyclic graphs (DAGs), not suitable for cyclic graphs.
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.
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
o It is the root of the DFS tree and has more than one child.
low[v] ≥ disc[u]
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
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.
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.
2. Graph Types:
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:
7. Topological Sorting:
o What is topological sorting? Why is it applicable only to Directed Acyclic Graphs (DAGs)?
9. Critical Connections:
o What are bridges and articulation points in a graph? How are they useful in network design?
• How do "Union by Rank" and "Union by Size" improve the efficiency of the Union-Find algorithm
• Write a program to represent a graph using an adjacency list and an adjacency matrix.
• 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.
• 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.
• Write a program to find all Strongly Connected Components (SCCs) in a directed graph using Kosaraju's
algorithm.
• Write a program to find the transitive closure of a directed graph using the Floyd-Warshall algorithm.
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:
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]
o Find the MST of a weighted graph. [Example Problem: Connecting Cities with Minimum Cost]
8. Graph Coloring:
9. Critical Connections:
• Find the number of SCCs in a graph. [Example Problem: Kosaraju’s Algorithm Variant]
• 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
• Find all articulation points in a graph. [Example Problem: Critical Nodes in a Network]
• Solve the max flow problem using the Ford-Fulkerson algorithm. [Example Problem: Max Flow Variants]
• Determine all reachable nodes from every node in a graph. [Example Problem: Transitive Closure Matrix]
• Find the shortest path in a weighted graph with negative weights. [Example Problem: Cheapest Flights Within
K Stops]
• Find whether a topological sort exists for a graph. [Example Problem: Course Schedule]
• Find the shortest path in a maze. [Example Problem: The Maze II]