prim.cc
Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *---------------------           WMPSNIFFER          --------------------
00003  *------------------------------------------------------------------------
00004  *                                                         V7.0B  11/05/10
00005  *
00006  *
00007  *  File: prim.cc
00008  *  Authors: Danilo Tardioli
00009  *  ----------------------------------------------------------------------
00010  *  Copyright (C) 2000-2012, Universidad de Zaragoza, SPAIN
00011  *
00012  *  Contact Addresses: Danilo Tardioli                   dantard@unizar.es
00013  *
00014  *  RT-WMP is free software; you can  redistribute it and/or  modify it
00015  *  under the terms of the GNU General Public License  as published by the
00016  *  Free Software Foundation;  either  version 2, or (at  your option) any
00017  *  later version.
00018  *
00019  *  RT-WMP  is distributed  in the  hope  that  it will be   useful, but
00020  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
00021  *  MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
00022  *  General Public License for more details.
00023  *
00024  *  You should have received  a  copy of  the  GNU General Public  License
00025  *  distributed with RT-WMP;  see file COPYING.   If not,  write to the
00026  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
00027  *  02111-1307, USA.
00028  *
00029  *  As a  special exception, if you  link this  unit  with other  files to
00030  *  produce an   executable,   this unit  does  not  by  itself cause  the
00031  *  resulting executable to be covered by the  GNU General Public License.
00032  *  This exception does  not however invalidate  any other reasons why the
00033  *  executable file might be covered by the GNU Public License.
00034  *
00035  *----------------------------------------------------------------------*/
00036 
00037 #include "prim.h"
00038 #include "matrix.h"
00039 #define DBL_MAX 10e6
00040 void Prim(Matrix & m){
00041         Matrix tree=m;
00042         Matrix graph=m; //tmp matrix
00043         tree*=0;
00044         unsigned numVertexOnTree=1,vertexOnTree[m.RowNo()];
00045         vertexOnTree[0]=0;
00046         while (numVertexOnTree < m.RowNo()){
00047                 double minWeight=DBL_MAX;
00048                 int minWeightIdxi=-1;
00049                 int minWeightIdxj=-1;
00050                 for (unsigned i=0;i<numVertexOnTree;i++){
00051                         unsigned vertex=vertexOnTree[i]; //TODO: was int
00052                         for (unsigned j=0;j<m.RowNo();j++){
00053                                 if (vertex==j)continue;
00054                                 if (graph(vertex,j) <= minWeight){
00055                                         minWeight=graph(vertex,j);
00056                                         minWeightIdxi=vertex;
00057                                         minWeightIdxj=j;
00058                                 }
00059                         }
00060                 }
00061                 double v=m(minWeightIdxi,minWeightIdxj);
00062                 tree(minWeightIdxi,minWeightIdxj)=v;
00063                 tree(minWeightIdxj,minWeightIdxi)=v;
00064                 for (unsigned i=0;i<numVertexOnTree;i++){
00065                         graph(vertexOnTree[i],minWeightIdxj)=DBL_MAX;
00066                         graph(minWeightIdxj,vertexOnTree[i])=DBL_MAX;
00067                 }
00068                 vertexOnTree[numVertexOnTree]=minWeightIdxj;
00069                 numVertexOnTree++;
00070         }
00071         m=tree;
00072 }
00073 
00074 
00075 void prim(char * q, int size){
00076         char * p=q;
00077         Matrix m(size,size);
00078         for (int i=0; i<size;i++){
00079                 for (int j=0; j<size;j++){
00080                         if (i!=j) m(i,j)=100000/((double)(*p));
00081                         else m(i,j)=0;
00082                         p++;
00083                 }
00084         }
00085         for (int i=0; i<size;i++){
00086                 for (int j = 0; j < size; j++) {
00087                         if (m(i, j) > m(j, i)) {
00088                                 m(j, i) = m(i, j);
00089                         } else {
00090                                 m(i, j) = m(j, i);
00091                         }
00092                 }
00093         }
00094 
00095         Prim(m);
00096         p=q;
00097         for (int i=0; i<size;i++){
00098                 for (int j=0; j<size;j++){
00099                         *p=(char) m(i,j);
00100                         p++;
00101                 }
00102         }
00103 }
00104 
00105 


ros_rt_wmp_sniffer
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:57