Thread: C linked list or hash table for matrix operations and representation

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    45

    C linked list or hash table for matrix operations and representation

    I have matrix in C with size m x n. Size n isn't known. I want to have operations on matrix such as : delete first element and find i-th element. (where size m woudn't be too big , from 10 to 50 columns).

    What is more efficient to use, linked list or hash table? How can I map each column of matrix to different element of linked list or hash table; depends what I choose to use?


    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anya
    I want to have operations on matrix such as : delete first element and find i-th element.
    Those don't really sound like quintessential matrix operations though. What does it mean to "delete first element"? What is the "i-th element" of this matrix?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    Delete first element means to delete first column from matrix, and delete i-th element means to delete it-h column from the matrix. So what is better in general to map 2d array to linked list or to hash table, and how?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anya
    Delete first element means to delete first column from matrix, and delete i-th element means to delete it-h column from the matrix.
    Oh, so your matrix is in column-major order?

    What I mean is that when people implement a matrix, a fairly common solution is to use an array of arrays such that the outer array is an array of rows and each inner array is an array of matrix entries. This way, we would use array notion for matrix[i][j] to denote the entry at the ith row and jth column, much like the subscript notation for denoting an entry in a matrix in mathematics. This is called row-major order. To remove the first column from a matrix represented in this way means removing the first element of each of the rows. This would be potentially very inefficient if "removing an element" means shifting the elements that come after by one position.

    But your matrix, on the other hand, seems to require a representation where it is efficient to remove columns. We could still use a row-major ordering by having an array of linked lists such that each linked list corresponds to a row in the matrix and each node in each linked list corresponds to an entry in the matrix. This way, removing the ith element just requires linking the previous and next nodes to exclude it. Problem is, finding the ith element will take linear time, so if you want to remove a column in the middle, you're going to get time complexity of O(m * n). Alternatively, you can have a linked list of arrays such that each array corresponds to a column in the matrix (hence column-major order). This way, finding the ith column will take O(n) time, but deletion takes O(1) time.

    Using a hash table does not appear appropriate since you have the notion of ith element, i.e., some kind of ordering.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I don't think you know what a hash table is, if you think it might have even the slightest suitability to the problem you describe.

    But in any case, the answer is neither. An array should always be the first data structure you consider.
    Even with having to copy elements over to remove a column, (which I might add is an extremely unusual and uncommon thing to do) it still tends to beat the time taken to follow pointers in a linked-list, due to what is known as the pre-fetcher.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Do you actually have a matrix? Or do you just have a table?

    The combination implied seems more like you want to build data structure not a mathematics construct.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-06-2013, 08:22 AM
  2. Graphs, linked list representation
    By stefanyco in forum C++ Programming
    Replies: 0
    Last Post: 12-08-2011, 12:01 AM
  3. Help in Linked List Operations
    By aren34 in forum C Programming
    Replies: 54
    Last Post: 03-22-2011, 08:35 PM
  4. linked list representation of set
    By Reemj in forum C++ Programming
    Replies: 3
    Last Post: 12-31-2010, 10:25 AM
  5. Array of Linked Lists - Hash table
    By FortinsM3 in forum C Programming
    Replies: 12
    Last Post: 02-25-2009, 10:20 PM

Tags for this Thread