Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C Algorithms – Topological Sort

By Exforsys | on September 15, 2011 |
C Algorithms

Let us say that the order relation that was defined the in introduction lesson was a partial one, for example:

a1 < a0, a1 < a2 < a3.

The problem is to determine a list of order, in which if ai < aj then ai will come before aj in the final sorted list .

For example, our list could be :

a1, a0, a2, a3

or

a1, a2, a0, a3

or

a1, a2, a3, a0

Any partial ordered list can be sorted topological.

For a graph, a topological sort of a directed graph refers to an ordering of the vertices, that for every edge u,v, u comes before v in the ordering.

A topological sort is possible only if the graph has no directed cycles, which it means it must be a directed acyclic graph or DAG. A cycle in a graph means having 3 nodes A, B and C, there is a path from A to B, from B to C and from C to A, which creates a cycle.

Step by step example:

Having the following DAG:

An order could be A -> B D -> E C-> D F, but this is wrong, it is not an topological order!

First, choose from the vertices the one that has no predecessors :

Let us choose A, so we delete A and all the lines from there, and A will be the first element:

Then again we choose the element that has no predecessors:

Choose D, and to the same like in the previous case and we now have A -> D:

We repeat the process:

We remove F, and now we have A -> D -> F:

B is the only option left:

A -> D -> F -> B

We now choose C:

A -> D -> F -> B -> C

And all the remains is E. The list will be A -> D -> F -> B -> C -> E.

Sample code:

  1.  #include &lt; iostream&gt;
  2. using namespace std;
  3.  # include &lt; math.h &gt;
  4.  
  5. int main()
  6. {
  7. 	int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
  8. 	cout &lt;&lt; "Enter the no of vertices:";
  9. 	cin &gt;&gt; n;
  10. 	cout &lt;&lt; "Enter the incidence matrix:" ;
  11. 		for(i=0;i&lt; n;i++)
  12. 			for(j=0;j&lt; n;j++)
  13. 				cin&gt;&gt;a[i][j];
  14. 	for(i=0;i&lt; n;i++)
  15. 	{
  16. 		indeg[i]=0;
  17. 		flag[i]=0;
  18. 	}
  19. 	for(i=0;i&lt; n;i++)
  20. 		for(j=0;j&lt; n;j++)
  21. 			indeg[i]=indeg[i]+a[j][i];
  22. 	cout &lt;&lt; "The topological order is:" ;
  23. 		while(count&lt; n)
  24. 		{
  25. 			for(k=0;k&lt; n;k++)
  26. 			{
  27. 				if((indeg[k]==0) &amp;&amp; (flag[k]==0))
  28. 				{
  29. 					cout &lt;&lt; (k+1) ;
  30. 						flag [k]=1;
  31. 				}
  32.  
  33. 				for(i=0;i&lt; n;i++)
  34. 				{
  35. 					if(a[i][k]==1)
  36. 						indeg[k]--;
  37. 				}
  38.  
  39. 			}
  40. 			count++;
  41. 		}
  42. 		return 0;
  43.  
  44. }

Output:

Code explanation:

At first, all the vertices are counted, I refer to the number of predecessors for each vertex. Then, a vertex with 0 is found (0 because it must not have any predecessors for the topological sort to work). It is removed, then the degrees are updated. In this sample code, the vertices are inserted in the form of an incidence matrix (you can read about this in the lesson regarding graph theory). As for this example, the matrix is :

  1 2 3 4
1 0 1 1 0
2 0 0 0 1
3 0 0 0 1
4 0 0 0 0

A visual representation of the graph:

The data will be inserted line by line, from the above matrix for the first vertex 1 is 0, 1, 1, 0, then for the vertex 2 is 0, 0, 0, 1 and so on.

Complexity:

How many operations are needed to compute the in-degrees? Depends on the representation:

Adjacency lists: O(|E|)

Matrix: O(|V|2)

Note that if the graph is complete |E| = O(|V|2).

Conclusion:

Topological sort is used for partial defined orders. In recent years, the algorithm was developed for parallel computation. In general use, this algorithm is used in cases for which a graphic model is available for people to understand bigger problems.

« « C Algorithms – Graph Theory
C Algorithms – Depth-First Search (DFS) » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • C Algorithms – The Problem of Sorting the Elements

    September 8, 2011 - 0 Comment
  • C Algorithms – Bucket Sort

    September 13, 2011 - 0 Comment
  • C Algorithms – Graph Theory

    September 15, 2011 - 0 Comment
  • C Algorithms – Gnome Sort

    September 14, 2011 - 0 Comment
  • C Algorithms – Merge Sort

    September 12, 2011 - 0 Comment
  • C Algorithms – Insertion Sort

    September 9, 2011 - 0 Comment
  • C Algorithms – Selection Sort

    September 11, 2011 - 0 Comment
  • C Algorithms – Dijkstra’s Algorithm

    September 17, 2011 - 0 Comment
  • C Algorithms – Heap Sort

    September 11, 2011 - 0 Comment
  • C Algorithms – Breadth-First Search (BFS)

    September 16, 2011 - 0 Comment
  • C Algorithms – Dijkstra’s Algorithm

    September 17, 2011 - 0 Comment
  • C Algorithms – Breadth-First Search (BFS)

    September 16, 2011 - 0 Comment
  • C Algorithms – Depth-First Search (DFS)

    September 16, 2011 - 0 Comment
  • C Algorithms – Graph Theory

    September 15, 2011 - 0 Comment
  • C Algorithms – Gnome Sort

    September 14, 2011 - 0 Comment
  • C Algorithms – Comb Sort

    September 14, 2011 - 0 Comment
  • C Algorithms – Bucket Sort

    September 13, 2011 - 0 Comment
  • C Algorithms – Quick Sort

    September 13, 2011 - 0 Comment
  • C Algorithms – Merge Sort

    September 12, 2011 - 0 Comment
  • C Algorithms – Counting Sort

    September 12, 2011 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C Algorithms – Dijkstra’s Algorithm
  • C Algorithms – Breadth-First Search (BFS)
  • C Algorithms – Depth-First Search (DFS)
  • C Algorithms – Graph Theory
  • C Algorithms – Gnome Sort

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT