Unit 5
Unit 5
UNIT V
GRAPH MATRICES AND APPLICATIONS
Let’s convert this control flow graph into a graph matrix. Since the graph has 4 nodes, so the graph matrix would
have a dimension of 4 X 4. Matrix entries will be filled as follows :
1. (1, 1) will be filled with ‘a’ as an edge exists from node 1 to node 1
2. (1, 2) will be filled with ‘b’ as an edge exists from node 1 to node 2. It is important to
note that (2, 1) will not be filled as the edge is unidirectional and not bidirectional
3. (1, 3) will be filled with ‘c’ as edge c exists from node 1 to node 3
4. (2, 4) will be filled with ‘d’ as edge exists from node 2 to node 4
5. (3, 4) will be filled with ‘e’ as an edge exists from node 3 to node 4
The graph matrix formed is shown below:
Connection Matrix:
A connection matrix is a matrix defined with edges weight.
2
In simple form, when a connection exists between two nodes of control flow graph, then the edge weight is 1,
otherwise, it is 0.
However, 0 is not usually entered in the matrix cells to reduce the complexity.
For example, if we represent the above control flow graph as a connection matrix, then the result would be :
As we can see, the weight of the edges are simply replaced by 1 and the cells which were empty before are left
as it is, i.e., representing 0.
A connection matrix is used to find the cyclomatic complexity of the control graph.
Although there are three other methods to find the cyclomatic complexity but this method works well too.
Following are the steps to compute the cyclomatic complexity :
1. Count the number of 1s in each row and write it in the end of the row
2. Subtract 1 from this count for each row (Ignore the row if its count is 0)
3. Add the count of each row calculated previously
4. Add 1 to this total count
5. The final sum in Step 4 is the cyclomatic complexity of the control flow graph
Let’s apply these steps to the graph above to compute the cyclomatic complexity.
We can verify this value for cyclomatic complexity using other methods:
Method-1: 3
Cyclomatic complexity
=e-n+2*P
Since here,
e=5
n=4
and, P = 1
Therefore, cyclomatic complexity,
=5-4+2*1
=3
A simple weight
A simplest weight we can use is to note that there is or isn’t a connection. Let “1”
mean that there is a connection and “0” mean that there isn’t.
The arithmetic rules are:
1+1=1 1*1=1
1+0=1 1*0=0
0+0=0 0*0=0
A matrix defined like this is called connection matrix.
Connection matrix
The connection matrix is obtained by replacing each entry with 1 if there is a link
and 0 if there isn’t.
As usual we don’t write down 0 entries to reduce the clutter.
5
Connection Matrix-continued
Each row of a matrix denotes the out links of the node corresponding to that row.
Each column denotes the in links corresponding to that node.
A branch is a node with more than one nonzero entry in its row.
A junction is node with more than one nonzero entry in its column.
A self-loop is an entry along the diagonal.
Cyclomatic Complexity
The cyclomatic complexity obtained by subtracting 1 from the total number of entries in
each row and ignoring rows with no entries, we obtain the equivalent number of
decisions for each row. Adding these values and then adding 1 to the sum yields the
graph’s cyclomatic complexity.
6
Relations
A relation is a property that exists between two objects of interest.
For example,
“Node a is connected to node b” or aRb where “R” means “is connected to”.
“a>=b” or aRb where “R” means greater than or equal”.
A graph consists of set of abstract objects called nodes and a relation R between thenodes.
If aRb, which is to say that a has the relation R to b, it is denoted by a link from a tob.
For some relations we can associate properties called as linkweights.
Transitive Relations
A relation is transitive if aRb and bRc implies aRc.
Most relations used in testing are transitive.
Examples of transitive relations include: is con
nected to, is greater than or equal to, is less than or equal to, is a relative of, is faster than,
is slower than, takes more time than, is a subset of etc
Reflexive Relations
A relation R is reflexive if, for every a, aRa.
A reflexive relation is equivalent to a self loop at everynode.
Examples of reflexive relations include: equals
Examples of irreflexive relations include: not equals, is on top of, is under.
If a relation is reflexive, then the directed graph will have an arrow from the vertex to itself (a
loop) at every vertex.
Symmetric Relations
A relation R is symmetric if for every a and b, aRb implies bRa.
A symmetric relation mean that if there is a link from a to b then there is also a link from b to a.
7
A graph whose relations are not symmetric are called directed graph.
Antisymmetric Relations
A relation R is antisymmetric if for every a and b, if aRb and bRa, then a=b, or they are
the same elements.
Examples of antisymmetric relations: is greater than or equal to, is a subset of, time.
Examples of non-antisymmetric relations: is connected to, can be reached from, is
greater than, is a relative of, is a friend of
Equivalence Relations
An equivalence relation is a relation that satisfies the reflexive, transitive, and
symmetric properties.
If a set of objects satisfy an equivalence relation, we say that they form an equivalence
class over that relation.
The importance of equivalence classes and relations is that any member of the
equivalence class is, with respect to the relation, equivalent to any other member of that
class.
(bfh*e)*x(d+bc+bfh*g)
Our main objective is to use matrix operations to obtain the set of all paths between all nodes or,
equivalently, a property (described by link weights) over the set of all paths from every node to
every other node, using the appropriate arithmetic rules for such weights. The set of all paths
between all nodes is easily expressed in terms of matrix operations. It’s given by the following
infinite series of matrix powers:
This is an eloquent, but practically useless, expression. Let I be an n by n matrix, where n is the
number of nodes. Let I’s entries consist of multiplicative identity elements along the principal
diagonal. For link names, this can be the number “1.” For other kinds of weights, it is the
multiplicative identity for those weights. The above product can be re-phrased as:
A(I + A + A2 + A3 + A4 . . . A∞)
n-1
∑ Ai =A(A+I)n -2
i=1
This is an interesting set of paths because, with n nodes, no path can exceed n – 1 nodes without
incorporating some path segment that is already incorporated in some other path or path
13
segment. Finding the set of all such paths is somewhat easier because it is not necessary to do
all the intermediate
14
1. Express n – 2 as a binarynumber.
2. Take successive squares of (A + I), leading to (A + I)2, (A + I)4, (A + 1)8, and soon.
3. Keep only those binary powers of (A + 1) that correspond to a 1 value in the binary
representation ofn– 2.
4. The set of all paths of length n – 1 or less is obtained as the product of the matrices
you got in step 3 with the original matrix.
As an example, let the graph have 16 nodes. We want the set of all paths of length less than or
equal to 15.
The binary representation of n – 2 (14) is 23 + 22 + 2. Consequently, the set of paths is given by
15
∑ Ai =A(A+I)8(A+I)4(A+I)2
i=1
15
Partitioning Algorithm
Consider any graph over a transitive relation. The graph may have loops.
We would like to partition the graph by grouping nodes in such a way that every loop is
contained within one group or another.
Such a graph is partially ordered.
There are many used for an algorithm that does that:
We might want to embed the loops within a subroutine so as to have a resulting graph
which is loop free at the top level.
Many graphs with loops are easy to analyze if you know where to break the loops.
While you and I can recognize loops, it’s much harder to program a tool to do it unless
you have a solid algorithm on which to base the tool.
16
4. The result is a matrix whose size has been reduced by 1. Continue until only the two
nodes of interestexist.
18
19
BUILDING TOOLS:
20
21
22
23