1

I would like to implement a tool that generates graphs whose memory will be allocated on a data structure so called "Tape". You can think of a Tape as an array of elements, each of which holds "Node ID", links to its "Parent Node" as well as its "Child Nodes".

What I am looking for is an approach in which identifying available slots in the array is cheap so that when a new node is to be added, an empty slot can be quickly identified.

And what if I implemented the Tape using a dynamic array? In the situation where the size of the array needs resizing, can I avoid copying the entire Tape over to a newly allocated array?

Anyone here has any idea ?

9
  • What is a graph? Does it have axes? Commented May 10, 2012 at 14:05
  • Is yours the kind of graph with an x axis and a y axis, or the kind of graph that salesman travel? Commented May 10, 2012 at 14:10
  • @Matt, takwing means a graph in the mathematical way (like a network of nodes), not a business graphic. Commented May 10, 2012 at 14:10
  • To be more specific, a graph will be generated from a mathematical function where its nodes represent arithmetic operations such as addition, subtraction, multiplication, division and intrinsic functions such as sin, cos , tan , exp and so on and its edges represent derivatives of the associated mathematical relationship between the parent and the child nodes. Commented May 10, 2012 at 14:12
  • So it is a direct acyclic graph, DAG. Commented May 10, 2012 at 14:13

1 Answer 1

4

I assume that you want to allocate a big 'Tape' beforehand of e.g. thousands of nodes.

You should combine 2 concepts:

  • First store the last used entry on your tape. The next time a new entry is needed, just pick the one just after the last used one.
  • Second keep a free list. Use the first 4 bytes (or 8 bytes in 64-bit) of your tape entry as a pointer to the next free entry. The beginning of the tape should point to the first free entry.

Whenever a entry on the Tape is freed, add it to the free list.

Whenever a new entry is needed in the tape:

  • check whether there are elements in the free list. if there are take the first entry and remove it from the free list
  • if the free list is empty, use the last used entry and take the one immediately after it.

You can also combine this with a reallocation scheme, where you keep the total allocate size of your tape and reallocate if the last used entry reaches the end of the tape.

Sign up to request clarification or add additional context in comments.

1 Comment

I did developments in C for more than 10 years, and with now an experience of 10 years in C++ I know the tricks to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.