B
B
BranchInCode2019-11-12 13:06:30
Prolog
BranchInCode, 2019-11-12 13:06:30

How to implement a relationship for any two representatives located in different parts of the database?

Task: Create a query to the database (DB), which gives a hierarchical relationship for any two representatives of the genus (ie, it turns out who is the ancestor and who is the descendant).
Database:

parent('Rurik', 'Igor').
parent('Igor', 'Olga').
parent('Olga', 'Svyatoslav Igorevich').
parent('Svyatoslav Igorevich', 'Yaropolk').
parent('Yaropolk', 'Svyatopolk Okoyanny').
parent('Svyatoslav Igorevich', 'Vladimir Svyatoy').
parent('Vladimir Svyatoy', 'Yaroslav I Mudry').
parent('Yaroslav I Mudry', 'Svyatoslav').
parent('Svyatoslav', 'Oleg').
parent('Oleg', 'Vsevolod II').
parent('Yaroslav I Mudry', 'Izyaslav I').
parent('Izyaslav I', 'Svyatopolk').
parent('Yaroslav I Mudry', 'Vsevolod I').
parent('Vsevolod I', 'Vladimir Monomah').
parent('Vladimir Monomah', 'Mstislav Veliky').
parent('Mstislav Veliky', 'Izyaslav II').
parent('Vladimir Monomah', 'Yaropolk II').
parent('Vladimir Monomah', 'Yuriy Dolgoruky').
parent('Yuriy Dolgoruky', 'Mikhail I').
parent('Yuriy Dolgoruky', 'Vsevolod Bolshoe Gnezdo').
parent('Vsevolod Bolshoe Gnezdo', 'Yuriy II').
parent('Vsevolod Bolshoe Gnezdo', 'Yaroslav II').
parent('Vsevolod Bolshoe Gnezdo', 'Konstantin I').
parent('Yaroslav II', 'Andrey').
parent('Andrey', 'Vasiliy').
parent('Vasiliy', 'Konstantin II').
parent('Konstantin II', 'Dmitriy Suzdalskiy').
parent('Yaroslav II', 'Vasiliy Kostramskoy').
parent('Yaroslav II', 'Yaroslav III Tverskoy').
parent('Yaroslav III Tverskoy', 'Mikhail II Svyatoy').
parent('Mikhail II Svyatoy', 'Alexandr II').
parent('Yaroslav II', 'Alexandr Nevskiy').
parent('Alexandr Nevskiy', 'Andrey Gorodeckiy').
parent('Alexandr Nevskiy', 'Dmitriy Pereyaslavskiy').
parent('Alexandr Nevskiy', 'Daniil Moskovskiy').
parent('Daniil Moskovskiy', 'Yuriy III Moskovskiy').
parent('Daniil Moskovskiy', 'Ioan I Kalita').
parent('Ioan I Kalita', 'Simeon Gordiy').
parent('Ioan I Kalita', 'Ioan II Krotkiy').
parent('Ioan II Krotkiy', 'Dmitriy Donskoy').
parent('Dmitriy Donskoy', 'Vasiliy I').
parent('Vasiliy I', 'Vasiliy II Temniy').
parent('Vasiliy II Temniy', 'Ioan III').
parent('Ioan III', 'Vasiliy III').
parent('Vasiliy III', 'Ioan IV').
parent('Ioan IV', 'Fedor').

Example:
?- relations('Rurik', 'Oleg').
Rurik - ancestor, Oleg - descendant
How to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antonio Solo, 2019-11-12
@solotony

1) add an "ancestor" field
2) add a "child" field
3) add a table with an "ancestor, descendant" relationship

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question