@@ -435,6 +435,7 @@ $TYPEDFIELDS
435435"""
436436struct TreeSet
437437 reverse_bfs_orders:: Vector{Vector{Tuple{Int,Int}}}
438+ is_star:: Vector{Bool}
438439end
439440
440441function TreeSet (forest:: DisjointSets{Tuple{Int,Int}} , nvertices:: Int )
@@ -499,9 +500,19 @@ function TreeSet(forest::DisjointSets{Tuple{Int,Int}}, nvertices::Int)
499500 # Create a queue with a fixed size nvmax
500501 queue = Vector {Int} (undef, nvmax)
501502
503+ # Specify if each tree in the forest is a star,
504+ # meaning that one vertex is directly connected to all other vertices in the tree
505+ is_star = Vector {Bool} (undef, ntrees)
506+
502507 for k in 1 : ntrees
503508 tree = trees[k]
504509
510+ # Boolean indicating whether the current tree is a star (a single central vertex connected to all others)
511+ bool_star = true
512+
513+ # Candidate hub vertex if the current tree is a star
514+ virtual_hub = 0
515+
505516 # Initialize the queue to store the leaves
506517 queue_start = 1
507518 queue_end = 0
@@ -527,10 +538,24 @@ function TreeSet(forest::DisjointSets{Tuple{Int,Int}}, nvertices::Int)
527538 degrees[leaf] = 0
528539
529540 for neighbor in tree[leaf]
541+ # Check if neighbor is the parent of the leaf or if it was a child before the tree was pruned
530542 if degrees[neighbor] != 0
531543 # (leaf, neighbor) represents the next edge to visit during decompression
532544 push! (reverse_bfs_orders[k], (leaf, neighbor))
533545
546+ if bool_star
547+ # Initialize the potential hub of the star with the first parent of a leaf
548+ if virtual_hub == 0
549+ virtual_hub = neighbor
550+ else
551+ # Verify if the tree still qualifies as a star
552+ # If we find leaves with different parents, then it can't be a star
553+ if virtual_hub != neighbor
554+ bool_star = false
555+ end
556+ end
557+ end
558+
534559 # reduce the degree of the neighbor
535560 degrees[neighbor] -= 1
536561
@@ -542,9 +567,12 @@ function TreeSet(forest::DisjointSets{Tuple{Int,Int}}, nvertices::Int)
542567 end
543568 end
544569 end
570+
571+ # Specify if the tree is a star or not
572+ is_star[k] = bool_star
545573 end
546574
547- return TreeSet (reverse_bfs_orders)
575+ return TreeSet (reverse_bfs_orders, is_star )
548576end
549577
550578# # Postprocessing, mirrors decompression code
@@ -605,17 +633,23 @@ function postprocess!(
605633 end
606634 else
607635 # only the colors of non-leaf vertices are used
608- (; reverse_bfs_orders) = star_or_tree_set
636+ (; reverse_bfs_orders, is_star ) = star_or_tree_set
609637 nb_trivial_trees = 0
610638
611639 # Iterate through all non-trivial trees
612640 for k in eachindex (reverse_bfs_orders)
613641 reverse_bfs_order = reverse_bfs_orders[k]
614- # Check if we have more than one edge in the tree
642+ # Check if we have more than one edge in the tree (non-trivial tree)
615643 if length (reverse_bfs_order) > 1
616- # TODO : Optimize by avoiding iteration over all edges
617- # Only one edge is needed if we know if it is a normal tree or a star
618- for (i, j) in reverse_bfs_order
644+ # Determine if the tree is a star
645+ if is_star[k]
646+ # It is a non-trivial star and only the color of the hub is needed
647+ (_, hub) = reverse_bfs_order[1 ]
648+ color_used[color[hub]] = true
649+ else
650+ # It is not a star and both colors are needed during the decompression
651+ (i, j) = reverse_bfs_order[1 ]
652+ color_used[color[i]] = true
619653 color_used[color[j]] = true
620654 end
621655 else
0 commit comments