Professional Documents
Culture Documents
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?
• 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.
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