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

steiner.c

00001 #include<LEDA/graph.h>
00002 #include<LEDA/graphwin.h>
00003 #include<LEDA/graph_misc.h>
00004 #include<LEDA/delaunay.h>
00005 #include<LEDA/stream.h>
00006 #include<LEDA/string.h>
00007 
00008 using namespace LEDA;
00009 using namespace std;
00010 
00011 #include"compute_steiner.c"
00012 
00013 double string_to_double(const LEDA::string& s) {
00014   LEDA::string_istream s1(s.cstring()); //, s.length());
00015   double d;
00016   s1>>d;
00017   return d;
00018 };
00019 
00020 void clear_nonred(GraphWin& GW) {
00021   edge e;
00022   GW.set_flush(false);
00023   forall_edges(e, GW.get_graph()) if(GW.get_color(e)!=red)
00024     GW.del_edge(e);
00025 
00026   GW.redraw();
00027   //GW.set_flush(true);
00028 };
00029 
00030 
00031 void read_steinlib(GraphWin& GW, LEDA::string& s) {
00032   int n, i, j, k;
00033 
00034   ifstream ifs(s);
00035   node* N;
00036   node lastterm;
00037   edge e;
00038 
00039   while(!ifs.eof()) {
00040     s.read_line(ifs);
00041 
00042     if(s.head(5)=="Nodes") {
00043       n=string_to_double(s.del(0,5));
00044       cout<<n<<" Nodes\n";
00045       N=new node[n];
00046       for(i=0; i<n; i++) {
00047         N[i]=GW.new_node(point(10*i,0));
00048       };
00049     };
00050     if(s.head(2)=="E ") {
00051       LEDA::string s1=s.del(0,1);
00052       string_istream is(s1); //,s1.length());
00053       is>>i;
00054       is>>j;
00055       LEDA::string s2;
00056       is>>s2;
00057       e=GW.new_edge(N[i-1],N[j-1]);
00058       GW.set_label(e, s2);
00059       e=GW.new_edge(N[j-1],N[i-1]);
00060       GW.set_label(e,s2);
00061     };
00062     if(s.head(2)=="A ") {
00063       LEDA::string s1=s.del(0,1);
00064       string_istream is(s1); //,s1.length());
00065       is>>i;
00066       is>>j;
00067       LEDA::string s2;
00068       is>>s2;
00069       if(s2!="1000000000") {
00070         e=GW.new_edge(N[i-1],N[j-1]);
00071         GW.set_label(e, s2);
00072       };
00073       is>>s2;
00074       if(s2!="1000000000") {
00075         e=GW.new_edge(N[j-1],N[i-1]);
00076         GW.set_label(e,s2);
00077       }
00078     };
00079     if(s.head(2)=="T ") {
00080       i=string_to_double(s.del(0,1));
00081       GW.set_color(N[i-1], red);
00082       lastterm=N[i-i];
00083     };
00084     if(s.head(3)=="DD ") {
00085       LEDA::string s1=s.del(0,2);
00086       string_istream is(s1); //,s1.length());
00087       is>>i;
00088       is>>j;
00089       is>>k;
00090       GW.set_position(N[i-1],point(j,k));
00091     };
00092   };
00093 
00094   node u;
00095   double xM,yM,xm,ym;
00096   bool haveroot=false;
00097   xM=-1e200;yM=-1e200; xm=1e200; ym=1e200;
00098   forall_nodes(u, GW.get_graph()) {
00099     xM=leda_max(xM,GW.get_position(u).xcoord());
00100     yM=leda_max(yM,GW.get_position(u).ycoord());
00101     xm=leda_min(xm,GW.get_position(u).xcoord());
00102     ym=leda_min(ym,GW.get_position(u).ycoord());
00103     if((GW.get_graph().indeg(u)==0)) {
00104       GW.set_color(u,green);
00105       haveroot=true;
00106       cout<<"Declare a root\n";
00107     };
00108   }
00109 
00110   if(haveroot==false) {
00111       GW.set_color(lastterm, green);
00112       cout<<"Declare last terminal as root\n";
00113   };
00114 
00115   double d=GW.get_xmax()/xM;
00116   d=leda_min(d, GW.get_ymax()/yM);
00117 
00118   forall_nodes(u,GW.get_graph()) {
00119     GW.set_position(u,point(GW.get_position(u).xcoord()*d, 
00120                             GW.get_position(u).ycoord()*d));
00121   };
00122 
00123   //GW.set_flush(true);
00124   GW.redraw();
00125 };
00126 
00127 void read_steinlib(GraphWin& GW) {
00128   GW.set_flush(false);
00129   cout<<"Filename : ";
00130   LEDA::string s;
00131   cin>>s;
00132 
00133   read_steinlib(GW, s);
00134 };
00135 
00136 
00137 void triang_points(GraphWin& GW) 
00138 {
00139   graph& G=GW.get_graph();
00140   int n=G.number_of_nodes();
00141   if (n!=0) {
00142   
00143     GW.set_flush(false);
00144 
00145     point_set T;  
00146     node_array<node> tr(T,n,nil);
00147     node_array<int> num(T,n,0);
00148     node v,v1;
00149     int i=0;
00150 
00151     double x,y;
00152     forall_nodes(v1,G) 
00153     {
00154       x=GW.get_position(v1).xcoord();
00155       y=GW.get_position(v1).ycoord();
00156       point p(x,y);
00157       v=T.insert(p);
00158       tr[v]=v1;
00159       num[v]=i++;
00160      }
00161 
00162     G.del_all_edges();
00163     GW.update_graph();
00164     edge e;
00165     forall_edges(e,T) 
00166       GW.new_edge(tr[T.source(e)],tr[T.target(e)]);
00167     GW.redraw();
00168     //  GW.set_flush(true);
00169     //GW.redraw();
00170   }
00171 }
00172 
00173 void node_handler(GraphWin&GW, const point& p) {
00174   graph& G=GW.get_graph();
00175   node v,u=G.first_node();
00176 
00177   forall_nodes(v, G) {
00178     if(GW.get_position(v).distance(p)<GW.get_position(u).distance(p))
00179       u=v;
00180   };
00181 
00182   if(GW.get_color(u)==red) GW.set_color(u, green);
00183   else {
00184     if(GW.get_color(u)==green) GW.set_color(u, white);
00185     else GW.set_color(u, red);
00186   }
00187   GW.redraw();
00188 }
00189 
00190 void generate_del(GraphWin& GW) 
00191 {
00192   graph& G=GW.get_graph();
00193   if (!G.empty()) GW.clear_graph();
00194 
00195   int n=10;
00196 
00197   panel p;
00198   p.int_item("Number of nodes : ",n);
00199   p.open(GW.get_window());
00200  
00201   random_source ran;
00202 
00203   double x,y;
00204   for(int i=0;i<n;i++) 
00205   { ran >> x >> y;
00206     x=(x+0.08)*(GW.get_xmax()-GW.get_xmin())*0.9+GW.get_xmin();
00207     y=(y+0.08)*(GW.get_ymax()-GW.get_ymin())*0.9+GW.get_ymin();
00208     point p1(x,y);
00209     GW.new_node(p1);
00210    }
00211   triang_points(GW);
00212 }
00213 
00214 int main(int argc, char** argv)
00215 {
00216   cout.precision(10);
00217 
00218   GraphWin GW;
00219 
00220 // Setup of the graph window
00221   GW.set_node_width(12);
00222   GW.set_node_height(12);
00223   GW.set_flush(false);
00224   GW.set_node_color(white);
00225   GW.set_node_label_type(no_label);
00226   GW.set_edge_direction(directed_edge);
00227 
00228 // Additional functionality of the graph window; the implementation is hidden
00229   int gn=gw_add_menu(GW,"Generation");
00230   gw_add_simple_call(GW,triang_points,"Triangulate defined points",gn);
00231   gw_add_simple_call(GW,generate_del, "Generate random Delaunay-Graph",gn);
00232   gw_add_simple_call(GW,read_steinlib, "Read Steinlib Graph", gn);
00233   gw_add_simple_call(GW,clear_nonred, "Remove all black edges", gn);
00234 
00235   GW.set_action(A_LEFT |A_NODE | A_CTRL, node_handler);
00236 
00237   GW.display();
00238 
00239   if(argc==2) {
00240       LEDA::string s=argv[1];
00241       read_steinlib(GW, s);
00242       graph& G=GW.get_graph();
00243 
00244       edge e;
00245       edge_array<double> C(G);
00246       forall_edges(e, G) 
00247           C[e]=string_to_double(GW.get_label(e));
00248       
00249       node_array<bool> R(G, false);
00250       node u, r;
00251       forall_nodes(u, G) {
00252           if(GW.get_color(u)==red) R[u]=true;
00253           if(GW.get_color(u)==green) r=u;
00254       };
00255 
00256       list<edge> T;
00257       ComputeSteinerTree(G, C, r, R, T);
00258 
00259       return 0;
00260   };
00261 
00262   while(GW.edit())
00263   {
00264     graph& G=GW.get_graph();
00265 
00266     cout<<"l\n";
00267 
00268     edge e;
00269     edge_array<double> C(G);
00270     forall_edges(e, G) 
00271       C[e]=string_to_double(GW.get_label(e));
00272 
00273     cout<<"t\n";
00274 
00275     node_array<bool> R(G, false);
00276     node u, r;
00277     forall_nodes(u, G) {
00278       if(GW.get_color(u)==red) R[u]=true;
00279       if(GW.get_color(u)==green) r=u;
00280     };
00281 
00282     cout<<"HERE\n";
00283 
00284     list<edge> T;
00285     ComputeSteinerTree(G, C, r, R, T);
00286 
00287     forall_edges(e,G) {
00288       GW.set_color(e, black);
00289     }
00290     forall(e,T) { 
00291       GW.set_color(e,red);
00292     }
00293     GW.redraw();
00294   }
00295   return 0;
00296 }

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