Advertisement
rootUser

DAG (OWN)

Jul 9th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | None | 0 0
  1. #include<iostream>
  2. #include<stack>
  3. using namespace std;
  4.  
  5. int vertex,edge,source,time;
  6. int adjacency_matrix[100][100],weight_matrix[100][100],cost[100];
  7. int parent[100],Distance[100],Finishing_time[100],tSVertex[100],visited[100];
  8. string color[100];
  9. stack<int> result;
  10. void inputGraph();
  11. void doDFS();
  12. void doDFSvisit(int);
  13. void printTSpath();
  14. void Initialize_Single_Source();
  15. void relax();
  16. void DAG();
  17. void printCostAndPath();
  18.  
  19. int main(void)
  20. {
  21.     inputGraph();
  22.     //initialize();
  23.     doDFS();
  24.     printTSpath();
  25.     //printAll();
  26.     Initialize_Single_Source();
  27.     DAG();
  28.     printCostAndPath();
  29.     return 0;
  30. }
  31. void inputGraph()
  32. {
  33.     cout<<"Total vertex : ";
  34.     cin>>vertex;
  35.     cout<<"Total edge   : ";
  36.     cin>>edge;
  37.     int i,j;
  38.     //make all weight infinity and vertex disconnected
  39.     for(i=1; i<=vertex; i++)
  40.     {
  41.         for(j=1; j<=vertex; j++)
  42.         {
  43.             adjacency_matrix[i][j]=0;
  44.             weight_matrix[i][j]=999999;
  45.         }
  46.  
  47.     }
  48.     // scan adjacency matrix and weight
  49.     for(i=1; i<=edge; i++)
  50.     {
  51.         int start,finish;
  52.         cout<<"Enter start and end node for edge "<<i<<" : ";
  53.         cin>>start;
  54.         cin>>finish;
  55.         adjacency_matrix[start][finish]=1;
  56.         cout<<"Enter weight for edge "<<i<<" : ";
  57.         cin>>weight_matrix[start][finish];
  58.     }
  59.     cout<<"The adjacency matrix is : "<<endl;
  60.     for(i=1; i<=vertex; i++)
  61.     {
  62.         for(j=1; j<=vertex; j++)
  63.         {
  64.             cout<<adjacency_matrix[i][j]<<" ";
  65.         }
  66.         cout<<endl;
  67.     }
  68.     cout<<"The weight matrix is : "<<endl;
  69.     for(i=1; i<=vertex; i++)
  70.     {
  71.         for(j=1; j<=vertex; j++)
  72.         {
  73.             cout<<weight_matrix[i][j]<<" ";
  74.         }
  75.         cout<<endl;
  76.     }
  77. }
  78.  
  79. void doDFS()
  80. {
  81.     int i,j;
  82.     for(i=1; i<=vertex; i++)
  83.     {
  84.         color[i]="white";
  85.         parent[i]=0;
  86.     }
  87.     time=0;
  88.     //super correction : add this line ( even in algorithm if need )
  89.     doDFSvisit(source);
  90.     //
  91.     for(i=1; i<=vertex; i++)
  92.     {
  93.         if(color[i]=="white")
  94.         {
  95.             doDFSvisit(i);
  96.         }
  97.     }
  98. }
  99. void doDFSvisit(int node)
  100. {
  101.     int i;
  102.     time=time+1;
  103.     Distance[node]=time;
  104.     color[node]="grey";
  105.     for(i=1; i<=vertex; i++)
  106.     {
  107.         if(adjacency_matrix[node][i]==1)
  108.         {
  109.             if(color[i]=="white")
  110.             {
  111.                 parent[i]=node;
  112.                 doDFSvisit(i);
  113.             }
  114.         }
  115.     }
  116.     color[node]="black";
  117.     //extra line for result
  118.     result.push(node);
  119.     //cout<<"Pushed : "<<node<<endl;
  120.     //
  121.     time=time+1;
  122.     Finishing_time[node]=time;
  123. }
  124. void printTSpath()
  125. {
  126.     cout<<"Topologically Sorted Path (after DFS) :"<<endl;
  127.     int i,counter=1;
  128.     //cout<<"Stack size : "<<result.size()<<endl;
  129.     //int stackSize = result.size();
  130.     for(i=1; i<=vertex; i++)
  131.     {
  132.         cout<<result.top()<<" -> ";
  133.         tSVertex[counter]=result.top();
  134.         counter++;
  135.         result.pop();
  136.     }
  137.     cout<<" End"<<endl;
  138. }
  139. void Initialize_Single_Source()
  140. {
  141.     cout<<"Enter source : ";
  142.     cin>>source;
  143.     int i;
  144.     for(i=1; i<=vertex; i++)
  145.     {
  146.         cost[i]=999999;
  147.         parent[i]=0;
  148.         //extra line
  149.         visited[i]=0;
  150.     }
  151.     cost[source]=0;
  152. }
  153. void relax(int u,int v)
  154. {
  155.     if( cost[v]> (cost[u]+weight_matrix[u][v]))
  156.     {
  157.         cost[v]=(cost[u]+weight_matrix[u][v]);
  158.         parent[v]=u;
  159.     }
  160. }
  161. void DAG()
  162. {
  163.     int i,j,k,counter=1;
  164.     for(i=1;i<=vertex;i++)
  165.     {
  166.         int node = tSVertex[counter];
  167.         for(j=1;j<=vertex;j++)
  168.         {
  169.             if(adjacency_matrix[node][j]==1)
  170.             {
  171.                 relax(node,j);
  172.             }
  173.         }
  174.         counter++;
  175.     }
  176. }
  177. void printCostAndPath()
  178. {
  179.     int i;
  180.     for(i=1; i<=vertex; i++)
  181.     {
  182.         cout<<"For node "<<i<<" : "<<endl;
  183.         if(cost[i]>=999999)
  184.         {
  185.             cout<<"Cost : "<<"Infinity"<<endl;
  186.         }
  187.         else
  188.         {
  189.             cout<<"Cost : "<<cost[i]<<endl;
  190.         }
  191.         cout<<"Path : End <- "<<i<<" <- ";
  192.         int l = parent[i];
  193.         while(true)
  194.         {
  195.             if(l==source||l==0)
  196.             {
  197.                 if(l==source)
  198.                 {
  199.                     cout<<l<<" <- ";
  200.                 }
  201.                 break;
  202.             }
  203.             else
  204.             {
  205.                 cout<<l<<" <- ";
  206.                 l=parent[l];
  207.  
  208.             }
  209.         }
  210.         cout<<" Start"<<endl;
  211.  
  212.     }
  213.  
  214.     //for minimum cost
  215.     int minCost = 999999;
  216.     int nodeSaver;
  217.     for(i=1; i<=vertex; i++)
  218.     {
  219.         // do not count if it is source node
  220.         if(i!=source)
  221.         {
  222.             if(cost[i]<minCost)
  223.             {
  224.                 minCost=cost[i];
  225.                 nodeSaver=i;
  226.             }
  227.         }
  228.     }
  229.     cout<<"From source node the minimum cost (without source node) is : "<<endl;
  230.     cout<<"Node number : "<<nodeSaver<<endl;
  231.     cout<<"Cost : "<<minCost<<endl;
  232.     cout<<"Path : End <- "<<nodeSaver<<" <- ";
  233.     int l = parent[nodeSaver];
  234.     while(true)
  235.     {
  236.         if(l==source||l==0)
  237.         {
  238.             if(l==source)
  239.             {
  240.                 cout<<l<<" <- ";
  241.                 break;
  242.             }
  243.         }
  244.         else
  245.         {
  246.             cout<<l<<" <- ";
  247.             l=parent[l];
  248.  
  249.         }
  250.     }
  251.     cout<<" Start"<<endl;
  252. }
  253. /*
  254. input:
  255. 6
  256. 10
  257. 1 2
  258. 5
  259. 2 3
  260. 2
  261. 3 4
  262. 7
  263. 4 5
  264. -1
  265. 5 6
  266. -2
  267. 2 4
  268. 6
  269. 4 6
  270. 1
  271. 1 3
  272. 3
  273. 3 5
  274. 4
  275. 3 6
  276. 2
  277. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement