- #include
- #include
- class topologicalorder
- {
- private:int n;
- int a[10][10];
- int indegree[10];
- public :void read_data();
- void find_indegree();
- void topological_sort();
- };
- void topologicalorder::read_data()
- {
- cout<<"Enter the no of jobs :\n";
- cin>>n;
- cout<<"Enter the adjancency matrix :\n";
- for(int i=0;i
- {
- for(int j=0;j
- {
- cin>>a[i][j];
- }
- }
- }
- void topologicalorder::find_indegree()
- {
- for(int j=0;j
- {
- int sum=0;
- for(int i=0;i
- {
- sum+=a[i][j];
- }
- indegree[j]=sum;
- }
- }
- void topologicalorder::topological_sort()
- {
- int u,v,t[10],s[10];
- find_indegree();
- int top=-1;
- int k=0;
- for(int i=0;i
- {
- if(indegree[i]==0)
- s[++top]=i;
- }
- while(top!=-1)
- {
- u=s[top--];
- t[k++]=u;
- for(v=0;v
- {
- if(a[u][v]==1)
- {
- indegree[v]--;
- if(indegree[v]==0)
- s[++top]=v;
- }
- }
- }
- cout<<"the topological sequence is :\n";
- for(i=0;i
- cout<
" " ; - }
- void main()
- {
- topologicalorder t;
- clrscr();
- t.read_data();
- t.topological_sort();
- getch();
- }
Loading
Replies
Know the answer? Post it — somebody with the same question will find it here.