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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:49:40+00:00 2026-05-19T11:49:40+00:00

Suppose I am given a undirected tree and I need to find a path(the

  • 0

Suppose I am given a undirected tree and I need to find a path(the only path) between two nodes.

What is the best algorithm to do it.I probably could use a Dijkstra’s algorithm but there a probably something better for trees.

C++ example would be helpful but not necessary

Thank you

  • 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-19T11:49:41+00:00Added an answer on May 19, 2026 at 11:49 am

    Supposing you have

    struct Node
    {
        std::vector<Node *> children;
    };
    

    then what could be done is traversing the whole tree starting at root keeping the whole chain during the traversal. If you find e.g. node1 then you save the current chain, if you find node2 then you check for the intersection… in code (UNTESTED):

    bool findPath(std::vector<Node *>& current_path, // back() is node being visited
                  Node *n1, Node *n2,                // interesting nodes
                  std::vector<Node *>& match,        // if not empty back() is n1/n2
                  std::vector<Node *>& result)       // where to store the result
    {
        if (current_path.back() == n1 || current_path.back() == n2)
        {
            // This is an interesting node...
            if (match.size())
            {
                // Now is easy: current_path/match are paths from root to n1/n2
                ...
                return true;
            }
            else
            {
                // This is the first interesting node found
                match = current_path;
            }
        }
        for (std::vector<Node *>::iterator i=current_path.back().children.begin(),
                                           e=current_path.back().children.end();
             i != e; ++i)
        {
            current_path.push_back(*i);
            if (findPath(current_path, n1, n2, match, result))
              return true;
            current_path.pop_back(); // *i
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose there is given two String: String s1= MARTHA String s2= MARHTA here we
Suppose there are given two integers, a and b, and we know that a>b.
Suppose we are given a small number of objects and distances between them --
suppose you are given the following problem. You have two index sets that have
Suppose you're given an URL, http://site.com . How do you find out what it's
Suppose you are given an input string: my name is vikas Suggest an algorithm
I need to extract the dateformat from a given date. How am I suppose
Suppose in a given binary tree if each node contains number of child elements
Suppose I am given a weighted, connected graph. I'd like to find a list
Suppose I'm given a large dictionary in flat file with 200 million words and

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.