You are on page 1of 18

MODULE IV

GRAPH

• Graphs are powerful data structures that represent real-life relationships


between entities.
• Graphs are used everywhere, from social networks, Google maps, and
the world wide web to block chains and neural networks.
• Due to their ability to provide abstractions to real-life, they are used in
a variety of practical problems.
GRAPH IN DATA STRUCTURE
• Graphs are non-linear data structures that are made up of a set of nodes
(or vertices), connected by edges (or arcs).
• Nodes are entities where the data is stored and their relationships are
expressed using edges.
• Edges may be directed or undirected.
• Graphs demonstrate complicated relationships with ease and are used to
solve many real-life problems.
REAL LIFE APPLICATIONS

• Facebook uses a graph data structure that consists of a group of entities


and their relationships.
• On Facebook, every user, photo, post, page, place, etc. that has data is
represented with a node.
• Every edge from one node to another represents their relationships,
friendships, ownerships, tags, etc.
• Whenever a user posts a photo, comments on a post, etc., a new edge is
created for that relationship.
• Both nodes and edges have meta-data associated with them.
GRAPH EXAMPLE
• Example
• The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and
E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
GRAPH TERMINOLOGIES

• Vertex − Each node of the graph is represented as a vertex. In the following


example, the labeled circle represents vertices. Thus, A to G are vertices. We
can represent them using an array as shown in the following image. Here A
can be identified by index 0. B can be identified using index 1 and so on.
• Edge − Edge represents a path between two vertices or a line between two
vertices. In the following example, the lines from A to B, B to C, and so on
represents edges. We can use a two-dimensional array to represent an array
as shown in the following image. Here AB can be represented as 1 at row 0,
column 1, BC as 1 at row 1, column 2 and so on, keeping other
combinations as 0.
• Adjacency − Two node or vertices are adjacent if they are connected to
each other through an edge. In the following example, B is adjacent to
A, C is adjacent to B, and so on.
• Path − Path represents a sequence of edges between the two vertices. In
the following example, ABCD represents a path from A to D.
Terminology Explanation

Undirected edge It is bidirectional

Directed edge It is unidirectional

Weighted edge An edge with value (cost) on it

Degree The total number of edges connected to a vertex in a graph

Indegree The total number of incoming edges connected to a vertex

Outdegree The total number of outgoing edges connected to a vertex


• Self-loop : An edge is called a self-loop if its two endpoints coincide
with each other.
• Adjacency: Vertices are said to be adjacent to one another if there is an
edge connecting them.

• Types of Graphs in Data Structure


• Undirected: A graph in which all the edges are bi-directional. The
edges do not point in a specific direction.
•  Directed: A graph in which all the edges are uni-directional. The edges
point in a single direction.
TYPES OF GRAPHS IN DATA STRUCTURE

• Weighted Graph: A graph that has a value associated with every edge.
• The values corresponding to the edges are called weights.
• A value in a weighted graph can represent quantities such as cost,
distance, and time, depending on the graph.
• Weighted graphs are typically used in modeling computer networks.
GRAPH REPRESENTATION IN DATA
STRUCTURE

• The most frequent graph representations are the two that follow:
• Adjacency matrix(sequential representation)
• Adjacency list(linked list representation)
ADJACENCY MATRIX

• In this representation, the graph is represented using a matrix of size total


number of vertices by a total number of vertices.
• That means a graph with 4 vertices is represented using a matrix of size
4X4.
• In this matrix, both rows and columns represent vertices. This matrix is
filled with either 1 or 0.
• Here, 1 represents that there is an edge from row vertex to column vertex
and 0 represents that there is no edge from row vertex to column vertex.
EXAMPLE
EXAMPLE

• Directed graph representation..


ADJACENCY LIST

• An adjacency list is used in the linked representation to store the Graph


in the computer's memory. It is efficient in terms of storage as we only
have to store the values for edges.
• Let's see the adjacency list representation of an undirected graph.
ADJACENCY LIST..

• In the above figure, we can see that there is a linked list or adjacency list for every
node of the graph.
• From vertex A, there are paths to vertex B and vertex D. These nodes are linked to
nodes A in the given adjacency list.
• An adjacency list is maintained for each node present in the graph, which stores the
node value and a pointer to the next adjacent node to the respective node.
• If all the adjacent nodes are traversed, then store the NULL in the pointer field of
the last node of the list.
• The sum of the lengths of adjacency lists is equal to twice the number of edges
present in an undirected graph.
• consider the weighted directed graph, and let's see the adjacency list
representation of that graph.

You might also like