Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Detect unconnected nodes (Read 2706 times)
unisnk
Junior Member
**
Offline


D'Oh!

Posts: 59
Joined: Sep 3rd, 2008
Detect unconnected nodes
Oct 1st, 2008 at 9:34am
Print Post  
Look at this image:



Starting from yellow node, I want to delete (or hide) automatically all unconnected nodes (the red ones).

There is a method that given two nodes, returns if they are "connected" or the only way to do this is to implement a graph visit algorithm? (maybe using the "tag" property to mark the connected nodes?)

Thank you!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Detect unconnected nodes
Reply #1 - Oct 1st, 2008 at 12:46pm
Print Post  
This code implements the depth-first search algorithm for finding connected components in a graph. After running it, boxes will have the same Tag value if they are in the same connected component.

Code
Select All
Sub FindConnectedComponents()
	Dim setIds() As Integer
	Dim i As Integer, component As Integer

	ReDim setIds(0 To fcx.boxes.Count - 1)

	component = 0
	For i = 0 To fcx.boxes.Count - 1
		fcx.boxes(i).Tag = i
		setIds(i) = -1
	Next i

	For i = 0 To fcx.boxes.Count - 1
		If setIds(i) = -1 Then
			TraverseComponent setIds, i, component
			component = component + 1
		End If
	Next i

	For i = 0 To fcx.boxes.Count - 1
		fcx.boxes(i).Tag = setIds(i)
	Next i
End Sub

Sub TraverseComponent(setIds() As Integer, node As Integer, component As Integer)
	setIds(node) = component

	Dim a As arrow
	Dim child As box

	For Each a In fcx.boxes(node).OutgoingArrows
		Set child = a.DestinationBox
		If setIds(child.Tag) = -1 Then
			TraverseComponent setIds, child.Tag, component
		End If
	Next a

	For Each a In fcx.boxes(node).IncomingArrows
		Set child = a.OriginBox
		If setIds(child.Tag) = -1 Then
			TraverseComponent setIds, child.Tag, component
		End If
	Next a
End Sub

Private Sub Command1_Click()
	Dim colors(0 To 9)
	colors(0) = vbRed
	colors(1) = vbGreen
	colors(2) = vbBlue
	colors(3) = vbWhite
	colors(4) = vbYellow
	colors(5) = vbRed
	colors(6) = vbGreen
	colors(7) = vbBlue
	colors(8) = vbWhite
	colors(9) = vbYellow

	FindConnectedComponents

	Dim b As box
	For Each b In fcx.boxes
		b.FillColor = colors(b.Tag)
	Next b
End Sub
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
unisnk
Junior Member
**
Offline


D'Oh!

Posts: 59
Joined: Sep 3rd, 2008
Re: Detect unconnected nodes
Reply #2 - Oct 1st, 2008 at 3:27pm
Print Post  
very good. It tooks a little of time to translate this VB code to delphi but now this is the result:



I don't know if the two red boxes is a bug. Anyway with the "tag" value now I'm able to isolate the unconnected boxes. I'll do some other tests before going on.

Thank you very much!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Detect unconnected nodes
Reply #3 - Oct 1st, 2008 at 5:22pm
Print Post  
They are not a bug - the colors array contains each color twice. I've employed some copy and paste techniques here Wink Anyway the code that colors the boxes is just for testing, and if the graph contains more than 10 connected components, you will get an index-out-of-bounds exception. So if you intend to do the same in production code, use the modulo operator to convert the component index (in box.Tag) to the color index. Should look something like this in Delphi:

colorIndex := box.Tag mod Length(colors);
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint