GATE / CS ARCHIVE

SIMILAR QUESTIONS / ENGINEERING MATHEMATICS & OTHER TOPICS

Count independent paths using decisions or flowgraph cyclomatic number

3 questions solved the same way, newest first. Back to Engineering Mathematics & Other Topics

2 marks · NAT
Not in GATE 2027 syllabusReport issue

Software Engineering

Question 38, GATE 2015: Find cyclomatic complexity of: while(first<=last){if(array[middle]<search) first=middle+1; else if(array[middle]==search) found=TRUE; else last=middle-1; middle=(first+last)/2;} if(first>last) notPresent=TRUE;.
Tap to enlarge
Answer & solution

Official answer: 5

For a single connected control-flow graph with binary conditions, McCabe complexity is one plus the number of decisions. Count while, if(<), else-if(==), and the final if(>): four decisions, hence five. An unconditional else is not an additional test; assignments are not branches. The code can fail to terminate after finding the value, but that semantic defect does not remove a syntactic decision from its control-flow graph.

Solution: The control-flow graph has four binary decisions: the while condition, the first if, the else-if, and the final if. For one connected entry/exit graph, cyclomatic complexity is decisions+1=5. This graph count does not require every syntactic path to be feasible or terminating.

Original paper, page 14 · Answer key

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Software Engineering

Question 58, GATE 2015: Find McCabe cyclomatic complexities of X,Y,Z in that order. Program X:
sumcal(int maxint,int value) {
 int result=0,i=0;
 if(value<0) { value=-value; }
 while((i<value) AND (result<=maxint)) { i=i+1; result=result+1; }
 if(result<=maxint) { printf(result); } else { printf("large"); }
 printf("end of program");
}
The shown control-flow graph Y has eight vertically ordered nodes v1,...,v8, edges vi→v(i+1) for i=1,...,7, and back edges v3→v1, v6→v4, v8→v1. Graph Z is serial composition of the graph of X followed by the graph of Y. Options: (A) 4,4,7; (B) 3,4,7; (C) 4,4,8; (D) 4,3,8. State the convention for counting the compound while condition.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

1. Treat each compound source-level condition as one decision: X has its first if, the while, and the final if, so complexity 4.

2. The shown Y graph has 8 vertices and 10 edges, giving E−N+2=4. Serial composition adds complexities minus 1, so Z=4+4−1=7. Thus A. Expanding the short-circuit AND into separate predicate nodes would increase X to 5 and Z to 8; the official answer uses source-statement predicate counting rather than that expanded control-flow model.

Original paper, page 76 · Answer key

1 mark · MCQ
Not in GATE 2027 syllabusReport issue

Cyclomatic complexity of sequential composition

Software Engineering

Question 21, GATE 2010: Modules A and B each have cyclomatic complexity 10. The combined flow executes A and then B in sequence, with no additional branch between them. What is the cyclomatic complexity of this sequential integration?
Options: (A) 19; (B) 21; (C) 20; (D) 10.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

For a connected single-entry/single-exit flow graph, cyclomatic complexity is the number of decision points plus one. Complexity 10 in each component means nine independent decisions each. Sequentially composing them adds their decision counts but introduces only one overall baseline path: 9+9+1=19. Equivalently V(A;B)=V(A)+V(B)−1, so A.

The flow-graph formula gives the same result. Connecting A’s exit to B’s entry adds one edge while leaving N_A+N_B vertices. Thus E−N+2=(E_A−N_A+2)+(E_B−N_B+2)−1=19. Simply adding 10+10 counts the straight-through baseline path twice.

Original paper, page 5