Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:45:25+00:00 2026-05-28T19:45:25+00:00

I am trying to do an efficient sparse matrix multiplication. Right now I am

  • 0

I am trying to do an efficient sparse matrix multiplication. Right now I am reading the data into memory and this is how my data structure looks like:

typedef struct node{
int x;
int y;
int value;
struct node* row;
struct node* col;
}node;

typedef struct matrix{
int height;
int width; 
node** rowList;
node** colList;
}matrix;

My current code for the insertion is:

void insert(matrix** M, int row_index, int col_index, int value)
{
    node* currNode=(node*)malloc(sizeof(node));
    currNode->x=row_index;
    currNode->y=col_index;
    currNode->value=value;

    if ((*M)->rowList[row_index] == NULL) { /* index is empty */
        currNode->row = NULL;
        (*M)->rowList[row_index] = currNode;
    }
    else if ((*M)->rowList[row_index]->y > col_index) { /* insert node to front */
        //printf("%d, %d\n", (*M)->rowList[row_index]->y, col_index);
        currNode->col = (*M)->rowList[row_index];
        (*M)->rowList[row_index] = currNode;
    }
    else if ((*M)->rowList[row_index]->y < col_index) { /* insert node to front */  
        node* rowptr = (node*)malloc(sizeof(node));
        rowptr = (*M)->rowList[row_index];
        while(rowptr->col!=NULL&&rowptr->col->y < col_index)
            rowptr=rowptr->col;

        currNode->col=rowptr->col;
        rowptr->col=currNode;
        //printf("-----------------%d\n", rowptr->y);
    }

    if ((*M)->colList[col_index] == NULL) { 
        currNode->col = NULL;
        (*M)->colList[col_index] = currNode;
    }
    else
    if ((*M)->colList[col_index]->x > row_index) { 
        //printf("%d, %d\n", (*M)->colList[col_index]->x, row_index);
        currNode->row = (*M)->colList[col_index];
        (*M)->colList[col_index] = currNode;
    }
}

In case of you ask, this is my print function:

void print_matrix(matrix *M){
    for(int i=0;i<M->height;i++){
        while(M->rowList[i]!=NULL){
            printf("i=%d, j=%d, v=%d\n",M->rowList[i]->x, M->rowList[i]->y,
                   M->rowList[i]->value);
            M->rowList[i]=M->rowList[i]->col;
        }
    }
}

For this input:

5,5
0,0,1
0,1,2
0,3,3
0,4,4

where (5,5) matrix dimensions and (0,0,1) = i,j,value, I get this:

i=0, j=0, v=1
i=0, j=1, v=2
i=0, j=3, v=3
i=0, j=4, v=4
i=0, j=4, v=4

For this input:

5,5
0,0,1
0,1,2
0,3,3
0,4,4
0,2,5

I get this:

i=0, j=0, v=1
i=0, j=1, v=2
i=0, j=2, v=5
i=0, j=2, v=5

I think the problem is here:

else if ((*M)->rowList[row_index]->y < col_index) {
    node* rowptr = (node*)malloc(sizeof(node));
    rowptr = (*M)->rowList[row_index];
    while(rowptr->col!=NULL&&rowptr->col->y < col_index)
        rowptr=rowptr->col;

        currNode->col=rowptr->col;
        rowptr->col=currNode;
    }
    [ ... ]

Somehow I remove one of the values when I add a new element that is smaller.

The question is : how can I get this code to load my sparse matrix values into memory using the data structure provided correctly?

Thank you ^^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:45:26+00:00Added an answer on May 28, 2026 at 7:45 pm

    Here:

    if ((*M)->colList[col_index] == NULL) {
        currNode->col = NULL;
        (*M)->colList[col_index] = currNode;
    }
    

    where you write currNode->col, you should have written currNode->row. After making this change the output is correct for the second input file.

    While looking at the code I noticed other odd things; for example, the print_matrix function also destroys the matrix ->col pointer chains. Also, in these two lines

        node* rowptr = (node*)malloc(sizeof(node));
        rowptr = (*M)->rowList[row_index];
    

    you’re allocating memory and then immediately overwriting the pointer to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find a good, efficient way to run a query like this:
Situation: I am trying to write a efficient query using "LIKE" statement to look
I'm trying to build an efficient string matching algorithm. This will execute in a
Since I'm trying to be efficient in this program I'm making, I thought I'd
Trying to get data in the most efficient way possible for some reports, using
I've been trying to find an efficient way to represent nested data in java/hibernate.
I'm trying to determine what is the most efficient way to load videos into
I am trying to find a highly efficient method of auditing changes to data
I am just trying to clarify that this is the easiest and most efficient
I'm trying to find the most efficient way of dealing with this but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.