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

  • Home
  • SEARCH
  • 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 8989563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:17:29+00:00 2026-06-15T22:17:29+00:00

Below is the general code for DFS with logic for marking back edges and

  • 0

Below is the general code for DFS with logic for marking back edges and tree edges. My doubt is that back edges from a vertex go back and point to an ancestor and those which point to the parent are not back edges (Lets assume undirected graph).
In an undirected graph we have an edge back and forth between 2 vertices x and y. So after visiting x when I process y, y has x as an adjacent vertex but as it’s already visited, the code will mark it as a back edge.

Am I right in saying that? Should we add any extra logic to avoid this in case my assumption is valid?

DFS(G)
for v in vertices[G] do
    color[v] = white    
    parent[v]= nil
    time = 0        

for v in vertices[G] do
    if color[v] = white then
    DFS-Visit(v)

Induce a depth-rst tree on a graph starting at v.

DFS-Visit(v)
    color[v]=gray
    time=time + 1
    discovery[v]=time
    for a in Adj[v] do
       if color[a] = white then
        parent[a] = v
        DFS-Visit(a)
        v->a is a tree edge
       elseif color[a] = grey then  
        v->a is a back edge
    color[v] = black
    time = time + 1

white means unexplored, gray means frontier, black means processed

  • 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-15T22:17:30+00:00Added an answer on June 15, 2026 at 10:17 pm

    Yes, this implementation determines frontier nodes only by color (visited, not visited) and, thus, doesn’t separate parent and ancestor nodes. So, each edge in DFS search tree will be marked as back edge.

    In order to separate tree and back edges you need to separate edges to parent and ancestor nodes. Simple way is to provide parent node as a parameter to DFS-Visit (p). For example:

    DFS-Visit(v, p)
     color[v]=gray
     time=time + 1
     discovery[v]=time
     for a in Adj[v] do
       if color[a] = white then
           parent[a] = v
           DFS-Visit(a,v)
           v->a is a tree edge
       elseif color[a] = grey and (a is not p) then
           v->a is a back edge
     color[v] = black 
     time = time + 1
    

    UPDATE: I haven’t noticed you already store parent nodes. So, there is no need to introduce parameter:

    DFS-Visit(v)
     color[v]=gray
     time=time + 1
     discovery[v]=time
     for a in Adj[v] do
       if color[a] = white then
           parent[a] = v
           DFS-Visit(a)
           v->a is a tree edge
       elseif color[a] = grey and (a is not parent[v]) then
           v->a is a back edge
     color[v] = black 
     time = time + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a general question on the class definition and its use..THe below code
Below is code to an inherited ComboBox. The issue is that the ComboBox is
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
below is the code to download a txt file from internet approx 9000 lines
Below is the code from a plugin I use for sitemaps. I would like
Just a general c++ curiosity: This code below shouldn't compile because it's impossible to
Below is my code: function email(from, to, subject, body){ if(subject == Website Feedback){ to
Below I have included my code that uploads multiple images to a folder and
As you can see from the code below we, use TinyMce a lot in
I've recently seen some code similar to that outlined below. public void someMethod() {

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.