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

BM _Unit 5_Graph

Uploaded by

Meet Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

BM _Unit 5_Graph

Uploaded by

Meet Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

UNIT- 5

Graphs and Trees Graphs: Introduction, Definition; Initial & Terminal Nodes,
Adjacent Nodes; Directed Edge, Undirected Edge, Directed Graph (Digraph),
Undirected Graph, Mixed Graph; Loop (Sling); Distinct Edges, Parallel Edges; Multi-
graph, Simple Graph; Weighted Graph; Isolated Nodes, Null Graph; Isomorphic
Graphs; In-degree, Out-degree, Total-degree; Subgraphs; Reflexive, Symmetric,
Transitive Digraphs; Paths, Length of Path of a Graph; Simple Path (Edge Simple),
Elementary Path (Node Simple), Cycle (Circuit), Simple Cycle, Elementary Cycle;
Path of Minimum Length (Geodesic), Distance between Two Nodes, Triangle
Inequality; Reachability, Reachable Set of a Node, Connected Graphs: Strongly,
Unilaterally, Weakly Connected Graphs & Components;
Trees: Introduction, Definition, Root, Branch Nodes, Leaf (Terminal Node); Different
Representations of Trees; Forests, Subtrees; M-ary Tree, Full or Complete M-ary
Tree; Binary Tree, Full (Complete) Binary Tree; Conversion of M-ary Tree to Binary
Tree; Traversal of Binary Tree: Pre-order, In-order, and Post-order Traversal;
MOTIVATION
What is the common link between the following problems:
 Traffic network design and cancer research?
 Arranging marriages and scheduling flights?
 Finding cure for mental illness, computer chip design, architectural
floor planning, fighting terror online?
 Fighting epidemics, e-commerce, designing voting schemes, job
assignment, designing electrical networks, deciding on facility
location, etc. etc. etc.

1
GRAPH THEORY

Computer networks, circuit design, data structures

How to color a map? How to send data efficiently?


1
HOW DOES IT STARTED?
Wikipedia: The city of Konigsberg (1652) in Prussia (now Kaliningrad, Russia) was set
on both sides of the Pregel River, and included two large islands which were connected
to each other and the mainland by seven bridges. The problem was to find a walk
through the city that would cross each bridge once and only once.“
In 1735, Euler published the paper “The solution of a problem relating to the geometry
of position", showing that the problem had no solution. This is regarded as the first
paper in graph theory.

4
ROLE OF GRAPH THEORY
Graph theory play an important role is several areas of computer
science such as
 switching theory and logical design,
 artificial intelligence,
 formal languages,
 computer graphics,
 operating systems,
 compiler writing,
 information organization and retrieving and many more.

5
TOPICS TO BE DISCUSSED FOR INTRODUCTION TO GRAPH
Graphs: Introduction, definition, examples; Nodes, edges, adjacent
nodes, directed and undirected edge, Directed graph, undirected
graph, examples; Initiating and terminating nodes, Loop (sling),
Distinct edges, Parallel edges, Multi-graph, simple graph, weighted
graphs, examples, Isolated nodes, Null graph; Isomorphic graphs,
examples; Degree, In- degree, out-degree, total degree of a
node, examples; Subgraphs: definition, examples; Converse
(reversal or directional dual) of a digraph, examples;
DEFINITIONS AND FUNDAMENTAL CONCEPTS
As the name suggests Graphs can be represented
Graphically or diagrammatically, but this
representation is suitable in very simple cases.
Hence we have to use alternate ways to represent
graphs.

Graph : Conceptually, a graph is formed by vertices


and edges connecting the vertices.
A graph is a pair of sets (V, E), where V is the set of
vertices and E is the set of edges.
We label the vertices with letters , for example: a, b, If we label the vertices
c, . . . or by v1, v2 , . . . or numbers 1, 2, . . . 7
We have
V = {v1, . . . , v5} for the vertices
and
E = {(v1, v2), (v2, v5), (v5, v5), (v5, v4), (v5, v4)} for
the edges.
We often label the edges with letters (for example:
a, b, c, . . . or e1, e2, . . . ) or numbers 1, 2, . . . for
simplicity.
The two edges (u, v) and (v, u) are the same. In other
words, the pair is not ordered.
If we label the edges then set of edges for graph
is,
E = {e1, . . . , e5}.

8
GRAPH (DEF.)
A graph G = < V , E, ø > consists of a nonempty set V called the set of
nodes (points, vertices) of the graph, E is said to be the set of edges of the
graph, and ø is a mapping from the set of edges E to a set of ordered (e.g.
<u,v>) or unordered (e.g. (u,v)) pairs of elements of V.
We have to assume that both the sets V and E are finite. The graph G can
be represented as <V,E>, or simply G.

9
In the diagrams
the directed
edges are shown
by means of
arrows which
also show the
directions
Adjacent Nodes: Any pair of nodes which are connected by an edge in a graph is
called adjacent nodes. i.e. Two vertices u and v are adjacent if they are connected
by an edge, in other words, unordered pair (u, v) or ordered pair <u,v> are
Adjacent nodes if an edge e joins them.
(e . g in following Fig. node 1 & 2 , 1 & 3 , 2 &3 are adjacent node)

2
Directed
edge
V1 V2

1 3 Directed
Un Directed edge
edge Directed graph

11
Directed Edge:
In a graph G=<V,E>, an edge which is associated
with an ordered pair of V X V is called directed
edge of G.
Undirected Edge :
2
In a graph G=<V,E>, an edge which is associated
with an unordered pair of nodes is called V1 v2
undirected edge.
Undirected edge
Digraph/Directed Graph : 1 3

A graph in which every edge is directed is called a Undirected


graph
digraph or directed graph.
Undirected Graph :
A graph in which every edge is undirected is called
an undirected graph.
Mixed Graph:
A graph in which some of the edges are directed & some are
undirected then the graph is called Mixed Graph. Eg:
 In the diagrams the directed edges are shown by means of arrows
which also shows the directions.
 The city map showing only the one-way streets in an example of
