@@ -376,7 +376,8 @@ $TYPEDFIELDS
376376struct TreeSet{T}
377377 reverse_bfs_orders:: Vector{Tuple{T,T}}
378378 is_star:: Vector{Bool}
379- num_edges_per_tree:: Vector{T}
379+ tree_edge_indices:: Vector{T}
380+ nt:: T
380381end
381382
382383function TreeSet (
@@ -388,20 +389,20 @@ function TreeSet(
388389 S = pattern (g)
389390 edge_to_index = edge_indices (g)
390391 nv = nb_vertices (g)
391- nt = forest. num_trees
392+ (; nt, ranks) = forest
392393
393394 # root_to_tree is a vector that maps a tree's root to the index of the tree
394- # We can recycle forest. ranks because we don't need it anymore to merge trees
395- root_to_tree = forest . ranks
395+ # We can recycle the vector " ranks" because we don't need it anymore to merge trees
396+ root_to_tree = ranks
396397 fill! (root_to_tree, zero (T))
397398
398- # Contains the number of edges per tree
399- num_edges_per_tree = zeros (T, nt)
399+ # vector specifying the starting and ending indices of edges for each tree
400+ tree_edge_indices = zeros (T, nt + 1 )
400401
401402 # vector of dictionaries where each dictionary stores the neighbors of each vertex in a tree
402403 trees = [Dict {T,Vector{T}} () for i in 1 : nt]
403404
404- # current number of roots found
405+ # number of roots found
405406 nr = 0
406407
407408 rvS = rowvals (S)
@@ -418,18 +419,20 @@ function TreeSet(
418419 root_to_tree[root] = nr
419420 end
420421
421- # index of the tree T that contains this edge
422+ # index of the tree that contains this edge
422423 index_tree = root_to_tree[root]
423- num_edges_per_tree[index_tree] += 1
424424
425- # Update the neighbors of i in the tree T
425+ # Update the number of edges for the current tree (shifted by 1 to facilitate the final cumsum)
426+ tree_edge_indices[index_tree + 1 ] += 1
427+
428+ # Update the neighbors of i in the current tree
426429 if ! haskey (trees[index_tree], i)
427430 trees[index_tree][i] = [j]
428431 else
429432 push! (trees[index_tree][i], j)
430433 end
431434
432- # Update the neighbors of j in the tree T
435+ # Update the neighbors of j in the current tree
433436 if ! haskey (trees[index_tree], j)
434437 trees[index_tree][j] = [i]
435438 else
@@ -439,6 +442,12 @@ function TreeSet(
439442 end
440443 end
441444
445+ # Compute a shifted cumulative sum of tree_edge_indices, starting from one
446+ tree_edge_indices[1 ] = one (T)
447+ for k in 2 : (nt + 1 )
448+ tree_edge_indices[k] += tree_edge_indices[k - 1 ]
449+ end
450+
442451 # degrees is a vector of integers that stores the degree of each vertex in a tree
443452 degrees = buffer
444453
@@ -529,7 +538,7 @@ function TreeSet(
529538 is_star[k] = bool_star
530539 end
531540
532- return TreeSet (reverse_bfs_orders, is_star, num_edges_per_tree )
541+ return TreeSet (reverse_bfs_orders, is_star, tree_edge_indices, nt )
533542end
534543
535544# # Postprocessing, mirrors decompression code
@@ -597,15 +606,17 @@ function postprocess!(
597606 end
598607 else
599608 # only the colors of non-leaf vertices are used
600- (; reverse_bfs_orders, is_star, num_edges_per_tree ) = star_or_tree_set
609+ (; reverse_bfs_orders, is_star, tree_edge_indices, nt ) = star_or_tree_set
601610 nb_trivial_trees = 0
602611
603- # Index of the first edge in reverse_bfs_orders for the current tree
604- first = 1
605-
606612 # Iterate through all non-trivial trees
607- for k in eachindex (num_edges_per_tree)
608- ne_tree = num_edges_per_tree[k]
613+ for k in 1 : nt
614+ # Position of the first edge in the tree
615+ first = tree_edge_indices[k]
616+
617+ # Total number of edges in the tree
618+ ne_tree = tree_edge_indices[k + 1 ] - first
619+
609620 # Check if we have more than one edge in the tree (non-trivial tree)
610621 if ne_tree > 1
611622 # Determine if the tree is a star
@@ -622,14 +633,17 @@ function postprocess!(
622633 else
623634 nb_trivial_trees += 1
624635 end
625- first += ne_tree
626636 end
627637
628638 # Process the trivial trees (if any)
629639 if nb_trivial_trees > 0
630- first = 1
631- for k in eachindex (num_edges_per_tree)
632- ne_tree = num_edges_per_tree[k]
640+ for k in 1 : nt
641+ # Position of the first edge in the tree
642+ first = tree_edge_indices[k]
643+
644+ # Total number of edges in the tree
645+ ne_tree = tree_edge_indices[k + 1 ] - first
646+
633647 # Check if we have exactly one edge in the tree
634648 if ne_tree == 1
635649 (i, j) = reverse_bfs_orders[first]
@@ -642,7 +656,6 @@ function postprocess!(
642656 color_used[color[j]] = true
643657 end
644658 end
645- first += ne_tree
646659 end
647660 end
648661 end
0 commit comments