MeshKit  1.0
Dijkstra.cpp
Go to the documentation of this file.
00001 
00002 #include "meshkit/MKCore.hpp"
00003 #include "Dijkstra.hpp"
00004 #include <iostream>
00005 #include <algorithm>
00006 #include <math.h>
00007 #include <map>
00008 
00009 namespace MeshKit
00010 {
00011 
00012 
00013 //---------------------------------------------------------------------------//
00014 // construction function for Dijkstra class
00015 Dijkstra::Dijkstra(vector<vector<double> > t)
00016 {
00017         dist.insert(dist.begin(), t.begin(), t.end());
00018 }
00019 
00020 void Dijkstra::getResults(vector<vector<double> > &d)
00021 {
00022         d.resize(dist.size());
00023         order_dist.resize(dist.size());
00024         for (unsigned int i = 0; i < dist.size(); i++){
00025                 algs(i, d[i]);
00026                 order_dist[i].insert(order_dist[i].begin(), d[i].begin(), d[i].end());
00027         }
00028 }
00029 
00030 void Dijkstra::getTopMostSurf()
00031 {
00032 
00033         for (unsigned int i = 0; i < order_dist.size(); i++){
00034                 bool is_negative = true;
00035                 for (unsigned int j = 0; j < order_dist[i].size(); j++)
00036                         is_negative = is_negative && (order_dist[i][j] <= 0);
00037                 if (is_negative)
00038                         topmost_target_surf = i;
00039         }
00040 
00041         std::cout << "topmost target surf index = " << topmost_target_surf << std::endl;
00042 }
00043 
00044 void Dijkstra::getSurfList(vector<vector<vector<int> > > &l, int src_size, int tgt_size)
00045 {
00046         std::cout << "====================\n";
00047         for (unsigned int i = 0; i < order_dist.size(); i++){
00048 
00049                 for (unsigned int j = 0; j < order_dist[i].size(); j++)
00050                         std::cout << order_dist[i][j] << "\t";
00051                 std::cout << std::endl;
00052         }
00053 
00054         std::cout << "====================\n";
00055 
00056         std::vector<double> dist_array;
00057         getTopMostSurf();
00058 
00059         for (unsigned int i = 0; i < order_dist[topmost_target_surf].size(); i++)
00060                 std::cout << order_dist[topmost_target_surf][i] << "\t";
00061         std::cout << std::endl;
00062 
00063         dist_array.insert(dist_array.begin(), order_dist[topmost_target_surf].begin(), order_dist[topmost_target_surf].end());
00064         std::vector<double> bak_dist_array;
00065         bak_dist_array.insert(bak_dist_array.begin(), dist_array.begin(), dist_array.end());
00066         //sort the distances
00067         std::sort(dist_array.begin(), dist_array.end());
00068         //get the index
00069         int layer_index = -1;
00070         double previous_dist = 1.0e10;
00071 
00072         std::cout << "sorted array\n";
00073         for (unsigned int i = 0; i < dist_array.size(); i++)
00074                 std::cout << dist_array[i] << "\t";
00075         std::cout << std::endl;
00076 
00077 
00078         for (int i = dist_array.size() - 1; i >= 0; i--){
00079                 std::vector<double>::iterator it;
00080                 it = std::find(bak_dist_array.begin(), bak_dist_array.end(), dist_array[i]);
00081                 int index = std::distance(bak_dist_array.begin(), it);
00082                 surf_list.push_back(index);
00083                 //l.push_back(index);
00084                 if (i==( (int)dist_array.size()-1)){
00085                         layer_index++;
00086                         l.resize(layer_index+1);
00087                         addSurfToList(layer_index, index, src_size, l[layer_index]);
00088                 }
00089                 else{
00090                         if (fabs(dist_array[i]-previous_dist) < 1.0e-5){//on the same layer
00091                                 addSurfToList(layer_index, index, src_size, l[layer_index]);
00092                         }
00093                         else{
00094                                 layer_index++;
00095                                 l.resize(layer_index+1);
00096                                 addSurfToList(layer_index, index, src_size, l[layer_index]);
00097                         }
00098 
00099                 }
00100                 previous_dist = dist_array[i];
00101                 bak_dist_array[index] = 1.0e10;
00102         }
00103 
00104         //
00105         for (unsigned int i = 0; i < l.size(); i++){
00106                 for (unsigned int j = 0; j < l[i].size(); j++){
00107                         std::cout << "layer = " << i << "\tsurf index = " << j << "\tsurf_id = " << l[i][j][0] << std::endl;
00108                 }
00109         }
00110 }
00111 
00112 void Dijkstra::addSurfToList(int layer_index, int value, int src_size, vector<vector<int> > &datalist){
00113         int surf_index = datalist.size();
00114         datalist.resize(surf_index+1);
00115         if (value < src_size){
00116                 datalist[surf_index].push_back(value);
00117                 datalist[surf_index].push_back(0);
00118         }
00119         else{
00120                 datalist[surf_index].push_back(value-src_size);
00121                 datalist[surf_index].push_back(1);
00122         }
00123 }
00124 
00125 void Dijkstra::algs(int s, vector<double> &f_list)
00126 {
00127         //Initializaton: set every distance to INFINITY until we discover a path
00128         vector<double> d(dist.size(), 1.0e10);
00129         //sptSet[i] will true if vertex i is included in the shortest path tree or
00130         //shortest distance from s to i is finalized.
00131         vector<bool> sptSet(dist.size(), false);
00132         //the distance from the source to the source is defined to be zero.
00133         d[s] = 0.0;
00134         /*
00135         This loop corresponds to sending out the explorers walking the paths,
00136         where the step of picking "the vertex, v, with the shortest path to s"
00137         corresponds to an explorer arriving at an unexplored vertex.
00138         */
00139         int V = (int)dist.size();
00140         // Find shortest path for all vertices
00141         for (int count = 0; count < V-1; count++){
00142                 //pick the minimum distance vertex from the set of vertices not yet
00143                 //processed. u is always equal to s in the first iteration
00144                 int u = minDistance(d, sptSet);
00145                 //Mark the picked vertex as processed.
00146                 sptSet[u] = true;
00147                 //Update d value of the adjacent vertices of the picked vertex
00148                 for (int v = 0; v < V; v++){
00149                         //update d[v] only if is not in sptSet, there is an edge from u to
00150                         //v, and total weight of apth from s to v through u is smaller than
00151                         //current value of d[v]
00152                         if (!sptSet[v] && dist[u][v] && d[u] != 1.0e10 && d[u]+dist[u][v]<d[v])
00153                                 d[v] = d[u]+dist[u][v];
00154                 }
00155         }
00156 
00157         //pass results to the list
00158         for (unsigned int i = 0; i < d.size(); i++)
00159                 f_list.push_back(d[i]);
00160 
00161 }
00162 //A utility function to find the vertex with minimum distance value, from the
00163 //set of vertices not yet included in shortest path tree
00164 int Dijkstra::minDistance(vector<double> d, vector<bool> sptSet)
00165 {
00166         //Initialize min value
00167         double minvalue = 1.0e10;
00168         int min_index = 0;
00169         for (int v = 0; v < (int) dist.size(); v++){
00170                 if (sptSet[v] == false && d[v] <= minvalue){
00171                         minvalue = d[v];
00172                         min_index = v;
00173                 }
00174         }
00175         return min_index;
00176 }
00177 
00178 //---------------------------------------------------------------------------//
00179 // deconstruction function for Dijkstra class
00180 Dijkstra::~Dijkstra()
00181 {
00182         std::cout << "Dijkstra algorithm is over\n";
00183 }
00184 
00185 
00186 
00187 
00188 }
00189 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines