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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:29:30+00:00 2026-06-01T13:29:30+00:00

The Algorithm Design Manual describes BFS and DFS quite well. The code for dfs

  • 0

The Algorithm Design Manual describes BFS and DFS quite well. The code for dfs in the book has a problem when deciding whether or not to avoid double processing edges. I found the Errata and applied the errata to the code, but still I think the refined code has a problem of checking double processing edges.

I paste the refined code as follows:

dfs(graph *g, int v) {
       edgenode *p;
       int y;
       if (finished) return;
       discovered[v] = TRUE;
       time = time + 1;
       entry_time[v] = time;
       process_vertex_early(v);
       p = g->edges[v];
       while (p != NULL) {
             /* temporary pointer */
             /* successor vertex */
             /* allow for search termination */
             y = p->y;
             if (discovered[y] == FALSE) {
                   parent[y] = v;
                   process_edge(v,y);
                   dfs(g,y);
             }
             else if (**(!processed[y] && parent[v] != y)** || (g->directed))
                   process_edge(v,y);
             if (finished) return;
             p = p->next;
       }
       process_vertex_late(v);
       time = time + 1;
       exit_time[v] = time;
       processed[v] = TRUE;
}

The place where I think there is a problem is marked via ** **.

So the questionable place is one of the conditions. Let’s assume it is undirected graph, so we can just ignore the condition of (g->directed).

Ok, let’s focus on parent[v] != y first. if parent[v] == y, of course, we don’t need to process edge v->y again because y->v is already processed once when processing vertex y.

And if parent[v] != y, my question is whether !processed[y] && is necessary or not.

So, if y is not v’s parent, and processed[y] == false which means y has been found (otherwise the execution can’t reach else if part) but not been processed, so y must be a grandma or above of v. This is a back edge, but no problem, we can process it as it hasn’t been processed yet.

Now what if processed[y] == true? I think this “if” will never happen and this condition will never be true.

Why? Ok, let’s assume processed[y] can be true. This means that all paths which include y have been processed and also all vertexes in those paths have been processed, right? So if this is a case, how can a “not yet finished processing” vertex v approach y?

I think in dfs, no vertex will ever approach a processed vertex, am I right?

So if we never will meat a processed vertex, we can just remove !processed[y] as it will be always true.

  • 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-01T13:29:32+00:00Added an answer on June 1, 2026 at 1:29 pm

    No, processed[y] == true could yield true.

    Look at the following graph, and assume the following [feasible] DFS traversal:

    v0,v1,v2
    

    graph example

    v0 starts, and then recursively invoke dfs(v1) after processing (v0,v1). Now, v1 processes (v1,v2) and recursively invoke dfs(v2). v2 sees that v0 is discovered and go to the else if statement, and sees that (v0,v2) was not processed and parent[v2] != v0 [v2 was discovered via v1].

    Now, when we go back from the recursion we reach back v0, and when we check the next “son” – it is v2. v2 was already discovered, so we go to else if, and v2 is not the parent of v0, so without the !processed[y] part, we would have processed (v0,v2) twice.

    The only thing that prevent that from hapenning is the fact that processed[y] == true

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

Sidebar

Related Questions

This Code is a code I built from the algorithm design manual book but
In Algorithm Design Manual , page 178 describes some properties of Graph, and one
In Algorithm Design Manual , there is such an excise 4-26 Consider the problem
Here is a excise from The book Algorithm Design Manual . In the bin-packing
I am currently reading the Algorithm Design Manual , but my mathematical notation has
Here is an exercise (3-15) in the book Algorithm Design Manual. Design a data
In the book The Algorithm Design Manual by Skiena, computing the mode (most frequent
Here is an exercise in the Algorithm Design Manual . Consider the problem of
I read the book Algorithm Design, chapter 1, it gave a very short description
I've been going through Skiena's excellent The Algorithm Design Manual and got hung up

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.