logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Find Transitive Closure of a Graph

Posted on April 21, 2014 by Manish
This C++ program displays the transitive closure matrix of a graph. The reachability of a particular node ‘i’ towards all node pairs (‘i’,’j’) is known as the transitive closure of a graph.

Here is the source code of the C++ program to display the transitive closure of a particular directed graph consisting of four nodes. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Find Transitive Closure of a Graph
  3.  */
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<iostream>
  7. using namespace std;
  8. void printSolution(int reach[][4])
  9. {
  10.     cout<<"Following matrix is transitive closure of the given graph\n";
  11.     for (int i = 0; i < 4; i++)
  12.     {
  13.         for (int j = 0; j < 4; j++)
  14.         {
  15.             cout<<reach[i][j];
  16.         }
  17.         cout<<endl;
  18.     }
  19. }
  20. void transitiveClosure(int graph[][4])
  21. {
  22.     int reach[4][4], i, j, k;
  23.     for (i = 0; i < 4; i++)
  24.     {
  25.         for (j = 0; j < 4; j++)
  26.         {
  27.             reach[i][j] = graph[i][j];
  28.         }
  29.     }
  30.     for (k = 0; k < 4; k++)
  31.     {
  32.         for (i = 0; i < 4; i++)
  33.         {
  34.             for (j = 0; j < 4; j++)
  35.             {
  36.                 reach[i][j] = reach[i][j] || (reach[i][k] && reach[k][j]);
  37.             }
  38.         }
  39.     }
  40.     printSolution(reach);
  41. }
  42. int main()
  43. {
  44.     int graph[4][4] = { {1, 1, 0, 1},
  45.                         {0, 1, 1, 0},
  46.                         {0, 0, 1, 1},
  47.                         {0, 0, 0, 1}
  48.                       };
  49.     transitiveClosure(graph);
  50.     getch();
  51. }

advertisement
Output
Following matrix is transitive closure of the given graph
1111
0111
0011
0001

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program For Inorder Tree Traversal without Recursion
» Next Page - C++ Program to Implement Ford–Fulkerson Algorithm

« C++ Program to Find the Number of Permutations of a Given String
Program to Perform Searching Using Self-Organizing Lists »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Combinatorial Problems & Algorithms
  2. Java Algorithms, Problems & Programming Examples
  3. C Algorithms, Problems & Programming Examples
  4. C++ Algorithms, Problems & Programming Examples
  5. Java Programming Examples on Hard Graph Problems & Algorithms
  6. C++ Programming Examples on Hard Graph Problems & Algorithms
  7. C Programming Examples on Hard Graph Problems & Algorithms
  8. Java Programming Examples on Graph Problems & Algorithms
  9. C++ Programming Examples on Graph Problems & Algorithms
  10. C Programming Examples on Graph Problems & Algorithms
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.