Click here to Skip to main content
15,884,099 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Jc Christ5-Jun-20 1:57
Jc Christ5-Jun-20 1:57 
AnswerRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Richard Deeming11-Dec-17 1:58
mveRichard Deeming11-Dec-17 1:58 
AnswerRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Member 1488614010-Jul-20 3:04
Member 1488614010-Jul-20 3:04 
GeneralRe: Writing a Sorting Algorithm -- NEED HELP! Pin
Dave Kreskowiak23-Jul-20 3:39
mveDave Kreskowiak23-Jul-20 3:39 
QuestionA permutation problem Pin
Member 135615816-Dec-17 4:04
Member 135615816-Dec-17 4:04 
AnswerRe: A permutation problem Pin
Richard MacCutchan6-Dec-17 4:09
mveRichard MacCutchan6-Dec-17 4:09 
AnswerRe: A permutation problem Pin
Luc Pattyn20-Dec-17 12:03
sitebuilderLuc Pattyn20-Dec-17 12:03 
QuestionAlgorithm for insert element in compressed sparse row matrix Pin
drudox 16-Nov-17 4:23
drudox 16-Nov-17 4:23 
Hi everybody, i'm developing a Modified Compress Sparse Row Matrix Class (in C++, but i think this is not important for the algorithm) you can read a short explanation of the method here I wrote the constructor as follow :

template <typename T>
constexpr MCSRmatrix<T>::MCSRmatrix( std::initializer_list<std::initializer_list<T>> rows)
{
      this->dim  = rows.size();
      auto _rows = *(rows.begin());
            
      aa_.resize(dim+1);
      ja_.resize(dim+1);
      
      if(dim != _rows.size())
      {
          throw InvalidSizeException("Error in costructor! MCSR format require square matrix!");  
      }
      
      itype w = 0 ;
      ja_.at(w) = dim+2 ;
      for(auto ii = rows.begin(), i=1; ii != rows.end() ; ++ii, i++)
      {
          for(auto ij = ii->begin(), j=1, elemCount = 0 ; ij != ii->end() ; ++ij, j++ )   
          {
              if(i==j)
                 aa_[i-1] = *ij ;
              else if( i != j && *ij != 0 )
              {   
                 ja_.push_back(j); 
                 aa_.push_back(*ij); 
                 elemCount++ ;
              }
              ja_[i] = ja_[i-1] + elemCount;           
          }
      }     
}


and it works well ! I also wrote a findIndex(i,j) method that return the value at i,j of the whole matrix and it works fine, so i'm able to print out the whole matrix using the operator overloading () who return 0 if in this position the element is zero otherwise the right value

template <typename T>
std::size_t constexpr MCSRmatrix<T>::findIndex(const itype row ,  const itype col) const noexcept 
{    
     assert( row > 0 && row <= dim && col > 0 && col <= dim ); 
     if(row == col)
     {
       return row-1;
     }
     int i = -1;
     
      for(int i = ja_.at(row-1)-1 ; i < ja_.at(row)-1 ; i++ )
      {
            if( ja_.at(i) == col ) 
            { 
                  
              return i   ;
            }
      }
      return -1;
}


the operator is defined as :
template <typename T>
const T MCSRmatrix<T>::operator()(const itype r , const itype c) const noexcept 
{     
     auto i = findIndex(r,c); 
     if( i != -1 && i < aa_.size() )
     {      
            return aa_.at(i) ;
     }
     else
     {
            return 0.0 ;
     }       
}


Now I need to write an alghoritm that give 2 index of position is able to insert in the 2 vector the element in the right position could you help me about ?

I wrote this one but dosn't works fine !

template<typename T>
auto constexpr MCSRmatrix<T>::insertAt(itype row ,itype col , T elem) noexcept 
{
      if(elem == 0)
      {
          std::cerr << "zero element to insert ! Exit 1" << std::endl; 
      }
      
      int index = findIndex(row,col);
      if( index >= dim+1  || row == col)
      {     
          aa_.at(index) = elem ;
      }
      else if(index == -1) 
      {

          for(auto i=row; i< dim+1 ; i++) 
          {     
                ja_.at(i) += 1 ;
          }
          
          if(ja_.at(row) >= aa_.size() ) //std::cout << aa_.size() << ' ' <<ja_.at(row) << std::endl;
          {
              aa_.push_back(elem);
              ja_.push_back(col);
          }
          else if ( ja_.at(row) < aa_.size() )
          {
                ja_.insert(ja_.begin() + dim  + row , col );
                aa_.insert(aa_.begin() + dim  + row , elem);
          }
      }
}


I hope you give me a way to write down the correct way to inert element ! thanks in advance
P.S. itype is a typedef of std::size_t !
QuestionNo-intersection edge detection. Pin
primem0ver11-Nov-17 6:19
primem0ver11-Nov-17 6:19 
QuestionDatagap finder Pin
V.7-Nov-17 0:10
professionalV.7-Nov-17 0:10 
AnswerRe: Datagap finder Pin
Peter_in_27807-Nov-17 0:46
professionalPeter_in_27807-Nov-17 0:46 
AnswerRe: Datagap finder Pin
Gerry Schmitz7-Nov-17 12:21
mveGerry Schmitz7-Nov-17 12:21 
QuestionPrefix sum: GenomicRangeQuery Pin
Yugo Amaryl5-Nov-17 23:20
Yugo Amaryl5-Nov-17 23:20 
AnswerRe: Prefix sum: GenomicRangeQuery Pin
Gerry Schmitz6-Nov-17 6:08
mveGerry Schmitz6-Nov-17 6:08 
AnswerRe: Prefix sum: GenomicRangeQuery Pin
honey the codewitch17-Jan-18 17:02
mvahoney the codewitch17-Jan-18 17:02 
Questionfind largest number of pages i can buy from a list of books within a certain budget. Pin
Member 135013603-Nov-17 1:16
Member 135013603-Nov-17 1:16 
AnswerRe: find largest number of pages i can buy from a list of books within a certain budget. Pin
OriginalGriff3-Nov-17 1:19
mveOriginalGriff3-Nov-17 1:19 
GeneralRe: find largest number of pages i can buy from a list of books within a certain budget. Pin
Member 135013603-Nov-17 1:56
Member 135013603-Nov-17 1:56 
AnswerRe: find largest number of pages i can buy from a list of books within a certain budget. Pin
Ralf Meier3-Nov-17 2:08
mveRalf Meier3-Nov-17 2:08 
GeneralRe: find largest number of pages i can buy from a list of books within a certain budget. Pin
Member 135013603-Nov-17 2:15
Member 135013603-Nov-17 2:15 
QuestionWhat is the best search strategy? Pin
daxue2-Nov-17 18:07
daxue2-Nov-17 18:07 
QuestionLoad balancing with 3 factors Pin
LabVIEWstuff1-Nov-17 4:58
LabVIEWstuff1-Nov-17 4:58 
AnswerRe: Load balancing with 3 factors Pin
Afzaal Ahmad Zeeshan1-Nov-17 5:36
professionalAfzaal Ahmad Zeeshan1-Nov-17 5:36 
GeneralRe: Load balancing with 3 factors Pin
LabVIEWstuff3-Nov-17 1:45
LabVIEWstuff3-Nov-17 1:45 
GeneralRe: Load balancing with 3 factors Pin
Afzaal Ahmad Zeeshan3-Nov-17 1:55
professionalAfzaal Ahmad Zeeshan3-Nov-17 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.