Skip to content

Add DFS with parent-completion constraint for DAG traversal #6467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

StathisVeinoglou
Copy link

Description

This PR adds a new GraphTraversal class that implements a modified DFS traversal order for directed graphs, ensuring that a node is only visited if all its parents have already been visited.
This approach is useful in dependency resolution and neural network computation graph execution order.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2025

Codecov Report

❌ Patch coverage is 77.77778% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.11%. Comparing base (57c6b03) to head (f0fe5f7).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...thealgorithms/graph/PredecessorConstrainedDfs.java 77.77% 6 Missing and 6 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6467      +/-   ##
============================================
+ Coverage     75.06%   75.11%   +0.05%     
- Complexity     5540     5575      +35     
============================================
  Files           685      687       +2     
  Lines         19219    19318      +99     
  Branches       3709     3732      +23     
============================================
+ Hits          14426    14511      +85     
- Misses         4239     4245       +6     
- Partials        554      562       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@StathisVeinoglou StathisVeinoglou marked this pull request as ready for review August 14, 2025 09:26
@@ -1,4 +1,4 @@
/**
/*
Copy link
Collaborator

@DenizAltunkapan DenizAltunkapan Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please undo your changes? Or is there any specific reason you did this? @StathisVeinoglou

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I’ve reverted the change as requested.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that this class name clearly reflects what your implementation does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the spot! I renamed it to better explain what the implementation is for.

return Collections.unmodifiableList(events);
}

private static <T> void dfs(T u, Map<T, List<T>> succ, Map<T, List<T>> pred, Set<T> visited, int[] order, List<TraversalEvent<T>> out) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it is better to use full names. It will look more descriptive for learning purposes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea good point! I believe that i replaced all abbreviated names with full descriptive ones.

}

/** An event emitted by the traversal: either a VISIT with an order, or a SKIP with a note. */
public static final class TraversalEvent<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be better to use recod here:

/** An event emitted by the traversal: either a VISIT with an order, or a SKIP with a note. */
public record TraversalEvent<T>(
    T node,
    Integer order, // non-null for visit, null for skip  
    String note    // non-null for skip, null for visit
) {
    public TraversalEvent {
        Objects.requireNonNull(node);
        // order and note can be null based on event type
    }
    
    /** A visit event with an increasing order (0,1,2,...) */
    public static <T> TraversalEvent<T> visit(T node, int order) {
        return new TraversalEvent<>(node, order, null);
    }
    
    /** A skip event with an explanatory note (e.g., not all parents visited yet). */
    public static <T> TraversalEvent<T> skip(T node, String note) {
        return new TraversalEvent<>(node, null, Objects.requireNonNull(note));
    }
    
    public boolean isVisit() { return order != null; }
    public boolean isSkip() { return order == null; }
    
    @Override
    public String toString() {
        return isVisit() ? "VISIT(" + node + ", order=" + order + ")" 
                        : "SKIP(" + node + ", " + note + ")";
    }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I’ve incorporated this exact record definition.

return true;
}

private static <T> boolean appearsAnywhere(Map<T, List<T>> succ, T node) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Seems like it's better to use full descriptive names.

…raightforward about the implementation.Added full names instead of shortcuts, and included record.
@StathisVeinoglou
Copy link
Author

Thanks for all the suggestions! I believe that i implemented the changes.
Please let me know if anything further is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants