Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic LayeredLayout flow and nodes (Read 1165 times)
jonsberndt
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 14
Joined: Sep 18th, 2008
LayeredLayout flow and nodes
Oct 28th, 2008 at 11:28am
Print Post  
Are there any features supplied to permit parsing through a list of nodes such that the nodes are stepped through in order of dependence? That is I'd like to be able to step through the nodes so that for each node I encounter in a list, I've already encountered any nodes that the current node depends on. Put another way, the direction of travel is first from subnode to parent node, and secondarily from sibling node to sibling node. I don't want to encounter any node for which all subnodes have not already been encountered.

Is that clear? Smiley Note that this parsing would take place after an Arrange() call has already been made.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: LayeredLayout flow and nodes
Reply #1 - Oct 28th, 2008 at 12:36pm
Print Post  
Hi,

In graph theory this is called scheduling problem, and your particular kind of scheduling problem is called "Reverse topological sort" 8) There is no built-in algorithm to do that, but it is easy to implement:

Code
Select All
Hashtable visitedNodes = new Hashtable();
int order = 0;

void TopologicalSort(Diagram diagram)
{
	foreach (DiagramNode node in diagram.Nodes)
	{
		if (!visitedNodes.ContainsKey(node))
			Search(node);
	}
}

void Search(DiagramNode node)
{
	visitedNodes[node] = true;
	foreach (DiagramLink link in node.OutgoingLinks)
	if (!visitedNodes.ContainsKey(link.Destination))
		Search(link.Destination);
	Process(node, order++);
}

void Process(DiagramNode node, int order)
{
	// process the node here
	if (node is ShapeNode)
		(node as ShapeNode).Text = order.ToString();
}
 



There is a very nice book with solutions to graph problems: "Algorithms in Java, Part 5: Graph Algorithms" by Robert Sedgewick, and the code above is one of the algorithms in the book adapted to the Flowchart.NET API.

This works only for acyclic graphs, because if there is a cycle, you won't have a well defined dependency order. You might use the PathFinder.FindCycle method to find and remove cycles before running that method.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint