0% found this document useful (0 votes)
8 views

DS_Unit_4 Graphs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DS_Unit_4 Graphs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

UNIT IV

Dr. Rupali R. Gangarde


 A graph is another non-linear data structure that is widely used to solve
many real-life computing problems

 Use a graph to find out whether two places on a road-map are connected
and what is the shortest distance between them

 In simulating electrical circuits to find out current flows and voltage


drops at various points in the circuit.

 Telephone and computer networks.


 Graph: A graph G is a collection of two sets V and E. V is a
finite non empty set of vertices (or nodes) and E is a finite
non empty set of edges (or arcs) connecting a pair of
vertices.
An edge is represented by two adjacent vertices G is
represented as G = (V,E)
V1
G1= (V,E)
V = { V1, V2 V3, V4}
V2 V4 E = { ( V1, V2), (V2, V3) }
(V3 , V4), (V4, V1) }
V3
G1

V1 V1 V1

V2 V4 V2 V3 V2 V3

V3 V4
V4 V5 V6 V7

G2 G3 G4
 Undirected Graph: A graph is an undirected graph if the pairs of vertices
that make up the edges are unordered pairs.
i.e. an edge(Vi, Vj ) is the same as (Vj, Vi). The graph G1 shown above is an
undirected graph.

 Directed Graph: In a directed graph, each edge is represented by a pair of


ordered vertices i.e. an edge has a specific direction.
 In such a case, edge (Vi, Vj)  (Vj, Vi)
 Complete Graph: If an undirected graph has n vertices, the maximum
number of edges it can have is nC2 = n (n-1) / 2. If an undirected graph G
has ‘n’ vertices and nC2 edges, it is called a complete graph.
If the graph G is directed and has n vertices, G is complete if it has n(n-1)
edges.

 Multigraph: A multigraph is a graph in which the set of edges may have


multiple occurrences of the same edge.
 Degree of Vertex: The degree of a vertex in an undirected graph is the
number of edges incident to that vertex.
In the undirected graph G1, the degree of each vertex = ?.

degree of each vertex = 2

 Indegree of a Vertex: If G is a directed graph, the indegree of a vertex is


the number of edges for which it is head i.e. the numbers edges coming to
it.
Example In graph G5, indegree (V4)=?
indegree(V1)= ?
indegree (V4)=
indegree(V1)=

A node whose indegree is 0 is called a source node.


 Outdegree of a vertex
If G is directed graph, the out degree of a vertex is the number of edges for
which it is the tail i.e. the number of edges going out of it.
Example outdegree (V1) =?
outdegree (V2) =?
 outdegree (V1) =3
 outdegree (V2) =1
 A node whose outdegree is 0 is called a sink node.
 Sink Node=? V4

 Adjacent vertices: If (Vi, Vj) is an edge in G, then we say that Vi and Vj


are adjacent and the edge (Vi, Vj) is incident on Vi and Vj.
 Path: A path from vertex Vp to Vq exists if there exists vertices Vi1, Vi2,
…..Vin such that there exist edges (Vp, Vi1) , (Vi1, Vi2),………(Vin, Vq)

 Length of a Path: The length of a path is the number of edges on it.

 Linear Path: A linear path is a path whose first and last vertices are distinct.
 Cycle: A cycle is a path whose first and last vertices are the same. Cycle
in graph?
Example V1 V2 V3 V1 is a cycle in G5.

A graph with no cycles is called an acyclic graph. A directed acyclic graph is


called dag.
 Connected Graph
Two vertices Vi and Vj are said to be connected if there is a path in G from Vi
to Vj.

 Strongly Connected Graph: A directed graph G is said to be strongly


connected if for every pair of distinct vertices Vi, Vj, there is a directed path
from Vi to Vj and also from Vj to Vi.

 Weakly Connected Graph : A directed graph G is said to be weakly


connected there exists at least one set of distinct vertices Vi, Vj, such that
there is a directed path from Vi to Vj but no path from Vj to Vi.
 Subgraph: A subgraph of G is a graph G such that
 V(G)  V(G) and E(G)  E(G)
Example: The subgraphs of G are
 Forest:
forest is an undirected, disconnected, acyclic graph. In other words, a
disjoint collection of trees is known as forest. Each component of a forest is
tree.

 Weighted Graph or Network: A number (weight) may be associated with


each edge of a graph. Such a graph is called a weighted graph or a
network.
 Example The number may represent the distance, cost, etc.

V1
10 5

V2 V3 V4
4 6
 When a graph G is connected, a traversal method visits all
its vertices. In this case the edges of G are partitioned
into two sets.
 T for the edges traversed.
 B (Back edges) which were not traversed.

 The edges in T form a tree which connects all vertices of


graph G. Such a tree is called a spanning tree.
 A spanning tree consists of the minimum number of edges
to connect all the vertices.
graph (i) (ii) (iii)
Spanning trees

A graph and its spanning trees


 The spanning tree having the minimum sum of weights of
edges is called minimum cost spanning tree.
 These weights may represent the lengths, distances, cost,
etc.
 Such trees are widely used in practical applications such as
network of road lines between cities, etc.
 i. Adjacency matrix
 ii. Adjacency list
 The adjacency matrix A of a graph G is a two
dimensional array of n  n elements where n is the
numbers of vertices. The entries of A are defined
as
 A[i][j] = 1, if there exist an edge between
vertices i and j in G.
 A[i][j] = 0, if no edge exists between i and j.
The degree of vertex i is the number of 1’s in its row (or the sum of row i )
The degree of vertex i is the number of 1’s in its row (or the sum of row i )
i. Undirected graph
The degree of vertex i is the number of 1’s in its row (or the sum of row i )

ii. Directed graph


(Indegree) : Total number of 1’s in column i.
(Outdegree) : Number of 1’s in row i.
 It uses arrays and hence is memory inefficient.
 From the adjacency list we can calculate the outdegree of any vertex i. The
total number of nodes in list ‘i’ is its outdegree.
 To obtain the indegree, we can construct an inverse adjacency list. The
inverse adjacency list of G7 is as shown.
 Draw inverse adjacency list to get indegree
 Two methods are used for graph traversal
 Depth First Search
 Breadth First Search
Starting from vertex ‘v’, we follow a path in the graph as deeply
as we can go marking the vertices in the path as ‘visited’. When
there are no adjacent vertices that are not visited, we proceed
backwards (back track) to a vertex in the path which has an
‘unvisited’ adjacent vertex and proceed from this vertex. The
process continues till all vertices have been visited.

V1

V2 V3

V4 V7
V5 V6

V8
Algorithm recDFS (int v)
// visited is a global array initialized to zero
// v is the starting vertex
{
visited [v] = 1; /* Mark v visited */
display v
for each vertex w adjacent to v do
if (w is not visited)then
recDFS (w); //recursive call
}
V1

V2 V3

V4 V7
V5 V6

V
V1

V2 V3

V4 V7
V5 V6

V8
v w Visited Stack
V1 V1 V1
V1 V2 V1 V2 V 1 V2
V2 V4 V1V2 V4 V1V2 V4
V4 V8 V1V2 V4 V8 V1V2 V4 V8
V8 V5 V1V2 V4 V8 V5 V1V2 V4 V8 V5
V5 Not found V1V2 V4 V8 V5 V1V2 V4 Pop,po
p

V8 V6 V1V2 V4 V8 V5 V6 V1V2 V4 V6
V6 V3 V1V2 V4 V8 V5 V6 V3 V1V2 V4 V6 V3

V3 V7 V1V2 V4 V8 V5 V6 V3 V7 V1V2 V4 V6 V3 V7
V7 Not found V1V2 V4 V8 V5 V6 V3 V7 V1V2 V4 V6 V3 Pop
V7 Not found V1V2 V4 V8 V5 V6 V3 V7 V1V2 V4 V6 Pop
V3 Not found V1V2 V4 V8 V5 V6 V3 V7 V1V2 V4 Pop
V6 Not found V1V2 V4 V8 V5 V6 V3 V7 V1V2 Pop
V4 Not found V1V2 V4 V8 V5 V6 V3 V7 V1 Pop
V2 Not found V1V2 V4 V8 V5 V6 V3 V7 Empty Pop
V1 Not found V1V2 V4 V8 V5 V6 V3 V7 Empty
 When DFS is initiated from a vertex v, DFS visits all
vertices connected to v.

 So,all the vertices visited and the edges in G, which


are incident on these vertices forms a connected
component of G.

 Thus,
DFS can be used to find all connected
components of G by making repeated calls to DFS.
V1, V2, V3, V4, V5, V6, V7, V8
 Representation of electric circuits. Calculation of current flows,
voltage drops at various points in the circuits.
 Maps indicating connectivity and distances between different
places.
 Telephone and computer networking.
 Routing from one location to another.
 Scheduling of interdependent tasks or activities in an AOV
(Activity on Vertex ) Network.
 Computing project completion time, delays, early start and late
finish times for a project, which is made up of several tasks.
 Computing the critical path in an AOE (Activity on Edge) Network
using CPM ( Critical Path Method).
 Several sophisticated techniques like PERT ( Performance
Evaluation and Review Technique), RAMPS ( Resource Allocation
and Multi-Project Scheduling) have been developed to evaluate
projects which can be represented using network models. These
techniques extensively use graphs.
V2

V1 V3 V5

V4 V6

 Ifthe starting vertex is V1, The


 DFS of this graph is and the BFS is
V2

V1 V3 V5

V4 V6

 Ifthe starting vertex is V1, The


 DFS of this graph is V1 V2 V5 V3 V6 V4

 and the BFS is V1 V2 V3 V4 V5 V6


 IfG is a graph containing n vertices, then the
minimum number of edges needed to connect the
n vertices = n-1. All connected graphs with n-1
edges are trees. These are called spanning trees.
 Definition of Spanning Tree: Let G = (V, E) be an
undirected connected graph. A subgraph t = (V, E)
of G is a spanning tree of G iff t is a tree.
 A minimum spanning tree is a spanning tree with
the lowest number of edges.
Step Consider Select Spanning Tree

1. (V1, V6) (V1, V6) 20


v1 v2
(V1, V2), 8
2. (V5, V6) 7
(V5, V6) v3
4
v7
(V1, V2), (V5, V7), 6
3. (V4, V5) 9
(V4, V5) 11
v6 v5 v4
15 10
(V1, V2), (V5, V7),
4. (V3, V4)
(V3, V4), (V4, V7)

(V1, V2), (V5, V7),


5. (V2, V3)
(V4, V7), (V2, V3)

(V1, V2), (V5, V7),


6. (V2, V7)
(V4, V7), (V2, V7)
(V4, V7) Forms a cycle
(V1, V2), (V5, V7),
7.
(V4, V7)
8. (V5, V7), (V1, V2) (V5, V7) Forms a cycle
9. (V1, V2) (V1, V2) Forms a cycle
Remove all loops and parallel edges from the given graph.
In case of parallel edges, keep the one which has the least cost associated
and remove all others.
We select the edges one-by-one. We select an unvisited edge
having smallest cost and add it to the partially complete
spanning tree. If the edge forms a cycle, it is not considered.
When n-1 edges have been added to the spanning tree, the
process stops. 20
v1 v2
Step Consider Spanning Tree 8
7 v3
1 (V1, V6)
4
2. (V3, V4) v7
3. (V2, V7) 9 6
4. (V2, V3) 11
5. (V4, V7) Forms a Cycle, Reject v6 v5 v4
6. (V4, V5) 15 10
7. (V5, V7) Forms a Cycle, Reject 20
v1 v2
8. (V5, V6) 8
7 v3
4
v7
9 6
11
v6 v5 v4
15 10
Kruskal's algorithm to find the minimum cost spanning tree
A person wishing to travel from city A to city B
could require the following information.
 i. Is there a path from A to B?
 ii. If there is more than one path from A to B,
which is the shortest?
 Inputs
1. This algorithm takes the input graphs in the
form of a cost matrix which is an adjacency matrix
with each element,
A[i] [j] = cost of edge (i,j), if it exists
= infinity, otherwise
2. visited is an array of n elements which
indicates whether a vertex has been visited or not.
3. dist is an array of n elements which stores the
distances of all vertices from the source vertex.
80 120 150
V1 V2 V3 V4

30 100 25
100
V0 V5
0
170 14 90

V7 V6
100

dist[] array
80 120 150
V1 V2 V3 V4

30 100 25 V-starting vertex


100 u-neighbouring(short
V0 V5 distance)
0 W-rechable vertice from
170 14 90
u
V7 V6
100

Visited u Dist Array Remark


1 2 3 4 5 6 7
80 120 150
V1 V2 V3 V4

30 100 25 V-starting vertex


100 u-neighbouring(short
V0 V5 distance)
0 W-rechable vertice from
170 14 90
u
V7 V6
100
AOV network: A directed graph in which the vertices represent activities or
tasks and the edges represent the precedence is called an AOV network.
There should be no cycles in an AOV network i.e there should be atleast one
activity which does not have a predecessor(start activity).
Purpose: To represent project planning by graph.
Topological sort: If we have an AOV network, then we would like to find out
whether the project is feasible or not and if it is feasible, then in what order
should the activities be performed so that the project can be completed. The
process of converting a set of precedences represented by an AOV network
into a linear list in which no later activity precedes an earlier one is called
Topological Sorting. This method identifies the order of the activities.

V2 V2

V1 V4 V1 V4

V3 V3

feasible not feasible


We start with the activity / activities having no predecessor i.e. having indegree 0. These are
then marked as visited and pushed into a stack. We then pop an activity from the stack, find
out the activities dependent on this activity and reduce their indegrees by 1. The same
process repeats again till all the activities have been visited and the stack is empty.

AOV network Adjacency list


Indegree ptr
V1 0 2 3 NULL
V2 V4
V2 1 4 NULL

V3 1 4 5 NULL
V1
V4 2 5 NULL
V3 V5 V5 2 NULL
Thus , the topological order is :V1, V3, V2, V4, V5
 1.
Find the indegree and outdegree of every vertex in
the following graph.

Vertex Indegree Outdegr


ee
1 0 2
2 1 2
3 1 2
4 2 1
5 2 1
6 2 0
Step Consider Action

1. (V4, V5) Select

2. (V2, V4) Select

3. (V3, V5) Select

4. (V3, V4) Reject

5. (V5, V6) Select

6. (V1, V2) Select


 For the following graph, perform the following
 Draw adjacency matrix
 Draw adjacency list
 Indegree and Outdegree of vertices
 DFS and BFS Spanning Tree ( starting vertex = 2)
 Strongly Connected components

You might also like