upsidedown

prims algo

Apr 5th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. #include<iostream.h>
  2.  
  3. class prims
  4. {
  5. private:
  6.     int n;
  7.     int graph_edge[250][4];
  8.     int g;
  9.     int tree_edge[250][4];
  10.     int t;
  11.     int s;
  12.  
  13.  
  14.     int T1[50],t1;
  15.     int T2[50],t2;
  16.  
  17. public:
  18.     void input();
  19.     int findset(int);
  20.     void algorithm();
  21.     void output();
  22. };
  23. void prims::input()
  24. {
  25.     cout<<"Enter the no. of nodes in the undirected weighted graph\n";
  26.     cin>>n;
  27.    
  28.     g=0;
  29.    
  30.     cout<<"Enter the weights for the following edges\n";
  31.     for(int i=1;i<=n;i++)
  32.     {
  33.         for(int j=i+1;j<=n;j++)
  34.         {
  35.             cout<<" < "<<i<<" , "<<j<<" > ";
  36.             int w;
  37.             cin>>w;
  38.             if(w!=0)
  39.             {
  40.                 g++;
  41.  
  42.                 graph_edge[g][1]=i;
  43.                 graph_edge[g][2]=j;
  44.                 graph_edge[g][3]=w;
  45.             }
  46.         }
  47.     }
  48.  
  49.  
  50.  
  51.     cout<<"\n\nThe edges in the given graph are:\n";
  52.     for(i=1;i<=g;i++)
  53.     cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" > :"<<graph_edge[i][3]<<endl;
  54. }
  55.  
  56. int prims::findset(int x)
  57. {
  58.     for(int i=1;i<=t1;i++)
  59.         if(x==T1[i])
  60.             return 1;
  61.  
  62.     for(i=1;i<=t2;i++)
  63.         if(x==T2[i])
  64.             return 2;
  65.         return -1;
  66. }
  67.  
  68. void prims::algorithm()
  69. {
  70.     t=0;
  71.  
  72.     t1=1;
  73.     T1[1]=1;
  74.  
  75.     t2=n-1;
  76.     int i;
  77.     for(i=1;i<=n-1;i++)
  78.         T2[i]=i+1;
  79.     while(g!=0 && t!=n-1)
  80.     {
  81.  
  82.         int min=9999;
  83.         int p;
  84.         int u,v,w;
  85.         for(i=1;i<=g;i++)
  86.         {
  87.             bool flag1=false,flag2=false;
  88.  
  89.  
  90.             if(findset(graph_edge[i][1])!=findset(graph_edge[i][2]))
  91.             {
  92.                 if(min>graph_edge[i][3])
  93.                 {
  94.                     min=graph_edge[i][3];
  95.                     u=graph_edge[i][1];
  96.                     v=graph_edge[i][2];
  97.                     w=graph_edge[i][3];
  98.    
  99.                     p=i;
  100.                 }
  101.             }
  102.         }
  103.  
  104.  
  105.  
  106.         cout<<"The edge included in the tree is :";
  107.         cout<<" < "<<u<<" , "<<v<<" > "<<endl;
  108.  
  109.  
  110.  
  111.         for(int l=p;l<g;l++)
  112.         {
  113.             graph_edge[l][1]=graph_edge[l+1][1];
  114.             graph_edge[l][2]=graph_edge[l+1][2];
  115.             graph_edge[l][3]=graph_edge[l+1][3];
  116.         }
  117.         t++;
  118.         tree_edge[t][1]=u;
  119.         tree_edge[t][2]=v;
  120.         tree_edge[t][3]=w;
  121.  
  122.  
  123.         t1++;
  124.  
  125.         int m;
  126.         if(findset(v)==2)
  127.         {
  128.             T1[t1]=v;
  129.             m=v;
  130.         }
  131.         else
  132.             if(findset(u)==2)
  133.             {
  134.                 T1[t1]=u;
  135.                 m=u;
  136.             }
  137.  
  138.         int x;
  139.         for(x=1;T2[x]!=m;x++);
  140.             for(;x<t2;x++)
  141.                 T2[x]=T2[x+1];
  142.         int k;
  143.         cout<<"NOW\nT1 :: ";
  144.         for(k=1;k<=t1;k++)
  145.             cout<<T1[k]<<"  ";
  146.         cout<<endl;
  147.  
  148.         cout<<"T2 :: ";
  149.         for(k=1;k<=t2;k++)
  150.             cout<<T2[k]<<" ";
  151.         cout<<endl;
  152.  
  153.         cout<<"The graph edges are ::\n";
  154.         for(i=1;i<=g;i++)
  155.             cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" > ::"<<graph_edge[i][3]<<endl;
  156.  
  157.         cout<<endl<<endl;  
  158.  
  159.     }
  160. }
  161.  
  162. void prims::output()
  163. {
  164.     cout<<"\nThe selected edges are ::\n";
  165.     for(int i=1;i<=t;i++)
  166.         cout<<" < "<<tree_edge[i][1]<<" , "<<tree_edge[i][2]<<" > ::"<<tree_edge[i][3]<<endl;
  167. }
  168. int main()
  169. {
  170.     prims obj;
  171.     obj.input();
  172.     obj.algorithm();
  173.     obj.output();
  174.     return 0;
  175. }
  176.  
  177. OUTPUT:
  178. Enter the no. of nodes in the undirected weighted graph
  179. 6
  180. Enter the weights for the following edges
  181.  < 1 , 2 > 12
  182.  < 1 , 3 > 0
  183.  < 1 , 4 > 0
  184.  < 1 , 5 > 15
  185.  < 1 , 6 > 17
  186.  < 2 , 3 > 1
  187.  < 2 , 4 > 2
  188.  < 2 , 5 > 0
  189.  < 2 , 6 > 3
  190.  < 3 , 4 > 6
  191.  < 3 , 5 > 0
  192.  < 3 , 6 > 0
  193.  < 4 , 5 > 14
  194.  < 4 , 6 > 10
  195.  < 5 , 6 > 19
  196.  
  197.  
  198. The edges in the given graph are:
  199.  < 1 , 2 > :12
  200.  < 1 , 5 > :15
  201.  < 1 , 6 > :17
  202.  < 2 , 3 > :1
  203.  < 2 , 4 > :2
  204.  < 2 , 6 > :3
  205.  < 3 , 4 > :6
  206.  < 4 , 5 > :14
  207.  < 4 , 6 > :10
  208.  < 5 , 6 > :19
  209. The edge included in the tree is : < 1 , 2 >
  210. NOW
  211. T1 :: 1  2
  212. T2 :: 3 4 5 6 6
  213. The graph edges are ::
  214.  < 1 , 5 > ::15
  215.  < 1 , 6 > ::17
  216.  < 2 , 3 > ::1
  217.  < 2 , 4 > ::2
  218.  < 2 , 6 > ::3
  219.  < 3 , 4 > ::6
  220.  < 4 , 5 > ::14
  221.  < 4 , 6 > ::10
  222.  < 5 , 6 > ::19
  223.  < 5 , 6 > ::19
  224.  
  225.  
  226. The edge included in the tree is : < 2 , 3 >
  227. NOW
  228. T1 :: 1  2  3
  229. T2 :: 4 5 6 6 6
  230. The graph edges are ::
  231.  < 1 , 5 > ::15
  232.  < 1 , 6 > ::17
  233.  < 2 , 4 > ::2
  234.  < 2 , 6 > ::3
  235.  < 3 , 4 > ::6
  236.  < 4 , 5 > ::14
  237.  < 4 , 6 > ::10
  238.  < 5 , 6 > ::19
  239.  < 5 , 6 > ::19
  240.  < 5 , 6 > ::19
  241.  
  242.  
  243. The edge included in the tree is : < 2 , 4 >
  244. NOW
  245. T1 :: 1  2  3  4
  246. T2 :: 5 6 6 6 6
  247. The graph edges are ::
  248.  < 1 , 5 > ::15
  249.  < 1 , 6 > ::17
  250.  < 2 , 6 > ::3
  251.  < 3 , 4 > ::6
  252.  < 4 , 5 > ::14
  253.  < 4 , 6 > ::10
  254.  < 5 , 6 > ::19
  255.  < 5 , 6 > ::19
  256.  < 5 , 6 > ::19
  257.  < 5 , 6 > ::19
  258.  
  259.  
  260. The edge included in the tree is : < 2 , 6 >
  261. NOW
  262. T1 :: 1  2  3  4  6
  263. T2 :: 5 6 6 6 6
  264. The graph edges are ::
  265.  < 1 , 5 > ::15
  266.  < 1 , 6 > ::17
  267.  < 3 , 4 > ::6
  268.  < 4 , 5 > ::14
  269.  < 4 , 6 > ::10
  270.  < 5 , 6 > ::19
  271.  < 5 , 6 > ::19
  272.  < 5 , 6 > ::19
  273.  < 5 , 6 > ::19
  274.  < 5 , 6 > ::19
  275.  
  276.  
  277. The edge included in the tree is : < 4 , 5 >
  278. NOW
  279. T1 :: 1  2  3  4  6  5
  280. T2 :: 6 6 6 6 6
  281. The graph edges are ::
  282.  < 1 , 5 > ::15
  283.  < 1 , 6 > ::17
  284.  < 3 , 4 > ::6
  285.  < 4 , 6 > ::10
  286.  < 5 , 6 > ::19
  287.  < 5 , 6 > ::19
  288.  < 5 , 6 > ::19
  289.  < 5 , 6 > ::19
  290.  < 5 , 6 > ::19
  291.  < 5 , 6 > ::19
  292.  
  293.  
  294.  
  295. The selected edges are ::
  296.  < 1 , 2 > ::12
  297.  < 2 , 3 > ::1
  298.  < 2 , 4 > ::2
  299.  < 2 , 6 > ::3
  300.  < 4 , 5 > ::14
  301. Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment