What is Graph Matrix?
A graph matrix is a square matrix whose size represents the number of nodes in the control flow graph.
Each row and column in the matrix identifies a node and the entries in the matrix represent the edges or links between these nodes.
Conventionally, nodes are denoted by digits, and edges are denoted by letters.
Graph Examples |
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) will be filled with ‘a’ as an edge exists from node 1 to node 1
(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
- (1, 3) will be filled with ‘c’ as edge c exists from node 1 to node 3
- (2, 4) will be filled with ‘d’ as edge exists from node 2 to node 4
- (3, 4) will be filled with ‘e’ as an edge exists from node 3 to node 4
Connection Matrix
A connection matrix is a matrix defined with edge weight.
In simple form, when a connection exists between two nodes of the 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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPgaQt6OpHFnnjmtEVY7_03ce3vaXTtaRSwH3v7wtm0FGsjOF6U71wAgr5yeTZqbqIoFBZOaCL1phwGaqBvqSp5opwV2mCNWwfoLCY1f2LwvTd2fTLTTQRPVOzW26m6Jpsw9zHxhfn4qjq/s16000/graph.png)
The weight of the edges is 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.
Count the number of 1s in each row and write it at the end of the row
Subtract 1 from this count for each row (Ignore the row if its count is 0)
Add the count of each row calculated previously
Add 1 to this total count
The final sum in Step 4 is the cyclomatic complexity of the control flow graph
![cyclomatic complexity cyclomatic complexity](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAv1QMQgt3uzZSYc8kwnMLSYkn_78rKwSGdxOz5Fzhm9DdI9eTxRAxsh2LbNtE3yqYG9-wTpV_9M-YGhYgSmsXVdcQTTMTOKpaC_5AxXvlQj-gw5sDJUpPelp1-q6lI8w-arBbsTK8DDDD/s16000/cyclomatic+complexity.png)
Other methods to calculate cyclomatic complexity :
Cyclomatic complexity = e – n + 2 * p
Where,
e : no. of edges
n : no. of nodes
p : no. of disconnected parts of the flow graph
Cyclomatic complexity = p + 1
Where,
p: no. predicate nodes contained in the control flow graph
Cyclomatic complexity = Number of regions in the graph + 1
![example of connection matrix example of connection matrix](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCxk5fQeW7xK5OES06UMlMrWxIl8MNksQ54z5HAOMAtQa9bY4-ut9R7h-fepzIxhGnT9049JQcdA50RLBz7ubhp_r-HfJHUdrMXl8ToTvLg4slM36Tc1C3N5Nk2J-hWSVxxmYEST11a1JI/s16000/program.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTIAivWi3wdt__6RmZZhiMCIBr8phFUMhMilfdYOjaAIeC7klTO8XTFjQ9QGjjZFNePhYLXKcSath1qAbucIVsEzdgkzKcoNDBpQ3jtNa32-ED9QcB3YSwpEc4sLjVobVbnVbub9B7v6i3/s16000/ex-1.png)
Nodes: 7
Edges: 7
Connected components: 1
Cyclomatic complexity = E-N + 2*p = 7-7+2![control flow graph cyclomatic complexity](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNPxwRY5E2Gr2ImZmDsQ6Uw4ZmOTNJhjuYtN809VsPb5OpZNqRblqh_xgDcGdDiNgmS2P08kusOtYBryyfcbKSr5EW9AI9A_74RmGfBvvsm9UlGsGxEekS_2NNTD-h6_oOB_WQku5eCJLT/s16000/ex-2.png)
![control flow graph control flow graph](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMR627pn3w8ezYNINcyCr9qNlaARYAyNwl5a2sjIkl3t1A1cTPJe1QR5hznrPWeHM8HEN-oi_JGTWYgkq9fHAwJ9B3de_ixaa8UQxq5zT5upC-Fpx29jexlIAlGYPEkVt7piIUQZCWOHKL/s16000/ex-3.png)
Comments
Post a Comment