Chapter 7
Chapter 7
Graphs
This concept is used in many areas, like planning the best route on a road
trip (where cities are nodes and roads are edges), understanding how
diseases spread in a community (where people are nodes and their
interactions are edges), or even figuring out how web pages are linked to
each other on the internet.
A graph, in the context of mathematics and computer science, is a data structure
used to represent relationships between entities. It consists of two sets:
Vertices (V): Represent the entities themselves, also called nodes or points.
Edges (E): Represent the relationships between the entities, also called links or
lines.
Definition
A graph is denoted as G = (V, E), where:
-V is a set of vertices.
-E is a set of edges.
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). Cycles are essential in understanding the connectivity and
structure of a graph and play a significant role in cycle detection algorithms.
Advanced Graph Terminology:
2. Undirected Graph
In an undirected graph, edges do not have a direction. They simply connect
two nodes without any particular order.
Example:
Think of a Facebook friendship where if Alice is friends with Bob, then Bob
is also friends with Alice. The edge goes both ways.
3. Weighted Graph
In a weighted graph, edges have weights or costs associated with them.
These weights can represent distances, costs, or any other metric.
Example:
A road map where the weights on the edges represent the distance
between cities.
4. Unweighted Graph
In an unweighted graph, all edges have the same weight, typically
considered as 1.
Example:
A simple social network where each friendship has the same importance.
5. Cyclic Graph
A cyclic graph contains at least one cycle, meaning you can start at a node
and follow a path that leads back to the same node
Example:
A graph representing routes between cities where some routes form a loop.
6. Acyclic Graph
An acyclic graph does not contain any cycles.
Example:
A family tree where no child node points back to its ancestors.
7. Connected Graph
A connected graph has a path between every pair of nodes.
Example:
A small network where every computer can reach every other computer
directly or indirectly.
8. Disconnected Graph
A disconnected graph has at least one pair of nodes with no path between
them.
9. Complete Graph
In a complete graph, there is an edge between every pair of nodes.
Example:
A small social network where everyone is friends with everyone else.
Representation of Graphs:
Graphs are fundamental data structures used to represent relationships between
entities. To store and manipulate these relationships efficiently, several different
graph representations exist. Here's an overview of the most common ones:
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
An adjacency matrix is a 2D array where each cell [i, j] represents the
presence or absence of an edge between vertices i and j. If the graph is
weighted, the cell can store the weight of the edge.
Here's an overview of the key aspects of adjacency matrices:
Adjacency List
The n rows of adjacency matrix are represented as n linked lists. There is
one list for each vertex in the graph. The nodes in the list i represent the
vertices that are adjacent from vertex i. Each node has at least two
fields: vertex and link. The vertex field contains the indices of the vertices
adjacent to vertex i. The adjacency lists for the undirected graph and directed
graph, are illustrated below.
Here's an overview of the key features of adjacency lists:
• Can be less efficient for dense graphs (where the number of edges is
close to the maximum possible).
• Determining whether an edge exists between two vertices can be less
efficient than with an adjacency matrix.
Transitive Closure
Definition:
Warshall's Algorithm
Key Points:
In R(1), we can see the existence of a path, which has intermediate vertices.
The number of the vertices is not greater than 1, which means just a vertex.
It contains a new path from d to b. We will get R(2) with the help of a boxed
row and column in R(1).
In R(2), we can see the existence of a path, which has intermediate vertices.
The number of the vertices is not greater than 2, which means a and b. It
contains two new paths. We will get R(3) with the help of a boxed row and
column in R(2).
In R(3), we can see the existence of a path, which has intermediate vertices.
The number of the vertices is not greater than 3, which means a, b and c. It
does not contain any new paths. We will get R(4) with the help of a boxed
row and column in R(3).
In R(4), we can see the existence of a path, which has intermediate vertices.
The number of the vertices is not greater than 4, which means a, b, c and d.
It contains five new paths.
Graph traversals
Graph traversal means visiting every vertex and edge exactly once in a well-defined order. While
using certain graph algorithms, you must ensure that each vertex of the graph is visited exactly
once. The order in which the vertices are visited are important and may depend upon the algorithm
or question that you are solving.
During a traversal, it is important that you track which vertices have been visited. The most common
way of tracking vertices is to mark them.
BFS is a traversing algorithm where you should start traversing from a selected node (source or
starting node) and traverse the graph layerwise thus exploring the neighbour nodes (nodes which
are directly connected to source node). You must then move towards the next-level neighbour
nodes.
BFS algorithm
A standard BFS implementation puts each vertex of the graph into one of two
categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding
cycles.
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the
visited list to the back of the queue.
BFS example
Let's see how the Breadth First Search algorithm works with an example. We
use an undirected graph with 5 vertices.
We start from vertex 0, the BFS algorithm starts by putting it in the Visited list
and putting all its adjacent vertices in the queue.
Visit start vertex and add its adjacent vertices to queue
Next, we visit the element at the front of queue i.e. 1 and go to its adjacent
nodes. Since 0 has already been visited, we visit 2 instead.
Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is already
visited. We visit it.
Visit last remaining item in the queue to check if it has unvisited neighbors
Since the queue is empty, we have completed the Breadth First Traversal of
the graph.
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding
cycles.
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the
visited list to the top of the stack.
Example:
Note : There can be multiple DFS traversals of a graph according to the
order in which we pick adjacent vertices. Here we pick vertices as per the
insertion order.
Input: adj = [[1, 2], [0, 2], [0, 1, 3, 4], [2], [2]]
Output: 1 2 0 3 4
Explanation: The source vertex s is 1. We visit it first, then we visit an
adjacent.
Start at 1: Mark as visited. Output: 1
Move to 2: Mark as visited. Output: 2
Move to 0: Mark as visited. Output: 0 (backtrack to 2)
Move to 3: Mark as visited. Output: 3 (backtrack to 2)
Move to 4: Mark as visited. Output: 4 (backtrack to 1)
Q. Input: [[2,3,1], [0], [0,4], [0], [2]]
Output: 0 2 4 3 1
The minimum spanning tree has all the properties of a spanning tree with an
added constraint of having the minimum possible weights among all possible
spanning trees. Like a spanning tree, there can also be many possible MSTs
for a graph.
This is one of the popular algorithms for finding the minimum spanning
tree from a connected, undirected graph. This is a greedy algorithm. The
algorithm workflow is as below:
• First, it sorts all the edges of the graph by their weights,
• Then starts the iterations of finding the spanning tree.
• At each iteration, the algorithm adds the next lowest-weight edge one
by one, such that the edges picked until now does not form a cycle.
2. Take the edge with the lowest weight and add it to the spanning tree. If adding
the edge created a cycle, then reject this edge.
The weight of the edges of the above graph is given in the below table –
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the
cycle.
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any
cycle or loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming
the cycle.
AD
Step 5 - After that, pick the edge AE with weight 5. Including this edge will
create the cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the
cycle, so discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create
the cycle, so discard it.
So, the final minimum spanning tree obtained from the given weighted graph
by using Kruskal's algorithm is -
Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or O(V logV),
where E is the no. of edges, and V is the no. of vertices.
Auxiliary Space: O(V + E), where V is the number of vertices and E is
the number of edges in the graph.
Prim’s Algorithm:
Prim's Algorithm is a greedy algorithm that is used to find the minimum
spanning tree from a graph. Prim's algorithm finds the subset of edges that
includes every vertex of the graph such that the sum of the weights of the
edges can be minimized.
How does the prim's algorithm work?
Prim's algorithm is a greedy algorithm that starts from one vertex and
continue to add the edges with the smallest weight until the goal is reached.
The steps to implement the prim's algorithm are given as follows -
Prim's algorithm starts with the single node and explores all the adjacent
nodes with all the connecting edges at every step. The edges with the
minimal weights causing no cycles in the graph got selected.
Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that
has minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tre
eT
5. [END OF LOOP]
6. Step 5: EXIT
Step 1 - First, we have to choose a vertex from the above graph. Let's
choose B.
Step 2 - Now, we have to choose and add the shortest edge from vertex B.
There are two edges from vertex B that are B to C with weight 10 and edge
B to D with weight 4. Among the edges, the edge BD has the minimum
weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all
the other edges. In this case, the edges DE and CD are such edges. Add
them to MST and explore the adjacent of C, i.e., E and A. So, select the edge
DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as
it would create a cycle to the graph. So, choose the edge CA and add it to
the MST.
So, the graph produced in step 5 is the minimum spanning tree of the given
graph. The cost of the MST is given below -
1. Single Source Shortest Path (SSSP): Find the shortest path from a
single source vertex to all other vertices.
o Algorithms: Dijkstra’s Algorithm, Bellman-Ford Algorithm
o
2. All Pairs Shortest Path (APSP): Find the shortest path between
every pair of vertices.
o Algorithms: Floyd-Warshall Algorithm, Johnson’s Algorithm
o
3. Single Pair Shortest Path: Find the shortest path between a specific
pair of vertices.
o Algorithms: Dijkstra’s Algorithm (for positive weights), A*
Algorithm (for heuristics)
o
4. K Shortest Paths: Find the top K shortest paths between two
vertices.
o Algorithms: Yen’s Algorithm
Relaxation:
If d(u) + C(u,v) < d(v)
d(v) = d(u) + C(u,v)