Effective representation of solvable graphics in Prolog?

I would like to introduce a volatile graph in Prolog in an efficient way. I will look for subsets in the graph and replace them with other subsets.

I managed to get something using the database as my graph repository. For example, I have:

:- dynamic step/2. % step(Type, Name). :- dynamic sequence/2. % sequence(Step, NextStep). 

Then I use a few rules for the retract subsets that I have mapped, and replace them with new steps with assert . I really like this method ... it is easy to read and process, and I let Prolog do a lot of hard work on pattern matching.

Another way I know to represent graphs is by using lists of nodes and adjacencies. I have seen many sites using this method, but I doubt a little because it is more overhead.

Lead time is important to me, as is simplicity for myself.

What are the pros and cons for any approach?

+6
source share
3 answers

As usual: using a dynamic database gives you indexing, which can speed up your work (when searching) and slow down your work (when approving). In general, a dynamic database is not so good when you submit more often than you look. The main disadvantage is that it also greatly complicates testing and debugging, since you cannot test your predicates in isolation and must take into account the current implicit state of the database. Lists of nodes and joins are a good representation in many cases. Another view that I really like, especially if you need to save additional attributes for nodes and edges, is to use one variable for each node and use attribute attributes (get_attr / 3 and put_attr / 3 in SWI-Prolog) for example, [edge_to ( E1, N_1), edge_to (E2, N_2), ...] where N_i are variables representing other nodes (with their attributes), and E_j are also variables to which you can add additional attributes if necessary to store additional information (weights, capacities, etc.) about each rib.

+5
source

Do you consider using the SWI-Prolog RDF database? http://www.swi-prolog.org/pldoc/package/semweb.html

+1
source

as matte said, dynamic predicates have an extra cost.
in case you can plot the chart and then you don’t need to change it, you can compile the predicate and it will be as fast as a regular predicate.

usually in sw-prolog predicates are searched using hash tables for the first argument. (they change in the case of dynamic predicates)

another solution is lists of associations , where the cost of searching, etc. - o (log (n))
after you understand how they work, you can easily write an interface if necessary.

in the end, you can always use the SQL database and use the ODBC interface to send queries (although this sounds like iterating over the application you mentioned)

0
source

Source: https://habr.com/ru/post/893870/


All Articles