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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:27:18+00:00 2026-06-08T22:27:18+00:00

I know the answer to this particular question is O(V + E) and for

  • 0

I know the answer to this particular question is O(V + E) and for a Graph like a tree, it makes sense because each Vertex is being explored once only.

However let’s say there is a cycle in the graph.

For example, let’s take up an undirected graph with four vertices A-B-C-D.
A is connected to both B and C, and Both B and C are connected to D. So there are four edges in total. A->B, A->C, B->D, C->D and vice versa.

Let’s do DFS(A).

It will explore B first and B’s neighbor D and D’s neighbor C. After that C will not have any edges so it will come back to D and B and then A.

Then A will traverse its second edge and try to explore C and since it is already explored it will not do anything and DFS will end.

But over here Vertex “C” has been traversed twice, not once. Clearly worst case time complexity can be directly proportional to V.

Any ideas?

  • 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-08T22:27:20+00:00Added an answer on June 8, 2026 at 10:27 pm

    If you do not maintain a visited set, that you use to avoid revisitting already visited nodes, DFS is not O(V+E). In fact, it is not complete algorithm – thus it might not even find a path if there is a one, because it will be stuck in an infinite loop.

    Note that for infinite graphs, if you are looking for a path from s to t, even with maintaining a visited set, it is not guaranteed to complete, since you might get stuck in an infinite branch.

    If you are interested in keeping DFS’s advantage of efficient space consumption, while still being complete – you might use iterative deepening DFS, but it will not trivially solve the problem if you are looking to discover the whole graph, and not a path to a specific node.

    EDIT: DFS pseudo code with visited set.

    DFS(v,visited):
      for each u such that (v,u) is an edge:
           if (u is not in visited):
                visited.add(u)
                DFS(u,visited)
    

    It is easy to see that you invoke the recursion on a vertex if and only if it is not yet visited, thus the answer is indeed linear in the number of vertices and edges.

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

Sidebar

Related Questions

I know there's no one answer to this question, but I'd like to get
I know there must be a really simple answer to this question, but I
I know there are duplicates of this question, and the answer was to refactor
This is inspired by this question and the comments on one particular answer in
I would love to have an answer to this particular question, asked at the
this may seem like a trivial question and one that the answer serves no
I think I already know the answer to this one, but i hope maybe
I think I already know the answer to this but thought I would ask
I think i already know the answer to this, but i cannot find anything
I'm afraid I know the answer to this but I'll ask on the longshot

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.