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

  • SEARCH
  • Home
  • 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 6180727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:53:43+00:00 2026-05-24T00:53:43+00:00

I have a graph in Prolog represented by the edges & weights: connected(a,b,2). connected(b,e,1).

  • 0

I have a graph in Prolog represented by the edges & weights:

connected(a,b,2).
connected(b,e,1).
connected(b,l,5).
connected(b,g,2).
connected(c,s,2).
connected(d,a,2).
connected(d,k,4).
connected(d,l,7).
connected(e,m,2).

I need to write a predicate which takes a list of nodes & distance.

?- dist([a,b,e],X).
X=3

I’ve tried to write it, but it is very clumsy & doesn’t give the intended result.

The basic idea I have is:
If it is a list of 2 elements then see if they are connected.
If more then 2 elements in the list: see if the 1st element & 2nd element are connected,
recursively see if the next elements are connected.
I have defined 2 auxiliary predicates for head & tail.

dist([A, B], X) :-
    connected(A, B, X).
dist([A|B], Length) :-
    connected(A, hd(B,H,N), X),  % sees if A & next element in the list are connected
    dist(tl(B,H,N), Length1),    % recursive call with the list excluding element A
    Length is X + Length1.       

hd([H|T],H,Q).
tl([H|T],T,Q).

I am very new to Prolog land and I am still trying to comprehend the language semantics.
Please suggest an efficient way to go about this problem.

  • 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-24T00:53:43+00:00Added an answer on May 24, 2026 at 12:53 am
    dist([_], 0).            % path of length 0 has distance 0
    dist([A, B | T], L) :-
        connected(A, B, L1), % A and B are connected directly, the distance is L1
        dist([B|T], L2),     % the distance between B and the last element is L2
        L is L1 + L2.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To simplify the problem, I have a graph that contains nodes and edges which
I have a graph, with X nodes and Y edges. Weighted edges. The point
I have a graph object, that I need to serialize. Nomatter which members i
I have directed graph with lot of cycles, probably strongly connected, and I need
I have Directed Graph in networkx. I want to only keep those nodes which
Suppose I have a graph composed of nodes and edges, and I want to
Suppose we have a graph with bidirectional edges, no weights. How can I store
I have a graph with n nodes as an adjacency matrix . Is it
I have a graph which contains an unknown number of disconnected subgraphs. What's a
I have oriented graph. Graph can be strongly connected. Every vertex can have a

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.