Memory efficient way to represent shortest path?

流过昼夜 提交于 2021-01-28 19:08:57

问题


Say I am given the shortest path from node A to node B. The path is simply a list of edges, so a path A->B->C, will be represented as [(A,B),(B,C)].

Currently, each node is of type string, a path from one node to another, say (A, B), is a set of string, and the path is a list of sets.

Now, the path consists of over 60k edges, and it must be saved to a database for later retrieval.

So obviously, I need a very nice way to represent this path in C++ such that:

  1. the size of the path is significantly reduced compared to the original,
  2. when retrieving the path from the database, the retrieval time is small enough.

Can anyone provide me with some insight?

Thank you.


回答1:


It's hard to see exactly what you want, but maybe you just store your path like:

std::map<int, std::string> route; // int is how many edges you've already travelled (starting from 0 for the first node) and the string is the node letter you're at by that point.

This gives you ordered iteration of your route, and decent efficiency for operations on the map (search, removal, and insertion operations have logarithmic complexity).




回答2:


Why do you want to represent edges? Store whole paths in the database. And feel free to store them as strings (as @vahancho mentioned, use ABCD instead of ABBCCD). It is obvious which edges belong to this path when you look at it. And you may even use wildcards later to search, which paths contain an edge like BC by using %BC% in relational databases.




回答3:


Memory efficiency depends on how you can represent your data (path), if the example you have provided is the case, then you can try Mr. vahancho suggestion.


You can use std:: unordered_map it's fair for both memory and time complexity, or might be better to try to build your own data structure for storing only useful information, hashing your path string,... etc, may by wrapping SLT and overloading some functionalities.

In case of string like you provide ABBCCD then you may instead of store all paths as strings you can try store them as g-trie data-structure by this way you are going to reduce the redundancy on the graph significantly as ABB and AB paths, going to be stored as three nodes of char (1 byte),the idea is ignoring common substructure such (AB).

You can check this paper too.




回答4:


If you copy the strings alot in the c++ code and they are very large you might consider using a pointer or smart pointer instead. There's also the option of hashing the strings, but that will mostly be usefull for comparisons. Generally why worry about using strings in the first place, except if they are very large like text documents.

As for container, you could just use a std::vector<std::string>

The most efficient way of storing the shortest path of A to B in the end is just storing the end points, then only calculate the result when needed.



来源:https://stackoverflow.com/questions/62572763/memory-efficient-way-to-represent-shortest-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!