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

MultiPlatform

  1. #include <LEDA/ugraph.h>
  2. #include <LEDA/graph_alg.h>
  3. #include <LEDA/graph_edit.h>
  4. #include <math.h>
  5. window W;
  6. //window w1(440,440,0,125);
  7. void bold_edge(GRAPH<point,int>& G, edge e)
  8. { point p = G[source(e)];
  9.   point q = G[target(e)];
  10.   W.set_mode(xor_mode);
  11.   W.draw_edge_arrow(p,q,blue);
  12.   int save = W.set_line_width(5);
  13.   W.draw_edge(p,q,blue);
  14.   W.set_line_width(save);
  15.   W.set_mode(src_mode);
  16. }
  17. void unbold_edge(GRAPH<point,int>& G, edge e)
  18. { int save = W.set_line_width(5);
  19.   point p = G[source(e)];
  20.   point q = G[target(e)];
  21.   W.set_mode(xor_mode);
  22.   W.draw_edge(p,q,blue);
  23.   W.set_line_width(save);
  24.   W.draw_edge_arrow(p,q,blue);
  25.   W.set_mode(src_mode);
  26. }
  27. void show_edge_inf(GRAPH<point,int>& G, edge_array<int>& edge_num)
  28. { edge e;
  29.   W.set_mode(xor_mode);
  30.   forall_edges(e,G)
  31.   { point p = G[source(e)];
  32.     point q = G[target(e)];
  33.     W.draw_text((p.xcoord()+q.xcoord())/2, (p.ycoord()+q.ycoord())/2,
  34.                   string("%d", edge_num[e]));
  35.    }
  36.   W.set_mode(src_mode);
  37.  }
  38. void show_edge_inf(UGRAPH<point,int>& G, edge_array<int>& edge_num)
  39. { edge e;
  40.   W.set_mode(xor_mode);
  41.   forall_edges(e,G)
  42.   { point p = G[source(e)];
  43.     point q = G[target(e)];
  44.     W.draw_text((p.xcoord()+q.xcoord())/2, (p.ycoord()+q.ycoord())/2,
  45.                   string("%d", edge_num[e]));
  46.    }
  47.   W.set_mode(src_mode);
  48.  }
  49. void show_node_inf(UGRAPH<point,int>& G, node_array<int>& node_num)
  50. { node v;
  51.   forall_nodes(v,G)
  52.     W.draw_int_node(G[v],node_num[v],violet);
  53. }
  54. void show_node_inf(GRAPH<point,int>& G, node_array<int>& node_num)
  55. { node v;
  56.   forall_nodes(v,G)
  57.      W.draw_int_node(G[v],node_num[v],violet);
  58. }
  59. void show_two_node_inf(GRAPH<point,int>& G, node_array<int>& node_num1,
  60.                                             node_array<int>& node_num2)
  61. { node v;
  62.   forall_nodes(v,G)
  63.   { string s("%d|%d",node_num1[v],node_num2[v]);
  64.     W.draw_text_node(G[v],s);
  65.    }
  66. }
  67. void draw_graph(GRAPH<point,int>& G) 
  68. { node v,w;
  69.   int i = 0;
  70.   forall_nodes(v,G)
  71.    { W.draw_int_node(G[v],i++,yellow);
  72.      forall_adj_nodes(w,v)
  73.         W.draw_edge_arrow(G[v],G[w],blue);
  74.     }
  75. }
  76. void draw_graph(UGRAPH<point,int>& G) 
  77. { node v,w;
  78.   int i = 0;
  79.   forall_nodes(v,G)
  80.    { W.draw_int_node(G[v],i++,yellow);
  81.      forall_adj_nodes(w,v)
  82.         W.draw_edge(G[v],G[w],blue);
  83.     }
  84. }
  85. void generate_graph(GRAPH<point,int>& G)
  86. {
  87.   panel P_gen;
  88.   int n = 0;
  89.   int m = 0;
  90.   P_gen.int_item("# nodes", n);
  91.   P_gen.int_item("# edges", m);
  92.   P_gen.button("random");
  93.   P_gen.button("complete");
  94.   P_gen.button("bi_random");
  95.   P_gen.button("bi_complete");
  96.   list<node> A,B;
  97.   node v;
  98.   int xmin = (int)W.xmin();
  99.   int ymin = (int)W.ymin();
  100.   int xmax = (int)W.xmax();
  101.   int ymax = (int)W.ymax();
  102.   int i = P_gen.open();
  103.   switch(i)
  104.   {
  105.     case 0: random_graph(G,n,m);
  106.             break;
  107.     case 1: complete_graph(G,n);
  108.             break;
  109.     case 2: random_bigraph(G,n,n,m,A,B);
  110.             break;
  111.     case 3: complete_bigraph(G,n,n,A,B);
  112.             break;
  113.    }
  114.    if (i > 1)
  115.       { double dy = (ymax-ymin)/(n+1);
  116.         double y = ymin + dy;
  117.         forall(v,A) 
  118.         { G[v] = point(xmin + (xmax-xmin)/4,y);
  119.           y += dy;
  120.          }
  121.         y = ymin + dy;
  122.         forall(v,B) 
  123.         { G[v] = point(xmax - (xmax-xmin)/4,y);
  124.           y += dy;
  125.          }
  126.         }
  127.     else // circular embedding 
  128.        { double R  = (xmax-xmin)/2.5;
  129.          double x0 = (xmax-xmin)/2;
  130.          double y0 = (ymax-ymin)/2;
  131.          point  M(x0,y0);
  132.          double alpha = 0;
  133.          double step  = 2*LEDA_PI/n;
  134.          forall_nodes(v,G)  
  135.          { G[v] = M.translate(alpha,R);
  136.            alpha+=step;
  137.           }
  138.         }
  139. }
  140. main()
  141. {
  142.   panel P("Graph Algorithms");
  143.   list<string> alg_menu;
  144.   alg_menu.append(string("topsort"));
  145.   alg_menu.append(string("dfsnum"));
  146.   alg_menu.append(string("components"));
  147.   alg_menu.append(string("strongcomp"));
  148.   alg_menu.append(string("bicomp"));
  149.   alg_menu.append(string("matching"));
  150.   string alg = alg_menu.head();
  151.   P.string_item("algorithm", alg, alg_menu);
  152.   P.button("file");   // 0
  153.   P.button("gen");    // 1
  154.   P.button("edit");   // 2
  155.   P.button("run");    // 3
  156.   P.button("quit");   // 4
  157.   P.display(0,0);
  158.   string file      = "graph.1";
  159.   panel file_panel("FILE PANEL");
  160.   file_panel.string_item("file", file);
  161.   file_panel.button("load");
  162.   file_panel.button("save");
  163.   GRAPH<point,int>  G;
  164.   edge e;
  165.   for(;;)
  166.   { 
  167.     W.del_messages();
  168.     int key = P.read();
  169.     if (key == 4) break;
  170.     switch(key) 
  171.     {
  172.       case 0: { int b = file_panel.open(); 
  173.                 if (b==0)  // load
  174.                 { W.message(string("loading graph from %s",~file));
  175.                   G.clear();
  176.                   G.read(file);
  177.                   W.clear();
  178.                   draw_graph(G);
  179.                  }
  180.                 if (b==1)  // save
  181.                 { G.write(file);
  182.                   W.message(string("writing graph to %s",~file));
  183.                 }
  184.                 break;
  185.                }
  186.       case 1: G.clear();
  187.               generate_graph(G);
  188.               W.clear();
  189.               draw_graph(G);
  190.               break;
  191.       case 2: { //graph_edit(w1,G);
  192.                 //W.clear();
  193.                 //draw_graph(G);
  194.                 graph_edit(W,G);
  195.                 break;
  196.                }
  197.       case 3: { if (alg == "bicomp")
  198.                 { W.message("BICONNECTED COMPONENTS");
  199.                   UGRAPH<point,int> U = G;
  200.                   edge_array<int> edge_num(U);
  201.                   BICONNECTED_COMPONENTS(U,edge_num);
  202.                   show_edge_inf(U,edge_num);
  203.                   W.read_mouse();
  204.                   show_edge_inf(U,edge_num);
  205.                   break;
  206.                  }
  207.                 if (alg == "dfsnum")
  208.                 { W.message("DFS NUMBERING");
  209.                   node_array<int> dfs_num(G);
  210.                   node_array<int> comp_num(G);
  211.                   DFS_NUM(G,dfs_num,comp_num);
  212.                   show_two_node_inf(G,dfs_num,comp_num);
  213.                   W.read_mouse();
  214.                   draw_graph(G);
  215.                   break;
  216.                  }
  217.                 if (alg == "components")
  218.                 { W.message("COMPONENTS");
  219.                   UGRAPH<point,int> U = G;
  220.                   node_array<int> node_num(U);
  221.                   COMPONENTS(U,node_num);
  222.                   show_node_inf(U,node_num);
  223.                   W.read_mouse();
  224.                   draw_graph(G);
  225.                   break;
  226.                  }
  227.           
  228.                 if (alg == "strongcomp")
  229.                 { W.message("STRONG COMPONENTS");
  230.                   node_array<int> node_num(G);
  231.                   STRONG_COMPONENTS(G,node_num);
  232.                   show_node_inf(G,node_num);
  233.                   W.read_mouse();
  234.                   draw_graph(G);
  235.                   break;
  236.                  }
  237.                 if (alg == "topsort")
  238.                 { W.message("TOPSORT");
  239.                   node_array<int> node_num(G);
  240.                   if (TOPSORT(G,node_num)==false)
  241.                   { W.acknowledge("Graph is cyclic, cannot sort");
  242.                     break;
  243.                    }
  244.                   show_node_inf(G,node_num);
  245.                   W.read_mouse();
  246.                   draw_graph(G);
  247.                   break;
  248.                  }
  249.                 if (alg == "matching")
  250.                 { W.message("MAX_CARD_MATCHING");
  251.                   list<edge> L = MAX_CARD_MATCHING(G);
  252.                   forall(e,L) bold_edge(G,e);       
  253.                   W.read_mouse();
  254.                   forall(e,L) unbold_edge(G,e);       
  255.                   break;
  256.                  }
  257.                 W.acknowledge(alg + " not found");
  258.                 break;
  259.           
  260.               }
  261.     } //switch
  262.   } // for(;;)
  263.   return 0;
  264. }