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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:30:21+00:00 2026-06-03T05:30:21+00:00

I am trying to implement a recursive depth first traversal on a weighted digraph(,

  • 0

I am trying to implement a recursive depth first traversal on a weighted digraph(, but it seems as if my output is always off. As in, I am getting extra visits to nodes. This is what I am currently working with:

void Dfs( int u, vector<bool> visited, vector < char > label, vector < vector < int > > adj)
{
visited[u] = true;
cout << label[u];

for ( int i = 0; i < (signed)visited.size(); i++)
    {
    if (visited[i] != true && adj[u][i] != 0)
            {
            cout << "->";
            Dfs( i, visited, label, adj);
            }
    }

}

Where label is the letter assigned to the verticies (A = 0, etc…), visited is a vector holding whether or not the vertex at a certain index has been visited, and adj is the adjacency matrix.

Say I have a graph and the correct depth first search is A->D->B->C->E, what I end up getting is A->D->B->C->E->C->B->E. If it helps, for this example the adjacency matrix looks like:

  |  A  B  C  D  E
--|---------------
A |  -  -  -  6  -
B |  -  -  8  3  2
C |  -  8  -  7  -
D |  6  3  7  -  -
E |  -  2  -  -  -
  • 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-03T05:30:22+00:00Added an answer on June 3, 2026 at 5:30 am

    The parameters to your function are pass by value; so your visited vector is not getting updated. Also, use pass by reference for your other vectors so that each recursive call is not copying the vectors.

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

Sidebar

Related Questions

I´ve been trying to implement a tail-recursive version of chainl1, but even with the
I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up
How can I implement recursive MySQL Queries. I am trying to look for it
So I'm trying to implement a recursive function that generates the entire game tree
I've been trying to implement a recursive backtracking maze generation algorithm in javascript. These
I'm trying to implement a recursive Fibonacci series which returns the value at an
im trying to implement the recursive Ackermann-Peter-Function in x86 NASM-Assembly. The Function is defined
I m trying to implement Tree Traversal PreOrder using yield return which returns an
I was trying to implement binary search tree but I think I have made
I'm trying implement pull to refresh on a ListFragment but right now none of

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.