DS Unit-5
DS Unit-5
GRAPHS
Graph:
Graph is a nonlinear data structure; it contains a set of points known as nodes (or vertices) and set of
links known as edges (or Arcs) which connects the vertices. A graph, is defined as follows... A graph, G
consists of two sets V & E. V is a finite non-empty set of vertices. E is a set of pairs of vertices, these pairs are
called edges.
E.g.:
A
C
B D
Graph Terminology:
Vertex
An individual data element of a graph is called as Vertex. Vertex is also known as node. In above
example graph, A, B, C, D & E are known as vertices.
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented
as (starting Vertex, ending Vertex).
E.g. The link between vertices A and B is represented as (A, B). There are 7 edges in the
graph (i.e., (A, B), (A, C), (A, D), (B, D), (B, E), (C, D), (D, E)).
Edges are three types.
1.Undirected Edge - An undirected edge is a bidirectional edge. If there is an undirected edge between
vertices A and B then edge (A, B) is equal to edge (B, A).
2.Directed Edge - A directed edge is a unidirectional edge. If there is a directed edge between vertices A
and B then edge (A, B) is not equal to edge (B, A).
3.Weighted Edge - A weighted edge is an edge with cost on it
Directed Graph:
If the pair of nodes that make up the arcs are ordered pairs the graph is said to be a directed graph.
Or a graph in which all the edges are directed is called as Directed graph or a Digraph i.e., each edge is
represented by a directed pair and denoted as (v1, v2) where v1 is the starting vertex and v2 the ending vertex
of the edge. Eg:
Vertices ={ A, B, C, D, E, F}
Edges={(A, B), (B, C), (C, E), (E, F), (E, D), (D, B)}
Department of Computer Science, SSBN Degree College, ATP
1
Eg:
Vertices= {A, B, C, D, E}
Edges = {{A, B}, {A, D}, {A. E}, {B, E}, {B, D}, {D, C}}
Weighted Graph:
A graph in which a number is associated with each arc is called a weighted graph or network.
E.g.
90
20
BC
60
Degree:
Total number of edges connected to a vertex is said to be degree of that vertex.
In degree
Total number of incoming edges connected to a vertex is said to be in degree of that
vertex. Out degree
Total number of outgoing edges connected to a vertex is said to be out degree of that
vertex. Parallel edges or Multi edges
If there are two undirected edges to have the same end vertices, and for two directed edges to have
the same origin and the same destination. Such edges are called parallel edges or multiple edges. Self-loop
An edge (undirected or directed) is a self-loop if the starting vertex and ending vertex is the same
vertex. Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.
Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In other
words, two vertices A and B are said to be adjacent if there is an edge whose end vertices are A and B.
Path:
A path of length ‘k’ from node ‘a’ to node ‘b’ is defined as a sequence of ‘KH’ nodes. n1, n2,….nk+1
such that n1=A, nk+1=B and adjacent (ni, ni+1) is true for all i between 1 and k.
Note:
»A path from a node to itself is called a cycle and a graph containing it is called cyclic.
»A graph with at least one cycle is called as cyclic graph.
»A graph without cycles is called as acyclic graph.
Representation of Graph:
A graph can be represented in two ways in memory. One is sequential representation by means of
matrix called adjacency matrix, the other way is linked representation by means of linked lists. Sequential
representation:
A graph is conveniently represented by a matrix (two-dimensional array) called adjacency matrix. A
graph containing n nodes can be represented by a matrix containing n rows and n columns.
Adjacency Matrix:
Let us suppose ‘G’ is a simple directed graph with ‘m’ nodes and suppose the nodes of G have been
ordered and are named v1,v2,….vm then the adjacency matrix A={aij} of the graph ‘G’ is the mxm matrix
defined as
Note:
» If the graph is an undirected graph then the adjacency matrix is symmetric.
Disadvantages:
» Graphs with few edges would have a lot of wasteful zeros in the adjacency matrix i.e., the
corresponding adjacency matrix is sparse
» Insertion & deletion of nodes is difficult
Department of Computer Science, SSBN Degree College, ATP
3
class Node
{
String data;
Data Next Adjacent
Node next;
Edge adjacent;
}
Where
» Data – holds the value of associated with the vertex in the graph
» Next – address field that points to next vertex in the node list
» Adjacent – address field that points to the first node of adjacency list of the node.
In Edge List, each node corresponds to an edge of graph G A node in the edge list contain two or three fields.
class Edge
{
Terminal Pointer Weight
Node terminal;
Edge pointer;
double weight;
}
Where
» Terminal – address field that points to node in the node list which is terminal vertex of the edge in the
graph
» Pointer –Address field that points to next node in the adjacency list which are all initiated from same
Vertex in the graph
Consider the following directed graph & form the adjacent list of the graph.
Applications of Graphs:
Most of the real-world problems can be represented by using Graphs with the help of standard algorithms
most problems can be solved
• Social Networking (Facebook): Each user is represented as a vertex and two people are friends when
there is an edge between two vertices. Similarly, friend suggestion also uses graph theory concept. •
Navigation Systems (Google Maps): Various locations are represented as vertices and the roads are
represented as edges and graph theory is used to find shortest path between two nodes. •
Recommendations on e-commerce websites (Amazon, Flipkart): The “Recommendations for you” section
on various e-commerce websites uses graph theory to recommend items of similar type to user’s choice.
• In computer network, the relationships among interconnected computers within the network, follow
the principles of graph theory.
GC
B
Path from A to B: - A – C – G – B
E F A D
Department of Computer Science, SSBN Degree College, ATP
5
3. Ti contain all nodes that are reachable from the root of Tj and are not contained in Ti i.e., j<i.
Graph G
B C
Spanning Trees
A
A
A
BC
BC
BC
BFS - A B E D C F G
Algorithm:
1. All nodes are initialized as ready states & initialize queue to empty
2. Begin with any node, which is in ready state & put into queue. Mark the states of that node to
waiting 3. while queue is not empty do
Begin
a) Delete the first node k from queue & process it. Mark the states of that node to visited b) Add
all the adjacent nodes of k which are in ready state to the rear side of the queue and mark the
states of those nodes to waiting
End
4. If the graph still contains nodes which are in ready state then go to step2
5. Return
Efficiency:
The efficiency of both Breadth First Traversal is quadratic order (O (V2)) of growth when the graph is
represented using adjacency matrix and O(V+E) when the graph is represented using adjacency list where v is
the number of vertices and E is number edges in the graph
Applications:
• In Peer-to-Peer Networks like BitTorrent, Breadth First Search is used to find all neighbor nodes.
• Crawlers in Search Engines: Crawlers build index using Breadth First
• Social Networking Websites: In social networks, we can find people within a given distance ‘k’ from a
person using Breadth First Search till ‘k’ levels.
• GPS Navigation systems: Breadth First Search is used to find all neighboring locations. • Broadcasting in
Network: In networks, a broadcasted packet follows Breadth First Search to reach all nodes.
Department of Computer Science, SSBN Degree College, ATP
9