@@ -481,34 +481,45 @@ function isTree(gbp::Union{GraphicalModel, GraphicalModelTree})
481
481
482
482
# # Pilling the factor graph
483
483
hasSingleVariable = true ; hasSingleFactor = true ;
484
- @inbounds while hasSingleVariable || hasSingleFactor
485
- for i in iterateVariable
486
- for (k, j) in enumerate (rowptr[colptr[i][1 ]])
487
- if i == j
488
- deleteat! (rowptr[colptr[i][1 ]], k)
489
- deleteat! (colptr[i], 1 )
484
+ @inbounds while hasSingleFactor || hasSingleVariable
485
+ for variable in iterateVariable
486
+ factor = colptr[variable][1 ]
487
+ for (k, variables) in enumerate (rowptr[factor])
488
+ if variable == variables
489
+ deleteat! (rowptr[factor], k)
490
+ deleteat! (colptr[variable], 1 )
490
491
end
491
492
end
492
- if length (rowptr[colptr[i][ 1 ] ]) == 1
493
- push! (iterateFactor, colptr[i][ 1 ] )
493
+ if length (rowptr[factor ]) == 1
494
+ push! (iterateFactor, factor )
494
495
end
495
496
end
496
497
if isempty (iterateFactor)
497
498
hasSingleFactor = false
498
499
end
499
500
iterateVariable = Int64[]
500
501
501
- for i in iterateFactor
502
- for (k, j) in enumerate (colptr[rowptr[i][1 ]])
503
- if i == j
504
- deleteat! (colptr[rowptr[i][1 ]], k)
505
- deleteat! (rowptr[i], 1 )
502
+ for factor in iterateFactor
503
+ variable = rowptr[factor][1 ]
504
+ for (k, factors) in enumerate (colptr[variable])
505
+ if factor == factors
506
+ deleteat! (colptr[variable], k)
507
+ deleteat! (rowptr[factor], 1 )
506
508
end
507
509
end
508
- if length (colptr[rowptr[i][ 1 ] ]) == 1
509
- push! (iterateVariable, rowptr[i][ 1 ] )
510
+ if length (colptr[variable ]) == 1
511
+ push! (iterateVariable, variable )
510
512
end
511
513
end
514
+
515
+ flag = true
516
+ for i in colptr[iterateVariable]
517
+ if ! isempty (i)
518
+ flag = false
519
+ end
520
+ end
521
+ if flag break end
522
+
512
523
if isempty (iterateVariable)
513
524
hasSingleVariable = false
514
525
end
0 commit comments