DS_Unit_4 Graphs
DS_Unit_4 Graphs
Use a graph to find out whether two places on a road-map are connected
and what is the shortest distance between them
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.
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.
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.
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.
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
V1 V3 V5
V4 V6
30 100 25
100
V0 V5
0
170 14 90
V7 V6
100
dist[] array
80 120 150
V1 V2 V3 V4
V2 V2
V1 V4 V1 V4
V3 V3
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.