I need to store a non directed graph with multiple nodes (unknown to the size.. can be vast) on an adjacency matrix. Would a 2D arrayList be an efficient way to store this? If not, what is a better way to store this data? Any comment is appreciated thanks!

I think it would be easier if you would also store the size of the graph in the first line of the file. That way the allocation and whole process would be much more efficient, without a bit loss in anything else.

In case you need to do allocations as you increase the matrix, you should start with a reasonable size for the domain in question, and thing increase it with a multiplier of 2, so that along the time, the ammount of allocations tends to stabilize and you do not have to reallocate and recopy the contents as many times.

Hopes that helps!

I would definitely go for a 2d ArrayList. You can add rows/columns in O(n) time (n is the number of rows), if inserting at the end (which make sense). Access to the list is O(1), and removing an arbitrary row will be O(n^2).

The other option is to use a doubly linked list. In this case, insertion to the ends will be O(n) time, access is O(n), and removing a row is O(n^2).

So it seems that for a matrix, the ArrayList is best, but only because of more efficient access.

You say that the adjacency list could be "vast." If it is also sparse, you may be better off with some sort of sparse matrix representation. It might even be that, because of a vastly reduced memory footprint, it outperforms a 2d ArrayList.

Similar Questions

What's the most efficient way to tell if a single character is alphanumeric in PHP? I know I could do something like: function isSingleAlphaNum($chr){ if(!is_string($chr){ return false; } //capture th

I've seen a lot of discussion on SO related to rounding float values, but no solid Q&A considering the efficiency aspect. So here it is: What is the most efficient (but correct) way to round a fl

All these do it, but what is the most efficient way? $(selector).html(''); $(selector).text(''); $(selector).children().remove();

What's the most efficient way to calculate the first day of the current (Australian) Financial Year? The Australian FY begins on 01-July. E.g. SELECT dbo.FinancialYearStart('30-Jun-2011') returns 01-J

I need to store a billion histograms in SQL. These histograms have identical buckets but can have very large variation in their counts, however, most buckets are 0 a lot of the time. My initial attemp

Similar to how google analytics sends beacons from javascript that track events, what are the most efficient ways to collect that beacon data and return back to the client in the fastest time? For ex

I'm making an application, and I need to store snapshots of the canvas so I can use them later. I'm currently turning the canvas into a dataUrl, creating a 'new Image()' with it, and storing it into

I am downloading files using an URLConnection, DataInputStream and FileOutputStream. I am creating a huge byte[] with the size of the online file (with getContentLength()). The thing is that when I tr

What's the most efficient way to iterate through an entire table using Datamapper? If I do this, does Datamapper try to pull the entire result set into memory before performing the iteration? Assume,

I need too create an adjacency matrix in C++ for data read from a file like this ABC CDE 100 ZXY ABC 25 TER ZXY 11 POP ABC 66 ABC CDE POP TER ZXY ABC 100 CDE POP 66 TER 11 ZXY 25 #include <fstream&

I am planning on creating an app to store twitter sentiment in a database for specific topics. I don't need to store the tweets, just the sentiment of the topic. I can use either a SQL or noSQL DB. Th

What is the most efficient way to find the common prefix of many strings. For example: For this set of strings /home/texai/www/app/application/cron/logCron.log /home/texai/www/app/application/jobs/log

I have a collection of AR objects in a loop, that will likely be run many times, so I want to empty the collection at the end of each iteration. What's the most efficient way to do that? Edit 1 Here i

What is the best way to uppercase/lowercase a string in .Net without using toUpper/toLower?

I am working at an OS independent file manager, and I am looking at the most efficient way to copy a file for Linux. Windows has a built in function, CopyFileEx(), but from what I've noticed, there is

I am trying to implement radial average of matrix element in CUDA, where I have to find and print average of all neighbouring elements (including itself) of every matrix element. Following is what I e

What is most preferred way to center align text like this without using line break <br>?

I'm trying to figure out the most efficient way to send multiple queries to a MySQL database with PHP. Right now I'm doing two separate queries but I know there are more efficient methods, like using

I have got a nx3 adjacency matrix that contains nodes in the first two dimension and the correspondant weight in the third dimension. I want to filter the matrix for specific thresholds (for nodes ind

The following function returns a randomly generated adjacency matrix of size nxn, representing a graph. import random def random_adjacency_matrix(n): matrix = [[random.randint(0, 1) for i in range(n)]

As the title states, I want to know the most efficient ways to invoke a lazy loaded object property. Consider the following class definition: Class MyObject { private _stringList = null; public List&l

What is the most efficient way to implement a phonetic search in C++ and/or Java? By phonetic search I mean substituting vowels or consonants that sound similar. This would be especially useful for na

I have a collection of hierarchical items in an unsorted collection. Each of these items has a field previousItem: public class Item{ public Item previousItem; } What is the most efficient way of pro

I'm working with integers greater up to 128 bits long. They're used to store a large set of flags and I don't need to do any weird math with them, so I can treat it like a string in php to get around

I am going to have a lot of images and trying to find the most efficient way of storing these images to keep the page snappy. So far I have thought of just the two ways: load with javascript eg pictur

I'm writing a utility to calculate π to a million digits after the decimal. On a 32- or 64-bit consumer desktop system, what is the most efficient way to store and work with such a large number accura

I would like to use a 2-D array but I can't know its size in advance. So my question is: how to declare it? And then how to add values into it? String[,] tabConfig = new String[?, 4]; foreach(blabla w

Let's say that I'd like to store all the bullets, that anyone is shooting in my game to calculate new position every frame etc. If there are 10 players and everyone's shot rate is 10 shots per second

I am learning the way of computing Path Matrix from Adjacency Matrix(say AM1). A Path Matrix of a graph G with n vertices is a Boolean n*n matrix whose elements can be defined as: p[i][j]=1(if there

What's the most efficient way to convert a Matrix{T} of size 1*N or N*1 in Julia to a Vector{T}? For example, say I have a = [1,3,5] b = a' Both a and b are of type Array{Int,2} (i.e. Matrix{Int}). W

I am looking for the most efficient way to do matrix * matrix and matrix * vector operations for 3x3 rotation and 4x4 transformation matrices in C#. I currently store my matrices in multidimensional a

I have a quick graph theory question. I have a 13 x 13 adjacency matrix in Matlab. I've already taken just the lower diagonal (this is an undirected graph) and subtracted off the identity matrix (so

I need to find the position( or index ) say i of an integer array A of size 100, such that A[i]=0. 99 elements of array A are 1 and only one element is 0. I want the most efficient way solving this pr

I want to delete several specific values from a matrix (if they exist). It is highly probable that there are multiple copies of the values in the matrix. For example, consider an N-by-2 matrix interse

What is the most efficient way to convert a std::vector to a .NET List? To give some context, I am wrapping an unmanaged C++ class with C++/CLI. The C++/CLI class holds a pointer to the C++ class and

I was wondering what the most computationally efficient Python way of cracking this problem would be. Say you have two strings (or lists from splitting those strings--doesn't matter), this is the rig

I'm storing nested lists in my database using the adjacency list model. Each list might have 50-150 nodes, so we'll call it 100 nodes on average. The situation has come up where users want to clone a

Are there any algorithms for which adjacency matrices outperform adjacency lists? What about vice versa?

This question already has an answer here: Most efficient way to see if an ArrayList contains an object in Java 12 answers What is the most efficient way to do this ? I have a ArrayList list ca

What is the most efficient DHT? I am looking for name and/or some kind of implementation or related work, but I am not looking for the one that is most used. Efficient in terms of CPU execution cycles

What is the most efficient way to get an array of months, from a specified date, up until the present day, grouped by year. Eg getMonths(August 2012) would output array( array(Year=>2013, mo

Is it possible use a MySQL query to perform this kind of check? If not, what's the most efficient method or algorithm for performing this check in a language like PHP? I've been tasked with the follow

I wanted to know how to create adjacency matrix from euclidean distance matrix i've created before. for example : Edm = [0 7.7466 7.7534 0 3.7296 2.8171; 7.7466 0 0.0068 7.7466 4.0170 4.9295; 7.7534

I need to return some values based on a sample size. So if a user selects 5% they would get a query resulting in a random 5% of rows from the origonal query. Is there a way I can achieve this with jus

The website I am re-building uses its own API to gather most of its content (e.g. http://api.example.com/). Although the answer to this is quite self explanatory, I just want to be sure before I proce

How can I implement graph colouring in python using adjacency matrix? Is it possible? I implemented it using list. But it has some problems. I want to implement it using matrix. Can anybody give me th

In MS SQL 2000 and 2005, given a datetime such as '2008-09-25 12:34:56' what is the most efficient way to get a datetime containing only '2008-09-25'? Duplicated here.

On a oracle database I have a foreign key, without knowing its name, just the column_name and the reference_column_name. I want to write a sql script which should drop this foreign key if it exists, s

I need to store a collection of ints and doubles (representing nominal and real valued data) in c++. I could obviously store them all in a std::vector<double> , but this feels a bit wrong and do

I have a DIV with content that is inserted dynamically using jQuery. I want that div to appear as if it was enlarged from size 0px to the size it suppose to take when the div is rendered with its cont