This specification details how the graph should behave with:
- Changes in position
- Archiving
- Unarchiving
interface Base {
id: number | string,
previous_id: null | number | string
}
Every object in the graph should contain at least these two columns.
Constraints
- There can only be one node that depends on
null
- A node cannot depend on itself
- A node cannot have more than one unarchived dependent
- An archived node cannot be a parent to any other node
For demonstration, I will use the following graph with 5 nodes:
Note
Website used to simulate the graph: https://csacademy.com/app/graph_editor/
In which, the node 1
has the previous_id
set to null.
Archived nodes will have the suffix -a
To archive a node, we need to isolate it from the normal graph and connect the dependent of the node being archived to the node that the archived node originally depended on:
Observe our original graph:
Now we will archive node 2
, the graph should look like this:
If node 3
is archived, we should make node 4
depend on node 1
:
To unarchive a node, we need to find the direct dependent of the parent of the node being unarchived, where "direct" in this context means unarchived.
We will use the previous example:
Now let's unarchive node 2
, the graph should look like this:
If we were to unarchive node 3
, the graph should look like this:
To change the position of a node within the graph, we must perform two operations:
- If any node depends on the node being updated, we must update it to depend on the node that the node being updated originally depended on.
- If any node depends on the node that the updated node will depend on, we must update that node to depend on the node that is being updated.
Using the original graph, let's update the position of node 4
to depend on node 1
:
As we can see, we performed the following operations:
- Node
4
now depends on node1
- Node
2
now depends on node4
- Node
5
now depends on node3