Main Page   Class Hierarchy   Compound List   File List   Contact   Download   Symbolic Constraints   Examples  

broadcast.c

00001 #include<scil/scil.h>
00002 #include<scil/constraints/spantree.h>
00003 #include<scil/constraints/stronglyconnected.h>
00004 #include<LEDA/graph.h>
00005 #include<LEDA/graphwin.h>
00006 #include<LEDA/graph_alg.h>
00007 #include<LEDA/graph_misc.h>
00008 #include<LEDA/delaunay.h>
00009 #include<LEDA/string.h>
00010 #include<LEDA/node_pq.h>
00011 #include<LEDA/stream.h>
00012 
00013 using namespace LEDA;
00014 using namespace SCIL;
00015 using namespace std;
00016 
00017 #define MAX_DOUBLE 10e+300
00018 #define MAX_INT 1000000
00019 
00020 #include"sym_connect.c"
00021 #include"asym_connect.c"
00022 
00023 void triang_points(GraphWin& GW) 
00024 {
00025   graph& G=GW.get_graph();
00026   int n=G.number_of_nodes();
00027   if (n!=0) {
00028   
00029     GW.set_flush(false);
00030 
00031     point_set T;  
00032     node_array<node> tr(T,n,nil);
00033     node_array<int> num(T,n,0);
00034     node v,v1;
00035     int i=0;
00036 
00037     double x,y;
00038     forall_nodes(v1,G) 
00039     {
00040       x=GW.get_position(v1).xcoord();
00041       y=GW.get_position(v1).ycoord();
00042       point p(x,y);
00043       v=T.insert(p);
00044       tr[v]=v1;
00045       num[v]=i++;
00046      }
00047 
00048     G.del_all_edges();
00049     GW.update_graph();
00050     edge e;
00051     forall_edges(e,T) if (num[T.source(e)]<num[T.target(e)])
00052                         GW.new_edge(tr[T.source(e)],tr[T.target(e)]);
00053     GW.redraw();
00054     GW.set_flush(true);
00055     GW.redraw();
00056   }
00057 }
00058 
00059 void complete_points(GraphWin& GW) 
00060 {
00061   graph& G=GW.get_graph();
00062   int n=G.number_of_nodes();
00063   if (n!=0) {
00064   
00065     GW.set_flush(false);
00066 
00067     G.del_all_edges();
00068 
00069     node u, v;
00070     forall_nodes(u, G) forall_nodes(v,G) if(u>v) {
00071       G.new_edge(u,v);
00072     };
00073 
00074     GW.update_graph();
00075 
00076     GW.redraw();
00077     GW.set_flush(true);
00078     GW.redraw();
00079   }
00080 }
00081 
00082 void generate_del(GraphWin& GW) 
00083 {
00084   graph& G=GW.get_graph();
00085   if (!G.empty()) GW.clear_graph();
00086 
00087   int n=10;
00088 
00089   panel p;
00090   p.int_item("Number of nodes : ",n);
00091   p.open(GW.get_window());
00092  
00093   random_source ran;
00094 
00095   double x,y;
00096   for(int i=0;i<n;i++) 
00097   { ran >> x >> y;
00098     x=(x+0.08)*(GW.get_xmax()-GW.get_xmin())*0.9+GW.get_xmin();
00099     y=(y+0.08)*(GW.get_ymax()-GW.get_ymin())*0.9+GW.get_ymin();
00100     point p1(x,y);
00101     GW.new_node(p1);
00102    }
00103   triang_points(GW);
00104 }
00105 
00106 
00107 
00108 void make_tests(GraphWin& GW) {
00109 
00110   ofstream tr("test_results");
00111   double x,y;
00112   edge e;
00113   list<edge> T;
00114   int i=4;
00115   int j=0;
00116   int l;
00117   
00118   while(i<6) {
00119     while(j<10) {
00120 
00121       graph G;
00122 
00123       LEDA::string s("random_%d_%d.pts",i*5,j);
00124 
00125       cout<<"Instance: "<<s<<endl;
00126 
00127       ifstream outp(s);
00128       outp>>l;
00129 
00130       node_map<point> P(G);
00131       node u,v;
00132 
00133       node_map<int> N(G);
00134 
00135       //GW.clear_graph();
00136       for(int k=0; k<i*5; k++) {
00137         outp >> x;
00138         outp >> y;
00139         //x=(x+0.08)*(GW.get_xmax()-GW.get_xmin())*0.9+GW.get_xmin();
00140         //y=(y+0.08)*(GW.get_ymax()-GW.get_ymin())*0.9+GW.get_ymin();
00141         point p1(x,y);
00142         u=G.new_node();
00143         P[u]=p1;
00144         N[u]=k;
00145       }
00146 
00147 #define COMPLETE_GRAPH
00148 
00149       #ifdef COMPLETE_GRAPH
00150       // COMPLETE GRAPH
00151       forall_nodes(u,G) forall_nodes(v, G) if(u>v) {
00152         G.new_edge(u,v);
00153       };
00154       #else
00155       // DELAUNAY GRAPH
00156       point_set TD;  
00157       node_array<node> tr(TD,G.number_of_nodes(),u);
00158       node v1;
00159 
00160       forall_nodes(v1,G) {
00161         v=TD.insert(P[v1]);
00162         tr[v]=v1;
00163       }
00164 
00165       forall_edges(e,TD) if (source(e)<target(e))
00166         G.new_edge(tr[source(e)],tr[target(e)]);
00167       #endif
00168 
00169       edge_array<double> Cost(G);
00170       forall_edges(e, G) { 
00171         Cost[e]=P[source(e)].distance(P[target(e)]); 
00172       }
00173 
00174       ComputeBroadcast(G, Cost, T, GW);
00175       LEDA::string so("random_%d_%d.opt",i*5,j);
00176       ofstream opts(so);
00177       opts<<"p edges "<<G.number_of_nodes()<<" "<<T.size()<<endl;
00178       forall(e, T) opts<<"e "<<N[source(e)]<<" "<<N[target(e)]<<" "<<Cost[e]<<endl;
00179       T.clear();
00180 
00181       list<edge> MST=MIN_SPANNING_TREE(G, Cost);
00182       LEDA::string sm("random_%d_%d.mst",i*5,j);
00183       ofstream msts(sm);
00184       msts<<"p edges "<<G.number_of_nodes()<<" "<<T.size()<<endl;
00185       forall(e, MST) msts<<"e "<<N[source(e)]<<" "<<N[target(e)]<<" "<<Cost[e]<<endl;
00186 
00187       if(x<0) for(;;);
00188       j++;
00189     };
00190     i++; j=0;
00191   };
00192 };
00193 
00194 void make_instances(GraphWin& GW) {
00195 
00196   random_source RS;
00197   double x,y;
00198 
00199   int i=9;
00200   int j=0;
00201   
00202   while(i<21) {
00203     while(j<50) {
00204 
00205       graph G;
00206 
00207       LEDA::string s("random_%d_%d.pts",i*5,j);
00208 
00209       cout<<"Instance: "<<s<<endl;
00210 
00211       ofstream outp(s);
00212       outp<<i*5<<endl;
00213 
00214       for(int k=0; k<i*5; k++) {
00215         RS>>x;
00216         RS>>y;
00217         outp << ((int) (x*10000)) <<" ";
00218         outp << ((int) (y*10000)) <<endl;
00219       }
00220       j++;
00221     };
00222     i++; j=0;
00223   };
00224 };
00225 
00226 void read_graph(GraphWin& GW) {
00227   cout<<"File Name : ";
00228   LEDA::string s;
00229   cin>>s;
00230 
00231   ifstream inp(s);
00232   int i,x,y;
00233   inp>>i;
00234   cout<<i<<endl;
00235 
00236   for(int j=0; j<i; j++) {
00237     inp>>x;
00238     inp>>y;
00239     cout<<x<<" "<<y<<endl;
00240     GW.new_node(point(x,y));
00241   };
00242   cout<<"reading done\n";
00243 }
00244 
00245 
00246 int main()
00247 {
00248   GraphWin GW;
00249 
00250 // Setup of the graph window
00251   GW.set_node_width(8);
00252   GW.set_node_height(8);
00253   GW.set_flush(true);
00254   GW.set_edge_direction(undirected_edge);
00255 
00256 // Additional functionality of the graph window; the implementation is hidden
00257   int gn=gw_add_menu(GW,"Generation");
00258   gw_add_simple_call(GW,triang_points,"Triangulate defined points",gn);
00259   gw_add_simple_call(GW,complete_points,"Make complete graph",gn);
00260   gw_add_simple_call(GW,generate_del, "Generate random Delaunay-Graph",gn);
00261   gw_add_simple_call(GW,make_tests, "Make Test", gn);
00262   gw_add_simple_call(GW,make_instances, "Make Instances", gn);
00263   gw_add_simple_call(GW,read_graph, "Read Graph", gn);
00264 
00265   GW.display();
00266 
00267   while(GW.edit())
00268   {
00269     graph& G=GW.get_graph();
00270 
00271     edge e;
00272     edge_array<double> C(G);
00273     forall_edges(e, G) 
00274       C[e]=GW.get_position(source(e)).distance(GW.get_position(target(e)));
00275 
00276     list<edge> T;
00277     ComputeBroadcast(G, C, T, GW);
00278 
00279     node_array<double> W(G,0);
00280     forall(e, T) { 
00281       W[source(e)]=leda_max(W[source(e)], C[e]);
00282       W[target(e)]=leda_max(W[target(e)], C[e]);
00283     };
00284 
00285     forall_edges(e,G) {
00286       GW.set_color(e, black);
00287       if((C[e]<=W[source(e)]) && (C[e]<=W[target(e)]))
00288         GW.set_color(e,red);
00289     }
00290     
00291     node u;
00292     forall_nodes(u, G) {
00293       GW.get_window().draw_circle(GW.get_position(u), W[u], green);
00294     };
00295   }
00296   return 0;
00297 }
00298 

Generated on Tue Nov 16 15:18:13 2004 for SCIL by doxygen1.2.16