Skip to content

[Feature-request] Topological sort with keying function that exposes internal state #143

Description

@kaushikcfd

Consider the DAG below:

from pytools.graph import compute_topological_order

dag = {"A": {"C"},
       "B": {"E"},
       "C": {"D"},
       "D": set(),
       "E": set(),
       }

colors = {"A": "red",
          "B": "red",
          "C": "blue",
          "D": "red",
          "E": "blue"
          }

sorted_nodes = compute_topological_order(dag, key=lambda x: x)
print(sorted_nodes)                            # ['A', 'B', 'C', 'D', 'E']
print([colors[node] for node in sorted_nodes]) # ['red', 'red', 'blue', 'red', 'blue']

My application needs a valid topological sort that minimizes the jumps from 'red'<->'blue' nodes in the final schedule. And so for this problem, the topological sort order I'm after would be ['A', 'B', 'C', 'E', 'D']. I don't think that's possible with the current interface of compute_topological_order.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions