Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Graph Data Structure and Algorithms

A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect
any two nodes in the graph. More formally a Graph can be defined as,
A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect
a pair of nodes.

In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01,
12, 23, 34, 04, 14, 13}.
Graphs are used to solve many real-life problems. Graphs are used to represent
networks. The networks may include paths in a city or telephone network or circuit
network. Graphs are also used in social networks like linkedIn, Facebook. For
example, in Facebook, each person is represented with a vertex(or node). Each node
is a structure and contains information like person id, name, gender, locale etc.

We can use graphs to model a wide variety of systems. For example:

• Web page linking — The graph nodes are web pages, and the edges represent
hyperlinks between pages.

• Airports — The graph nodes are airports, and the edges represent flights
between airports.

A graph is a pictorial representation of a set of objects where some pairs of


objects are connected by links. The interconnected objects are represented by
points termed as vertices, and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is
the set of edges, connecting the pairs of vertices. Take a look at the following
graph −

In the above graph,


V = {a, b, c, d, e}

Graph Data Structure


Mathematical graphs can be represented in data structure. We can represent a
graph using an array of vertices and a two-dimensional array of edges. Before we
proceed further, let's familiarize ourselves with some important terms −
 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.
 Loop- A degenerate edge of a graph which joins a vertex to itself, also
called a self-loop. 

Directed Graphs or Digraph

A directed graph is a set of vertices (nodes) connected by edges, with each node


having a direction associated with it.
Edges are usually represented by arrows pointing in the direction the graph can be
traversed.

In the example above, the graph can be traversed from vertex A to B, but not from
vertex B to A.
Undirected Graphs

In an undirected graph the edges are bidirectional, with no direction associated


with them. Hence, the graph can be traversed in either direction. The absence of an
arrow tells us that the graph is undirected.

In the example above, the graph can be traversed from node A to B as well as
from node B to A.
Some more complex directed and undirected graphs might look like the following:

A graph in which individual nodes have no distinct identifications except through


their interconnectivity. Graphs in which labels (which are most commonly
numbers) are assigned to nodes are called labeled graphs. Unless indicated
otherwise by context, the unmodified term "graph" generally refers to
an unlabeled graph.
GRAPH REPRESENTATION

Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in
a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge
from vertex i to vertex j. Adjacency matrix for undirected graph is always
symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j]
= w, then there is an edge from vertex i to vertex j with weight w.
The adjacency matrix for the above example graph is:

Pros: Representation is easier to implement and follow. Removing an edge takes


O(1) time. Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are
efficient and can be done O(1).
Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less
number of edges), it consumes the same space. Adding a vertex is O(V^2) time.
Please see this for a sample Python implementation of adjacency matrix.

Path Matrix
Let G be a graph with m edges, and u and v be any two vertices in G. The path
matrix for vertices u and v denoted by P(u, v) = [pi j]q×m, where q is the number of
different paths between u and v, is defined as

Clearly, a path matrix is defined for a particular pair of vertices, the rows in P(u,
v) correspond to different paths between u and v, and the columns correspond to
different edges in G. For example, consider the graph in the figure below
The different paths between the vertices v3 and v4 are
p1 = {e8, e5}, p2 = {e8, e7, e3} and p3 = {e8, e6, e4, e3}.
The path matrix for v3, v4 is given by

Basic Operations
Following are basic primary operations of a Graph −
 Add Vertex − Adds a vertex to the graph.
 Add Edge − Adds an edge between the two vertices of the graph.
 Display Vertex − Displays a vertex of the graph.

You might also like