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
00013 struct vertex_properties {
00014 int x;
00015 int y;
00016
00017 vertex_properties() {}
00018
00019 vertex_properties(int _x, int _y) {
00020 x = _x;
00021 y = _y;
00022 }
00023 };
00024
00025
00026 struct edge_properties {
00027 double distance;
00028
00029
00030
00031
00032
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
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
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
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
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
00092
00093 std::cout << "Constructed Graph:\n";
00094 BGL_FORALL_EDGES(e, G, Graph) {
00095
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
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