Unit No 7:Introduction to Graph Data Structure
Graph Data Structure is a non-linear data structure consisting of vertices and edges. It is useful in
fields such as social network analysis, recommendation systems, and computer networks. In the field
of sports data science, graph data structure can be used to analyze and understand the dynamics of
team performance and player interactions on the field.
What is Graph Data Structure?
Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More
formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted
by G(V, E).
Imagine a game of football as a web of connections, where players are the nodes and their
interactions on the field are the edges. This web of connections is exactly what a graph data structure
represents, and it’s the key to unlocking insights into team performance and player dynamics in
sports.
Components of Graph Data Structure
● Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also
known as vertex or nodes. Every node/vertex can be labeled or unlabelled.
● Edges: Edges are drawn or used to connect two nodes of the graph. It can be 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.
● Types Of Graphs in Data Structure and Algorithms
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.
Representation of Graph Data Structure:
There are multiple ways to store a graph: The following are the most common
representations.
● Adjacency Matrix
● Adjacency List
1)Adjacency Matrix Representation of Graph Data Structure:
In this method, the graph is stored in the form of the 2D matrix where rows and columns
denote vertices. Each entry in the matrix represents the weight of the edge between those
vertices.
2)Adjacency List of graph:-
This graph is represented as a collection of linked lists. There is an array of pointer which
points to the edges connected to that vertex.
Finding in and out degrees of all vertices in a graph
Given a directed graph, the task is to count the in and out degree of
each vertex of the graph.
Examples:
Input:
Output:
Vertex In Out
0 1 2
1 2 1
2 2 3
3 2 2
4 2 2
5 2 2
6 2 1
Approach: Traverse adjacency list for every vertex, if size of the
adjacency list of vertex i is x then the out degree for i = x and
increment the in degree of every vertex that has an incoming edge
from i. Repeat the steps for every vertex and print the in and out
degrees for all the vertices in the end.
Breadth First Search or BFS for a Graph
Breadth First Search (BFS) is a fundamental graph traversal
algorithm. It begins with a node, then first traverses all its adjacent.
Once all adjacent are visited, then their adjacent are traversed. This
is different from DFS in a way that closest vertices are visited before
others.
BFS from a Given Source:
The algorithm starts from a given source and explores all reachable
vertices from the given source. It is similar to the Breadth-First
Traversal of a tree. Like tree, we begin with the given source (in tree,
we begin with root) and traverse vertices level by level using a
queue data structure. The only catch here is that, unlike trees,
graphs may contain cycles, so we may come to the same node again.
To avoid processing a node more than once, we use
a boolean visited array.
Initialization: Enqueue the given source vertex into a queue and
mark it as visited.
1. Exploration: While the queue is not empty:
● Dequeue a node from the queue and visit it (e.g., print its
value).
● For each unvisited neighbor of the dequeued node:
o Enqueue the neighbor into the queue.
o Mark the neighbor as visited.
2. Termination: Repeat step 2 until the queue is empty.
This algorithm ensures that all nodes in the graph are visited in a
breadth-first manner, starting from the starting node.
How Does the BFS Algorithm Work?
Frequently Asked Questions(FAQs) on Graph Data Structure:
1. What is a graph?
A graph is a data structure consisting of a set of vertices (nodes) and a set of edges that connect pairs
of vertices.
2. What are the different types of Graph Data Structure?
Graph Data Structure can be classified into various types based on properties such as directionality of
edges (directed or undirected), presence of cycles (acyclic or cyclic), and whether multiple edges
between the same pair of vertices are allowed (simple or multigraph).
3. What are the applications of Graph Data Structure?
Graph Data Structure has numerous applications in various fields, including social networks,
transportation networks, computer networks, recommendation systems, biology, chemistry, and
more.
4. What is the difference between a directed graph and an undirected graph?
In an undirected graph, edges have no direction, meaning they represent symmetric relationships
between vertices. In a directed graph (or digraph), edges have a direction, indicating a one-way
relationship between vertices.
5. What is a weighted graph?
A weighted graph is a graph in which each edge is assigned a numerical weight or cost. These
weights can represent distances, costs, or any other quantitative measure associated with the edges.
6. What is the degree of a vertex in a graph?
The degree of a vertex in a graph is the number of edges incident to that vertex. In a directed graph,
the indegree of a vertex is the number of incoming edges, and the outdegree is the number of
outgoing edges.
7. What is a path in a graph?
A path in a graph is a sequence of vertices connected by edges. The length of a path is the number of
edges it contains.
8. What is a cycle in a graph?
A cycle in a graph is a path that starts and ends at the same vertex, traversing a sequence of distinct
vertices and edges in between.
9. What are spanning trees and minimum spanning trees?
A spanning tree of a graph is a subgraph that is a tree and includes all the vertices of the original
graph. A minimum spanning tree (MST) is a spanning tree with the minimum possible sum of edge
weights.
10. What algorithms are commonly used to traverse or search Graph Data Structure?
Common graph traversal algorithms include depth-first search (DFS) and breadth-first search (BFS).
These algorithms are used to explore or visit all vertices in a graph, typically starting from a specified
vertex. Other algorithms, such as Dijkstra’s algorithm and Bellman-Ford algorithm, are used for
shortest path finding.