0% found this document useful (0 votes)
17 views23 pages

Unit 5

Uploaded by

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

Unit 5

Uploaded by

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

1

UNIT V
GRAPH MATRICES AND APPLICATIONS

What is a Graph Matrix?


1. A graph matrix is a square matrix whose size represents the number of nodes in the control
flow graph.
2. Each row and column in the matrix identify a node and the entries in the matrix represent the
edges or links between these nodes.
3. Conventionally, nodes are denoted by digits and edges are denoted by letters.
Let’s take an example.

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

The Matrix of a Graph


 A graph matrix is a square array with one row and one column for every node in the
graph.
 Each row-column combination corresponds to a relation between the node
corresponding to the row and the node corresponding to the column.
 The relation for example, could be as simple as the link name, if there is a link
between the nodes.
 Some of the things to be observed:
 The size of the matrix equals the number of nodes.
 There is a place to put every possible direct connection or link between any and any other
node.
 The entry at a row and column intersection is the link weight of the link that
connects the two nodes in that direction.
 A connection from node i to j does not imply a connection from node j to node i.
 If there are several links between two nodes, then the entry is a sum; the “+” sign
denotes parallel links as usual.
4

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.

Partial Ordering Relations


 A partial ordering relation satisfies the reflexive, transitive, and antisymmetric properties.
 Partial ordered graphs have several important properties: they are loop free, there is at
least one maximum element, and there is at least one minimum element.
8
9
10

Removing the loop term yields (bfh*e)

(bfh*e)*x(d+bc+bfh*g)

The final result yields to :


a(bfh*e)*(d + bc + bfh * g)
11

The Powers of a Matrix


 Each entry in the graph’s matrix expresses a relation between the pair of nodes that
corresponds to that entry.
 Squaring the matrix yields a new matrix that expresses the relation between each pair
of nodes via one intermediate node under the assumption that the relation is
transitive.
 The square of the matrix represents all path segments two links long.
 The third power represents all path segments three links long.
12

Matrix Powers and Products


 Given a matrix whose entries are aij, the square of that matrix is obtained by replacing every entry with
 n
 aij=Σ aikakj
 k=1
 more generally, given two matrices A and B with entries aik and bkj, respectively, their product is a new
matrix C, whose entries are cij, where:
 n
 Cij=Σ aikbkj
 k=1

3.1. The Set of AllPaths

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∞)

But often for relations, A + A = A, (A + I)2 = A2 + A +A + I A2 + A + I. Furthermore, for

any finite n, (A + I)n= I + A + A2 + A3 . . . An.

Therefore, the original infinite sum can be replaced by



∑ Ai =A(A+I)∞
i=1
This is an improvement, because in the original expression we had both infinite products and
infinite sums, and now we have only one infinite product to contend with. The above is valid
whether or not there are loops. If we restrict our interest for the moment to paths of length n – 1,
where n is the number of nodes, the set of all such paths is givenby

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

products explicitly. The following algorithm iseffective:

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

Node Reduction Algorithm (General)


 The matrix powers usually tell us more than we want to know about mostgraphs.
 In the context of testing, we usually interested in establishing a relation between
two nodes- typically the entry and exitnodes.
 In a debugging context it is unlikely that we would want to know the path expression
between every node and every other node.
 The advantage of matrix reduction method is that it is more methodical than the
graphical method called as node by node removalalgorithm.
1. Select a node for removal; replace the node by equivalent links that bypass that node
and add those links to the links theyparallel.
2. Combine the parallel terms and simplify as youcan.
3. Observe loop terms and adjust the out links of every node that had a self loop to
account for the effect of theloop.
17

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

NODE – REDUCTION OPTIMIZATION:

You might also like