You are on page 1of 59

Graphs

By
Mr. Abhijit T. Somnathe
What is Graph?
⚫ 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.
⚫ Graphs are used to solve many real-life
problems.
What is Graph?

⚫ 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}.
What is Graph?
⚫ 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, etc.
⚫ 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.
Directed and Undirected Graph
⚫ A graph can be directed or undirected.
⚫ In an undirected graph, edges do not
have any direction associated with them.
⚫ If an edge is drawn between nodes A and
B, then the nodes can be traversed from
A to B as well as from B to A.
Directed and Undirected Graph
⚫ Figure below shows an undirected graph
because it does not give any information
about the direction of the edges.
Directed and Undirected Graph
⚫ Figure below shows a directed graph.
Directed and Undirected Graph
⚫ In a directed graph, edges form an
ordered pair.
⚫ If there is an edge from A to B, then
there is a path from A to B but not from
B to A.
⚫ The edge (A, B) is said to initiate from
node A (also known as initial node) and
terminate at node B (terminal node).
Graph Terminology
⚫ Vertex − Each node of the graph is
represented as a vertex.
⚫ Edge − Edge represents a path between
two vertices or a line between two
vertices.
⚫ Adjacency − Two node or vertices are
adjacent if they are connected to each
other through an edge.
Graph Terminology
⚫ Path − Path represents a sequence of edges
between the two vertices. In the following
example, ABCD represents a path from A-D.
Graph Terminology
⚫ Degree of a node - Degree of a node u,
deg(u), is the total number of edges containing
the node u. If deg(u) = 0, it means that u does
not belong to any edge and such a node is
known as an isolated node.

• Here, Degree of
A=C=E=2
while degree of
B = D = 3.
Graph Terminology
⚫ Outdegree – The outdegree of a vertex A in a
directed graph, is the number of edges starting
from the vertex A.
⚫ Indegree – The indegree of a vertex A in a
directed graph, is the number of edges
termination at the vertex A.

• In the graph, Vertex C


has Indegree = 1 and
Outdegree = 1. Vertex B
has Indegree = 2 and
Outdegree = 1.
Graph Terminology
⚫ Source − A vertex V is known as Source if its
outdegree is 1 or more than 1 but its Indegree
is 0.
⚫ Sink − A vertex V is known as Sink if its
Indegree is 1 or more than 1 but its outdegree
is 0.
• In figure, Vertex 0
is Source while
Vertex 2 is Sink.
Graph Terminology
⚫ Strongly Connected − Graph is said to
be strongly connected if there is a path
from each vertex to every other vertex.
Graph Terminology
⚫ Weakly Connected − In a weakly
connected graph the nodes cannot be
visited by a single path.
1

