Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

Topological Sorting

Posted by Vinod on October 5, 2006

/********************************************************************

-> This C++ Program is to implement Topological sorting.

-> This program sorts the given integers in increasing order
using topological sorting

-> unwaighted digraph is used to sort the numbers

-> Data Structers:
Graph:Adjacency List

-> This program works in microsoft vc++ 6.0 environment.

*********************************************************************/

#include<iostream.h>
class graph
{
private:
int n;
int data[20];
int gptr[20][20];
public:
void create();
void topological();
};
void graph::create()
{
cout<<”**********************************************************\n”
<<”This program sorts the given numbers in increasing order\n”
<<”\t\t using topological sorting\n”
<<”***********************************************************\n”;
cout<<”Enter the no. of nodes in the directed unweighted graph ::”;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<”enter data for the node “<<i<<” ::”;
cin>>data[i];
}

cout<<”enter the adjacency matrix for the graph ::\n”;
cout<<”( type 1 for graph[i][j] if there is an edge from i to j”
<<”else type 0 )\n\n”;

int j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>gptr[i][j];
}
void graph::topological()
{
int flag;
int i,j;
int poset[20],included[20];
for(i=1;i<=n;i++)
{
poset[i]=0;
included[i]=false;
}
int k=1;
flag=true;
int zeroindegree;
int c=1;
while(flag==1)
{
for(i=1;i<=n;i++)
{
if(!included[i])
{
zeroindegree=true;
for(j=1;j<=n;j++)
{
if(gptr[j][i]>0)
{
zeroindegree=false;
break;
}
}

if(zeroindegree)
{
included[i]=true;
poset[k]=data[i];
k=k+1;
for(j=1;j<=n;j++)
{
gptr[i][j]=-1;
gptr[j][i]=-1;
}
break;
}
}
}
if(i==n+1)
{
if(zeroindegree==false)
{
cout<<”Graph is not acyclic\n”;
return;
}
else
{
poset[k]=data[i-1];
k=k+1;
flag=false;
}
}
}
cout<<”After topological sorting the numbers are :\n”;
for(i=1;i<=n;i++)
cout<<poset[i]<<”\t”;

cout<<endl<<endl;
}
int main()
{
graph obj;
obj.create();
obj.topological();
return 0;
}

Advertisement

10 Responses to “Topological Sorting”

  1. A user said

    POOR………

  2. ambot said

    can teach me those….

  3. prabhu said

    i want this codeing

  4. Grim Reaper said

    coul u upload programs of matrix chain multiplication , optimal binary search tree and floyd warshall in C++

    THANX IN ADVANCE

  5. Grim Reaper said

    could u pls upload
    floyd warshall
    binary search tree(optimal)
    matrix chain multiplication

  6. dhasn said

    it would have been nice if it was in simple c

  7. jhgkjg said

    boring ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

  8. moin sayyed said

    so boringggggggggggggggggggggggggggggggggggggggggggggggggg
    iwant break.

  9. alaa said

    i want this cod

  10. Cheap Downloadable DvDRip…

    [...]Topological Sorting « Data Structures through C & C++ for beginners[...]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 70 other followers