Connected Component 4 Connectivity Label Algorithm Issue

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? hi, i want to make Connected Component Algorithm To Remove The Unconnected Path In The Map, I done the First Pass Already

  2. What is the issue? I can not made the Second Pass and keep getting issue

  3. What solutions have you tried so far? i was try to read it in google, wiki or whatever but still dont understand

from what i know the first pass will check for the most connected path and save the tag that need to be replace if they’s tag value arent connect

second pass will fix the tag value that are not connect


so you can see the Table print on the left side, that is a tag value ([ReplaceTo] = [NeedToBeReplaced]) it will be used on the second pass, on the right side you can see the Value Object called Tag, that is tag value of block

local Tag = 0
	local ReplaceTag = {}
	local HighestTag = {}
	local CurrentFindBlock = MapFolder:WaitForChild("1/1")
	local MaxX = MapSizeX.Value
	local MaxZ = MapSizeZ.Value

local function Connected_Component_FirstPass(TheBlock)
		local IsSideVoid = nil
		local TagValue = TheBlock:WaitForChild("Tag")
		local XZ = {}

		for occurrence in (TheBlock.Name):gmatch("%d+") do
			table.insert(XZ, tonumber(occurrence))
		end	

		local X, Z = XZ[1], XZ[2]

		if MapFolder:FindFirstChild(X.."/"..(Z-1)) then
			local CheckAroundBlock = MapFolder:FindFirstChild(X.."/"..(Z-1))
			local CheckAroundTagValue = CheckAroundBlock:WaitForChild("Tag")

			if TheBlock.Transparency == 0 and (IsSideVoid == false or IsSideVoid == true or IsSideVoid == nil) and CheckAroundBlock.Transparency == 0 then
				IsSideVoid = false
			elseif TheBlock.Transparency == 0 and (IsSideVoid == true or IsSideVoid == nil) and CheckAroundBlock.Transparency == 1 then
				IsSideVoid = true
			end

			if TheBlock.Transparency == 0 and CheckAroundBlock.Transparency == 0 and TagValue.Value == 0 then
				TagValue.Value = Tag
			end

			if TagValue.Value ~= 0 and TagValue.Value ~= CheckAroundTagValue.Value and CheckAroundBlock.Transparency == 0 then
				ReplaceTag[CheckAroundTagValue.Value] = TagValue.Value --TagValue Get Replaced By CheckAroundTagValue
				if not table.find(HighestTag, TagValue.Value) then
					table.insert(HighestTag, TagValue.Value)
				end
			end
		elseif TheBlock.Transparency == 0 then
			if IsSideVoid == nil or IsSideVoid == true then
				IsSideVoid = true
			end

		end

		if MapFolder:FindFirstChild((X-1).."/"..Z) then
			local CheckAroundBlock = MapFolder:FindFirstChild((X-1).."/"..Z)
			local CheckAroundTagValue = CheckAroundBlock:WaitForChild("Tag")

			if TheBlock.Transparency == 0 and (IsSideVoid == false or IsSideVoid == true or IsSideVoid == nil) and CheckAroundBlock.Transparency == 0 then
				IsSideVoid = false
			elseif TheBlock.Transparency == 0 and (IsSideVoid == true or IsSideVoid == nil) and CheckAroundBlock.Transparency == 1 then
				IsSideVoid = true
			end

			if TheBlock.Transparency == 0 and CheckAroundBlock.Transparency == 0 and TagValue.Value == 0 then
				TagValue.Value = Tag
			end

			if TagValue.Value ~= 0 and TagValue.Value ~= CheckAroundTagValue.Value and CheckAroundBlock.Transparency == 0 then
				ReplaceTag[CheckAroundTagValue.Value] = TagValue.Value --TagValue Get Replaced By CheckAroundTagValue
				if not table.find(HighestTag, TagValue.Value) then
					table.insert(HighestTag, TagValue.Value)
				end
			end
		elseif TheBlock.Transparency == 0 then
			if IsSideVoid == nil or IsSideVoid == true then
				IsSideVoid = true
			end
		end

		if IsSideVoid == true then
			if TheBlock.Transparency == 0 and TagValue.Value == 0 then
				Tag += 1
				TagValue.Value = Tag
			end
		end

		if Z ~= MaxZ then
			if X ~= MaxX then
				CurrentFindBlock = MapFolder:WaitForChild((X+1).."/"..(Z))
				Connected_Component_FirstPass(CurrentFindBlock)
			else
				CurrentFindBlock = MapFolder:WaitForChild((1).."/"..(Z+1))
				Connected_Component_FirstPass(CurrentFindBlock)
			end
		else
			if Z == MaxZ and X == 1 then
				CurrentFindBlock = MapFolder:WaitForChild((X+1).."/"..(MaxZ))
				Connected_Component_FirstPass(CurrentFindBlock)
			elseif X ~= MaxX then
				CurrentFindBlock = MapFolder:WaitForChild((X+1).."/"..(MaxZ))
				Connected_Component_FirstPass(CurrentFindBlock)
			end
		end
	end

	local function Connected_Component_SecondPass()
		local CheckHTTable = #HighestTag
		
		if CheckHTTable ~= 0 then
			for INDEX = 1, #HighestTag do
				local HighestTagValue = math.max(unpack(HighestTag))
				
				for ToReplace, GetReplace in pairs(ReplaceTag) do
					if GetReplace == HighestTagValue then
						for i, TagBlock in pairs(MapFolder:GetChildren()) do

							local TagBlockValue = TagBlock:WaitForChild("Tag")

							if TagBlockValue and TagBlockValue.Value ~= 0 then
								if TagBlockValue.Value == GetReplace then
									print(TagBlock.Name.."Get Replaced By "..(ToReplace)..", Origin: "..GetReplace)
									TagBlockValue.Value = ToReplace
								end
							end
						end
						
						for IN, HV in pairs(HighestTag) do
							if HV == HighestTagValue then
								table.remove(HighestTag, IN)
								if HighestTagValue > 2 then
									print(HighestTagValue)
									Connected_Component_SecondPass()
									end
							end
						end
						
					end
				end
			end
		end
	end
	
	task.wait(1)
	Connected_Component_FirstPass(CurrentFindBlock)
	print(ReplaceTag)

^ First Pass Function And Attempt To Make Second Pass

Ex of connected component 4 connectivity label algorithem work
Connected Component Algorithm Image

First Pass (8 Connectivity)

Second Pass (Fixing Tag Value On The Right List)