Original: Step-by-step write algorithm (save of diagram)
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Previous several blogs, we have the basic definition to the diagram, also introduced the diagram creation, the diagram addition and the deletion and so on. Today, let's talk about how the diagram is stored in the peripherals. These external devices can be of various types, for example, can be hard disk, SD card, network hard disk and so on. In essence, the topic we are discussing today is how to keep the data of the graph permanently locally. And, if you need to load this data, you can quickly restore the image to its original appearance. The graph data structure has been remembered not very clear friends can review the following code, recall our previous definition method.
typedef struct _LINE{INT end;int weight;struct _line* Next;} line;typedef struct _vectex{int start;int number; line* neighbor;struct _vectex* Next;} vectex;typedef struct _graph{int count; vectex* Head;} GRAPH;
There are many pointers in the data structure. So how should you keep it in external memory? Because for external memory, pointers are meaningless. It is clear that we are thinking a little bit. We can actually think of all these pointers as offset values. Graph is made up of many points, and there are many side connections in the structure of points, so we can save the data in the following order.
/** ---------------------------------------------------------------------------* | GRAPH | Vectex1 | Vectex2 | ...... | Vectex N | LINE1 |......| Line n |* ---------------------------------------------------------------------------*/
So what is the offset value arrangement?
a) graph structure
Head is the offset address of the first Vectex.
b) Vectex structure
The neighbour records the offset position of the first edge, and next records the offset value of the next node.
c) Line structure
Next is the offset value for the next edge.
However, if we do some optimizations, we will save less data. For example, the first Vectex node is behind graph, so the head does not actually need to be saved, and in the VECTEX structure, because we need to know the offset value of line, the offset of the neighbour needs to be known, but next is no longer needed. Because the Vectex data itself is tied together, and finally because we need to put all of the same Vectex side together, then the next data in the line structure can actually be omitted. So the modified data structure should basically be like this:
typedef struct _LINE{INT end;int weight;} line;typedef struct _vectex{int start;int number; line* neighbor;} vectex;typedef struct _graph{int count;} GRAPH;
With the data structure above, loading data from the outer layer is a reverse operation, no longer complicated.
Step-by-step write algorithm (save the diagram)