_g_generate.c
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:9k
开发平台:

MultiPlatform

  1. #include <LEDA/array2.h>
  2. #include <LEDA/graph.h>
  3. #include <LEDA/ugraph.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. // some graph generators
  7. void complete_graph(graph& G, int n)
  8. { node v,w;
  9.   G.clear();
  10.   while (n--) G.new_node();
  11.   forall_nodes(v,G)
  12.      forall_nodes(w,G) 
  13.         G.new_edge(v,w);
  14. }
  15. void complete_ugraph(ugraph& G, int n)
  16. { int i,j;
  17.   node* V = new node[n];
  18.   G.clear();
  19.   for(i=0;i<n;i++) V[i] = G.new_node();
  20.   for(i=0;i<n;i++) 
  21.     for(j=i+1;j<n;j++) 
  22.        G.new_edge(V[i],V[j]);
  23. }
  24. void grid_graph(graph& G, int n) 
  25. { node_array<double> xcoord; 
  26.   node_array<double> ycoord;
  27.   grid_graph(G,xcoord,ycoord,n);
  28.  }
  29. void grid_graph(graph& G, node_array<double>& xcoord, 
  30.                           node_array<double>& ycoord, int n) 
  31. {
  32.   array2<node>  A(n,n);
  33.   node v;
  34.   int N = n*n;
  35.   int x;
  36.   int y;
  37.   double d = 1.0/(n+1);
  38.   G.clear();
  39.   xcoord.init(G,N,0);
  40.   ycoord.init(G,N,0);
  41.   for(y=0; y<n; y++)
  42.     for(x=0; x<n; x++)
  43.       { A(x,y) = v = G.new_node();
  44.         xcoord[v] = (x+1)*d;
  45.         ycoord[v] = (y+1)*d;
  46.        }
  47.   for(x=0; x<n; x++)
  48.     for(y=0; y<n; y++)
  49.        { if (x < n-1) G.new_edge(A(x,y),A(x+1,y));
  50.          if (y < n-1) G.new_edge(A(x,y),A(x,y+1));
  51.         }
  52. }
  53. void complete_bigraph(graph& G, int n1, int n2, list<node>& A, list<node>& B)
  54. { node v,w;
  55.   for(int a=0; a < n1; a++)  A.append(G.new_node());
  56.   for(int b=0; b < n2; b++)  B.append(G.new_node());
  57.   forall(v,A) 
  58.   {  forall(w,B) 
  59.        G.new_edge(v,w);
  60.    }
  61.  }
  62. void user_graph(graph& G)
  63. { int  n = read_int("|V| = ");
  64.   int  i,j;
  65.   node* V = new node[n];
  66.   for(j=0; j<n; j++) V[j] = G.new_node();
  67.   for(j=0; j<n; j++) 
  68.   { list<int> il;
  69.     int ok = false;
  70.     while (!ok)
  71.     { ok = true;
  72.       cout << "edges from [" << j << "] to: ";
  73.       il.read();
  74.       forall(i,il) 
  75.         if (i < 0 || i >= n) 
  76.         { ok=false;
  77.           cout << "illegal node " << i << "n";
  78.          }
  79.      }
  80.     forall(i,il) G.new_edge(V[j],V[i]);
  81.   }
  82.   G.print();
  83.   if (Yes("save graph ? ")) G.write(read_string("file: "));
  84. }
  85. void test_graph(graph& G)
  86.   G.clear();
  87.   char c;
  88.   do c = read_char("graph: f(ile) r(andom) c(omplete) p(lanar) u(ser): ");
  89.   while (c!='f' && c!='r' && c!='c' && c!='p'&& c!='u');   
  90.   switch (c) {
  91.    case 'f' : { G.read(read_string("file: "));
  92.                 break;
  93.                }
  94.    case 'u' : { user_graph(G);
  95.                 break;
  96.                }
  97.    case 'c' : { complete_graph(G,read_int("|V| = "));
  98.                 break;
  99.                }
  100.    case 'r' : { int n = read_int("|V| = ");
  101.                 int m = read_int("|E| = ");
  102.                 random_graph(G,n,m);
  103.                 break;
  104.                }
  105.    case 'p' : { random_planar_graph(G,read_int("|V| = "));
  106.                 break;
  107.                }
  108.    }//switch
  109. }
  110. void test_ugraph(ugraph& G)
  111.   G.clear();
  112.   char c;
  113.   do c = read_char("graph: f(ile) r(andom) c(omplete) p(lanar) u(ser): ");
  114.   while (c!='f' && c!='r' && c!='c' && c!='p'&& c!='u');   
  115.   int  i;
  116.   node v;
  117.   
  118.   switch (c) {
  119.   case 'f' : { G.read(read_string("file: "));
  120.                break;
  121.               }
  122.    case 'u' : { int  n = read_int("|V| = ");
  123.                 int  j = 0;
  124.                 node* V = new node[n];
  125.                 for(i=0; i<n; i++) V[i] = G.new_node();
  126.                 forall_nodes(v,G)
  127.                   { list<int> il;
  128.                     cout << "edges from " << j++ << " to: ";
  129.                     il.read();
  130.                     forall(i,il) 
  131.                       if (i >= 0 && i < n) G.new_edge(v,V[i]);
  132.                       else cerr << "illegal node " << i << " (ignored)n";
  133.                    }
  134.                 G.print();
  135.                 if (Yes("save graph ? ")) G.write(read_string("file: "));
  136.                 break;
  137.                }
  138.    case 'c' : { int n = read_int("|V| = ");
  139.                 complete_graph(G,n);
  140.                 break;
  141.                }
  142.    case 'r' : { int n = read_int("|V| = ");
  143.                 int m = read_int("|E| = ");
  144.                 random_graph(G,n,m);
  145.                 break;
  146.                }
  147.    }//switch
  148. }
  149. void test_bigraph(graph& G, list<node>& A, list<node>& B)
  150. {
  151.   int  a,b,n1,n2;
  152.   char c;
  153.   do c = read_char("bipartite graph: f(ile) r(andom) c(omplete) u(ser): ");
  154.   while (c!='f' && c!='r' && c!='c' && c!='u');   
  155.   A.clear();
  156.   B.clear();
  157.   G.clear();
  158.   if (c!='f') 
  159.    { n1 = read_int("|A| = ");
  160.      n2 = read_int("|B| = ");
  161.     }
  162.   
  163.   
  164.   switch (c) {
  165.   case 'f' : { G.read(read_string("file: "));
  166.                node v;
  167.                forall_nodes(v,G) 
  168.                if (G.outdeg(v) > 0) A.append(v);
  169.                else B.append(v);
  170.                break;
  171.               }
  172.    case 'u' : { node* AV = new node[n1+1];
  173.                 node* BV = new node[n2+1];
  174.                 for(a=1; a<=n1; a++)  A.append(AV[a] = G.new_node());
  175.                 for(b=1; b<=n2; b++)  B.append(BV[b] = G.new_node());
  176.                 for(a=1; a<=n1; a++)
  177.                 { list<int> il;
  178.                   cout << "edges from " << a << " to: ";
  179.                   il.read();
  180.                   forall(b,il) 
  181.                     if (b<=n2) G.new_edge(AV[a],BV[b]);
  182.                     else break;
  183.                   if (b>n2) break;
  184.                  }
  185.                 break;
  186.                }
  187.    case 'c' : complete_bigraph(G,n1,n2,A,B);
  188.               break;
  189.    case 'r' : { int m = read_int("|E| = ");
  190.                 random_bigraph(G,n1,n2,m,A,B);
  191.                 break;
  192.                }
  193.        } // switch
  194. }
  195. void cmdline_graph(graph& G, int argc, char** argv)
  196.   // construct graph from cmdline arguments
  197.   if (argc == 1)           // no arguments 
  198.      { test_graph(G);
  199.        return;
  200.       }
  201.   else 
  202.      if (argc == 2)       // one argument
  203.         { if (isdigit(argv[1][0])) 
  204.              { cout << "complete graph |V| = " << argv[1];
  205.                newline;
  206.                newline;
  207.                complete_graph(G,atoi(argv[1]));
  208.               }
  209.           else 
  210.              { cout << "reading graph from file " << argv[1];
  211.                newline;
  212.                newline;
  213.                G.read(argv[1]);
  214.               }
  215.           return;
  216.          }
  217.      else
  218.         if (argc == 3 && isdigit(argv[1][0]) && isdigit(argv[1][0])) 
  219.            { cout << "random graph |V| = " << argv[1] << "  |E| = " << argv[2];
  220.              newline;
  221.              newline;
  222.              random_graph(G,atoi(argv[1]),atoi(argv[2]));
  223.              return;
  224.             }
  225.   error_handler(1,"cmdline_graph: illegal arguments");
  226. }
  227. //------------------------------------------------------------------------------
  228. // triangulated planar graph
  229. //------------------------------------------------------------------------------
  230. #include <LEDA/d_array.h>
  231. class triang_point {
  232. public:
  233. double x;
  234. double y;
  235. LEDA_MEMORY(triang_point)
  236. triang_point(double a=0, double b = 0) { x = a; y = b; }
  237. triang_point(const triang_point& p)    { x = p.x; y = p.y; }
  238. ~triang_point() {};
  239. bool operator==(const triang_point& p) { return x==p.x && y==p.y; }
  240. friend bool right_turn(const triang_point& a, const triang_point& b, const triang_point& c)
  241. { return (a.y-b.y)*(a.x-c.x)+(b.x-a.x)*(a.y-c.y) > 0; }
  242. friend bool left_turn(const triang_point& a, const triang_point& b, const triang_point& c)
  243. { return (a.y-b.y)*(a.x-c.x)+(b.x-a.x)*(a.y-c.y) < 0; }
  244. };
  245. inline int compare(const triang_point& p, const triang_point& q)
  246. { int c = compare(p.x,q.x);
  247.   if (c==0) c = compare(p.y,q.y);
  248.   return c;
  249.  }
  250. inline void Print(const triang_point&, ostream&) {}
  251. inline void Read(triang_point&, istream&) {}
  252. #if !defined(__TEMPLATE_FUNCTIONS__)
  253. LEDA_TYPE_PARAMETER(triang_point)
  254. #endif
  255. void triangulated_planar_graph(graph& G, node_array<double>& xcoord,
  256.                                          node_array<double>& ycoord, int n)
  257.   list<triang_point> L;
  258.   double x;
  259.   double y;
  260.   while(n--)
  261.   { rand_int >> x; 
  262.     rand_int >> y;
  263.     L.append(triang_point(x,y));
  264.    }
  265.   L.sort();  // sort triang_points lexicographically
  266.   list<triang_point> CH;
  267.   list_item last;
  268.   triang_point p,q;
  269.   // eliminate multiple triang_points
  270.   list_item it;
  271.   forall_items(it,L)
  272.   { list_item it1 = L.succ(it);
  273.     while (it1 != nil && L[it1] == L[it])
  274.     { L.del(it1);
  275.       it1 = L.succ(it);
  276.      }
  277.    }
  278.   n = L.length();
  279.   d_array<triang_point,node> V(nil);
  280.   xcoord.init(G,n,0);
  281.   ycoord.init(G,n,0);
  282.   forall(q,L) 
  283.   { node v = G.new_node();
  284.     xcoord[v] = q.x;
  285.     ycoord[v] = q.y;
  286.     V[q] = v;
  287.    }
  288.   // initialize convex hull with first two points
  289.   p = L.pop();
  290.   CH.append(p);
  291.   while (L.head() == p) L.pop();
  292.   q = L.pop();
  293.   last = CH.append(q);
  294.   G.new_edge(V[p],V[q]);
  295.   // scan remaining points
  296.   forall(p,L)
  297.   {
  298.     node v = V[p];
  299.     G.new_edge(v,V[CH[last]]);
  300.     // compute upper tangent (p,up)
  301.     list_item up = last;
  302.     list_item it = CH.cyclic_succ(up);
  303.     while (left_turn(CH[it],CH[up],p))
  304.     { up = it;
  305.       it = CH.cyclic_succ(up);
  306.       G.new_edge(v,V[CH[up]]);
  307.      }
  308.     // compute lower tangent (p,low)
  309.     list_item low = last;
  310.     it = CH.cyclic_pred(low);
  311.     while (right_turn(CH[it],CH[low],p))
  312.     { low = it;
  313.       it = CH.cyclic_pred(low);
  314.       G.new_edge(v,V[CH[low]]);
  315.      }
  316.     // remove all points between up and low
  317.     if (up != low)
  318.     { it = CH.cyclic_succ(low);
  319.       while (it != up)
  320.       { CH.del(it);
  321.         it = CH.cyclic_succ(low);
  322.        }
  323.      }
  324.     // insert new point
  325.     last = CH.insert(p,low);
  326.    }
  327. }
  328. void triangulated_planar_graph(graph& G, int m)
  329. { node_array<double> xcoord;
  330.   node_array<double> ycoord;
  331.   triangulated_planar_graph(G,xcoord,ycoord,m);
  332.  }