0% found this document useful (0 votes)
8 views15 pages

Unit 5

Uploaded by

tanujtrivedi30
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)
8 views15 pages

Unit 5

Uploaded by

tanujtrivedi30
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/ 15

UNIT - 5

Graphs

Er. Asim Ahmad


Assistant Professor, CSE
Shri Ramswaroop Memorial College of Engineering & Management
4.1 WHAT IS THE GRAPH IN DATA STRUCTURES?
In computer science, a graph is a non-linear data structure that consists of a set of vertices (also called
nodes or points) and a set of edges that connect pairs of vertices. A vertex represents an object or an
entity, while an edge represents the relationship between the two vertices it connects. Graph data
structures are powerful for representing and analysing complex relationships between objects or
entities. Graphs are often used to model complex relationships between objects in various domains, such
as social networks, transportation systems, communication networks, recommendation systems, and
computer networks. In the field of sports data science, graph data structures can be used to analyse and
understand the dynamics of team performance and player interactions on the field. In the graph, there
are two main parts:

Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as
vertices or nodes. Every node/vertex can be labelled or unlabelled.
Edges: Edges are drawn or used to connect two nodes of the graph. It can be an ordered pair of
nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no
rules. Sometimes, edges are also known as arcs. Every edge can be labelled/unlabelled.

4.1.1 Kind of Graphs


There are several types of graphs that can be used in data structures:

Directed graph: In a directed graph, the edges have a direction and can only be traversed in one
direction.

Undirected graph: In an undirected graph, the edges do not have a direction and can be traversed
in both directions.
Weighted graph: In a weighted graph, each edge has a weight or cost associated with it.

Unweighted graph: In an unweighted graph, each edge does not have any weight or cost
associated with it.

Connected graph: A connected graph is a graph in which every vertex is reachable from any other
vertex.

Disconnected graph: A disconnected graph is a graph in which some vertices are not connected
to any other vertices.
Cyclic graph: A cyclic graph is a graph that contains at least one cycle, i.e., a path that starts and
ends at the same vertex.

Acyclic graph: An acyclic graph is a graph that does not contain any cycles.

4.1.2 Data Structure for Graph representation


A graph can be represented using three data structures: adjacency matrix, adjacency list and adjacency
set.
Adjacency Matrix:
An adjacency matrix is a two-dimensional array where the rows and columns represent vertices of the
graph and the cells store information about whether there is an edge between the vertices or not. If
there is an edge between vertex i and vertex j, then the matrix cell (i, j) will be set to 1, otherwise, it will
be set to 0. The adjacency matrix is efficient for dense graphs, where most of the vertex pairs have edges
between them. However, for sparse graphs, it can be very memory inefficient.
ADJACENCY MATRIX

Adjacency List:
An adjacency list is a collection of linked lists, where each linked list represents the neighbours of a
vertex. For each vertex, we maintain a linked list of all the vertices adjacent to it. The adjacency list
representation is efficient for sparse graphs, where most of the vertex pairs do not have edges between
them. However, for dense graphs, it can be slower than the adjacency matrix representation.

ADJACENCY LIST

Adjacency Multi-lists:
In an adjacency multi-list, each vertex of the graph is represented by a node, and each node maintains a
list of all its adjacent vertices. However, in addition to the list of adjacent vertices, each node also
maintains a list of all the edges connecting the current node to its adjacent vertices. This extra
information makes adjacency multi-list more memory-intensive than the simple adjacency list
representation. However, it has the advantage of making it easy to traverse both the vertices and edges
of the graph in constant time.

NOTE: In summary, if the graph is dense, then the adjacency matrix is a good choice, and if the graph is
sparse, then the adjacency list is a good choice.

4.2 GRAPH TRAVERSAL TECHNIQUES


Graph traversal is a technique used to search for a vertex in a graph. It is also used to decide the order
of vertices to be visited in the search process. A graph traversal finds the edges to be used in the search
process without creating loops. There are two common methods of graph traversal: depth-first search
(DFS) and breadth-first search (BFS).

Depth-First Search
DFS (Depth-First Search) is a graph traversal algorithm that starts at a specified vertex in a graph
and explores as far as possible along each branch before backtracking. The algorithm works by
visiting a vertex, marking it as visited, and then recursively visiting all of its adjacent unvisited
vertices. This process continues until all reachable vertices have been visited.

This algorithm starts at a vertex and visits as far as possible along each branch before
backtracking. This means that the algorithm will first visit all the nodes in one path, and then
backtrack and visits the nodes in another path. When a dead-end occurs in any iteration, the
Depth First Search (DFS) method traverses a network in a deathward motion and uses a stack
data structure to remember to acquire the next vertex to start a search.

Breadth-First Search
BFS stands for Breadth-First Search, which is a graph traversal algorithm used to visit all the nodes
in a graph in a breadth-first manner. It starts at the root node and explores all the neighbour
nodes at the present depth before moving on to the nodes at the next depth level. The algorithm
works by maintaining a queue of nodes to be explored. It starts with the root node, adds it to the
queue, and marks it as visited. Then, it dequeues the node and adds all its unvisited neighbours
to the queue, marking them as visited. The algorithm repeats this process until all nodes have
been visited.

In BFS, the algorithm starts at a vertex and visits all of its neighbours before moving on to their
neighbours. This means that the algorithm will first visit all the nodes at the same level, and then
move on to the nodes at the next level. BFS is typically implemented using a queue, which stores
the vertices to be visited. Breadth-First Search uses a queue data structure to store the node and
mark it as "visited" until it marks all the neighbouring vertices directly related to it. The queue
operates on the First In First Out (FIFO) principle, so the node's neighbours will be viewed in the
order in which it inserts them in the node, starting with the node that was inserted first.

4.3 SPANNING TREES


A spanning tree of a connected graph is a tree that contains all the vertices of the graph and some, but
not all, of the edges of the graph. It is defined as a subset of a connected undirected graph that has all
the vertices covered with the minimum number of edges possible. A spanning tree can be thought of as
a "skeleton" of the graph, which preserves its connectivity but eliminates any cycles. Let the original
graph by:

ORIGINAL GRAPH

Some of the possible spanning trees that can be created from the above graph are:

TWO OF THE POSSIBLE SPANNING TREES

Spanning trees have many applications in computer science and other fields. For example, in network
design, a spanning tree can be used to ensure that all nodes in a network can communicate with each
other without creating any loops or redundancy.

4.3.1 Minimum Cost Spanning Tree (MCST)


The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can be many
spanning trees. The minimum spanning tree is the spanning tree where the cost is the minimum among
all the spanning trees. A Minimum Cost Spanning Tree (MCST) is a subset of the edges of a connected,
weighted graph that forms a tree and has the minimum possible sum of edge weights. In other words,
an MCST is a tree that connects all the vertices of the graph and has the smallest possible total edge
weight. There are several algorithms to find an MCST of a given graph, such as Kruskal's algorithm, Prim's
algorithm, and Boruvka's algorithm.
4.3.1.1 Kruskal’s Algorithm:
Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a connected,
weighted graph. The MST of a graph is a subset of its edges that form a tree and connect all the vertices
in the graph with minimum total edge weight.

Kruskal's algorithm works by sorting the edges of the graph by their weights and then iterating over the
edges in ascending order of weight. For each edge, the algorithm checks if adding it to the growing set
of edges in the MST creates a cycle. If not, the edge is added to the MST. If adding the edge creates a
cycle, it is discarded. The algorithm maintains a forest of trees, where each tree initially consists of a
single vertex. As edges are added to the MST, the trees are merged together until a single tree remains,
which is the MST.

Here are the steps of Kruskal's algorithm:

Sort the edges of the graph by weight in non-decreasing order.


Initialize an empty set of edges as the MST.
For each edge in the sorted list of edges:
If adding the edge to the MST does not create a cycle, add it to the MST.
If adding the edge creates a cycle, discard it.
Repeat step 3 until all edges have been considered.
The final set of edges is the MST.