directed graph in which the nodes are the intersections and the edges
are the streets.
 A map showing only the two-way streets is an example undirected
graph.
 A map showing all the one-way and two-way streets is an example
of a mixed graph.
Initiating/originating and Terminating/Ending node:-
Let <V,E> be a graph and let x ∈ E be a directed edge associated with the
ordered pair of nodes <u,v>. Then edge x is said to be initiating or originating in
the node u and terminating or ending in the Node v.
The nodes u & v are also called the initial and terminal nodes of the edge x.
An edge x∈E which joins the nodes u and v, whether it be directed or undirected, is
said to be incident to the nodes u and v.
Loops:
An edge that starts and ends at the same vertex
or node is called a loop. In the example, vertex d
has a loop. i.e. An edge of the form (v, v) is a
loop.
(The direction of a loop is of no significance
hence, it can be considered either a directed or
undirected edge)
Distinct edges: In case of directed edges, the two possible
edges between a pair of nodes which are opposite in
direction are considered as distinct edges.
In Fig the edges connecting node 2 and 3 are distinct edges.
Parallel edges: In some directed as well as undirected graph
we may have certain pairs of nodes joined by more than one
edges, such edges are called parallel edges. In below fig, a
and b are joined by two parallel Edges.
2 2

1 1
3
3

4
4
Undirected parallel e=(a,b) Directed parallel Undirected parallel
( e = [1,2], e = [2,3], e =[2,2]) (e = [1,4] , e =[4,3])
Multigraph: Any graph which contain some parallel edges is called a multi graph.
Simple graph:
Simple graphs are graphs without multiple edges or self-loops.
(no more than one directed edge in the case of directed graph)
Weighted graph:
A graph in which weights are assigned to every edge is called Weighted Graph.
e.g. A graph representing a system of pipelines in which the weighted assigned
indicate the amount of some commodity transferred through the pipe is an
example of a weighted graph. Similarly, a graph of city streets may be assigned
weights according to the traffic density on each street.
V2

2
1
V1 V3
3
Multi Graphs

Weighted graphs
Isolated Node:
In a graph any node which is not adjacent to any
other node is called isolated node.
Here V3 is an isolated node.

Null Graph & Isolated Graph Node:


A graph containing only isolated nodes is called
a null graph.
Set of edges is null in isolated or null graph. e.g.
(note : the graph shown in fig besides can be V1 V2
considered as either directed or undirected.)
The definition of graph contains no
reference to the length or the shape
and positioning of the arc joining
any pair of nodes, nor does it
prescribe any ordering of positions
of the nodes.
For a given graph, there is no
unique diagram which represents
the graph.
Look at Fig. 5-1.4a,a’, b, b’, and c,
c’ are representing same graph,
C and C’ are isomorphic because
of the existence of a mapping
1→ 𝑢3 2 → 𝑢1
3 → 𝑢4 𝑎𝑛𝑑 4 → 𝑢2
Isomorphic graphs:
Two graphs are isomorphic , if there exists a one-to-one correspondence between
the nodes of the two graphs which preserves adjacency of the nodes as well as the
direction of edges, if any.
If two graphs are isomorphic then they must have:
1. same no of vertices (nodes)
2. same no. of edges
3. same degree of corresponding vertices
4. same no. of connected components
5. same no of loops
6. same no of parallel edges
7. pairs of connected vertices must have the corresponding pair of vertices
connected
In Fig. the graph are isomorphic as there is a mapping; 𝐚 → 𝒂′ 𝒃 → 𝒃′ 𝒄 → 𝒄′
𝒅 → 𝒅′ 𝒂𝒏𝒅 𝒆 → 𝐞’ 16/02/21
G1 and G2 are Isomorphic. G3 is not isomorpic to G1 and G2, as in graph G3, vertex ‘w’ has
only degree 3, whereas all the other graph vertices has degree 2.

Two graphs which are isomorphic have the same


Not Isomorphic as the left-hand graph number of nodes and edges; however, this is not a
has 5 edges; the right hand graph has 6 sufficient condition for an isomorphism to exist. Above
edges. graphs are not isomorphic, look at direction of edges.
HOW TO CHECK WHETHER THE GRAPH ARE ISOMORPHIC OR NOT?

Number of vertices: both 5.


Number of edges: both 5.
Degrees of corresponding vertices: all
degree 2.
Firstly, label the graphs. It “looks” true, so Connectedness: Each is fully connected.
check all the things we know: Number of connected components: Both 1.
Pairs of connected vertices: All correspond.
Number of loops: 0.
Number of parallel edges: 0.
Everything is equal and so the graphs
are isomorphic.
If two graphs are isomorphic, they must have:
- same number of vertices , same number of edges, same degrees for
corresponding vertices , same number of connected components , same
number of loops and same number of parallel edges.
Further, both graphs are connected or both graphs are not connected, and
pairs of connected vertices must have the corresponding pair of vertices
connected.
Graph C and C’ are isomorphic because of the existence of a mapping
1→ 𝑢3 2 → 𝑢1 3 → 𝑢4 𝑎𝑛𝑑 4 → 𝑢2
Under this mapping, the edges <1,3> <1,2> <2,4> and <3,4> are mapped into <u3, u4>,
<u3, u1>, <u1,u2>, and <u4 ,u2> which are the only edges of the graph in c’.
It is easier to prove two graphs are not isomorphic by proving that one of the above properties
fails.
Outdegree : In directed graph , for any node v, the number of edges which have v
as their initial node is called the outdegree of the node v (outdegree for loop is 1)
Indegree : The number of edge which have v as their terminal node is called
indegree of v (for loop indegre is 1)
Total degree : Sum of outdegree and indegree of a node v is called total degree
(for loop total degree is 2 and for null node it is 0).

2
Node In degree Out degree total degree
1 1 2 3
2 0 2 2
1 3 3 3 0 3
4 1 1 2

4
 In case of an undirected graph, the total degree or the degree of a node v is
equal to the number of edges incident with v.
 The concept of the degree of a node can be generalized to a set of nodes.
 Let G= <V , E> be a directed graph and X ⊆ V be a subset of nodes.
 The number of edges of G which have their initial node in X but their terminal
node not in X is called the out degree of X.
 Similarly , the number of edges of G which have their terminal nodes in X but
their initial nodes not in X is called the indegree of X.
 A simple result involving the notion of the degree of nodes of a graph is that
the sum of the degrees of all the nodes of a graph must be an even number
which is equal to twice the number of edges in the graph.
Original Graph Sub Graph
Subgraph: Let V(H) be the set of nodes of
a graph H and V(G) be the set of nodes of
a graph G such that V (H) ⊆ V (G). If in
addition, every edge of H is also an edge
of G, then the graph H is called a subgraph
of the graph G, which is expressed by
writing H ⊆ G.
Naturally, the graph G itself, as well as the
null graph obtained from G by deleting all
the edges of G, is also a sub graph of G.
One can get a subgraph by deleting edges
or vertices or both.
Note that deleting a vertex also requires
deleting any edges incident to the vertex
(since we can’t have an edge with a missing
endpoint).
A simple digraph G= <V,E> is called reflexive, transitive, symmetric , antisymmetric etc
if the relation E is reflexive , transitive ,symmetric, antisymmetric etc.
Converse of digraph : The converse of digraph G=<V,E> to be a digraph Q=<V,~E> in
which the relation ~E is the converse of the relation E. The diagram of Q, is called reversal
or directional dual of a digraph G.
If we reverse all the arrows on a digraph, we obtain in general a different digraph—the
converse of the original. Clearly, a digraph and its converse are co-spectral since their
adjacency matrices are transposes of each other.
The converse of G is also called the reversal or directional dual of a digraph G.
A graph is reflexive if for every node v there is an edge (v,v) (a loop).

Original Diagraph Converse Diagraph


 If simple digraph G = <V , E> is reflexive, symmetric, and transitive, then the relation E
must be an equivalence relation on V, and hence V can be partitioned into equivalence
classes.
 If we consider any such equivalence class of nodes along with the edges that join these
nodes, we have subgraphs of G.
 These subgraph are such that every node in the subgraph is adjacent to every other
node of subgraph.
 However, no node of any one subgraph is adjacent to any node of another subgraph.
In this sense the graph G is partitioned into subgraphs which are disjoint.
https://home.cs.colorado.edu/~srirams/courses/csci2824-spr14/relationTypes-20.html
Example for reflexive, transitive, symmetric Graphs
https://web.stanford.edu/class/archive/cs/cs103/cs103.1132/lectures/05/Small05.pdf
PATH, REACHABILITY AND CONNECTEDNESS :
Path : Any sequence of edges of diagraph 1 2

such that the terminal node of any edge in


the sequence is the initial node of the edge,
if any, appearing next in the sequence
defines a path of the graph.
4 3
A path is said to traverse through the nodes
appearing in the sequence, originating in the
initial node of the first edge and ending in Some paths originating in node 2 and
the terminal node of the last edge in the ending in node 4 are
sequence. P1 = (<2,4>)
P2 = (<2,3>,<3,4>)
The no. of edges appearing in the sequence P3 = (<2,1>,<1,4>)
of a path is called length of the path. P4 = (<2,3>,<3,1>,<1,4>)
P5 = (<2,3>,<3,2>,<2,4>)
A path in a diagraph in which the edges are
P6 = (<2,2>,<2,4>)
distinct is called a simple path (edge
simple).
Let G = <V , E> be a simple digraph. Consider a
sequence of edges of G such that the terminal node of
any edge in the sequence is the initial node of the next
edge, if any, in the sequence. An example of such
sequence is
(<vi1, vi2,>, <vi2, vi3>,..........,<vik-2, vik-1>, <vik-1, vik>)
Where it is assumed that all the nodes and edges
appearing in a sequence are in V and E respectively.
The sequence can also be written as Some of the paths originating in node
1 and ending in node 3 are
(vi1, vi2,.......vik-1, vik)
Each node appearing in the sequence must be adjacent to p1= (<1,2>,<2,3>)
the nodes appearing just before and after it in the p2= (<1,4>,<4,3>)
sequence, except in the case of the first and last nodes. p3= (<1,2>,<2,4>,<4,3>)
p4= (<1,2>,<2,4>,<4,1>,<1,2>,<2,3>)
p5= (<1,2>,<2,4>,<4,1>,<1,4>,<4,3>)
p6=(<1, 1>,<1, 1>,........<1 , 2>, <2, 3>)
Length of the path : The number of edges appearing in the
sequences of a path is called the length of the path.
Cycle : A path which originates and ends in the same node is called
a cycle.
e.g. c1=(<1,1>) , c2=(<1,2>,<2,1>) ,
c3=(<1,2>,<2,3>,<3,1>)
c4=(<1,4>,<4,3>,<3,1>) , c5=(<1,4>,<4,3>,<3,2>,<2,1>).
Elementary Cycle : -A cycle is called elementary cycle if it is does
not traverse through any node more than once.
Acyclic : A simple diagraph which does not have any cycles is
called acyclic. (such graphs can not have any loops).
Every elementary path of a digraph is also simple.
The paths p1,p2 and p3 of the digraph in fig. are elementary
The path p5 is simple but not elementary.
If there exists a path from a node say, u, to another node v, then
there must also be an elementary path from u to v.
Any path which is not elementary contains cycles
traversing through those nodes which appear more
than once in the path.
By deleting such cycles one can obtain elementary
paths.
For example, in the path p5, if we delete the cycle
(<1 ,2>, <2, 4>, <4, 1>), we obtain the path p2,
which also originates at 1 and ends in 3 and is an
elementary path.
Similarly, if in the path p4 we delete the same cycle, Paths originating in node 1 and
we get the elementary path. P1. ending in node 3 are
Likewise, it is possible to obtain elementary cycles p1= (<1,2>,<2,3>)
at any node from a cycle at that node. p2= (<1,4>,<4,3>)
p3= (<1,2>,<2,4>,<4,3>)
A simple digraph which does not have cycles is p4= (<1,2>,<2,4>,<4,1>,<1,2>,<2,3>)
called acyclic, such graphs cannot have any loops.
p5= (<1,2>,<2,4>,<4,1>,<1,4>,<4,3>)
p6=(<1, 1>,<1, 1>,........<1 , 2>, <2, 3>)
Paths originates in node 1 & ending in node 3
p1= (<1,2>,<2,3>)
p2= (<1,4>,<4,3>)
p3= (<1,2>,<2,4>,<4,3>)
p4= (<1,2>,<2,4>,<4,1>,<1,2>,<2,3>)
p5= (<1,2>,<2,4>,<4,1>,<1,4>,<4,3>)

Elementary path : p1,p2,p3 (A path in which all the nodes through which it traverses are
distinct)
Simple path: p1,p2,p3,p5 (A path in a diagraph in which the edges are distinct is called a
simple path )
Cycle: c1,c2,c3,c4,c5
Elementary cycle: c1=(<1,1>) c2=(<1,2>,<2,1>) c3=(<1,2>,<2,3>,<3,1>)
c4=(<1,4>,<4,3>,<3,1>) c5=(<1,4>,<4,3>,<3,2>,<2,1>)
Accessible Node/ Reachable Node : A node v of a simple digraph is said to be
reachable/accessible from the node u of the same digraph, if from the node u of
the same digraph, there exists a path from u to v.
Geodesic (Path of Minimum Length) : If a node v is reachable from node u, then a
path of minimum length from u to v is called a geodesic or path of minimum length.
Distance between Two Nodes : The distance from a node u to a node v, if v is
reachable from u, is written as d <u, v> and satisfies the following properties:
 d< u, v > ≥ 0
 d< u, u > =0
 d<u, v> + d<v, w > ≥ d<u, w>
The last inequality is called the Triangle inequality. If v is not reachable from u
then it is customary to write d <u, v> = ∞.
Note also that if v is Reachable from u ,and u is reachable from v, then d <u, v>
is not necessarily equal to d <v, u>.
Theorem 5-1.1 In a simple digraph, the length of any elementary path is less than or equal
n-1, where n is the number of nodes in the graph. Similarly, the length of any cycle does not
exceed n.
Reachability Set of a node: The set of nodes which are reachable from a given node v is said to
be the reachable set of v. The reachable set of v is written as R(v).
Reachable Set of a Set of Nodes : For a digraph G = < V, E> , any subset S ⊆ V, the
reachable set of S is the set of nodes which are reachable from any node of S, and this set is
denoted by R(S).
For the digraph given in below Fig. , some of the reachable sets are as follows:
R (v1) = {v1, v2, v3, v4, v5, v6} = R (v2) = R (v3) = R (v4) = R (v5)
R (v6) = {v6}
R (v7) = {v6, v7}
R (v8) = {v6, v7 , v8}
R (v9) = {v9}
R (v10) = {v10}
R (v5, v8, v9, v10) = V = R (v1, v8, v9, v10)
Node Base : In a digraph G = < V, E>, a subset X ⊆ V
is called a node base if its reachable set is V and if no
proper subset of X has this property.
In the digraph of following Fig. the set {v1, v8, v9, v10}
is a node base and so is the set {v5, v8, v9, v10} .
 R (v5, v8, v9, v10) = V = R (v1, v8, v9, v10)
 Any node whose indegree is zero must be present in any node base.
 Every isolated node/ point of a digraph must be present in a node base.
 No node in the node base is reachable from another node in the node base.
 From the nodes lying on an elementary cycle any one node could be chosen as
an element of a node base, hence, any node that does not have indegree zero
and that does not lie on a cycle cannot be present in a node base.
 In an acyclic graph , a node base consists of only those nodes whose indegree
is zero.
 For a given simple digraph, we may have more than one node base; however,
every node base has the same number of nodes.
CONNECTED GRAPHS :STRONGLY, UNILATERALLY, WEAKLY CONNECTED GRAPHS & COMPONENTS
An undirected graph is said to be connected if for any pair of nodes of the graph the two nodes
are reachable from one another.

i.e. An undirected graph is connected if there is a path from every node to every other
node. For example:
Connected Not connected

This definition cannot be applied to directed graphs without some further modification,
because in a directed graph if a node u is reachable from another node v, the node v may not
be reachable from u.
A digraph is called connected (weakly connected) if it is connected as an
undirected graph in which the direction of the edges is neglected.

Thus Weakly connected Graph can be defined as : A Digraph is said to be


weakly connected if it is connected as an undirected graph in which the direction
of the edges is neglected.

Unilaterally Connected Graph: A simple digraph is said to be unilaterally


connected if for any pair of nodes of the graph at least one of the nodes of the
pair is reachable from other node.
i.e. A digraph is unilaterally connected or unilateral, if either u is
reachable from v or v is reachable from u.
* A unilaterally Connected diagraph is weakly connected, but a weakly connected
diagraph is not necessarily unilaterally connected.
Strongly Connected: If for any pair of nodes of the graph both the nodes of the
pair are reachable from one another then the graph is called strongly connected.
* A Strongly Connected diagraph is both Unilaterally and weakly connected.

(a) is strongly connected, (b) is weakly connected but not unilaterally connected,
while (c) is unilaterally connected but not strongly connected.
Connectivity in directed graphs
Definition: A directed graph is said to be strongly connected if for any pair of nodes
there is a path from each one to the other.
Definition: A directed graph is said to be unilaterally connected if for any pair of
nodes at least one of the nodes is reachable from the other i.e. A digraph is
unilaterally connected or unilateral, if either u is reachable from v or v is
reachable from u.

Definition: A directed graph is said to be weakly connected if the underlying


undirected graph is connected.

Strongly connected graph Unilaterally connected graph: Weakly connected graph


weakly connected but
not strongly connected

neither weakly nor


