@@ -558,23 +558,24 @@ function postprocess!(
558558 color_used[color[i]] = true
559559 end
560560 end
561+
561562 if star_or_tree_set isa StarSet
562563 # only the colors of the hubs are used
563564 (; hub, spokes) = star_or_tree_set
564- nb_trivial_star = 0
565+ nb_trivial_stars = 0
565566
566567 # Iterate through all non-trivial stars
567568 for s in eachindex (hub)
568569 j = hub[s]
569570 if j > 0
570571 color_used[color[j]] = true
571572 else
572- nb_trivial_star += 1
573+ nb_trivial_stars += 1
573574 end
574575 end
575576
576577 # Process the trivial stars (if any)
577- if nb_trivial_star > 0
578+ if nb_trivial_stars > 0
578579 for s in eachindex (hub)
579580 j = hub[s]
580581 if j < 0
@@ -595,12 +596,43 @@ function postprocess!(
595596 else
596597 # only the colors of non-leaf vertices are used
597598 (; reverse_bfs_orders) = star_or_tree_set
599+ nb_trivial_trees = 0
600+
601+ # Iterate through all non-trivial trees
598602 for k in eachindex (reverse_bfs_orders)
599- for (i, j) in reverse_bfs_orders[k]
600- color_used[color[j]] = true
603+ reverse_bfs_order = reverse_bfs_orders[k]
604+ # Check if we have more than one edge in the tree
605+ if length (reverse_bfs_order) > 1
606+ # TODO : Optimize by avoiding iteration over all edges
607+ # Only one edge is needed if we know if it is a normal tree or a star
608+ for (i, j) in reverse_bfs_order
609+ color_used[color[j]] = true
610+ end
611+ else
612+ nb_trivial_trees += 1
613+ end
614+ end
615+
616+ # Process the trivial trees (if any)
617+ if nb_trivial_trees > 0
618+ for k in eachindex (reverse_bfs_orders)
619+ reverse_bfs_order = reverse_bfs_orders[k]
620+ # Check if we have exactly one edge in the tree
621+ if length (vertices_by_tree[k]) == 1
622+ (i, j) = reverse_bfs_orders[k][1 ]
623+ if color_used[color[i]]
624+ # Make i the root to avoid possibly adding one more used color
625+ # Switch it with the (only) leaf
626+ reverse_bfs_orders[k][1 ] = (j, i)
627+ else
628+ # Keep j as the root
629+ color_used[color[j]] = true
630+ end
631+ end
601632 end
602633 end
603634 end
635+
604636 # if at least one of the colors is useless, modify the color assignments of vertices
605637 if any (! , color_used)
606638 # assign the neutral color to every vertex with a useless color
0 commit comments