Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic accessing outgoing links in top down manner (Read 1821 times)
Ivan Gamo
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 30
Joined: Nov 10th, 2012
accessing outgoing links in top down manner
Dec 29th, 2012 at 7:35am
Print Post  
how to access each outgoing link of every nodes in diagramview in top to bottom manner. not in its Zindex.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: accessing outgoing links in top down manner
Reply #1 - Dec 31st, 2012 at 8:21am
Print Post  
Here are two different ways to traverse the diagram links, based respectively on depth-first search and breadth-first search in a graph:

Code
Select All
private delegate void ProcessLinkDelegate(DiagramLink link);

private void miGraphDepthSearch_Click(object sender, EventArgs e)
{
	linkCounter = 0;

	DiagramNode start = diagram.ActiveItem as DiagramNode;
	if (start != null)
		DepthSearch(start, ProcessLink, new Dictionary<DiagramNode, bool>());
}

private void miGraphBreadthSearch_Click(object sender, EventArgs e)
{
	linkCounter = 0;

	DiagramNode start = diagram.ActiveItem as DiagramNode;
	if (start != null)
		BreadthSearch(start, ProcessLink);
}

void DepthSearch(DiagramNode start, ProcessLinkDelegate processLink,
	Dictionary<DiagramNode, bool> visitedNodes)
{
	visitedNodes[start] = true;

	foreach (DiagramLink link in start.OutgoingLinks)
	{
		processLink(link);
		if (visitedNodes.ContainsKey(link.Destination))
			continue;
		DepthSearch(link.Destination, processLink, visitedNodes);
	}
}

void BreadthSearch(DiagramNode start, ProcessLinkDelegate processLink)
{
	Dictionary<DiagramNode, bool> visitedNodes = new Dictionary<DiagramNode, bool>();
	Queue<DiagramNode> queue = new Queue<DiagramNode>();

	queue.Enqueue(start);
	visitedNodes[start] = true;

	while(queue.Count > 0)
	{
		DiagramNode node = queue.Dequeue();
		foreach (DiagramLink link in node.OutgoingLinks)
		{
			processLink(link);
			if (visitedNodes.ContainsKey(link.Destination))
				continue;
			queue.Enqueue(link.Destination);
			visitedNodes[link.Destination] = true;
		}
	}
}

private void ProcessLink(DiagramLink link)
{
	link.Text = linkCounter.ToString();
	linkCounter++;
}

private int linkCounter = 0; 



For the theory behind these methods, check Wikipedia:

http://en.wikipedia.org/wiki/Depth-first_search
http://en.wikipedia.org/wiki/Breadth-first_search

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