Coder Social home page Coder Social logo

Comments (3)

backtracking avatar backtracking commented on August 24, 2024

Indeed, it may seem confusing at first sight. But there is nothing wrong with this API (unless I'm mistaken).

The idea is that the meaningful functions are rather iter/fold_component, where the starting point of the DFS is user-provided. I guess this is exactly what you suggest in your third solution.
If you look for instance in Sedgewick&Wayne's Algorithms, you'll see that this is the only function in the DFS API. See https://algs4.cs.princeton.edu/41graph/DepthFirstSearch.java.html

The iter and fold functions are also provided, for your convenience. Indeed they perform as you observed: they run the user-provided iter/fold_vertex functions and, for each vertex not yet visited, start a new DFS from this vertex. Hence the rather cryptic comment saying that iterating over DFS roots only is fine. Said otherwise, it is up to you to come up with an iter_vertex function that will identify suitable DFS roots, e.g. vertices with no predecessors.

My understanding is that you'd like some additional function in the DFS API that would identify vertices with no predecessors for you and run DFS from these vertices. Indeed we could do that, though it would raise some issues such as what to do when you have a strongly-connected component (i.e. with no vertex with no predecessors).

I hope I understood your question correctly.

from ocamlgraph.

eponier avatar eponier commented on August 24, 2024

You perfectly understood my question and carefully answered it.

I had missed the documentation of fold in DFS that could have hinted me in the right direction.

The function is applied each time a node is reached for the first time, before idoterating1 over its successors. Tail-recursive.

I think the main issue, here, is documentation. A few points could be improved:

  • The functions in BFS could get the same documentation as in DFS. For instance, the documentation quoted above appears only in DFS.
  • The documentation could highlight that the *_component versions are probably the ones to be used.
  • The descriptions of iter_vertex and fold_vertex could explain that the order is important if the user wants to use the non-component versions of the functions.

Thanks for taking the time to answer me!

1The typo is present in the documentation.

from ocamlgraph.

backtracking avatar backtracking commented on August 24, 2024

Fixed in commit a2d7918.

from ocamlgraph.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.