Graph Matrices and Applications
Note: Please use a slide sl ide show since transitions have been animated. Tip: dark red text red text corresponds to dark red entries red entries in the table for that slide and so on . . .
Overview: Graph matrices are just another representation for graphs We will be covering -
the need for matrices Matrix operations, relations node reduction algorithm equivalence class partition
Why
do we need matrices?
Path tracing can lead to errors
Links can be missed or covered twice (redundancy)
Interruptions while tracing tracing a path can cause errors and lead to confusion
There is a need for a more methodical and mechanical approach
Proving Proving theorems the orems by mathematical representations(matrices) helps in proving theorems about graphs
Operations
that comprise the
toolkit
Matrix multiplication, multiplication, which is used to get the path expression from from every node to every other node
A partitioning algorithm for converting graphs with loops into loop free graphs of equivalence classes
A collapsing process (analogous to the determinant of a matrix) which gets the path expression from from any node to any other node
The matrix of a graph
A graph matrix is a square array with one row and one column for every node of the graph
Each row-column combination corresponds to a relation b/w the node corresponding the row and the node corresponding the column. This could be the link name. 0
1 GRAPH
RIX M AT RIX
Graph matrix observations:
The size of the matrix (i.e. the t he number of rows and columns) equals the number of nodes GRAPH
1
0
a
RIX M AT RIX
If there are several links between two nodes, then, the entry is a sum. The + sign denotes parallel parallel links, as usual. a
GRAPH
1
b
a+b 2
M AT RIX RIX
Graph matrix observations (cont)
The entry at a row and column intersection is the link weight of the link (if any) that connects the two nodes in that direction a 1
e
2
b d GRAPH
d
b+e c
3
a
c M AT RIX RIX
Graph matrix observations (cont)
A connection from node i to node j does not imply(mean) a connection from node j to node i as seen in the graph.
There is a place to put every possible direct connection or link between any node and any other node. a
GRAPH
1
b
a 2
b M AT RIX RIX
Definitions:
The simplest weight that we can denote is to indicate indicate if there is a connection or not. Say 1 means that there is a connection and 0 means that there is no connection. The arithmetic rules as we already know are1
+1=
1
1
+0=
1
0+0= 0
1
*1=
1
1
*0= 0
0*0= 0
A matrix with weights defined is called a connection matrix .
Obtaining the connection matrix Replace each entry with 1 if there is a link and with 0 if there isnt. isn t. To reduce reduc e clutter, clutter, ignore igno re the 0 0s. s.
Each row row of the matrix denotes the out-link out-link of the node corresponding to that row
Each column denotes the in-link corresponding to that node
A branch node has more than one non-zero entry entry in its row
A j unction unction node has more than one non-zero entry entry in its column
A Self loop is an entry along the diagonal
ther) Graphs Graphs cyclo-matic complexity comp lexity (will be dealt fur th
Deducing the relation relation matrix 3
a
b
1
c
h m
5
g
d
6
e
7
8
f
2
l j
i
k
4 a
i
Repla eplace ce
b
each each entr entry y with the link weight if there is a link and leave it blan lank if the there isn isnt. t.
h
j m c
l
RELATION MATRIX
d e
f
k
g
Deducing the connection matrix &
calculating the cyclomatic complexity 3
a
b
1
c
h m
5
g
d
6
e
7
8
f
2
l j
i
k
4 1 Replace
each entry with 1 if there is a link and leave it blank if ther there e isnt. isnt.
1
Add
the entries in each row & subtract the total by 1. 1
1
2-1=1
1
1 - 1 = 0 1
C ONNE ONNE C CTION TION MATRIX
1
1
1 - 1 = 0
Cyclo-mati c Cyclo-mati c 3 - 1 = 2 co comp mplex lex it it y y
1 1
1
1
2-1=1
1
Now add the results from each row. You You get g et 6. Adding 1 to this gives you the cyclo-matic complexity (M).
1 - 1 = 0 3 - 1 = 2
6 + 6 + 1 = 7 = M
Simplifying references
Instead Instead of sayingsaying- entry at at row 6, column 7, we say entry at node i and column j.
So, the link weight between nodes i and j is represented by aij
Self loop at node i is represented by aii
So, the link weight between nodes j and i is represented by a ji
w.r.
abmd shoul shoul d d r ead a13 a35 a56 a67
3
b 1
a
5
c m
t the the g r a raph, ph,
6
d
her e, a13 = a
w
7
a35 = b
«
Examples
expression denotes a path from node i aij a jj a jm This expression to j, with a self loop at j and then a link from node j to m.
(where ere k = 1 to n) This expression denotes the aik akk akj (wh
set of all possible paths between b etween nodes i and j via one(or many) intermediate intermediate node(s).
Definitions:
The transpose of a matrix is the matrix with rows rows and columns interchanged (denoted by superscript T) . If C = AT , then cij = a ji
The intersection of two matrices of the same size, size, (denoted by A # B) is obtained by an element-by-element element-by-element multiplication multiplication of the entries. C = A # B cij = a ji (usually Boolean AND/ set intersection) intersection)
The union of two matrices is defined defined as as the element-by-element element-by-element addition (usually Boolean OR/ set union).
Relations:
A relation is a property that exists between two objects of interest. a and b are objects and R is used to denote that a has a relation to b.
E xamples:
aR b a is connected to b. R = is connected to a>= b or aR b R is is greater greater that or equal to A is a subset of b of b R is is a subset of
Redefining what we already know:
A graph consists of a set of abstract abstract objects obj ects called nodes and a relation R between the nodes. If aR aRb, it is denoted by a link (a to b).
Some relations can be associated with properties. These are called link weights.
is connected to is the simplest relation denoted by a link with no weight. A graph with this relation are called a connection /ad j acency acency matrix .
General relations form the relation matrix .
er vi vi ew ): Properties of relations (ov er ):
Transitive relations aRbbRcaRc
Reflexive relations for every a, a R a (eg. self loop)
Symmetric relations (undirected graph) for every a & b, a R b b R a (a (aij = a ji for all i, j )
Anti-symmetric relations for every a & b, a R b b R a, then a = b
Equivalence relations satisfies transitive, reflexive & symmetric
Partial ordering relations satis satisfie fiess transi transitiv tive, e, refle reflexiv xive e & antianti-ss mmetri mmetricc
Properties of relations:
Transitive relations aRbbRcaRc Most relations used in testing are transitive.
Examples for transitive relations: relations: is connected to, to, is greater than or equal to, is less than or equal to, is a relative of, is faster than, is slower than, takes takes more time t ime than, is a subset s ubset of, includes, shadows, is the boss of. Examples for intransitive relations: relations: is acquainted with, is a friend of, is a neighbor of, is lied to, has a du chain between.
Properties of relations (cont):
Reflexive relations for every a, a R a (eg. self loop) A reflexive reflexive relation is equivalent to a self se lf loop at every node.
Examples for reflexive relations: relations: equals, is acquainted with, is a relative of. Examples for ir-reflexive relations: relations: not equals, is a friend of(unfortunately), is on top of, is under.
Properties of relations (cont):
Symmetric relations (undirected graph) for every a & b, a R b b R a (a (aij = a ji for all i, j ) This implies that if there is a link from a to b, there is also a link from b to a. Which means that instead of two way links between nodes we can justSince have a we need to single undirected link . A graph whose relations relations use arrows to are not symmetric is called a directed graph. denote direction
of, Examples for symmetric relations: relations: is a relative of, equals, is alongside of, shares a room with, is married, brother b rother of, OR, AND, XOR.
Examples for asymmetric relations: relations: is the boss of, is the husband of, is greater than, controls, dominates, can be reached from.
Properties of relations (cont):
Anti-symmetric relations for every a & b, a R b b R a, then a = b
This implies that both a & b are the same elements. Examples for anti-symmetric relations: relations: is greater than or equal to, is a subset of, time. Examples for non-antisymmetric relations: relations : is connected to, can be reached from, is greater than, is a relative of, is a friend of.
Properties of relations (cont):
Equivalence relations satisfies transitive, reflexive & symmetric
Example for equivalence relations: relations: numerical equality.
If a set of objects obj ects satisfy an equivalence relation, they form an equivalence class over that relation.
Importance of equivalence classes & relations: any member of the equivalence class, is with respect to the relation , equivalen eq uivalentt to any ease refer th refer the e t ex ex t f t f or sho shor r t t other member of the class. [Pl ease not es] es]
Properties of relations (cont):
Partial ordering relations satisfies transitive, reflexive & anti-symmetric
Properties: loop free, there is at least 1 maximum Properties: element & one minimum element, if you reverse all the rows, the resulting graph is also partially ordered. Example for partial ordering relations: relations : trees, most data structures, integration integration plan.
A maximum element a, is one for for which the rela relation tion x Ra does not hold for any other element x .
A minimum element a, is one for which the th e relation relation aR x does not hold for any other element x .
ease refer th refer the e t ex ex t f t f or sho shor r t n t not es] es] [Pl ease
Partitioning algorithm: 1 1
1 1
2
7
1 1
8
1 1 1
1
3
1
1
1 1
5 1
4
1
1
1
RELATION MATRIX 6
Consid Consid er i in g that the the g r a may hav hav e loo ps. raph ph may Diag ona onal l en ent t r ie s ar e mad e t o r e pr esent ent self loo elf loo p. r i
«
1
Partitioning algorithm(cont): 1 1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
7
2 8 3
1
5 1
4
6
1
1
1
1
1
1
1
1
1 1
TRANSITIVE C TRANSITIVE C LOSURE LOSURE MATRIX E x p pl l anati on on for g etti ng the the t r e clo clos su r i x r ansitiv r e mat r i r & its t r spo ose is no nott i nclu nclu d d ed si nce nce w e ass assu u med that ra nsp you w oul oul d k no now this. P leas lease e g et bac bac k i f you f you nee need d any any i nfo nfo. «
Partitioning algorithm(cont): 1
1
2
1
7
8
1 1
1
1
1
1
1
1
1
1 1
1
3
1
5 4
INTERSE C CTION TION WITH ITS TRANSPOSE TRANSP OSE
Consid Consid er i in g the the ent ent r i e s is the the abo abov e mat r i x , w e ri ri hav e the the follow i in g E x ample ample:: t o g et value values s A=[1] for B ± It is is evide evident nt that that B = [ 2, 7 ] B(row 2) has entries at C = [ 3, 4, 5 ] column 2 & 7. Hence the D=[6] values. E=[8] «
6
1
Partitioning algorithm(cont): 1
A
The resulting gr ap aph is --2,7
A B C
A
B
1
1 1
C
D
E
8
B
E 1 1
1 1
D 1
1
E
From the earlier slide, we had -A=[1] B = [ 2, 7 ] C = [ 3, 4, 5 ] D=[6] E=[8]
3,4, 5
C
6
D
Consid Consid er i in g that the the g r a self loo loo ps. raph ph had self Diag ona onal l en ent t r ie s ar e mad e t o r e pr esent ent self self loo loo p. r i
«
Node Reduction Algorithm pr o per ti ti es: es: 1
2
1
3
4 5
mat r r i i x
i nt er c chan hangi ng r ow s 3 & 4 4
5
1
2
1
a
2 3
ome some *
*
5
a
2
d
b
c g
f e
c
*
5
1
To interch i nterch ange node names in a matrix, we must interchange both the corres ponding rows and the corres ponding columns«
b
g
e
2
4
1
h
3
5
a
2 4 3
i nt er c chan hangi ng colu g colu mns 3 & 4
d
*
h
f
«
5
c d g
f b e
h
«
*Node Reduction Algorithm*
Advantages: ±
The matrix reduction method is more methodical than the graphical method method
± Does not entail(involve) continually redrawing of
the graph
Steps to be followed: ±
Select a node for removal, replace replace the node nod e by equivalent equivalent links that bypass that node.
±
Add the parallel parallel terms & simplify if you can
±
Adjust out-links that have a self loop
±
The result is a matrix whose size is reduced by 1.
±
Continue until only two nodes are left. There should be only one link between these nodes.
*Node Reduction Algorithm* d 1
a
3
4
b e
f 5
h STEP 1: E l limi i mi nat e a no nod d e and r e p pl l ace it w ith ith a se set t of of equ ivalen ivalent t l l i in ks ks« « Say , w e star t Say t w ith ith el imi imi nati ng nod no d e 5-
2
c g
1
2
1
3
4
5
a
2 3 4 5
d
b
c g
f e
h
TIP: The The ou t- t- l li i n k k of of th the e no nod d e r emov ed w i ill co l l corr esp spon ond d t o the th e r ow and the the i n- l li i n k w i ill co l l corr esp spon ond d t o th the e colu mn
*Node Reduction Algorithm* E l limi i mi nati ng g no nod d e 5 th the e self elf loo loo p is r e pr esen ent t ed w ith ith a * and the the ou tg tg oi ng g l l i in k k f f r the no nod d e is mul mul tipl tipl i ie d . . . ro m the «
d 1
a
3
4
b e
f
2
c g
5 1
h
2
1
3
4
5
a
2 3
Say , w e star t Say t w ith ith el imi imi nati ng nod no d e 5 . Fi r st, w e r emov e th the e r st, self elf loo loo p-
4 5
d
b
c hg *g
f he *e
h
TIP: The The ou t- t- l li i n k k of of th the e no nod d e r emov ed w i ill co l l corr esp spon ond d t o the th e r ow and the the i n- l li i n k w i ill co l l corr esp spon ond d t o th the e colu mn
*Node Reduction Algorithm* E l limi i mi nati ng g no nod d e 5 th the e self elf loo loo p is r e pr esen ent t ed w ith ith a * and the the ou tg tg oi ng g l l i in k k f f r the no nod d e is mul mul tipl tipl i ie d . . . ro m the «
d 1
a
3
b
4
2
c
+ f h* e
1
(fh*g)
2
3
4
5
a
1
N ow , w e el imi imi nat e no nod d e 5-
TIP: The The ou t- t- l li i n k k of of th the e no nod d e ed w i ill co l l corr esp spon ond d t o th the e r emov the i n- l li i n k w i ill co l l corr esp spon ond d r ow and the t o th the e colu mn
2 3
d
4
C+fh c *g
5
g
h*g
b fh*e
f
e
h
h*e
*Node Reduction Algorithm* E l limi i mi nati ng g no nod d e 4 l i in ks mul mul tipl tipl i ie d . . .
«
the th e par allel llel l l i in k is add ed d u u p an and se ser ial ial
d +d 1
a
3
fh*e bfh*e
b
4
2
(fh*g) c ++b(fh*g)
bc
1
2
3
4
5
a
1 2 N ow , w e el imi imi nat e no nod d e 4-
3
d+bc+ d bfh*g
bfh*g
4
C+fh c *g
fh*e
5
h*g
g
e
h*e
b f h
*Node Reduction Algorithm* E l limi i mi nati ng g no nod d e 4 l i in ks mul mul tipl tipl i ie d . . .
X
a 1
«
the th e par allel llel l l i in k is add ed d u u p an and se ser ial ial
d+
bc
3
N ow , w e sh shoul oul d id eally el imi imi nat e no nod d e 3- Si nce nce r ow 2 an and d colu colu mn 2 ar e empt y y, the these ar e d r ppe pp ed . Va Value lues s of of no nod d es 1 r o and 3 ar e r etai ned ned . I f f r ow n and its corr espon spondi di ng colu g colu mn n is not not e empt y y, the them w e need need t o conti conti nue nue r emovi ng no g nod d e 3 and r etai n value values s for nod nod es 1 & 2 .
bfh*e
+ b(fh*g)
2 3
1
2
3
4
5
a
1 2 3
d+bc+ d bfh*g
bfh*g
4
C+fh c *g
fh*e
5
h*g
g
e
h*e
b f h
Building tools ( node degree & graph grap h density density ):
The out-degree of a node is the number of out-links out-links it has.
The in-degree of a node is the number of inlinks it has.
The degree of a node is the sum of in-degree and out-degree.
The average degree(mean) of a node is b/w 3 and 4.
Degree of a simple branch/junction is 3 Degree of a loop contained in 1 statement is 4
Mean degree of 4 or 5 very busy flow graph
Node Reduction Optimization (ti ps):
The optimum order for node reduction is to do the lowest degree nodes first. first.
When a
y be be 1 i n-li n-li nk nk & node with degree 3(may 2out -li nks nks or 2 i n-li n-li nks nks & 1 out -li nk nk ) is removed , it reduces the total link count by 1 link.
A degree 4 node keeps the link count the same & all higher degree nodes increase the link count.
Whats
wrong with arrays?
Matrix as a 2-dimensional array array is not convenient for larger graphs. Herez why!... We
have four reasons for the same-
Space: For a matrix representation of an array,
space grows as n2 whereas, for a linked list, it grows only as kn, where k is a small number such as 3 or 4. Weights:
Most weights in arrays are complicated and may have many components. This means that an additional weight matrix is required for for each such weight.
Whats
wrong with arrays? (cont )
Variable-length weights: If the
weights are are regular expressions/algebraic expressions, we would then need a 2-dimensional string array array (most of whose entries would be null).
Processing time:
Even though operations over null entries are fast, it still takes time to access such entries and discard them. The matrix representation representation forces forces us to spend a lot of time processing processing combinations combinations of entries that we know will yield null results.
Building tools - Linked list representations: d 1
a
b
3
c
4
1
2
5
f
h Ev er y no y nod d e is a uni uni que que name/ ame/ nu nu mbe mber. A l i in k is a pai r of nod d e r of no name ames. The The l i in k ed l d l ist en ist ent t r s for ri ie the the abo abov e ar e «
F ormat ormat for linked list entries: row, column ; data entry
g
4
node1, 3 ; a node1,2, node3, 2 ; d node3, 4; b node4, 2 ; c node4, 5 ; f
2
3
4
5
a
2 3
e
1
5
d
b
c g
f e
h
The The l i in k n k name ames w i ill u l l u su ally be poi nt er s t o ent ent r i e s i n a ri ri wh er e ac t st r i n g arr ay( w tu ua l ight ex pr essi ons ons ar e w eight ex st or ed).
I f th f the e w eights ar e f i i xed xe d node5, 2 ; g length, length, they they c c an be node5, 3 ; e assoc assoc iat iat ed w ith ith l i in ks i n -f f i i xed xe d en ent t r y gth ry len length node5, 5 ; h par allel - arr ay .
Building Building tools - Linked Linked list list representations: Cl ar i ify f y i in g g en ent t r ie s by by u u si si ng g no nod d e r i nam ame es & po poi nt er s «
1, 3
;a
2, 3, 2
;d
3, 4;
1
node1, 3 ; a
2
node2,exit
content
3,
1
node1, 3 ; a
4,
2
node2,exit
5,
3
node3, 2 ; d 3, 4;
4
4, 5 ; f
;g
5, 3
;e
5, 3
;e
5, 5
;h
5, 5
;h
node5, 2 ; g
mat is re presented in a more logic al for ma in F ig.2 ig.2 by re presenting the nodes under list y. F ig entr y ig 3 is more det ailed the with in-links to that node added (highlighted in bl ac k k ).
node3, 2 ; d 3, 4 ;
b
1,
node4, 2 ; c 4, 5 ; f
5
3
b
5, 2
ig.1 F ig.1
content
list entry
b
4, 2 ; c
list entry
5,
4
node4, 2 ; c 4, 5 ; f 3,
5
node5, 2 ; g ;e
3 5
;h
4, 5,
Matrix operations: op eration. tion. After Parallel Reduction: the easiest opera sorting, parallel links are adjacent entries with the same pair of node names. Example: Say we have 3 parallel links from node 17 to node 44. y, z & w are the pointers to the weight expressions. De pi c c ti ti ng all all th the e en ent t r i e s for ri ween en no par allel llel l l i in ks be bet w e nod d e 17 & no nod d e 44, w e hav e - node 17, 21 ; x
node 17, 21 ; x , 44 ; y
, 44 ; y , 44 ; z , 44 ; w
her e, y y = = y y + + z + w
w
Matrix operations: Loop Reduction: the self loop is identified. To remove the loop, the link weight must be multiplied with all the outlinks from that node. Start by identifying the out-links to be multiplied. Multiply the self loop (h*) where h i s the li nk ht o o f the se f l loo p with nk w ei ght the out-link. E xample: F r o it i s evi den y(5 ,5;h ) that rom m the below ent r ri i es, es, it i dent at ent r r y(5 it i it i s a se f l loo p at node 5 with li nk ht h. Al so , fr om om the 1st nk w ei ght h. two ent r t the out -li nks om node 5 are 2 and 3. ri i es, es, w e see that th nks fr om ly the se f l loo p (h* ) with the li nk We need to multi ply th nk w ei ght s o f om node 5 to node 2 & 3. nodes goi ng ng fr om
R efe efer f ig ig . on sl id id e 43 for th the e g r a raph ph. node 5, 2 ; h* h*g g
node 5, 2 ; g 5, 3
;e
5, 5
;h
5, 3
; h* h*e e
5, 5
;h
Matrix operations:
Cross-Term Cross-Term Reduction : Select a node for reduction. The cross-term step requires that you combine every in-link to the node with every out-link from that node. The in-links are obtained by back pointers. The new links created be removing the node will be associated with the nodes of the in-links. E xample: Say th y that th t the node to be remov ed ed w as as node 4.
list entry
2
content node2, exit
d 3
4
b
3
node3,2 ; d 3,4 ;
4
b
5
(inlink)4, 5
node2, exit (inlink) 4, 2 (inlink) 3,2
3
The changes are highlighted in dark red 3
d
2
4
node3,2 ; d 3,2 ;
bc
3,5 ;
bf bf
node4, 2 ; c 4, 5 ; f
bc
(inlink)3,4; b
2
5
node4, 2 ; c 4, 5 ; f
2
content
f
(inlink)4, 2 (inlink)3,2
c
list entry
(inlink) 3,4; b
bf 5
5
(inlink) 4, 5
The powers of a matrix has not been covered. covered. This includes basic matrix multiplication that you already know & sets of paths. Please read through Pag e 405 405 of of the th e Bor is is Be Beiz er t ex ex t t.
*** Some Questions picked up from the previous papers for units V, VI, VII, VIII *** --8 marks questions-1. Write the designers comments about state graphs. 2. Write an algorithm for all pair paths using matrix operations. element and minimum element element of a graph. 3. Define maximum element 4. Explain parallel reduction and loop reduction. Explai ain n5. Expl a) Distributive laws b) Absorption rule Illustrate te the applicat applications ions of Decision tables? 6. Illustra state machine is invariant under all encodings. Justify Justify? ? 7. The behavior of a finite state 8. What operations does a tool kit consist for the representation of graphs? 9. How can a relation relation matrix be represented represented and what are the properties properties of relations? relations? 10.What are the rules of Boolean algebra? --12 marks questions -testing. 1. Define the purpose of KV charts. Describe the use of KV charts in logic based testing. 2. Define a state chart. Define the method used to distinguish good and bad state graphs. Briefly y discuss discuss the follo following wing 3. Briefl a) matrix of a graph b) node reduction algorithms --16 marks questions-1. Draw a hard disk recovery system with a state graph and state table. 2. What is decision table and how is it useful in testing? Explain it with the help of an example. 3. Write short notes on b) Dead states a) Transition bugs c) State bugs d) Encoding bugs. State graphs with implementatio implementation? n? 4. Explain State 5. What is Decision table and how is a Decision table useful in testing? Explain with the help of an example? Explai ain n6. Expl b)state graph inputs and outputs c) transitions and state tables a) finite state machine
And ye yeah!! ah!!!! Plz Plz sol solve ve all the questions that appeared in previous papers!
Done!!! Do feel free to get back
incase you have any questions Note:
Please read through the text by Boris Beizer at atle leas astt on once ce. This PPT is just for your reference & for step by step understanding understanding of the processing.