UNIT - IV (Graphs)
UNIT - IV (Graphs)
Sc
GRAPHS
A graph is a set of nodes (or) vertices and edges (or) branches which connect
them. A graph can be represented as G=(V, E)
Where ‘V’ is the set of nodes and ‘E’ is the set of edges.
2 3
4 5 6 7
If two nodes are connected by an edge, those two nodes are said to be adjacent
(or) neighbors.
Ex: In above fig. the nodes adjacent to node 3 are 1,4,5 and that to node ‘2’ are 1,4.
undirected Graph:
The edge which has no specific direction is called undirected edge. The graph
with undirected edges is called a undirected graph. The graph shown in the above
fig. is a undirected graph.
Directed Graph:
An edge, which has direction, is called directed edge. Direct edge is
unidirectional. It goes from one node to another.
A directed graph (or) digraph is a graph in which all edges are directed edges.
Fig. below shows the directed graph.
A
B
C D
All edges in the above graph are directed. Directed edges are unidirectional .
Ex: There is a edge B to C, but there is no edge representing the reverse relationship.
A graph contains both directed and undirected edges is called mixed graph.
If a node does not have any adjacent nodes, then it is said to be isolated node.
Ex:
A
B C D
‘D’ is isolated node.
Null Graph:
Loop:
An edge in a graph which starts and ends on the same node is called a loop. A
loop can be considered either a directed (or) undirected.
Ex:
C
B D
Multigraph:
A pair of nodes may be connected by more than one edge. Such edges are
referred to as parallel. A graph with parallel edges is called multigraph. A simple
graph is a graph in which there are no parallel edges.
Path :
A path P of length n from node u to a node v is defined as sequence of n+1
nodes.
P=(v0,v1,v2,….vn)
where u=v0 and v=vn .
The path P is said to be closed if v0 = vn
The path P is said to be simple if all the nodes except first and last are different.
Cycle: a cycle is a closed simple path with length 3 or more. A cycle of length k is
called
k-cycle.
Connected graph:
a graph G is said to be connected if there is a path between any two of its nodes.
Complete graph:
a graph G is said to be complete if every node u in G is adjacent to every other node
v in G. A complete graph with n nodes will have n(n-1)/2 edges. A complete directed
graph with n nodes require n(n-1) directed edges.
Tree graph: a connected graph with out any cycles is called tree graph.
Sub graph: a graph H is a subgraph of another graph G, iff its vertex and edge sets
are subsets of those of G.
Spanning tree : A sub graph of G that contains all vertices of G, that contains no
cycle is called spanning tree of G.
Degree of a Node:
The degree of a node is the number of edges connected directed to that node
i.e. the number of edges incident on it.
In a directed graph, the indegree of a node is the number of edges coming into
the node. The outdegree of a node is the number of edges leaving the node. The sum
of indegree and outdegree of a particular node is called total degree of that node. In
case of undirected graphs, total degree is nothing but degree itself. The concept of
indegree and outdegree can not apply to undirected graph. A node whose indegree
is ‘0’ is called a source node and a node whose outdegree is ‘0’ is called sink node.
Ex: A C
B
E
For Node ‘B’: D For Node E:
In degree : 2 In degree: 2
Outdegree: 2 Out degree: 1
Total degree: 4 Total degree: 3
Weighted graph:
A weighted graph is a graph that has a weight associated with each edge.
Fig. below shows the weighted graph.
B
4 5
6
A C
8
7 15
D 9 E
Representation of Graphs
There are three major approaches to represent graphs.
i. Adjacency Matrix representation.
ii. Adjacency list representation (or) linked representation.
iii. Multilist representation.
A C
B D
There are 4 nodes, therefore adjacency matrix of the above graph consists ‘4’
rows and ‘4’ columns.
A B C D
A 0 1 1 0
0 0 0 1
B
0 1 0 0
C
D
Department of Computer Science 4 A.S.N. Degree College, Tenali
Data Structures Using Java UNIT-IV II Year IV Semester B.Sc
0 0 1 0
C
A
A B C D
A 0 1 0 1
1 0 1 1
B
0 1 0 1
C
1 1 1 0
D
The adjacency matrix for an undirected graph is symmetric and for directed
graph need not be symmetric. The space needed to represent a graph using its
adjancy matrix is n2 bits. For an undirected graph the degree of any vertex is its row
sum. For directed graph ,the row sum is out degree and column sum is in degree.
B
1.5 2
A 3 D
1 2.7
C
For weighted graph, the column, row intersection in matrix contains the
weight of that edge, otherwise it is ‘0’. The adjacency matrix of the graph is as shown
below.
A B C D
A 0 1.5 1 0
0 0 0 2
B
0 3 0 2.7
C
0 0 0 0
D
The directory contains one entry for each node of the graph. Each entry in the
directory points to a linked list that represents the nodes that are connected to that
node. The directory represents nodes and the linked list represents the edges.
Each node of the linked list has three fields one is the node identifier, second is a link
to the next field and the third is an optional weight field which contains the weight of
the edge.
1 3 5
4 6
Since No weights for edges the node of the linked list will be
Nodeid Next
4
1
2 1 3 5
3 4
5 6
4
5
Department of Computer Science 7 A.S.N. Degree College, Tenali
6
Data Structures Using Java UNIT-IV II Year IV Semester B.Sc
NULL
NULL
8 1
3 5
6
1 2 3 3 4 4 7
2
3 8
3
4 1 5 6
4
NULL
5 NULL
* Un directed graphs can be stored using adjacency lists, however, each edge will be
represented twice, once in each direction.
Graph Traversal
Breadth-first traversal:
Breadth-first search operates by processing nodes in layers. The breadth-first
search can begin at any arbitrary node. The nodes which are adjacent to the start
node are processed first, and proceeds to adjacent nodes of that nodes just visited.
This process continues until all the nodes are visited.
A data structure queue is used to place all waiting nodes i.e. nodes to be
visited. Following is the procedure for BFS.
1. All nodes are initialized as ready states and initialize queue to empty.
2. Begin with any node which is in ready state and put into queue. Mark the
status of that node to waiting.
3. While queue is not empty, repeat the following steps:
i. Delete the first node say ‘k’ from queue and process it. Mark the
status of that node to visited.
ii. Add all the adjacent nodes of K which are in ready state to the rear
side of the queue and mark the status of those nodes to waiting.
4. If the graph still contains nodes which are in ready state, then repeat the
process from step2.
2 3
4 5 6 7
8
If starting node 1, then BFS result is
1 2 3 4 5 6 7 8
Ex:
1
2 4
3 5
BFS result: 1 2 3 4 5
Depth-first Traversal:
The depth-first traversal of a graph is much similar to preorder traversal of an
ordered tree. The traversal of a graph may start at any arbitrary node, say A. If B, C,
D and E are the nodes adjacent to A. Then after A, B will be visited keeping C,D and
E is waiting. After visiting B, all the nodes adjacent to B will be visited before
returning to traverse C,D and E.
Data structure stack will be used to push all waiting nodes. Following is the
procedure for DFS.
1. All nodes are initialized to ready state and initialize stack to empty.
2. Begin with any node which is in ready state and push into stack. Mark the
status of that node to waiting.
3. While stack is not empty, repeat the following steps.
i. POP the top node, say ‘K’ and process it. Mark the status of that
node to visited.
ii. Push all the adjacent nodes of ‘K’ which are in ready state into stack
and mark the status of those nodes to waiting.
4. If the graph still contains nodes which are in ready state then, repeat the
process from step2.
Ex: Consider the following graph.
2 3
4 5 6 7
8
If starting node is 1, then DFS result is
1 2 5 8 4 3 7 6
(or)
1 3 7 8 6 2 5 4
Ex:
1
2 3
4 5
DFS result: 1 2 4 5 3
(or)
1 3 4 5 2
Spanning Tree
There can be weights assigned to every edge in a weighted graph. However, A minimum
spanning tree is a spanning tree which has minimal total weight. In other words, minimum
spanning tree is the one which contains the least weight among all other spanning tree of
some particular graph.
In this section of the tutorial, we will discuss the algorithms to calculate the
shortest path between two nodes in a graph. There are two algorithms which are
o Prim's Algorithm
o Kruskal's Algorithm
Prim's Algorithm
Prim's Algorithm is used to find the minimum spanning tree from a graph. Prim's
algorithm finds the subset of edges that includes every vertex of the graph such
that the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explore all the adjacent nodes with all
the connecting edges at every step. The edges with the minimal weights causing no
cycles in the graph got selected.
Example :
Construct a minimum spanning tree of the graph given in the following figure by using
prim's algorithm.
Solution
of the graph shown in the above figure. The cost of MST will be
calculated as;
cost(MST) = 4 + 2 + 1 + 3 = 10 units.
Kruskal's Algorithm
Kruskal's Algorithm is used to find the minimum spanning tree for a connected
weighted graph. The main target of the algorithm is to find the subset of edges by
using which, we can traverse every vertex of the graph. Kruskal's algorithm follows
greedy approach which finds an optimum solution at every stage instead of focusing
on a global optimum.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it
to the forest (for combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Example :
Solution: