speed.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 6k
Development Platform:

MultiPlatform

  1. #include <LEDA/graph.h>
  2. #include <LEDA/ugraph.h>
  3. #include <LEDA/graph_alg.h>
  4. main(int argc, char** argv)
  5. {
  6.   GRAPH<int,int> G0;
  7.   float T = used_time();
  8.   int n;
  9.   int m;
  10.   if (argc > 1)  // file argument
  11.     { cout << "read  graph          " << flush;
  12.       G0.read(argv[1]);
  13.       cout << string("%6.2f secn",used_time(T));
  14.       n = G0.number_of_nodes();
  15.       m = G0.number_of_edges();
  16.      }
  17.   else
  18.     { n = read_int("|V| = ");
  19.       m = read_int("|E| = ");
  20.       rand_int.set_seed(n*m);
  21.       T = used_time();
  22.       cout << "build graph          " << flush;
  23.       random_graph(G0,n,m);
  24.       cout << string("%6.2f secn",used_time(T));
  25.       G0.write("graph.1");
  26.      }
  27.   GRAPH<int,int> G1 = G0;
  28. cout << "n = " << G1.number_of_nodes() << endl;
  29. cout << "m = " << G1.number_of_edges() << endl;
  30. newline;
  31.   random_source ran(10); // random 10 bit numbers
  32.   ran.set_seed(n*m);
  33.   float T0 = used_time();
  34.   T = T0;
  35.   cout << "build ugraph         " << flush;
  36.   UGRAPH<int,int> U = G1;
  37.   cout << string("%6.2f secn",used_time(T));
  38.   cout << "build bi-graph       " << flush;
  39.   GRAPH<int,int> G2;
  40.   list<node> A;
  41.   list<node> B;
  42.   random_bigraph(G2,n/2,n/2,m,A,B);
  43.   cout << string("%6.2f secn",used_time(T));
  44.   node s = G1.first_node();
  45.   node t = G1.last_node();
  46.   cout << "node/edge arrays     " << flush;
  47.   edge_array<int>     cost1(G1);
  48.   edge_array<double>  cost2(G1);
  49.   node_array<int>     dist1(G1);
  50.   node_array<double>  dist2(G1);
  51.   node_array<edge>    pred(G1);
  52.   edge_array<int>     flow1(G1);
  53.   edge_array<double>  flow2(G1);
  54.   node_array<int>  ord(G1);
  55.   node_array<int>  compnum(G1);
  56.   node_array<bool> reached(G1,false);
  57.   node_array<int>  dfs_num(G1);
  58.   node_array<int>  comp_num(G1);
  59.   node_array<int>  layer(G1,-1);
  60.   edge_array<int>    cost3(G2);
  61.   edge_array<double> cost4(G2);
  62.   node_array<int>  compnum1(U);
  63.   cout << string("%6.2f secn",used_time(T));
  64.   edge e;
  65.   cout << "assign edge labels   " << flush;
  66.   forall_edges(e,G1) cost2[e] = cost1[e] = G1[e] = ran();
  67.   forall_edges(e,G2) cost4[e] = cost3[e] = G2[e] = ran();
  68.   cout << string("%6.2f secn",used_time(T));
  69.   newline;
  70.   cout << "TOPSORT              " << flush;
  71.   TOPSORT(G1,ord); 
  72.   cout << string("%6.2f secn",used_time(T));
  73.   cout << "DFS                  " << flush;
  74.   DFS(G1,s,reached);
  75.   cout << string("%6.2f secn",used_time(T));
  76.   cout << "DFS_NUM              " << flush;
  77.   DFS_NUM(G1,dfs_num,comp_num);
  78.   cout << string("%6.2f secn",used_time(T));
  79.   cout << "BFS                  " << flush;
  80.   BFS(G1,G1.first_node(),layer);
  81.   cout << string("%6.2f secn",used_time(T));
  82.   cout << "COMPONENTS           " << flush;
  83.   int c = COMPONENTS(G1,compnum);
  84.   cout << string("%6.2f sec  c = %d",used_time(T),c) << endl;
  85.   cout << "COMPONENTS1          " << flush;
  86.   c = COMPONENTS1(G1,compnum);
  87.   cout << string("%6.2f sec  c = %d",used_time(T),c) << endl;
  88.   cout << "STRONG_COMPONENTS    " << flush;
  89.   c = STRONG_COMPONENTS(G1,compnum);
  90.   cout << string("%6.2f sec  c = %d",used_time(T),c) << endl;
  91.   cout << "SPANNING_TREE        " << flush;
  92.   list<edge> EL = SPANNING_TREE(G1);
  93.   cout << string("%6.2f sec |T| = %d",used_time(T),EL.length()) << endl;
  94.   cout << "MS_TREE<int>         " << flush;
  95.   EL = MIN_SPANNING_TREE(G1,cost1);
  96.   c = 0;
  97.   forall(e,EL) c += cost1[e]; 
  98.   cout << string("%6.2f sec |T| = %d  W(T) = %d",used_time(T),EL.length(),c);
  99.   cout << endl;
  100.   cout << "MS_TREE<double>      " << flush;
  101.   EL = MIN_SPANNING_TREE(G1,cost2);
  102.   double c2 = 0;
  103.   forall(e,EL) c2 += cost2[e]; 
  104.   cout << string("%6.2f sec |T| = %d  W(T) = %.2f",used_time(T),EL.length(),c2);
  105.   cout << endl;
  106.   cout << "DIJKSTRA <int>       " << flush;
  107.   DIJKSTRA(G1,s,cost1,dist1,pred);
  108.   cout << string("%6.2f sec  d  = %d",used_time(T),dist1[t]) << endl;
  109.   cout << "DIJKSTRA <double>    " << flush;
  110.   DIJKSTRA(G1,s,cost2,dist2,pred);
  111.   cout << string("%6.2f sec  d  = %.2f",used_time(T),dist2[t]) << endl;
  112.   cout << "BELLMAN_FORD<int>    " << flush;
  113.   BELLMAN_FORD(G1,s,cost1,dist1,pred);
  114.   cout << string("%6.2f sec  d  = %d",used_time(T),dist1[t]) << endl;
  115.   cout << "BELLMAN_FORD<double> " << flush;
  116.   BELLMAN_FORD(G1,s,cost2,dist2,pred);
  117.   cout << string("%6.2f sec  d  = %.2f",used_time(T),dist2[t]) << endl;
  118.   if (G1.number_of_nodes() < 50)
  119.   { node_matrix<int> M(G1);
  120.     cout << "ALL PAIRS<int>       " << flush;
  121.     ALL_PAIRS_SHORTEST_PATHS(G1,cost1,M);
  122.     cout << string("%6.2f secn",used_time(T));
  123.    }
  124.   cout << "MAX FLOW<int>        " << flush;
  125.   int f1 = MAX_FLOW(G1,s,t,cost1,flow1) ;
  126.   cout << string("%6.2f sec  f  = %d",used_time(T),f1) << endl;
  127.   cout << "MAX FLOW<double>     " << flush;
  128.   double f2 = MAX_FLOW(G1,s,t,cost2,flow2) ;
  129.   cout << string("%6.2f sec  f  = %.2f",used_time(T),f2) << endl;
  130.   T = used_time();
  131.   cout << "MC  MATCHING         " << flush;
  132.   list<edge> M1 = MAX_CARD_MATCHING(G1);
  133.   cout << string("%6.2f sec |M| = %d",used_time(T),M1.length()) << endl;
  134. /*
  135.   list<edge> R = Make_Bidirected(G1);
  136.   GRAPH<int,int> G4 = G1;
  137.   edge y;
  138.   forall(y,R) G1.del_edge(y);
  139.   cout << "STATIC MC  MATCHING  " << flush;
  140.   list<edge> M5 = STATIC_MAX_CARD_MATCHING(G4);
  141.   cout << string("%6.2f sec |M| = %d",used_time(T),M5.length()) << endl;
  142. */
  143.   cout << "MCB MATCHING         " << flush;
  144.   list<edge> M2 = MAX_CARD_BIPARTITE_MATCHING(G2,A,B);
  145.   cout << string("%6.2f sec |M| = %d",used_time(T), M2.length()) << endl;
  146.   
  147.   cout << "MWB MATCHING<int>    " << flush;
  148.   list<edge> M3 = MAX_WEIGHT_BIPARTITE_MATCHING(G2,A,B,cost3);
  149.   int W1 = 0;
  150.   forall(e,M3) W1 += cost3[e];
  151.   cout << string("%6.2f sec |M| = %d  W(M) = %d",used_time(T), M3.length(),W1);
  152.   cout << endl;
  153.   cout << "MWB MATCHING<double> " << flush;
  154.   list<edge> M4 = MAX_WEIGHT_BIPARTITE_MATCHING(G2,A,B,cost4);
  155.   double W2 = 0;
  156.   forall(e,M4) W2 += cost4[e];
  157.   cout << string("%6.2f sec |M| = %d  W(M) = %.2f",used_time(T),M4.length(),W2);
  158.   cout << endl;
  159.   
  160.   
  161.   if (G1.number_of_nodes() < 500)
  162.   { cout << "TRANSITIVE_CLOSURE   " << flush;
  163.     graph clos = TRANSITIVE_CLOSURE(G1);
  164.     cout << string("%6.2f sec |E| = %d",used_time(T),clos.number_of_edges()) << endl;
  165.   
  166.    }
  167.   
  168.   newline;
  169.   cout << "TOTAL TIME           ";
  170.   cout << string("%6.2f secn",used_time(T0));
  171.   return 0;
  172. }