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 for Topological Sorting in Graphs

Posted on July 29, 2013 by Manish
This C++ program, using an adjacency matrix, displays the times at which the different times at which nodes are visited and left thereby producing a linear ordering of vertices in a graph.
A graph is a set of nodes connected by edges.

Here is the source code of the C++ program to display topological sort. The C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is also shown below.

  1. /*
  2.  * C++ Program for Topological Sorting in Graphs
  3.  */
  4. #include <iostream>
  5. #include <conio.h>
  6. using namespace std;
  7. struct node_info
  8. {
  9.     int no;
  10.     int lv_time, st_time;
  11. }*q = NULL, *r = NULL;
  12. struct node
  13. {
  14.     node_info *pt;
  15.     node *next;
  16. }*top = NULL, *p = NULL, *np = NULL;
  17. int c = 0;
  18. void push(node_info *ptr)
  19. {
  20.     np = new node;
  21.     np->pt = ptr;
  22.     np->next = NULL;
  23.     if (top == NULL)
  24.     {
  25.         top = np;
  26.     }
  27.     else
  28.     {
  29.         np->next = top;
  30.         top = np;
  31.     }
  32. }
  33. node_info *pop()
  34. {
  35.     if (top == NULL)
  36.     {
  37.         cout<<"underflow\n";
  38.     }
  39.     else
  40.     {
  41.         p = top;
  42.         top = top->next;
  43.         return(p->pt);
  44.         delete(p);
  45.     }
  46. }
  47. void topo(int *v,int am[][7],int i)
  48. {
  49.     q = new node_info;
  50.     q->no = i;
  51.     q->st_time = c;
  52.     cout<<"start time for node no "<<q->no<<":"<<c<<endl;
  53.     push(q);
  54.     v[i] = 1;
  55.     for (int j = 0; j < 7; j++)
  56.     {
  57.         if (am[i][j] == 0 || (am[i][j] == 1 && v[j] == 1))
  58.             continue;
  59.         else if(am[i][j] == 1 && v[j] == 0)
  60.         {
  61.             c++;
  62.             topo(v,am,j);
  63.         }
  64.     }
  65.     c++;
  66.     r = pop();
  67.     cout<<"leave time for "<<r->no<<":"<<c<<endl;
  68.     return;
  69. }
  70. int main()
  71. {
  72.     int v[7],am[7][7];
  73.     for (int i = 0; i < 7; i++)
  74.     v[i] = 0;
  75.     for (int i = 0; i < 7; i++)
  76.     {
  77.         cout<<"enter the values for adjacency matrix row:"<<i + 1<<endl;
  78.         for(int j = 0; j < 7; j++)
  79.         {
  80.             cin>>am[i][j];
  81.         }
  82.     }
  83.     topo(v,am,0);
  84.     getch();
  85. }

advertisement
Output
enter the values for adjacency matrix row:1
0
1
1
0
0
1
1
enter the values for adjacency matrix row:2
1
0
0
0
0
0
0
enter the values for adjacency matrix row:3
1
0
0
0
0
0
1
enter the values for adjacency matrix row:4
0
0
0
0
1
1
0
enter the values for adjacency matrix row:5
0
0
0
1
0
1
1
enter the values for adjacency matrix row:6
1
0
0
1
1
0
0
enter the values for adjacency matrix row:7
1
0
1
0
1
0
0
start time for node no 0:0
start time for node no 1:1
leave time for node no 1:2
start time for node no 2:3
start time for node no 6:4
start time for node no 4:5
start time for node no 3:6
start time for node no 5:7
leave time for node no 5:8
leave time for node no 3:9
leave time for node no 4:10
leave time for node no 6:11
leave time for node no 2:12
leave time for node no 0:13

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 to Traverse a Graph using BFS
» Next Page - C++ Program to Implement Queue using Linked List

« Java Program to Check Cycle in a Graph using Topological Sort
C++ Program to Find the maximum subarray sub using Divide and Conquer Approach »

Deep Dive @ Sanfoundry:

  1. C++ Programming Examples on Hard Graph Problems & Algorithms
  2. C Programming Examples on Hard Graph Problems & Algorithms
  3. C Programming Examples on Linked List
  4. C Programming Examples without using Recursion
  5. C Programming Examples on Trees
  6. C Programming Examples on Searching and Sorting
  7. C# Programming Examples on Sorting
  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.