4 5
Graph Terminology
⚫ Weighted Graph − Graph is said to be
weighted if edges in it are assigned
weights.
Graph Terminology
⚫ Multi Graph - Any graph which contain
some parallel edges but doesn’t contain
any self-loop is called multi graph. For
example - A Road Map.
Graph Representation
⚫ Graph can be represented using various
ways. The two standard approaches for
representing graphs are,
❖ Sequential representation by using
adjacency matrix.
❖ Adjacency List.
Adjacency Matrix
⚫ Suppose, G = {Vg, Eg} is a directed
graph having n nodes.
⚫ Suppose, vertices are ordered by using
v1, v2, v3, v4,…., vn. Then adjacency
matrix A for the graph G will be a square
matrix of order n such that,
⚫ aij = 1, if an edges lies between the
vertices vi and vj.
⚫ aij = 0, if there is no edges between the
vertices vi and vj.
Adjacency Matrix
⚫ For example, consider the following
undirected graph representation.
Adjacency Matrix
⚫ Consider the following directed graph
representation.
Incidence Matrix
⚫ In this representation, the graph is represented
using a matrix of size total number of vertices
by a total number of edges.
Incidence Matrix
⚫ In above graph having 5 vertices and 8
edges is represented using a matrix of size
5X8.
⚫ In this matrix, rows represent vertices and
columns represents edges.
⚫ This matrix is filled with 0 or 1 or -1.
⚫ Here, 0 represents that the row edge is not
connected to column vertex, 1 represents
that the row edge is connected as the
outgoing edge to column vertex and -1
represents that the row edge is connected as
the incoming edge to column vertex.
Incidence Matrix
Linked Representation
⚫ The adjacency matrix representation has
a number of drawbacks. In this
representation, it is difficult to store
additional information about the vertices
and edges in the graph.
⚫ One major problem with this
representation is its static nature. Before
storing any graph, it is necessary to find
the number of vertices in it.
Linked Representation
⚫ It becomes very difficult to insert and
delete the vertices from the graph when
it is represented using the adjacency
matrix, as it requires change in the
dimensions of the matrix.
⚫ In the linked representation, an
adjacency list is used to store the Graph
into the computer's memory.
Linked Representation
⚫ 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 last
node of the list.
⚫ Consider the undirected graph shown in the
following figure and check the adjacency
list representation.
Linked Representation
Linked Representation
⚫ Consider the directed graph shown in the
following figure and check the
adjacency list representation of the
graph.
Linked Representation
⚫ In a directed graph, the sum of lengths
of all the adjacency lists is equal to the
number of edges present in the graph.
⚫ In the case of weighted directed graph,
each node contains an extra field that is
called the weight of the node.
⚫ The adjacency list representation of a
directed graph is shown in the following
figure.
Linked Representation
Graph Traversal
⚫ The most fundamental graph problem is to
traverse every edge and vertex in a graph on
a systematic way.
⚫ Graph traversal is a process of visiting
(checking and/or updating) each vertex in a
graph. Such traversals are classified by the
order in which the vertices are visited.
⚫ There are mainly 2 types.
❖ Depth First Search (DFS)
❖ Breadth First Search (BFS)
Depth First Search (DFS)
⚫ Depth First Search (DFS) algorithm
traverses a graph in a depthward motion
and uses a stack to remember to get the
next vertex to start a search, when a
dead end occurs in any iteration.
Depth First Search (DFS)
Depth First Search (DFS)
⚫ As in the example given above, DFS algorithm
traverses from S to A to D to G to E to B first,
then to F and lastly to C. It employs the
following rules.
❖ Rule 1 − Visit the adjacent unvisited vertex.
Mark it as visited. Display it. Push it in a stack.
❖ Rule 2 − If no adjacent vertex is found, pop up
a vertex from the stack. (It will pop up all the
vertices from the stack, which do not have
adjacent vertices.)
❖ Rule 3 − Repeat Rule 1 and Rule 2 until the
stack is empty.
Depth First Search (DFS)
Depth First Search (DFS)
Depth First Search (DFS)
Depth First Search (DFS)
Depth First Search (DFS)
Depth First Search (DFS)
Depth First Search (DFS)
Depth First Search (DFS)
⚫ As C does not have any unvisited adjacent
node so we keep popping the stack until
we find a node that has an unvisited
adjacent node.
⚫ In this case, there's none and we keep
popping until the stack is empty.
Breadth First Search (BFS)
⚫ Breadth First Search (BFS) algorithm
traverses a graph in a breadthward motion
and uses a queue to remember to get the
next vertex to start a search, when a dead
end occurs in any iteration.
Breadth First Search (BFS)
Breadth First Search (BFS)
⚫ As in the example given above, BFS
algorithm traverses from S-A, S-B, S-C then
A-D, B-E and C-F then D-G. It employs the
following rules.
⚫ Rule 1 − Visit the adjacent unvisited vertex.
Mark it as visited. Display it. Insert it in a
queue.
⚫ Rule 2 − If no adjacent vertex is found,
remove the first vertex from the queue.
⚫ Rule 3 − Repeat Rule 1 and Rule 2 until the
queue is empty.
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
⚫ At this stage, we are left with no
unmarked (unvisited) nodes.
⚫ But as per the algorithm we keep on
dequeuing in order to get all unvisited
nodes.
⚫ When the queue gets emptied, the
program is over.
Topological Sorting
⚫ Topological sorting for Directed Acyclic
Graph (DAG) is a linear ordering of
vertices such that for every directed edge
u v, vertex u comes before v in the
ordering.
⚫ Topological Sorting for a graph is not
possible if the graph is not a DAG.
Topological Sorting

⚫ For example, a topological sorting of the


following graph is “5 4 2 3 1 0”.
Topological Sorting
⚫ There can be more than one topological
sorting for a graph.
⚫ For example, another topological sorting
of the following graph is “4 5 2 3 1 0”.
⚫ The first vertex in topological sorting is
always a vertex with in-degree as 0 (a
vertex with no incoming edges).
Topological Sorting vs Depth
First Traversal (DFS)
⚫ In DFS, we print a vertex and then recursively
call DFS for its adjacent vertices.
⚫ In topological sorting, we need to print a
vertex before its adjacent vertices.
⚫ For example, in the given graph, the vertex
‘5’ should be printed before vertex ‘0’, but
unlike DFS, the vertex ‘4’ should also be
printed before vertex ‘0’.
⚫ So Topological sorting is different from DFS.
⚫ For example, a DFS of the shown graph is “5
2 3 1 0 4 / 5 2 3 1 4 0”, but it is not a
topological sorting.
ANY DOUBTS?

You might also like