fork download
  1. #include<stdio.h>
  2. int cost[10][10],n,colsum[10];
  3. void cal_colsum()
  4. {
  5. for(int j=0;j<n;j++)
  6. {
  7. colsum[j]=0;
  8. for(int i=0;i<n;i++)
  9. colsum[j]+=cost[i][j];
  10. }
  11. }
  12. void source_removal()
  13. {
  14. int i,j,k,select[10]={0};
  15. printf("Topological ordering is:");
  16. for(i=0;i<n;i++)
  17. {
  18. //Calculate the outdegree for each vertices
  19. cal_colsum();
  20. for(j=0;j<n;j++)
  21. {
  22. if(select[j]==0 && colsum[j]==0)//source vertex
  23. break;
  24. }
  25. printf("%d ",j);
  26. select[j]=1;
  27. //Remove source vertex j from cost matrix
  28. for(k=0;k<n;k++)
  29. cost[j][k]=0;
  30. }
  31. }
  32. int main()
  33. {
  34. printf("Enter no. of Vertices: ");
  35. scanf("%d",&n);
  36. printf("Enter the cost matrix\n");
  37. for(int i=0;i<n;i++)
  38. for(int j=0;j<n;j++)
  39. scanf("%d",&cost[i][j]);
  40. source_removal();
  41. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Enter no. of Vertices: Enter the cost matrix
Topological ordering is: