Skill Tree Closed Cycle Detection

In the following script, my current problem is with “CanDisableSkill()”
That’s called for the purpose to check if the node is inside a closed cycle of turned on nodes. Which in that case you should be able to turn off any one node that maintains the cycle closed. Any after that you shouldn’t be able to because it no longer would be a closed cycle.

using System.Collections.Generic;
using UnityEngine;

public class SkillTreeManager : MonoBehaviour {
    public static SkillTreeManager Instance;

    private void Awake() {
        if (Instance == null) Instance = this;
    }

    private bool firstNodeOn = false; //Says if there's already a "First Node" turned on

    public bool CanDisableSkill(SkillTreeNode nodeToDisable) {
        HashSet<SkillTreeNode> visitedNodes = new();
        var adjacentNodes = nodeToDisable.GetAdjacentNodes;

        foreach (var node in adjacentNodes) {
            if (DetectCycle(node.GetSkillTreeNode, nodeToDisable, visitedNodes)) {
                Debug.Log("True");
                return true;
            }
        }
        Debug.Log("False");
        return false;
    }

    // DFS method to detect cycles in the skill tree
    private bool DetectCycle(SkillTreeNode currentNodeToCheck, SkillTreeNode parentNode, HashSet<SkillTreeNode> visitedNodes) {
        if (!currentNodeToCheck.TurnedOn) return false;
        if (visitedNodes.Contains(currentNodeToCheck)) return true; 

        visitedNodes.Add(currentNodeToCheck);

        var adjacentNodes = currentNodeToCheck.GetAdjacentNodes;
        foreach (var node in adjacentNodes) {
            SkillTreeNode adjacentNode = node.GetSkillTreeNode;
            if (adjacentNode == parentNode) continue;
            if (DetectCycle(adjacentNode, currentNodeToCheck, visitedNodes)) return true;
        }

        visitedNodes.Remove(currentNodeToCheck); // Backtrack - unmark the node
        return false;
    }

    public void CheckIfCanConnect(SkillTreeNode skillTreeNode) {
        var adjacentNodes = skillTreeNode.GetAdjacentNodes;
        var adjacentNodesTurnedOn = new List<SkillTreeNode.Node>();

        foreach (var node in adjacentNodes) {
            if (node.GetSkillTreeNode.TurnedOn) {
                adjacentNodesTurnedOn.Add(node);
            }
        }

        if (skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 0 && !skillTreeNode.TurnedOn && !firstNodeOn) {
            skillTreeNode.SetTurnedOn(true);
            firstNodeOn = true;
            Debug.Log("Turning First Node On");
            return;
        }

        if (skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 0 && skillTreeNode.TurnedOn) {
            skillTreeNode.SetTurnedOn(false);
            firstNodeOn = false;
            Debug.Log("Turning First Node Off");
            return;
        }

        if (!skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 0) return;

        if (adjacentNodesTurnedOn.Count > 0 && !skillTreeNode.TurnedOn) {
            skillTreeNode.SetTurnedOn(true);
            return;
        }
        else if (!skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count == 1 && skillTreeNode.TurnedOn) {
            skillTreeNode.SetTurnedOn(false);
            return;
        }

        if (skillTreeNode.TurnedOn && !skillTreeNode.GetisFirstNode && adjacentNodesTurnedOn.Count > 1 && CanDisableSkill(skillTreeNode)) {
            skillTreeNode.SetTurnedOn(false);
            return;
        }
    }
}

Now, my problem, in the following image, is that, the node in the red square, once activated screws the hole thing. Because if the nodes in the green rectangle are all on, any node connected to the nodes from that rectangle will always count as being in a closed cycle of turned on nodes because it can do the loop check through the nodes in that rectangle and come back to the same node which is not what I desire.
skill tree testing

I don’t know if I made my problem clear so if anything, you can ask and I’ll try to clarify the problem

Here are some example of the problem that can help to clarify my issue:
That red one shouldn’t be able to be turned off
undesired result

This one is correct though
desired result

But then you’re also able to do this which I didn’t want
undesired result

And when one of those two are turned off the problem no longer prevails
desired result

I understand the why everything is happening. I’m just having a hard time coming with something that fixes this little problem

What I wanted was that the player should be able to turn off a node, even if surrounded by more that one turned on node, as long as it was inside a closed cycle of turned on nodes but the current result is that if there another small cycle inside the big cycle it doesn’t work as intended!

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật