VIDHYA SAGAR WOMENS COLLEGE, CHENGALPATTU
DEPT OF COMPUTER SCIENCE
ONLINE CLASS
DATA
STRUCTURE
Staff Incharge: [Link] [Link].,[Link].,NET
[Link]
Dept of Computer Science
UNIT-5
Objectives
To Understand the concept of
Graphs:
Definitions
Graph types
Graph representation
Graph Traversals
GRAPH
A graph is an non linear abstract data
structure that is used to implement the
mathematical concept.
Graph is a collection of vertices (also called
nodes) and edges that connect these
vertices.
A graph G = (V, E)
V = { V1, V2, . . . } set of vertices
E = { E1, E2……} set of edges
B
A D V=A,B,C,D
C E=(a,b)(a,c)(b,d)(c,d))
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
Adjacent Vertices:- Two node or vertices are adjacent if they
are connected to each other through an edge. Vertex v1 is said
to be adjacent to a vertex v2 if there is an edge(v1, v2) or (v2,
v1).
Path:- connected line between edges called path
Cycle:- A cycle is a path in which first and last vertices are
the same.
Loop: An edge of a graph which join a vertex to itself is called
loop or a self-loop.
Degree:- The number of edges incident on a vertex
determine its degree.
Weight:- If every edge in the graph is assigned some with
value, called weight
Tree:- If all nodes are connected and there are no cycles in
the graph is called tree
GRAPH TYPES
Finite Graphs
Infinite Graph
Simple Graph
Multi Graph
Null Graph
Complete Graph
Directed Graphs
Undirected Graph
Bipartite Graph
Connected Graph
Disconnected Graph
Sub Graph
Weighted Graph
Cyclic Graph
Finite Graphs: A graph is said to be finite if
it has finite number of vertices and finite
number of edges.
Infinite Graph: A graph is said to be infinite
if it has infinite number of vertices as well as
infinite number of edges
Simple Graph: A simple graph is a graph
which does not contains more than one edge
between the pair of vertices. A simple railway
tracks connecting different cities is an
example of simple graph.
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.
Null Graph: A graph of order n and size zero
that is a graph which contain n number of
vertices but do not contain any edge.
Complete Graph: A simple graph with n
vertices is called a complete graph if the
degree of each vertex is n-1, that is, one
vertex is attach with n-1 edges. A complete
graph is also called Full Graph
Digraph Graph: A graph G = (V, E) with a
mapping f such that every edge maps onto
some ordered pair of vertices (Vi, Vj) is called
Digraph. It is also called Directed Graph.
Ordered pair (Vi, Vj) means an edge between
Vi and Vj with an arrow directed from Vi to Vj.
Here in the figure:
e1 = (V1, V2)
e2 = (V2, V3)
e4 = (V2, V4)
Undirected Graphs: An Undirected graph G
consists of a set of vertices, V and a set of
edge E. The edge set contains the unordered
pair of vertices. If (u, v)∈E then we say u and
v are connected by an edge where u and v
are vertices in the set V.
Example: Let V = {1, 2, 3, 4} and E = {(1,
2), (1, 4), (3, 4), (2, 3)}.
Bipartite Graph: A graph G = (V, E) is said to be
bipartite graph if its vertex set V(G) can be
partitioned into two non-empty disjoint subsets.
V1(G) and V2(G) in such a way that each edge e
of E(G) has its one end in V1(G) and other end in
V2(G).
The partition V1 U V2 = V is called Bipartite of G.
Here in the figure:
V1(G)={V5, V4, V3}
V2(G)={V1, V2}
Connected Graph: A graph is called
connected if there is a path from any vertex
u to v or vice-versa.
Disconnected Graph: A graph is called
disconnected if there is no path between any
two of its vertices.
Labelled Graph: If the vertices and edges of
a graph are labelled with name, data or
weight then it is called labelled graph. It is
also called Weighted Graph.
Subgraph: A graph G = (V1, E1) is called
subgraph of a graph G(V, E) if V1(G) is a
subset of V(G) and E1(G) is a subset of E(G)
such that each edge of G1 has same end
vertices as in G
Cyclic Graph: A graph G consisting of n
vertices and n> = 3 that is V1, V2, V3- – – – –
– – – Vn and edges (V1, V2), (V2, V3), (V3,
V4)- – – – – – – – — -(Vn, V1) are called cyclic
graph.
GRAPH REPRESENTATION
Adjacency Matrix:
The sequential representation
Adjacency List
The linked representation
ADJACENCY MATRIX:
The adjacency matrix shows which nodes are
adjacent to one another.
An Adjacency Matrix is a 2D array of size V x V
where V is the number of nodes in a graph. A
slot matrix[i][j] = 1 indicates that there is an
edge from node i to node j.
Two nodes are adjacent if there is an edge
connecting them.
In the case of a directed graph, if node j is
adjacent to node i, there is an edge from i to j .
In other words, if j is adjacent to i, you can get
from i to j by traversing one edge.
For a given graph with n nodes, the adjacency
matrix will have dimensions of nxn.
Adjacency matrix is a sequential
representation.
It is used to represent which nodes are
adjacent to each other. i.e. is there any edge
connecting nodes to a graph.
Undirected graph representation
Directed graph
ADJACENCY LIST
One way is to have the graph maintain a list
of lists, in which the first list is a list of
indices corresponding to each node in the
graph.
Each of these refer to another list that stores
a the index of each adjacent node to this
one.
It might also be useful to associate the
weight of each link with the adjacent node in
this list.
DIRECTED GRAPH REPRESENTATION
IMPLEMENTED USING LINKED LIST...
This representation can also be implemented using an array as
follows..
GRAPH TRAVERSAL
Graph traversal is the problem of visiting all
the nodes in a graph in a particular manner,
updating and/or checking their values along
the way.
The order in which the vertices are visited
may be important, and may depend upon the
particular algorithm.
The two common traversals:
Breadth-first search
Depth-first search
BREADTH-FIRST SEARCH
Breadth-first search (BFS) is a graph search
algorithm that begins at the root node and
explores all the neighboring nodes.
Then for each of those nearest nodes, the
algorithm explores their unexplored
neighbour nodes, and so on, until it finds the
goal.
A breadth-first search (BFS) explores nodes
nearest the root before exploring nodes
further away.
BFS TRAVERSING:
S->A->B->C->D->E->F->G
APPLICATIONS OF BREADTH-FIRST
SEARCH ALGORITHM
Breadth-first search can be used to solve
many problems such as:
Finding all connected components in a graph G.
Finding all nodes within an individual connected
component.
Finding the shortest path between two nodes, u
and v, of an unweighted graph.
Finding the shortest path between two nodes, u
and v, of a weighted graph.
DEPTH FIRST SEARCH
The depth-first-search algorithm is similar to
the standard algorithm for traversing binary
trees; it first fully explores one subtree before
returning to the current node and then
exploring the other subtree.
Another way to think of depth-first-search is
by saying that it is similar to breadth-first
search except that it uses a stack instead of
a queue.
DFS TRAVERSING:
S->A->D->G->E->B->F->->C
APPLICATIONS OF DEPTH-FIRST SEARCH
ALGORITHM
Depth-first search is useful for:
Finding a path between two specified nodes, u
and v, of an unweighted graph.
Finding a path between two specified nodes, u
and v, of a weighted graph.
Finding whether a graph is connected or not.
Computing the spanning tree of a connected
graph.
SHORTEST PATH ALGORITHM
The shortest path problem is the problem of finding a
path between two vertices (or nodes) in a graph such
that the sum of the weights of its constituent edges is
minimized.
This is analogous to the problem of finding the
shortest path between two intersections on a road
map: the graph's vertices correspond to intersections
and the edges correspond to road segments, each
weighted by the length of its road segment.
The Minimal Spanning Tree problem is to select a set
of edges so that there is a path between each node.
The sum of the edge lengths is to be minimized.
The Shortest Path Tree problem is to find the set of
edges connecting all nodes such that the sum of the
edge lengths from the root to each node is minimized.
APPLICATION OF GRAPHS:
Computer Science: In computer science, graph
is used to represent networks of communication,
data organization, computational devices etc.
Physics and Chemistry: Graph theory is also
used to study molecules in chemistry and
physics.
Social Science: Graph theory is also widely
used in sociology.
Mathematics: In this, graphs are useful in
geometry and certain parts of topology such as
knot theory.
Biology: Graph theory is useful in biology and
conservation efforts.
In circuit networks where points of connection are
drawn as vertices and component wires become the
edges of the graph.
In transport networks where stations are drawn as
vertices and routes become the edges of the graph.
In maps that draw cities/states/regions as vertices
and adjacency relations as edges.
In program flow analysis where procedures or
modules are treated as vertices and calls to these
procedures are drawn as edges of the graph.
Once we have a graph of a particular concept, they
can be easily used for finding shortest paths, project
planning, etc.
In flowcharts or control-flow graphs, the statements
and conditions in a program are represented as
nodes and the flow of control is represented by the
edges.