Skip to content

Commit

Permalink
Fix TransitiveReduction not to wrongly report cycles (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikbraun authored Mar 6, 2023
1 parent 8c01d5d commit c05ad31
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.16.1] - 2023-03-06

### Fixed
* Fixed `TransitiveReduction` not to incorrectly report cycles.

## [0.16.0] - 2023-03-01

**This release contains breaking changes of the public API (see "Changed").**
Expand Down
1 change: 1 addition & 0 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TransitiveReduction[K comparable, T any](g Graph[K, T]) (Graph[K, T], error

visited[current] = struct{}{}
onStack[current] = true
stack = append(stack, current)

// Also, if the vertex is a leaf node, remove it from the stack.
if len(adjacencyMap[current]) == 0 {
Expand Down
25 changes: 25 additions & 0 deletions dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ func TestDirectedTransitiveReduction(t *testing.T) {
},
shouldFail: true,
},
"graph from issue 83": {
vertices: []string{"_root", "A", "B", "C", "D", "E", "F"},
edges: []Edge[string]{
{Source: "_root", Target: "A"},
{Source: "_root", Target: "B"},
{Source: "_root", Target: "C"},
{Source: "_root", Target: "D"},
{Source: "_root", Target: "E"},
{Source: "_root", Target: "F"},
{Source: "E", Target: "C"},
{Source: "F", Target: "D"},
{Source: "F", Target: "C"},
{Source: "F", Target: "E"},
{Source: "C", Target: "A"},
{Source: "C", Target: "B"},
},
expectedEdges: []Edge[string]{
{Source: "_root", Target: "F"},
{Source: "F", Target: "D"},
{Source: "F", Target: "E"},
{Source: "E", Target: "C"},
{Source: "C", Target: "A"},
{Source: "C", Target: "B"},
},
},
}

for name, test := range tests {
Expand Down

0 comments on commit c05ad31

Please sign in to comment.