Kruskal's algorithm has a time complexity of O(E log E), where E is the number of edges in the graph, due
to the sorting step. However, if the edges are already sorted, the algorithm has a time complexity of O(E
log V), where V is the number of vertices in the graph. Here is an example of Kruskal’s algorithm:

If you observe this graph, you’ll find two looping edges connecting the same node to itself again. And
you know that the tree structure can never include a loop or parallel edge. Hence, primarily you will need
to remove these edges from the graph structure.
The next step that you will proceed with is arranging all edges in a sorted list by their edge weights.

EDGES WEIGHT
EF 2
DF 2
CF 3
CB 3
CD 4
DE 5
BF 5
BD 6
AB 7
AC 8

After this step, you will include edges in the MST such that the included edge would not form a cycle in
your tree structure. The first edge that you will pick is edge EF, as it has a minimum edge weight that is
2.
All other edges should be discarded because these are created in a loop and the loop is prohibited in
Spanning Tree.

4.3.1.2 Prim’s Algorithm:


Prim's algorithm is a popular algorithm for finding the minimum spanning tree of a connected, weighted,
undirected graph. The minimum spanning tree of a graph is a tree that connects all the vertices of the
graph with the minimum total edge weight. Here are the steps for Prim's algorithm:

Choose any vertex from the graph and add it to the minimum spanning tree.
For all the vertices that are not in the minimum spanning tree, calculate the weight of the edge
that connects them to the vertices in the minimum spanning tree.
Select the vertex with the minimum weight edge and add it to the minimum spanning tree.
Repeat steps 2 and 3 until all the vertices in the graph are in the minimum spanning tree.
The algorithm can be implemented using a priority queue to efficiently select the vertex with the
minimum weight edge. The time complexity of Prim's algorithm is O(E log V), where E is the number of
edges and V is the number of vertices in the graph. This makes it an efficient algorithm for finding the
minimum spanning tree of a graph. Here is an example of Prim’s algorithm:

THE GRAPH FOR FINDING MCST


4.4 SHORTEST PATH ALGORITHMS
A shortest path algorithm is an algorithm in the data structure that is used to find the shortest path or
distance between two nodes in a graph. The algorithm works by traversing the graph from the starting
node to the destination node and keeps track of the distance of each node from the starting node. There
are several algorithms for finding the shortest path, two of them which we will study are as follows:

4.4.1 Dijkstra's Algorithm:


Dijkstra's algorithm is a graph search algorithm that solves the single-source shortest path problem for
a graph with non-negative edge weights, producing the shortest path tree. The algorithm maintains a
set of visited vertices and their shortest known distances from the source vertex. Initially, the distance
to the source vertex is set to 0, and the distances to all other vertices are set to infinity.

The algorithm then repeatedly selects the vertex with the smallest distance from the source vertex that
has not yet been visited and adds it to the visited set. For each neighbouring vertex that has not yet been
visited, the algorithm updates its distance from the source vertex if the new path through the current
vertex is shorter than the current shortest path. This process continues until all vertices have been
visited, and the shortest path to each vertex has been found. It is easier to start with an example and
then think about the algorithm.
Now, we are able to find the shortest path in this graph.
4.4.2 Floyd-Warshall Algorithm:
Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices
in a weighted graph. This algorithm works for both the directed and undirected weighted graphs. But it
does not work for the graphs with negative cycles (where the sum of the edges in a cycle is negative).
Floyd-Warhshall algorithm is also called Floyd's algorithm, Roy-Floyd algorithm, Roy-Warshall algorithm,
or WFI algorithm. This algorithm follows the dynamic programming approach to find the shortest paths.

Let the given weighted graph be:

FILL EACH CELL WITH THE DISTANCE BETWEEN i th AND jth VERTEX

CALCULATE THE DISTANCE FROM THE SOURCE TO THE DESTINATION THROUGH THIS VERTEX K
Since A4 has minimum we ight from all t he e dges, thus it gives the shortest path between each
pair of vertices.

You might also like