strongly connected
Let G=<V,E> be a simple digraph and X⊆ V. A subgraph whose nodes are given by the set X
and whose edges consist of all those edges of G which have their initial nodes in X is called the
subgraph generated by X.
A subgraph G1 is said to be maximal with respect to some property if no other subgraph has
the property and also includes G1.
For a simple digraph, a maximal strongly connected subgraph is called a strong component i.e.
it is a subgraph that is strongly connected and which is as large as possible (there is no node
outside the subgraph that is strongly connected to all the nodes in the subgraph).
A weak component is a maximal weakly connected subgraph.
Similarly, a maximal unilaterally connected sub graph is called a unilateral component.

 Strong component :{1,2,3},{4},{5},{6}


 Unilateral component:{1,2,3,4,5},{6}
 Weak component:{1,2,3,4,5,6}
because the graph is weakly
connected.
Find the strongly connected components of each of these graphs.
Theorem 5-1.2 : In a simple digraph, G=<v ,E>, every node of the digraph lies in
exactly one strong component.
In a multi programmed computer system, it appears that several programs are
executed at one time. The programs are sharing the resources of the computer
system, such as tape units, disc devices, the central processor, main memory, and
compilers .
 The Operating system controls the allocation of these resources to the programs
as well assures that a program request is satisfied.
 It is possible that requests for resources are in conflict.
 For example ,program A may have control of resources r1 and require resources
r2, but program B has control of resource r2 and requires resource r1. In such a
case, the computer system is said to be in a state known as deadlock, and the
conflicting request must be resolved.
 A directed graph can be used to model resource request and assist in the
detection and correction of deadlocks.
Let Pt = {p1,p2……,pm} represent the set of programs in the computer system at
time t.
Let At is a set of active programs or set of programs that have been allocated at
least a portion of their resource requests at time t, thus At is subset of Pt.
Let Rt = {r1,r2,....,rn} represent the set of resources in the system at time t.
An allocation graph Gt is a directed graph representing the resource allocation
status of the system at time t and consisting of a set of nodes V = Rt and a set of
edges E.
Each resource is represented by a node of graph.
There is directed edge from node ri to rj , if and only if there is a program pk in At
that has been allocated resources ri but is waiting for rj.
For example , let Rt ={r1, r2, r3, r4}, At ={p1, p2, p3,
p4}, and the resource allocation status be
P1 has resource r4 and requires r1
P2 has resource r1 and requires r2 and r3
P3 has resource r2 and requires r3
P4 has resource r3 and requires r1 and r4
The allocation graph at time t is given in Fig.
It can be shown that the state of deadlock exists
in a computer system at time t if and only if the •If graph contains no cycles  no
deadlock
allocation graph Gt contains strongly connected •If graph contains a cycle 
components . •if only one instance per resource
One need to identify strongly connected components to type, then deadlock
•if several instances per resource
address deadlock condition. Given graph Gt contains
type, possibility of deadlock
strongly connected components.
MATRIX REPRESENTATION OF GRAPHS (ADJACENCY MATRIX) , IN-DEGREE, OUT-DEGREE
OF A GRAPH FROM ADJACENCY MATRIX .
Disadvantages of diagrammatic representation of Graph :
A diagrammatic representation of a graph has limited usefulness.
When the number of nodes and edges is reasonably small it is possible to have
manageable diagrammatic representation
Same graph can be represented using different diagrams, creating confusion
sometimes.
Matrix representation of Diagram :
It is easy to store and manipulate matrices and hence the graphs represented by them
in a computer.
Well known operations of matrix algebra can be used to calculate paths, cycles, and
other characteristics of a graph.
Matrix representation of Graph G depends upon the ordering of the nodes.*
Adjacency matrix : Let G= <V ,E> be a simple digraph in which V ={v1 ,v2,
……….} and the nodes are assumed to be ordered from v1 to vn.
An n X n matrix A whose elements aij are given by
aij = 1 if<vi ,vj> ∈ E
aij = 0 otherwise
is called the adjacency matrix of the graph G.
The adjacency matrix is the same as the relation matrix or the incidence matrix of
the relation E in V.
Any element of the adjacency matrix is either 0 or 1.
Any matrix whose elements are either 0 or 1 is called a bit matrix or a Boolean
matrix
Note that the ith row in the adjacency matrix is determined by the edges which
originate in the node vi.
The number of elements in the I th row whose value is 1 is equal to
the outdegree of the node vi.
 The number of elements whose value is 1 in a column, say the jth
column, is equal to the indegree of the node vj.
An adjacency matrix completely defines a simple digraph.
For a given digraph G = <V, E>, an adjacency matrix depends
upon the ordering of the elements of V.
For the different ordering of the elements of V we get different
adjacency matrices of the same graph G.
In fact, if two digraphs are such that the adjacency matrix of one can
be obtained from the adjacency matrix of the other by interchanging
some of the rows and the corresponding columns, then the digraph are
isomorphic.
Formally: G=(V,E), V-vertices, E-edges

e6
v1
v4
v3
v2 and v3 are adjacent. e2
e1 is incident with v2.
e2 e3
e5
v2
v5
e4
PRESENTATIONS OF GRAPHS
A graph can also be
represented by its n X m
v3
incidence matrix T . e2
v4

For an undirected graph e3

T (i ; k ) = T (j ; k ) = 1 e4 e5
v5
iff ek = (vi; vj). v2

e1 e6
For a loop we have to v1
take 2 as two legs of an e1 e2 e3 e4 e5 e6
Incidence matrix
edge are connected with v1 1 0 0 0 0 2
a node. v2 1 1 0 1 0 0
v3 0 1 1 0 1 0
v4 0 0 0 0 0 0
v5 0 0 1 1 1 0
A graph can also be represented by its n X m incidence matrix T .
For an undirected graph T (i ; k ) = T (j ; k ) = 1 iff ek = (vi; vj).
Incidence matrix: The incidence matrix of a digraph with n vertices, m arcs and no self-loops is an n × m matrix
Adjacency matrix
e2 v3
v4
e3
v1 v2 v3 v4 v5
e4 e5
v1 2 1 0 0 0 v5
v2

v2 1 0 1 0 1 e1 e6
v1
v3 0 1 0 0 2
Adjacency matrix : Let G= <V ,E> be a simple digraph
v4 0 0 0 0 0 in which V ={v1 ,v2, ……….} and the nodes are assumed
v5 0 1 2 0 0
to be ordered from v1 to vn. An n X n matrix A whose
elements aij are given by
aij = 1 if<vi ,vj> ∈ E
aij = 0 otherwise
ADJACENCY MATRIX FOR DIRECTED GRAPHS

e2 v3
v4
Adjacency matrix
e3

v1 v2 v3 v4 v5 e4 e5
v5
v2
v1 1 0 0 0 0
e1 e6
v2 1 0 1 0 0 v1

v3 0 0 0 0 1

v4 0 0 0 0 0

v5 0 1 1 0 0
*** Symmetric Matrix. If the transpose of a matrix is equal to itself, that matrix is said to be symmetric e.g.
PG 60
For the Graph in Fig. if we first order the nodes as v1, v2, v3,
and v4 and write its adjacency matrix, we will get matrix A1.

If we reorder the rows as v2, v3, v1, and v4 and write its
adjacency matrix we will get A2.

Both A1 and A2 are representing same graph, only order of


nodes is changed.
Properties of a simple digraph using adjacency matrix :
If a digraph is reflexive then the diagonal elements of the
adjacency matrix are 1s.
For a symmetric digraph ,the adjacency matrix is also 0 1 0 0 0 1 0 1
0 0 1 1
symmetric; that is, aij =aji for all i and j A1 =
1 1 0 1
A2 = 1
1
0
0
1
0
1
0
1 0 0 0 0 0 1 0
If a digraph is antisymmetric, then aij = 1 implies aji = 0 , and
aij = 0 implies aji = 1 for all i and j.
Let A={ 0,1,2,3} and a relation R on A be given by R={ (0,0), (0,1), (0,3), (1,0),
(1,1), (2,2), (3,0), (3,3) } .

We want to check,
Is R reflexive? symmetric? transitive?
We'll make use of the digraph for R on the right.
(a) R is reflexive, i.e. there is a loop at each vertex.
(b) R is symmetric, i.e. the arrows joining a pair of different vertices
always appear in a pair with opposite arrow directions.

(c)R is not transitive. This is because otherwise the arrow from 1 to 0


