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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:02:19+00:00 2026-06-04T16:02:19+00:00

In Prolog, how can I implement graph algorithm in order to find all path

  • 0

In Prolog, how can I implement graph algorithm in order to find all path in order to implement travel salesman problem in directed graph ?

example :

                                                                         graph
                    expected input     expected output                 X -----> Y
    start X             X  Y               X Z                         Y -----> T
    end   Z             Y  T               X Y Z                       T -----> Z
                        T  Z               X Y T Z                     Y -----> Z
                        Y  Z                                           X -----> Z
                        X  Z

As you know, in directed graph, there could be a cycle. However, no need to pass same point two times.

             graph             expected output             
           X ----> Y            
           Y ----> X               X Y Z
           Y ----> Z 

Why I am elimineting this case is because ;

      output :

      X Y X Y ...   Z
              ^^^
              god knows this length ( when program terminates )
              termination is np 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-06-04T16:02:20+00:00Added an answer on June 4, 2026 at 4:02 pm

    I placed some comment that explain what the code does…

    % your directed graph
    edge(x, y).
    edge(y, t).
    edge(t, z).
    edge(y, z).
    edge(x, z).
    
    % get a path from start to end
    path(Start, End, Path) :-
        path(Start, End, [Start], Path).
    
    % when target reached, reverse the visited list
    path(End, End, RPath, Path) :-
        reverse(RPath, Path).
    
    % take non deterministically an edge, check if already visited before use
    path(Start, End, Visited, Path) :-
        edge(Start, Next),
        \+ memberchk(Next, Visited),
        path(Next, End, [Next|Visited], Path).
    

    test:

    ?- path(x,z,P).
    P = [x, y, t, z] ;
    P = [x, y, z] ;
    P = [x, z] ;
    false.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a prolog program that can find the tiles that
I'm working on writing a simple Prolog interpreter in Java. How can I find
I am trying to understand how prolog represent first order logic. how can I
How can we get something from user in prolog : for example : animal(dog).
I want to limit executing of algorithm in prolog. Can you give me a
every one know the famous hanoi prolog and you can find it HERE and
I can't find any documentation on "not equal" sign in Visual Prolog. Please provide
In prolog we can get a solution for a map coloring problem as follows:
Can anyone recommend a Prolog compiler for .Net. The ones that I have been
How can I count nested list elements in Prolog? I have the following predicates

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.