DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Graph API for Entra ID (Azure AD) Object Management
  • Building Neural Networks With Automatic Differentiation
  • Bridging Graphviz and Cytoscape.js for Interactive Graphs
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 7)

Trending

  • How Clojure Shapes Teams and Products
  • How AI Is Changing the Way Developers Write Code
  • APIs for Logistics Orchestration: Designing for Compliance, Exceptions, and Edge Cases
  • Why Rate Limiting Matters in Istio and How to Implement It

How to draw a Control flow graph & Cyclometric complexity for a given procedure

By 
Pubudu Dissanayake user avatar
Pubudu Dissanayake
·
Oct. 13, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
136.5K Views

Join the DZone community and get the full member experience.

Join For Free

 cyclomatic complexity 

cyclomatic complexity is a software metric used to measure the complexity of a program.

this metric measures independent paths through the program's source code. an independent path is defined as a path that has at least one edge which has not been traversed before in any other paths.

cyclomatic complexity can be calculated with respect to functions, modules, methods or classes within a program. 

 compound condition 

where one or more boolean operators such as the logical or, and, nand, nor are present is a conditional statement:


 1.     if a or b   
 2.     then procedure x   
 3.     else   procedure y   
 4.     endif   

a  predicate  node will be created for each statement.

check the following code fragment:


 01.     insertion_procedure (   int   a[],   int   p [],   int   n)   
 02.     {   
 03.       int   i,j,k;   
 04.       for   (i=   0   ; i<=n; i++) p[i] = i;   
 05.       for   (i=   2   ; i<=n; i++)   
 06.       {   
 07.       k = p[i];   
 08.       j =   1   ;   
 09.       while   (a[p[j-   1   ]] > a[k]) {p[j] = p[j-   1   ]; j--}   
 10.       p[j] = k;   
 11.       }   
 12.     }   

first and foremost, start numbering the statement.


 01.     insertion_procedure (   int   a[],   int   p [],   int   n)   
 02.       {   
 03.     (   1   )    int i,j,k;   
 04.     (   2   )   for   ((2a)i=   0   ; (2b)i<=n; (2c)i++)   
 05.     (   3   )        p[i] = i;   
 06.     (   4   )   for   ((4a)i=   2   ; (4b)i<=n; (4c)i++)   
 07.       {   
 08.     (   5   )       k=p[i];j=   1   ;   
 09.     (   6   )   while   (a[p[j-   1   ]] > a[k]) {   
 10.     (   7   )           p[j] = p[j-   1   ];   
 11.     (   8   )           j--   
 12.       }   
 13.     (   9   )          p[j] = k;   
 14.       }   

now you can clearly see which statement executes first and which executes last, etc. so drawing the cfg becomes simple:

now, to calculate the cyclomatic complexity you use one of three methods:

  1. count the number of regions on the graph: 4
  2. no. of predicates (red on graph) + 1 : 3 + 1 = 4
  3. no of edges – no. of nodes + 2: 14 – 12 + 2 = 4

that’s about it. happy coding :)

Control Flow Graph Graph (Unix) Cyclomatic Complexity Flow (web browser)

Published at DZone with permission of Pubudu Dissanayake. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Graph API for Entra ID (Azure AD) Object Management
  • Building Neural Networks With Automatic Differentiation
  • Bridging Graphviz and Cytoscape.js for Interactive Graphs
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 7)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!