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

Graphs: fix isCyclic Digraph #3628

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions M2/Macaulay2/packages/Graphs.m2
Original file line number Diff line number Diff line change
Expand Up @@ -1154,16 +1154,7 @@ spectrum Graph := List => G -> sort toList eigenvalues (adjacencyMatrix G, Hermi



topologicalSort = method(TypicalValue =>List)
topologicalSort Digraph := List => D -> topologicalSort(D, "")
topologicalSort (Digraph, String) := List => (D,s) -> (
if instance(D, Graph) or isCyclic D then error "Topological sorting is only defined for acyclic directed graphs.";
s = toLower s;
processor := if s == "random" then random
else if s == "min" then sort
else if s == "max" then rsort
else if s == "degree" then L -> last \ sort transpose {apply(L, v -> degree(D, v)), L}
else identity;
attemptTopologicalSort = (D,processor) -> (
S := processor sources D;
L := {};
v := null;
Expand All @@ -1172,7 +1163,23 @@ topologicalSort (Digraph, String) := List => (D,s) -> (
L = L|{v};
S = processor join(drop(S, 1), select(toList children (D, v), c -> isSubset(parents(D, c), L)));
);
L
if #L == #(vertexSet D) then L else null
)

topologicalSort = method(TypicalValue =>List)
topologicalSort Digraph := List => D -> topologicalSort(D, "")
topologicalSort (Digraph, String) := List => (D,s) -> (
if instance(D, Digraph) then (
s = toLower s;
processor := if s == "random" then random
else if s == "min" then sort
else if s == "max" then rsort
else if s == "degree" then L -> last \ sort transpose {apply(L, v -> degree(D, v)), L}
else identity;
L := attemptTopologicalSort(D, processor);
if L =!= null then return L;
);
error "Topological sorting is only defined for acyclic directed graphs.";
)


Expand Down Expand Up @@ -1270,14 +1277,7 @@ isConnected Graph := Boolean => G -> numberOfComponents G <= 1

isCyclic = method()
isCyclic Graph := Boolean => G -> isConnected G and all(vertexSet G, v -> degree(G, v) == 2)
isCyclic Digraph := Boolean => G -> (
D := depthFirstSearch G;
any(vertexSet G, u ->
any(toList children(G, u), v ->
(D#symbol discoveryTime)#v < (D#symbol discoveryTime)#u and (D#symbol finishingTime)#u < (D#symbol finishingTime)#v
)
)
)
isCyclic Digraph := Boolean => G -> attemptTopologicalSort(G, identity) === null

isEulerian = method()
isEulerian Graph := Boolean => G -> all(apply(vertexSet G, v -> degree(G,v)), even) and isConnected G
Expand Down Expand Up @@ -5505,6 +5505,17 @@ TEST ///
assert(topologicalSort D==={2,3,1})
///

TEST ///
-- check cycle detection in digraphs
assert( isCyclic digraph({{1,2},{2,3},{3,1}}) === true )
assert( isCyclic digraph({{1,2},{2,3},{3,4},{4,3}}) === true )
assert( isCyclic digraph({{1,2},{2,3},{3,4},{2,4}}) === false )
assert( isCyclic digraph({{1,3},{1,4},{2,4},{3,2},{4,3}}) === true )
assert( isCyclic digraph({{1,3},{1,4}, {3,2},{4,3}}) === false )
assert( isCyclic digraph({{1,2},{2,3},{3,4}}) === false )
assert( isCyclic digraph({{1,2},{2,3},{3,4},{4,4}}) === true )
assert( isCyclic digraph({{1,2},{2,3},{3,4},{5,5}}) === true )
///

TEST ///

Expand Down
Loading