and arrow from 0 to 3 would imply the existence of an arrow from 1
to 3 (which doesn't exist). In other words (1,0) R, (0,3) R and
(1,3) R imply R is not transitive.
MATRIX REPRESENTATION TO MULTIGRAPHS AND WEIGHTED GRAPH

2 v3
v4
Adjacency matrix
2
v1 v2 v3 v4 v5 5
v1 3 0 0 0 0 3 v5
v2
v2 1 0 2 0 0
1 3
v1
v3 0 0 0 0 2
In the case of a multigraph or a weighted graph, we write aij =
v4 0 0 0 0 0
wij , where wij denotes either the multiplicity or the weight of the
v5 0 3 3 0 0 edge <Vi, Vj>.

If <Vi ,Vj> ∉ E, then we write wij =0.


68
For a null graph which consists of only n nodes but no edges,its adjacency matrix has
all its elements zero; i.e., the adjacency matrix is a null matrix.
If there are loops at each node but no other edges in the graph, then the adjacency
matrix is the identity matrix or the unit matrix.

If G = <V, E> is a simple digraph whose adjacency matrix is A, then the adjacency
matrix of ~G, the converse of G, is the transpose of A, that is AT.
For simple undirected graphs adjacency matrix is symmetric and for it A= AT
The adjacency matrix for a digraph need not be symmetric. (justify)
Symmetric Diagraph

v1 v2 v3 v4

v1 1 0 0 0
v2 0 1 0 0
v3 0 0 1 0
v4 0 0 0 1
FOR GIVEN MATRIX A= A 1 WE CAN GET A AT, AT A, AND A N FOR N = 2,3,4,.....

0 0 1 1 1 0 1 0
1 0 1 0 A AT = 0 2 1 0
AT =
0 1 0 0 1 1 3 1
0 1 1 0 0 0 1 1

2 1 0 1 0 1 0 0
0 0 1 1 0 0 1 1
AT A = 1 2 0 1 A2 = A1=A =
2 1 0 1 1 1 0 1
0 0 1 1
1 1 1 1 1 0 0 0
1 1 1 2
0 1 0 0
1 2 1 1
2 1 0 1
A3 = A4 = 2 2 2 3
1 2 1 1
3 3 2 3
2 2 1 2
2 1 0 1
0 0 1 1
Lect. 4
28/9
MERITS OF ADJACENCY MATRIX
We can use adjacency matrix, to determine the connection of vertices, it is easy
The degree of a vertex is

For a digraph, the row sum is the outdegree, while the column sum is the indegree

The diagonal entries of AAT simply show the out degree of the nodes.
The element in the ith row and jth column of AT A shows the number of nodes of the graph
which are such that the edges initiation from these nodes terminate in both vi and vj.
Also the diagonal entries show the indgrees of the nodes.
An entry of 1 in the ith row and jth column of A shows the existence of an edge <vi, vj>,
that is, a path of length 1 from vi to vj. Let us denote the element of A2 by aij(2). Then
n
aij(2) = 𝛴 aikakj
k=1
For any fixed k, aikakj = 1 iff both aik and akj equal 1 that is , iff <vi , vk> and <vk , vj>
are the edges of the graph. For each such k we get a contribution of 1 in the sum.
Now <vi, vk> and <vk, vj> imply that there is a path from vi to vj of length 2.
Therefore aij (2) is equal to the number of different paths of exactly length 2 from vi
to vj.
Similarly, the diagonal element aii (2) shows the number of cycle of length 2 at the
node vi for i = 1, 2,.....,n.
In general we can say that
Theorem : Let A be the adjacency matrix of a digraph G. The element in the ith row
and jth column of An is equal to the number of paths of length n from the ith node to
the jth node.
we see that there are two paths of length 2 from v2 to v1 , hence
the entry 2 in the second row and first column of A2. Similarly,
there are two paths of length 4 from v2 to v1, hence the
corresponding entry in A4.

0 0 1 1 1 0 1 0
1 0 1 0 A AT = 0 2 1 0
AT =
0 1 0 0 1 1 3 1 From the adjacency matrix of A we can
0 1 1 0 0 0 1 1 immediately determine whether there exists an
edge from vi to vj in G.

2 1 0 1 0 0 1 1
AT A = Also from the matrix Ar, where r is some positive
1 2 0 1 A2 = 2 1 0 1
integer, we can establish the number of paths of
0 0 1 1 1 1 1 1 length r from vi to vj.
1 1 1 2 0 1 0 0
If we add the matrices A, A2, A3,.........,Ar to get Br
1 2 1 1 Br = A + A2 + ………….. Ar
2 1 0 1
A3 = A4 = 2 2 2 3 Then from the matrix Br we can determine the
1 2 1 1
3 3 2 3 number of paths of length less than or equal to r
2 2 1 2
2 1 0 1 from vi to vj.
0 0 1 1
Element in the ith row and jth column of AT A shows the number of nodes of the graph which are
such that the edges initiation from these nodes terminate in both vi and vj. The diagonal entries
show the indgrees of the nodes.
An entry of 1 in the ith row and jth column of A shows the existence of an edge <vi, vj>, that is, a
path of length 1 from vi to vj. Let us denote the element of A2 by aij(2). Then
n
aij(2) = 𝛴 aikakj
k=1
For any fixed k, aikakj = 1 iff both aik and akj equal 1 that is , iff <vi, vk> and <vk, vj> are the
edges of the graph. For each such k we get a contribution of 1 in the sum.
Now <vi, vk> and <vk, vj> imply that there is a path from vi to vj of length 2. Therefore aij (2) is
equal to the number of different paths of exactly length 2 from vi to vj.
Similarly, the diagonal element aii (2) shows the number of cycle of length 2 at the node vi for i
= 1, 2,.....,n.
By a similarly argument, one can show that the element in the ith row and jth column of A3
gives the number of paths of exactly length 3 from vi to vj. In general, we have the following
theorem.
By a similarly argument, one can show that the element in the ith row and jth column of A3 gives
the number of paths of exactly length 3 from vi to vj. In general, we have the following
theorem.
Theorem 5-1.3 Let A be the adjacency matrix of a digraph G. The element in the ith row and
jth column An is equal to the number of paths of length n from the ith node to the jth node.

In a simple digraph with n nodes, the length of an elementary path or cycle does not exceed n.
One can obtain elementary path between any two nodes by deleting certain parts of the
path which are cycles.
If we are interested in determining whether there exists a path from vi to vj, all we need to
examine are the elementary paths of length less than or equal to n - 1.
In the case of vi = vj and the path is a cycle, we need to examine all possible elementary cycle
of length less than or equal to n.
We can use,
Bn = A + A2 + ………….. +An
The ith row and jth column of Bn shows the number of paths of length n or less
which exist from vi to vj. If this element is nonzero, then it is clear that vj is
reachable from vi.
Path Matrix : Let G = <V , E> be a simple digraph in which | V| = n and the
nodes of G are assumed to be ordered. An n× 𝑛 𝑚𝑎𝑡𝑟𝑖𝑥 P whose element are given
by

1 if there exists a path from vi to vj


pij =
0 otherwise
Is called the path matrix (reachability matrix) of the graph G.
The path matrix only shows the presence or absence of at least one path between a
pair of points and also the presence or absence of a cycle at any node.
It does not, however, shows all the paths that may exist.
Find the number of paths from a to e in the directed graph of length 2. Compute A2 and look at
the (1; 5)th entry

Find the number of paths from a to e in the


directed graph of length 3,Compute A3 and
look at the (1; 5)th entry
The path matrix can be calculated from the matrix Bn by choosing
pij = 1 if the element in the ith row and jth column of Bn is nonzero and
pij = 0 otherwise.

We have A= A1, A2, A3,A4


We can calculate
B4 = A + A2 + ………….. A4
To find out reachability of one node from another, it is
sufficient to calculate Bn-1 , because a path of length n
cannot be elementary. 3 4 2 3 1 1 1 1
The only difference between P calculated from Bn-1 and
P calculated from Bn is in the diagonal elements. B4 =
5 5 4 6
p=
1 1 1 1

For the purpose of reachability, every node is assumed to 7 7 4 7 1 1 1 1


be reachable from itself.
Some authors calculate the path matrix from Bn-1, while 3 2 1 2 1 1 11
others do it from Bn.
In the fig. if we consider the three nodes B, C and E we see that <B, C> = 1
and also <C, E> = 1 hence it is understood that there is a link directed from B
to E through C. Thus in order to show such relationship we use the concept
of path matrix. Just like its name describes it represents paths of certain
length. In the above case when we consider nodes B, C and E only two arcs
were used hence we say that it is a path of length 2.
The path matrix of length 2 can be easily obtained by getting the Boolean product of
the adjacent matrix with itself

A AXA AXAXA

We could move on further and consider all paths of length 3 in the above graph.
A path matrices of n length can be generated by multiplying A X A X A…. n times.(if they exist).
The method of calculating the path matrix P of a graph by calculating first A, A2,..........., An
and then Bn = A + A2 + ………….. +An is cumbersome.
To reduce the amount of calculation involved, Boolean matrix operations are used.
A matrix whose entries are the elements of a two-element Boolean algebra
< B, ∧, ∨,-----, 0, 1 > where entries B = 0, 1 𝑖𝑠 𝑐𝑎𝑙𝑙𝑒𝑑 a Boolean matrix .
The operations ∧ 𝑎𝑛𝑑 ∨ on B are given in table
∧ 0 1 ∨ 0 1
(Boolean product) (Boolean Sum)
A ∧B A∨ B
__________________________________
0 0 0 0 0 1
1 0 1 1 1 1
_________________________________
The adjacency matrix is a Boolean matrix, and so also is the path matrix. Let us write A ∧ A =
A(2), A ∧ A (r-1) = A (r) for any r = 2, 3,..... The only difference between A2 and A(2) is that A(2)
is a Boolean matrix and the entry in the ith row and jth column of A(2) is 1 if there is at least one
path of length 2 from vi to vj, while in A2 the entry in the ith row and jth column shows the
number of paths of length 2 from vi to vj.
the path matrix P is given by n
P = A ∨ A(2) ∨ A(3) ∨……………….∨ A(n) = V A(k)
k=1

For our sample example of the graph given in Fig. 5-1.15,

If we take the sum from


k =1 to k= n-1, we get a
1101 1111
0011 matrix which may differ if
1111 1111
1101
A(3) = at all from P in the
A(2) = 1111 A(4)= 1111
1111 diagonal terms only.
0011 1101
010 0
1111
1111
=A V A(2) V A(3) V A(4) = P
A V A(2) V A(3) = 1111
1111
A relation E in V, a relation E+, called the transitive closure of E, is defined as
E+ = E∪ E2 ∪……
Clearly, the relation matrix of E+ is given by
A+ = A ∨ A(2) ∨ A(3) ∨…… where A is the relation matrix of E.
It has been shown that if the number of elements in V is n, then no path or cycle
exceeds n in length; therefore A+ can be obtained by simply considering the sum up
to A(n), for powers higher than n will not change A+. Therefore
A+ = A ∨ A(2) ∨ A(3) ∨…… ∨ A(n) = P
The matrix A+ is the same as the path matrix.
This method of obtaining the transitive closure of a relation as well as the path
matrix of a simple digraph can easily be programmed by using Warshall algorithm.
Algorithm WARSHALL Given the adjacency matrix A, the following steps produce the path matrix
P or A+
1. P← A
2. k ← 1
3. i ← 1
4. pij ← pij ∨ (pik ∧ pkj) for all j from 1 to n
5. i ← i + 1. If i ≤ n, go to step 4.
6. k← 𝑘 + 1. If k ≤ 𝑛, 𝑔𝑜 𝑡𝑜 𝑠𝑡𝑒𝑝 3; 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒, ℎ𝑎𝑙𝑡.
A different algorithm due to Warshall which also permits the calculation of the path matrix P from a
given adjacency matrix is obtained from algorithm WARSHALL by replacing step 4 by step 4’
4’ If pik = 1, then pij= pij ∨ 𝐩𝐤𝐣 for all j from 1 to n.
Algorithm WARSHALL can be modified further to obtain a matrix which gives the lengths of
shortest paths between the nodes. For this purpose, let A be the adjacency matrix of the graph.
Replace all those elements of A which are zero by ∞ , which shows that there is no edge between
the nodes in question. The following algorithm produces the required matrix which shows the lengths
of minimum paths.
Algorithm MINIMA Given the adjacency matrix in which the zero elements are replaced by infinity
or by some very large number , let this matrix be denoted by B. the matrix C produced by the
following steps shows the minimum length of paths between the nodes.
1. C ← B.
2. k←1.
3. i ←1.
4. cij ← 𝑚𝑖𝑛 (cij ,cik+ ckj) for all j from 1 to n.
5. i ← i + 1. If i ≤ 𝑛, go to step 4.
6. k←k + 1. If k ≤ n, go to step 3; otherwise, halt.
Here + in step 4 means the ordinary adding of integers.
Let vi be any node of a simple digraph G and P be its path matrix. If P’ is the transpose of
the matrix P, then the ith row of the matrix P ∧ P’ which is obtained by the elementwise
product of the elements gives the strong component containing vi.
Notice that if 𝜐𝑗 is reachable from 𝜐𝑖, then clearly pij= 1; also, if vi is reachable from vj,
then pji = 1 or p’ij = 1.
Therefore , the element in the ith row and jth column of P ∧ P’ is 1 iff 𝜐𝑖 and 𝜐𝑗 are
mutually reachable. This is true for all j.
Use of Graph Theory to determine which procedures are recursive.
A recursive procedure is not necessarily one which invokes itself directly.
If procedure p1 invokes p2, procedure p2 invokes p3……., procedure pn-1 invokes pn, and
procedure pn invokes p1, then procedure p1 is recursive.
Let P = 𝑝1, 𝑝2, . . . . . . . . . 𝑝𝑛 be the set of procedures in a program. In a directed graph
consisting of nodes representing elements of p, there is an edge from pi to pj if procedure
pi invokes pj. E.g. the calls made by the set of procedures P = ={p1,p2,…P5} . 𝑡ℎ𝑒
𝑎𝑑𝑗𝑎𝑐𝑒𝑛𝑐𝑦 𝑚𝑎𝑡𝑟𝑖𝑥 𝑜𝑓 𝑡ℎ𝑒 𝑔𝑟𝑎𝑝ℎ 𝑖𝑠
p1
p1 p2 p3 p4 p5
0 1 0 1 1
p1
0 1 0 0 0
Q= 0 1 0 1 1
p5 A= P2
p2 0 0 0 1 0
P3 1 1 0 1 1
1 0 0 0 0
P4 0 1 0 1 1
0 0 0 0 1

p3
p5 0 1 0 1 1
p4
0 1 0 0 0
Fig : Procedure call

A procedure pi is recursive if there exists a cycle involving pi in the graph.


Such cycles can be detected from the diagonal elements of the path matrix (Q = A+
of the graph). i.e. pi is recursive iff q ii = 1.
The matrix Q can be obtained by using Warshall’s algorithm. For given graph, matrix Q is
given as above, here procedures p2, p4, and p5 are recursive.
Application of Warshall Algorithm: Minima (Breadth First search):
It is used to find transitive closure of a It is the only algorithm to find shortest
relation path for unweighted graphs.
Steps of Algorithm:- 1) Start from a vertex s let this vertex
be at level 0
Prepare a matrix for the given graph
2) Find all other vertices that are
List separately the rows that have 1’s in immediately accessible from s &
row k marked it as level 1.
Get cartesian product of obtained pair 3) Find all those vertices that are a
and put 0 in the remaining empty single edge away from level 1 &
positions marked it as level 2
4) Repeat this process until the graph is
finished.

Lect. 5
12/10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy