I want to implement a graph in C. I am confused on how should I store each node. I was first thinking of using a linked list but how can I store the next nodes connected to one node.
Any ideas what data structure should I use and how should I use it?
There are some well known ways to do that.
One is to use a bidimensional array of size [n][n] where n is the number of nodes. And then set graph[a][b]= 1 if there is a link from a to b. This method is in general fast but uses a lot of memory, expecially if there are not so many links and many nodes.
Another way is to make a list (or an array, for the matter) of all nodes and set the content of everyone of them to point to a dynamic array or to a list of nodes it is linked to.
The data structure that is helpful in case your graph is sparse is an adjacency list(linked list of linked lists) that is when you have few connections(edges) between the vertices.
If your graph is dense then use an adjacency matrix(nxn) 2 dimensional array that is the case your vertices have lots of edges between them.
Related
I have managed to implement the solution with storing an index of each node, but is it possible to it without the indexes? My assignment is to implement a bloxrolz game, sort of. In the example I'm about to show you, I am out of ideas how to write code for checking if two nodes are adjacent to one another.
This is just a random field for the game. When reading from a file, I store every charachter here in a linked list, with a x,y coordinate and ID(if the char is not '-'). Only those that are not "-" are to be stored in a adjacency matrix since they represent the playground and "-" are nodes that could be changed to "-".
I have successfully implemented a solution in a way that I'm not sure if its legitimate or not. Can you help me figure out how to do this without the indexes.
An adjacency matrix is by definition a square matrix where each node is represented by a row and a column. If the entry at row i and column j is 1 (or some value you pick), then those two nodes are connected in the graph. Therefore, you need some way to map rows and columns to nodes and vice versa. Putting the nodes in a list and using each node's index in the list is one way to do that, but it's not the only way. You could give each node an ID number and, as long as each node's ID is unique and within the bounds of the matrix, you can use that. You could store a pointer to the corresponding node at the beginning or end of each row and column. In short, any method that lets you find the entries in the matrix for a given pair of nodes is sufficient.
That said, because you're talking about ordered lists of rows and columns, any method you pick will be similar to putting the nodes in a list and using the index of a node in the list. Think hard about what problem you're actually trying to solve.
I want to merge N ordered linked lists into one ordered linked list.
However, I also want the individual ordered linked list to be retained. I am successful in doing so using an array of Node pointers with each array element as a node corresponding to first node of individual lists. However, with array size fixed, I cannot proceed with merging more than array size.
Now my question is, is there a way I can dynamically change array size. If not I am thinking of using Queue instead to hold the first node of individual lists in a queue linked list. Am I going right or wrong? Please give me some tips regarding the problem.
It sounds like you are looking for realloc, assuming you dynamically allocated your array in the first place.
I am looking for the best way to place a tree into an array
The idea is to follow this principle : Array Implementation of Trees
but I'am stuck on how to know what nodes are the children and what nodes are at the same level, because I'am not using a binary tree.
I might have to store ASCII but I can't simply allow arrays of 256 pointers !
Any idea would be welcome.
The purpose of this, is to send an array (tree) to my GPU, instead of using structures.
Well, here is my idea of converting tree into an array.
Take an array of size MAX_VAL, which is the total number of nodes in the tree. The type of the array should be same as that of a node but with one extra field. Its the index value for its parent. You store each node in this way. Store the root node at first position. Say 1. Now the child nodes of this node are stored subsequently with the extra field storing 1 (since this was where root was stored).
Apply this procedure on all nodes and you are done. You can get back the tree, by a simple recursive call on each node.
Hope this helps. :) :)
Ahnentafel lists are very big if not near-perfectly balanced. My guess is your tree isn't going to be balanced, so the benefit of implicit parent/child pointers will outweigh the cost. I'm never seen a non-binary Ahnentafel list, but I assume it's possible (were you asking for the implicit equations?).
Could you keep a sorted list of child pointers for each node (ASCII character + pointer/index)? In this case it might be best, as others suggest, to construct the tree using pointers and allow the children to grow. Then pack all the nodes into a list: work out an order to place the nodes, use prefix sums for their offsets into the array, store the position indices on each node and finally copy the children lists into the array (replacing the children pointers with list indices can be done by following the pointers and querying the index from the previous step).
Traversing to a child in CUDA won't be constant time, but since the order is know you can use a binary search to speed things up.
Is it possible to make an array of arrays in C?
More specifically, is it possible to make a list (array) of adjacency lists (arrays)?
And if so, how?
My textbook uses a list of adjacency lists for Dijkstra's algorithm (and in a lot of other algorithms) instead of using an adjacency matrix, though the book is in pseudocode and even then it makes no reference whatsoever on how to implement the list...
Supposedly, list of adjacency lists are more efficient in a lot of algorithms than adjacency matrices. I thought of using linked lists, but AFAIK it would be the same as using an adjacency matrix, so it would make no point at all to implement it this way...
Did you mean a multidimensional array?
int mdarr[10][20];
Is it possible to make an array of arrays in C?
Yes.
More specifically, is it possible to make a list (array) of adjacency
lists (arrays)?
Yes, it can be implemented using a linked list (or array in your case) of linked lists.
And if so, how?
One list (or array) could maintain nodes, each of which will point to the actual adjacency list as well as contain information about the current point of the graph. This can be implemented creating a struct with the relevant data (one information field and one next pointer, and one pointer to adjacency list)
The second list will be the actual adjacency list of each node. It will be pointed to by the corresponding nodes of the first list. It will contain nodes, each of which will be a graph point connected to the corresponding point in the initial list. It can be implemented similarly, by creating a struct having the relevant data (one field for the next pointer, and one information field).
This is not a multi-dimensional array but is completely different. This will use less space than an adjacency matrix, if your graph is sparse.
As an example, lets take A, B, C, D to be the nodes of an undirected graph such that A is connected to B and C.
Then the lists will be like this :
A -> B -> C -> D
| | |
B A A
|
C
Did you mean a three-dimensional array?.
Yo define an adjacency matrix like this aMat[3][3] (graph of three elements).
So, you could define, for example 4 graphs of three elements each like this: aMatMulti[4][3][3].
I am going to build my own bejeweld in c with Glut and Opengl game info(wiki).
short game info
The game consists of a grid, lets say 8x8, with different gems. When you create a row/ column, of at least 3 gems of the same type, by swapping two adjacent gems, the gems will dissapear. The playing field will be filled from the top.
Two design consepts
Linked lists vs array
There are several ways to implement this, but I would like the most easy to maintain and efficient way. My idea's:
(the 2D array represents the playing field/grid).
1)A linked list containing all the pointers to the gem objects (instances). And a 2D array which contains all the pointers to the linked list elements for fast acces of the list. The linked list can be used for easy printing the game objects. Every linked list object comes with a location (gems, score board, time ets)
2) The same idea as above, but a linked list for every row or column and a linked list for the other game elements
3) Only a 2D array with pointers to the objects/instances (gems).
required operations
-swap elements (gems), by swapping the pointers?
-"create" new elements (at the top of the grid)
-check for rows/colomns with at least 3 of the same sort of gems next to each other
I hope I made it all clear. What do you think will be the best solution?
I think it's overkill to use linked lists. The only thing I can think of that they would be useful is dropping gems from the top, but even that is fairly trivial with a 2D array.
A 2d array of int should be no problem. It's simple and fast.
I would use a 2D array. It's easier to use, since you can directly access an element rather than iterating through the list until you reach to a required element.
However, I can see the advantages of a linked list too, you can just delete a member after you swap and get 3 in a row; you can just add a new element to the head/tail after iterating through each element and shifting it like you would have to if using arrays.
My suggestion is implement both. You sound a bit without experience. You have to write more code in order to be able to answer these questions by yourself the next time you stumble on them. A good design will allow you to easily replace this part of the implementation so try to do that and learn from it.