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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:15:53+00:00 2026-06-02T02:15:53+00:00

I have coded DFS as the way it is on my mind and didn’t

  • 0

I have coded DFS as the way it is on my mind and didn’t referred any Text book or Pseudo-code for ideas. I think I have some lines of codes that are making unnecessary calculations. Any ideas on reducing the complexity of my algorithm ?

vector<int>visited;

bool isFound(vector<int>vec,int value)
{
    if(std::find(vec.begin(),vec.end(),value)==vec.end())
        return false;
    else
        return true;
}

void dfs(int **graph,int numOfNodes,int node)
{
    if(isFound(visited,node)==false)
        visited.push_back(node);
    vector<int>neighbours;
    for(int i=0;i<numOfNodes;i++)
        if(graph[node][i]==1)
            neighbours.push_back(i);

    for(int i=0;i<neighbours.size();i++)
        if(isFound(visited,neighbours[i])==false)
            dfs(graph,numOfNodes,neighbours[i]);
}

void depthFirstSearch(int **graph,int numOfNodes)
{
    for(int i=0;i<numOfNodes;i++)
        dfs(graph,numOfNodes,i);
}

PS: Could somebody please sent me a link teaching me how can to insert C++ code with good quality. I’ve tried syntax highlighting but it didn’t work out.

  • 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-02T02:15:54+00:00Added an answer on June 2, 2026 at 2:15 am

    Your DFS has O(n^2) time complexity, which is really bad (it should run in O(n + m)).

    This line ruins your implementation, because searching in vector takes time proportional to its length:

        if(std::find(vec.begin(),vec.end(),value)==vec.end())
    

    To avoid this, you can remember what was visited in an array of boolean values.

    Second problem with your DFS is that for bigger graph it will probably cause stack overflow, because worst case recursion depth is equal to number of vertices in graph. Remedy to this problem is also simple: use std::list<int> as your own stack.

    So, code that does DFS should look more or less like this:

    // n is number of vertices in graph
    bool visited[n]; // in this array we save visited vertices
    
    std::list<int> stack;
    std::list<int> order;
    
    for(int i = 0; i < n; i++){
      if(!visited[i]){
        stack.push_back(i);
        while(!stack.empty()){
          int top = stack.back();
          stack.pop_back();
          if(visited[top])
            continue;
    
          visited[top] = true;
          order.push_back(top);
          for(all neighbours of top)
             if(!visited[neighbour])
               stack.push_back(neighbour); 
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have coded the next function. But surely someone has a more elegant way
I have problem with turning this code: void dfs(int i = 1) { static
i have coded a forum and now im going to code the post a
I have now coded up various graph search (A*, DFS, BFS, etc..) algorithms many
I have coded a simple PHP Countdown Timer using this code: list($date,$time)=explode( ,$compounddate); list($thour,$tminute,$tsecond)=explode(:,$time);
I have coded the following code(some excerpts of the code) to display a node
I have coded my own tiny static DAL class for .NET Compact Framework and
I have coded up an ASP.NET website and running on win'08 (remotely hosted). The
I have coded a view that relies on a scalar function value for one
I have coded my calculator in simple mode, I have added a button for

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.