gn="justify"> {
Graph graph;
List lt; int gt; pathNodes=new List lt; int gt; ();
Image imgPathNode;
public GraphPathFinder (ref Graph _graph, string pathNodeImage)
{
graph=_graph;
imgPathNode=Image.FromFile (pathNodeImage);
}
public int GetPathLength ()
{
return pathNodes.Count;
}
public bool FindPath (int nodeStartID, int nodeEndID)
{
pathNodes.Clear ();
for (int i=0; i lt; graph.nodes.Count; i ++)
graph.nodes [i] .pfField=- 1;
bool isPathFound=false;
List lt; int gt; nodesToScan=new List lt; int gt; ();
nodesToScan.Add (nodeStartID);
int currentNum=0;
while (nodesToScan.Count gt; 0)
{
List lt; int gt; tempNodes=new List lt; int gt; ();
for (int i=0; i lt; nodesToScan.Count; i ++)
{
graph.nodes [nodesToScan [i]]. pfField=currentNum;
foreach (GraphEdge edge in graph.edges)
{
int n1, n2;
edge.GetLinkedNodes (out n1, out n2);
if (n1 == nodesToScan [i])
{
if (! (tempNodes.IndexOf (n2) gt; - 1))
if (graph.nodes [n2] .pfField == - 1)
tempNodes.Add (n2);
}
if (n2 == nodesToScan [i])
{
if (! (tempNodes.IndexOf (n1) gt; - 1))
if (graph.nodes [n1] .pfField == - 1)
tempNodes.Add (n1);
}
}
}
nodesToScan=tempNodes;
currentNum ++;
}
if (graph.nodes [nodeEndID] .pfField!=- 1)
{
int currPf=graph.nodes [nodeEndID] .pfField;
pathNodes.Add (nodeEndID);
while (currPf gt; 0)
{
currPf -;
foreach (GraphEdge edge in graph.edges)
{
int n1, n2;
edge.GetLinkedNodes (out n1, out n2);
if (n1 == pathNodes [pathNodes.Count - 1])
{
currPf=(int) Math.Min (currPf, graph.nodes [n2] .pfField);
}
if (n2 == pathNodes [pathNodes.Count - 1])
{
currPf=(int) Math.Min (currPf, graph.nodes [n1] .pfField);
}
}
bool isAdded=false;
for (int i=0; i lt; graph.nodes.Count; i ++)
{
if (graph.nodes [i] .pfField == currPf)
{
if (! isAdded)
{
foreach (GraphEdge edge in graph.edges)
{
int n1, n2;
edge.GetLinkedNodes (out n1, out n2);
if ((n1 == i || n1 == pathNodes [pathNodes.Count - 1]) amp; amp; (n2 == i || n2 == pathNodes [pathNodes.Count - 1 ]))
{
pathNodes.Add (i);
isAdded=true;
break;
}
}
}=true;
}
return isPathFound;
}
public void Draw (ref Graphics g)
{
Pen pen;
Pen normalPen=new Pen (Color.Black, 5);
Pen linkedPen=new Pen (Color.Red, 5);
for (int i=0; i lt; graph.edges.Count; i ++)
{
Point xFrom, xTo;
int node1, node2;
graph.edges [i] .GetLinkedNodes (out node1, out node2);
xFrom=new Point (graph.nodes [node1] .GetPositionX (), graph.nodes [node1] .GetPositionY ());