Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 209003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:52:35+00:00 2026-05-11T17:52:35+00:00

Does anybody know, how to get a list of leaf nodes in Prolog? Let’s

  • 0

Does anybody know, how to get a list of leaf nodes in Prolog?

Let’s say, I have a simple directed graph described by these directed edges:

de(0,1).
de(0,2).
de(2,3).
de(2,4).
de(3,4).
de(4,5).

Now, how to recursively browse the graph and write a list of those 2 leaf nodes (node 1 & 5)?

Thanks for any answer!

Edit:

Well, I have 1st predicate written & working:

isLeaf(Node) :-
not(de(Node,_)).

but now, i have no idea how to traverse the graph and write the output list of leaf nodes. I know, it’s quite easy but I have no experience in this way of thinking and programming:(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:52:35+00:00Added an answer on May 11, 2026 at 5:52 pm

    You need to define a predicate is_leaf/1 that is a generator,
    i.e. it instantiates the input variable with possible solutions.

    Something like this:

    % Directed graph
    de(0,1).
    de(0,2).
    de(2,3).
    de(2,4).
    de(3,4).
    de(4,5).
    
    % If Node is ground,
    %         then test if it is a child node that is not a parent node.
    % If Node is not ground,
    %         then bind it to a child node that is not a parent node.
    is_leaf(Node) :-
        de(_, Node),
        \+ de(Node, _).
    

    Usage examples:

    ?- is_leaf(Node).
    Node = 1 ;
    Node = 5.
    
    ?- is_leaf(Node), writeln(Node), fail ; true.
    1
    5
    true.
    
    ?- findall(Node, is_leaf(Node), Leaf_Nodes).
    Leaf_Nodes = [1, 5].
    

    Your solution immediately calls not. (Btw, SWI-Prolog recommends using \+ instead of not.)

    isLeaf(Node) :-
        not(de(Node,_)).
    

    This means that your isLeaf/2 is not a generator: it either fails or succeeds (once), and never binds the input argument if it happens to be a variable.
    Also, it never tests that the input is a leaf, it just tests if it’s not a parent node.

    % Is it false that 1 is a parent? YES
    ?- isLeaf(1).
    true.
    
    % Is it false that blah is a parent? YES
    ?- isLeaf(blah).
    true.
    
    % Is it false that 2 is a parent? NO
    ?- isLeaf(2).
    false.
    
    % Basically just tests if the predicate de/2 is in the knowledge base,
    % in this sense quite useless.
    ?- isLeaf(Node).
    false.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know of or have a detailed list of how the BCL in
Does anybody know, if i have an object like $('#input') how can I get
Possible Duplicate: List selectors for obj-c object Does anybody know how to get all
Does anybody know how to get thumbnail (still image) from 3gb video file? First
Does anybody know how I can get the number of the elements (rows*cols) returned
Does anybody know a good and free add-in for Visual Studio 2005 to get
Does anybody know of a way to list up the loaded plugins in Vim
Does anybody know how to get started hosting a cakePHP on Amazon Web Services,
Does anybody know, how to grab the single cooks distance plot that you get
Does anybody know any good resources for learning how to program CIL with in-depth

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.