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 8636023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:07:19+00:00 2026-06-12T10:07:19+00:00

I am trying to find a path in a graph in Prolog. I managed

  • 0

I am trying to find a path in a graph in Prolog. I managed to get this working with some snippet I found online. However, it keeps track of nodes to avoid visiting them twice, while I need it to not visit the same edge twice. This probably comes down to the same thing in most graphs, but because I want to use it to calculate paths from points on edges to other points on edges, I don’t want it to return a path from a point to an adjacent node to the opposite node (say, if we had an edge AC from node A to node C, with a point B in the middle, A-C-B would not be an acceptable path because it traveled AC twice).

Here is my current code:

path(A,B,Nodes,Edges) :- path1(A,[B],Nodes,Edges).
path1(A,[A|P1],[A|P1],[]).
path1(A,[Y|P1],Nodes,Edges) :- 
   connected(X,Y,Edge), \+ memberchk(X,[Y|P1]), path1(A,[X,Y|P1],N1,E1), append(E1,[Edge],Edges).

connected(a,c,ac). % connected(Node1,Node2,Edge) need not represent an edge:
connected(a,b,ac). % it may also refer to a point on the edge.
connected(b,c,ac).

For the above example this returns for example:

path(a,b,[a,b],[ac])
path(a,b,[a,c,b],[ac,ac])

Now I thought I could simply replace the \+ memberchk(X,[Y|P1]) with \+ memberchk(Edge,S) to make sure the same edge doesn’t end up in the path twice, which would solve the problem. However, when I do so, Prolog says that there is no path.

Can someone explain to me where this is going wrong?

  • 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-06-12T10:07:20+00:00Added an answer on June 12, 2026 at 10:07 am

    This answer helped me out a great deal: apparently it is because you can’t use the path that hasn’t yet been instantiated to check for loops. It suggests using freeze\2, but I don’t know how to handle that in JIProlog. I’ll open a new question for that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find a path between two nodes in a graph ,
I'm just trying to find a way to get the path to the directory
While trying to find an answer to Android Jasper Reporting I found out that
I'm trying to find the appropriate place to store a system path in PostgreSQL.
I am trying to find a way to configure a static context path i
I'm trying to write a piece of code that find shortest path in an
I'm trying to find the right direction and path i need to take in
I am trying to find code that would help me simplify a curved path
I am trying to find a way to add Git to the Windows path.
I am trying to find the path for the android database files on Ubuntu

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.