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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:13:14+00:00 2026-05-27T16:13:14+00:00

Suppose you have to implement Graph class, containing some algorithms, using dfs (depth-first search).

  • 0

Suppose you have to implement Graph class, containing some algorithms, using dfs (depth-first search). For instance, it might be connectivity check and Graph class would look like that:

class Graph {
    void dfsConnected(int v) {
        visited[v] = true;
        //indexing over v's adjacencies and calling dfsConnected recursively
    }
    bool isConnected {
        //indexing over vertice list and calling dfsConnected
    }
}

Suppose we have a bunch of algorithms, using dfs in this class (every of them uses specific dfs).
The problem is visited array:

  • we could define it as a private field for every dfs like visitedConnectivity, visitedTopSorting, visitedBridges, etc. So we will have a lot of private variables in the every instance of Graph. And what if we have 3-4 such “global” variables for every dfs?
  • we could pass visited as a dfs‘s argument . In this case, we will have overhead on the every dfs call.

So, what is the easiest and real-world used solution for this problem? Of course, it is related not only to graph algorithms, but I found it easier to explain it in dfs terms.

  • 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-05-27T16:13:15+00:00Added an answer on May 27, 2026 at 4:13 pm

    The more OOP way, in my opinion, is delcaring a field visited for each DFS class, and make it run its own DFS….

    It will prevent you from keeping track ‘what did I allocated? where is it connected to? etc…”

    Your DFS will be much more capsulated, and will require fewer data, then adding an extra parameter for each dfs, which you will have to maintain separately.

    The performance issue in here is neglected [in most cases] to the readability and maintainability you achieve with encapsulating as much data as possible in the class itself.

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

Sidebar

Related Questions

Suppose I have my own non-inline functions LockMutex and UnlockMutex, which are using some
Suppose I have an Interface with some properties: public interface IDummy { string First
i have this class called MemoryManager, it is supposed to implement a simple smart
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have the following class heirarchy: class A() class B(a:A) class C(b:B) class
Suppose I have a class T that I want to use as a key
Suppose we have a class called AccountService that manages the state of accounts. AccountService
I have to implement charts in my application. Suppose i have a table structure
Suppose I have a class MyClass to which I want to add certain 'observer'

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.