public ArrayList findShortWay(NodeClass fromThis, NodeClass toThis, GraphClass graph) { // Dijkstra's algorithm // d - minimum weights of ways to other nodes // u - marks of visited nodes // p - parent of each node, its necessary for back way finding d[graph.findNodeIndexByNodeNumber(fromThis.NodeNumber)] = 0; for (int i = 0; i < sizeOfNodes; ++i) { // weights to every node is infinite from the start int v = -1; for (int j = 0; j < sizeOfNodes; ++j) if (!u[j] && (v == -1 || d[j] < d[v])) v = j; if (d[v] == 999999999) break; u[v] = true; for (int j = 0; j < graph.GraphNodes[v].nodeEdges.Count(); ++j) { // if current way have lower weight sum to the next node - set new weight and parent int to = graph.findNodeIndexByNodeNumber(graph.GraphNodes[v].nodeEdges[j].NextNode.NodeNumber); int len = graph.GraphNodes[v].nodeEdges[j].Weight; if (d[v] + len < d[to]) { d[to] = d[v] + len; p[to] = v; } } } // find a way back from end to start and reverse it ArrayList path = new ArrayList(); // i have no idea, how index v could be less than 0, but it has happened a few times // programming is... magic! for (int v = graph.findNodeIndexByNodeNumber(toThis.NodeNumber); (v != graph.findNodeIndexByNodeNumber( fromThis.NodeNumber )) && (v >= 0) && (p[v] >= 0); v = p[v]) path.Add(v); path.Add(graph.findNodeIndexByNodeNumber( fromThis.NodeNumber )); path.Reverse(); return path; }
public EdgeClass(NodeClass cNextNode) { this.nextNode = cNextNode; this.weight = 10000000; }
public EdgeClass(NodeClass cNextNode, int cWeight) { this.nextNode = cNextNode; this.weight = cWeight; }
private void chooseTwoNodes(MouseEventArgs e) { //find which nodes was clicked if (nodeClickedFirst == null) { nodeClickedFirst = graph.whichNodeWasClicked(new Point(e.X, e.Y)); if (nodeClickedFirst != null) nodeClickedFirst.drawNode(paintBox, Color.Black, graph.NodeSelectedColor); } else { if (nodeClickedSecond == null) { nodeClickedSecond = graph.whichNodeWasClicked(new Point(e.X, e.Y)); if (nodeClickedSecond != null) nodeClickedSecond.drawNode(paintBox, Color.Black, graph.NodeSelectedColor); } } // if two nodes was clicked - do something if (nodeClickedFirst != null && nodeClickedSecond != null) { switch (stateOfForm) { case stateEnum.stateEdgeAdding: graph.addEdge(nodeClickedFirst, nodeClickedSecond); break; case stateEnum.stageShortWayFinding: if (!graph.shortPathCalculation(nodeClickedFirst, nodeClickedSecond)) infoUpdate("PathNotExistS"); else infoUpdate("InfoTextDefaultS"); break; } nodeClickedFirst = null; nodeClickedSecond = null; } }
public void addEdge(NodeClass nextNode) { int weight = calculateWeight(nextNode); EdgeClass newEdge = new EdgeClass(nextNode, weight); nodeEdges.Add(newEdge); }
private int calculateWeight(NodeClass nextNode) { // weight is a distance between nodes in px int result = 0; double x = Math.Pow(Convert.ToDouble(this.nodePosition.X - nextNode.nodePosition.X), 2); double y = Math.Pow(Convert.ToDouble(this.nodePosition.Y - nextNode.nodePosition.Y), 2); result = (int)Math.Floor(Math.Sqrt(x + y)); return result; }