Advertisement
rootUser

DAG 1

Jul 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3.  
  4. vector<int>Graph[1000];
  5. bool visited[1000];
  6. int topologicalSort[1000];
  7. bool top[1000];
  8. int cost[1000][1000];
  9. int path[1000];
  10. int dis[1000];
  11. int n;
  12. void inputGraph()
  13. {
  14.     int vertex,edge;
  15.  
  16.  
  17.     printf("Enter the number of vertex : ");
  18.     scanf("%d",&vertex);
  19.     n=vertex;
  20.     printf("\nEnter the number of edges : ");
  21.     scanf("%d",&edge);
  22.     for(int i = 1 ; i<=edge ; i++)
  23.     {
  24.         printf("\nInput edge no %d : ",i);
  25.         int u,v;
  26.         scanf("%d %d",&u,&v);
  27.         printf("Cost: ");
  28.         scanf("%d",&cost[u][v]);
  29.         Graph[u].push_back(v);
  30.     }
  31. }
  32.  
  33. void TopologicalSort()
  34. {
  35.     printf("Topological Sort: \n");
  36.     stack<int>s;
  37.     stack<int>tp;
  38.     int i,j;
  39.  
  40.     for(j=1; j<=n; j++)
  41.     {
  42.         s.push(j);
  43.         visited[j]=true ;
  44.         while(!s.empty())
  45.         {
  46.             for(i = 0 ; i<Graph[s.top()].size() ; i++)
  47.             {
  48.                 if(visited[Graph[s.top()][i]]==false)
  49.                 {
  50.                     visited[Graph[s.top()][i]]=true ;
  51.                     s.push(Graph[s.top()][i]);
  52.                     i = -1 ;
  53.                 }
  54.             }
  55.             if(top[s.top()]==false)
  56.             {
  57.                 tp.push(s.top());
  58.                 top[s.top()]=true;
  59.             }
  60.  
  61.             s.pop();
  62.         }
  63.     }
  64.     i=0;
  65.     while(!tp.empty())
  66.     {
  67.         printf("%d -> ",tp.top());
  68.         topologicalSort[i]=tp.top();
  69.         i++;
  70.         tp.pop();
  71.     }
  72.     printf("End!\n");
  73. }
  74.  
  75. int selectSource()
  76. {
  77.     int sr;
  78.     printf("\nEnter the source node for DAG shortest path: ");
  79.     scanf("%d",&sr);
  80.     return sr;
  81. }
  82.  
  83. void InitilizeSingleSource(int source)
  84. {
  85.     for(int j=1; j<=n; j++)
  86.     {
  87.         path[j]=0;
  88.         dis[j]=99999;
  89.     }
  90.     dis[source]=0;
  91.  
  92. }
  93. void relax(int u,int v)
  94. {
  95.     if(dis[v]>dis[u]+cost[u][v])
  96.     {
  97.         dis[v]=dis[u]+cost[u][v];
  98.         path[v]=u;
  99.     }
  100. }
  101. void DAG(int source)
  102. {
  103.     InitilizeSingleSource(source);
  104.     int i,j;
  105.     for(i=0; i<n; i++)
  106.     {
  107.         for(j = 0 ; j<Graph[topologicalSort[i]].size() ; j++)
  108.         {
  109.             relax(topologicalSort[i],Graph[topologicalSort[i]][j]);
  110.         }
  111.     }
  112.  
  113.  
  114.     //Print
  115.     for(i=1; i<=n; i++)
  116.     {
  117.         printf("\nFor Node %d: ",i);
  118.         if(dis[i]<99999)
  119.             printf("\nCost from source : %d\n\nPath: ",dis[i]);
  120.         else
  121.             printf("\nCost from source : Infinity\n\nPath: ",dis[i]);
  122.  
  123.         stack <int> s;
  124.         s.push(i);
  125.         while(s.top()!=0)
  126.         {
  127.             s.push(path[s.top()]);
  128.         }
  129.         while(!s.empty())
  130.         {
  131.             if(s.top()!=0)
  132.                 printf("%d -> ",s.top());
  133.             s.pop();
  134.         }
  135.         printf("End!\n");
  136.     }
  137.  
  138. }
  139. int main()
  140. {
  141.     int source;
  142.     inputGraph();
  143.  
  144.     TopologicalSort();
  145.     DAG(selectSource());
  146.     return 0 ;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement