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

dmst.cc

00001 #include <boost/foreach.hpp>
00002 #include <boost/graph/adjacency_list.hpp>
00003 #include <boost/graph/iteration_macros.hpp>
00004 #include <math.h>
00005 #include <scil/scil.h>
00006 #include <scil/constraints/path.h>
00007 #include <stdlib.h>
00008 #include <time.h>
00009 
00010 using namespace boost;
00011 
00012 //Vertex properties
00013 struct vertex_properties {
00014    int x;
00015    int y;
00016    //trivial constructor
00017    vertex_properties() {}
00018    //constructor setting coordinates
00019    vertex_properties(int _x, int _y) {
00020       x = _x;
00021       y = _y;
00022    }
00023 };
00024 
00025 //Edge properties
00026 struct edge_properties {
00027    double distance;
00028    /* back_edge is true this edge is a reverse edge (v,u) to an already
00029       existing edge (u,v) and will not be displayed in the output. The edge
00030       (v,u) is necessary because the constraint PATH used in function 
00031       ComputeDMST requires a directed graph. The edge (v,u) is not necessary for
00032       the output since Diameter_MST is a problem on undirected Graphs.
00033     */
00034    bool back_edge;
00035 };
00036 
00037 typedef adjacency_list<vecS, vecS, directedS, vertex_properties,
00038                        edge_properties> Graph;
00039 typedef graph_traits<Graph>::edge_descriptor edge_descriptor;
00040 typedef graph_traits<Graph>::vertex_descriptor vertex_descriptor;
00041 
00042 #include "compute_dmst.cc"
00043 
00044 void make_complete(Graph &G) {
00045    //Add edges
00046    graph_traits<Graph>::vertex_iterator v_it, u_it, v_it_end, u_it_end;
00047    for(tie(v_it, v_it_end) = vertices(G); v_it != v_it_end; v_it++)
00048       for(tie(u_it, u_it_end) = vertices(G); u_it != u_it_end; u_it++)
00049          if (*u_it != * v_it) {
00050             edge_descriptor e = add_edge(*u_it, *v_it, G).first;
00051             // Set property back_edge to true if an edge (u,v) already exists
00052             G[e].back_edge = edge(*v_it, *u_it, G).second;
00053          }
00054 }
00055 
00056 void generate_graph(Graph &G) {
00057    int n;
00058 
00059    std::cout << "Please enter the number of nodes: ";
00060    std::cin >> n;
00061    std::cout << std::endl;
00062 
00063    //Assign random vertex coordinates
00064    srand(time(NULL));
00065 
00066    for(int i=0; i<n; i++) {
00067       vertex_properties vp((rand() % 20),(rand() % 20));
00068       add_vertex(vp, G);
00069    }
00070    make_complete(G);
00071 }
00072 
00073 //Calculate Euclidian distances for all edges
00074 void calculate_edge_lengths(Graph &G) {
00075    BGL_FORALL_EDGES(e, G, Graph) {
00076       double xDifference = G[source(e,G)].x - G[target(e,G)].x;
00077       double yDifference = G[source(e,G)].y - G[target(e,G)].y;
00078       G[e].distance = sqrt(pow(xDifference,2)+pow(yDifference,2));
00079    }
00080 }
00081 
00082 int main() {
00083    Graph G;
00084    generate_graph(G);
00085 
00086    calculate_edge_lengths(G);
00087 
00088    std::list<edge_descriptor> T;
00089    ComputeDMST(G, T);
00090 
00091    //Generate Output
00092 
00093    std::cout << "Constructed Graph:\n";
00094    BGL_FORALL_EDGES(e, G, Graph) {
00095       //Do not display reverse edges
00096       if (!G[e].back_edge)
00097          printf("x1: %d y1: %d x2: %d y2: %d d: %f\n", G[source(e,G)].x,
00098                 G[source(e,G)].y, G[target(e,G)].x, G[target(e,G)].y,
00099                 G[e].distance);
00100    }
00101 
00102    std::cout << "\nDiameter_MST:\n";
00103    BOOST_FOREACH(edge_descriptor e, T) {
00104       //Swap output if e is a reverse edge
00105       if (!G[e].back_edge)
00106          printf("x1: %d y1: %d x2: %d y2: %d d: %f\n", G[source(e,G)].x, 
00107                 G[source(e,G)].y, G[target(e,G)].x, G[target(e,G)].y,
00108                 G[e].distance);
00109       else
00110          printf("x1: %d y1: %d x2: %d y2: %d d: %f\n", G[target(e,G)].x, 
00111                 G[target(e,G)].y, G[source(e,G)].x, G[source(e,G)].y,
00112                 G[e].distance);
00113    }
00114 };
00115 
Generated on Mon Mar 28 22:03:48 2011 for SCIL by  doxygen 1.6.3