Skip to content
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

Implement topological sort. #18

Merged
merged 7 commits into from
May 2, 2020
Merged

Conversation

csukuangfj
Copy link
Collaborator

@csukuangfj csukuangfj commented Apr 28, 2020

For the following fsa

a

after TopSort, it produces the following fsa

b

with the state map (mapping state from the sorted fsa to the original fsa)

0 -> 0
1 -> 4
2 -> 5
3 -> 2
4 -> 3
5 -> 1
6 -> 6

More unit test cases will be added later (possibly tomorrow).

@csukuangfj csukuangfj changed the title WIP: Implement topologically sort. WIP: Implement topological sort. Apr 29, 2020
@danpovey
Copy link
Collaborator

Great. Please call it TopSort though, not TopoSort.

k2/csrc/fsa_algo.cc Outdated Show resolved Hide resolved
k2/csrc/fsa_algo.cc Outdated Show resolved Hide resolved
k2/csrc/fsa_algo.cc Outdated Show resolved Hide resolved
k2/csrc/fsa_algo.h Outdated Show resolved Hide resolved
k2/csrc/fsa_algo.cc Outdated Show resolved Hide resolved
k2/csrc/fsa_algo.cc Outdated Show resolved Hide resolved
k2/csrc/fsa_algo.cc Outdated Show resolved Hide resolved
@csukuangfj
Copy link
Collaborator Author

It happens that not all un-connected graphs can be sorted topologically.

For the following fsa:
trim

What should state 5 be numbered in a TopSorted FSA?

@danpovey
Copy link
Collaborator

danpovey commented Apr 29, 2020 via email

@danpovey
Copy link
Collaborator

danpovey commented Apr 29, 2020 via email

@csukuangfj
Copy link
Collaborator Author

But can we make sure that Connect() is able to produce top-sorted output as
long as the input is free of cycles (which are not self-loops)?

I am going to implement it in the current ConnectCore.

k2/csrc/CMakeLists.txt Outdated Show resolved Hide resolved
k2/csrc/fsa_algo.cc Outdated Show resolved Hide resolved
@danpovey
Copy link
Collaborator

danpovey commented Apr 30, 2020 via email

@danpovey
Copy link
Collaborator

danpovey commented Apr 30, 2020 via email

@danpovey
Copy link
Collaborator

danpovey commented Apr 30, 2020 via email

The transition table is represented by a string which
has the same format with OpenFST.
@csukuangfj
Copy link
Collaborator Author

I've added support to build an FSA from a transition table, like OpenFST, simplifying
the task of creating FSAs in unittests.

Copy link
Collaborator

@danpovey danpovey left a comment

Choose a reason for hiding this comment

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

Looks good to me

@csukuangfj csukuangfj changed the title WIP: Implement topological sort. Implement topological sort. May 1, 2020
@csukuangfj
Copy link
Collaborator Author

Unit tests are added. Please have a review and merge if necessary
@qindazhu @danpovey

k2/csrc/fsa_util.cc Outdated Show resolved Hide resolved
k2/csrc/fsa_util.cc Outdated Show resolved Hide resolved
@danpovey
Copy link
Collaborator

danpovey commented May 2, 2020 via email

k2/csrc/fsa_util.cc Outdated Show resolved Hide resolved
@csukuangfj
Copy link
Collaborator Author

please use squash and merge. there are too many commits in this pullrequest.

@danpovey danpovey merged commit 9fb792d into k2-fsa:master May 2, 2020
is successful; false otherwise
@return The converted integer.
*/
int32_t StringToInt(const std::string &s, bool *is_ok = nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was just thinking that maybe we sould not put those helper functions in fsa_utils as all functions are not related with fsa except stringToFsa

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree.

When such helper functions reach a certain number, we can place them separately.

qindazhu pushed a commit to qindazhu/k2 that referenced this pull request May 2, 2020
@csukuangfj csukuangfj deleted the fangjun-topo-sort branch May 2, 2020 04:25
@danpovey
Copy link
Collaborator

danpovey commented May 2, 2020 via email

@csukuangfj
Copy link
Collaborator Author

csukuangfj commented May 3, 2020

@danpovey

But can we make sure that Connect() is able to produce top-sorted output as
long as the input is free of cycles (which are not self-loops)?

I have a question about this.

For the following input fsa a
a

it is acyclic and topsorted.

I've changed the ConnectCore to produce a topsorted output fsa as long as the input
fsa is acyclic. It produces the following output for the above input fsa

b


Notice that the state 2 in the output fsa maps to the state 3 in the input fsa.

My question is that the topological sort of an acyclic fsa is not unique, does the
output fsa have to keep the same state number as the input fsa if the input fsa is topsorted,
i.e., does the state map have to be an identity map ?

@pzelasko
Copy link
Contributor

pzelasko commented May 3, 2020

Shouldn't the topological sort respect the arcs ordering? E.g. if the FSA is label sorted then follow the same order in these cases (and if it's not sorted then whatever arc comes first in the underlying data structure)?

@danpovey
Copy link
Collaborator

danpovey commented May 3, 2020 via email

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