public string breadthFirstTraversal() { string str = null; int counter = 0; Queue <DirectedNode> queue = new Queue <DirectedNode>(); nodes[0].setVisited(true); queue.Enqueue(nodes[0]); while (queue.Count != 0) { //Remove the vertex at the beginning of the queue DirectedNode currentNode = queue.Dequeue(); //Get the children vertices of the newly dequeued node List <DirectedNode> nodeList = currentNode.getChildList(); //go through list foreach (DirectedNode dn in nodeList) { //If the node has NOT been visited if (dn.isVisisted() == false) { //set it to have been visisted and add it to the queue dn.setVisited(true); dn.distance = counter; queue.Enqueue(dn); int count = 0; str += "DISTANCE FROM " + dn.getNodeName() + " TO A IS " + (dn.traceBack(nodes[0], ref count)) + "\n"; } } } return(str); }
private bool HaveOnlyOnePathWithoutCycles(DirectedNode start, DirectedNode finish) { if (start == finish) { return(true); } var numberOfPaths = 0; var queue = new Queue <DirectedNode>(start.ChildNodes); var visitedNodes = new List <DirectedNode>(); while (queue.Any()) { var item = queue.Dequeue(); if (visitedNodes.Contains(item)) { return(false); //cycle } visitedNodes.Add(item); if (item == finish) { numberOfPaths++; continue; } foreach (var node in item.ChildNodes) { queue.Enqueue(node); } } return(numberOfPaths == 1); }
void Reset() { var node1 = (DirectedNode)DirectedNode.CreateInstance(typeof(DirectedNode)); var node2 = (DirectedNode)ScriptableObject.CreateInstance(typeof(DirectedNode)); var node3 = (DirectedNode)ScriptableObject.CreateInstance(typeof(DirectedNode)); node1.position = new float3(1, 1, 1); node2.position = new float3(0, 0, 0); node3.position = new float3(-1, -1, -1); node2.AddOutbound(node1); node2.AddOutbound(node3); nodes = new List <DirectedNode>() { node1, node2, node3 }; }
public int traceBack(DirectedNode targetNode, ref int counter) { counter++; if (parents.Count != 0) { DirectedNode dn = parents[0]; if (dn.getNodeName() == targetNode.getNodeName()) { return(counter); } else { dn.traceBack(targetNode, ref counter); } } return(counter); }
public void setDirectedNodeParent(List <GameObject> nodesList) { if (nodesList.Count % 2 == 0) { for (int i = 0; i < nodesList.Count; i++) { DirectedNode dn = getNode(nodesList[i].name.ToUpper()); //Debug.Log(getNode(nodesList[i + 1].name.ToUpper()).getNodeName() + " is parent of " + dn.getNodeName()); dn.addParent(getNode(nodesList[i + 1].name.ToUpper())); i++; } } else { for (int i = 1; i < nodesList.Count; i++) { DirectedNode dn = getNode(nodesList[i].name.ToUpper()); //Debug.Log(getNode(nodesList[i + 1].name.ToUpper()).getNodeName() + " is parent of " + dn.getNodeName()); dn.addParent(getNode(nodesList[i + 1].name.ToUpper())); i++; } } }
public void addParent(DirectedNode dn) { parents.Add(dn); }
public void addChild(DirectedNode dn) { children.Add(dn); }
public float DistanceSq(DirectedNode node) => math.distancesq(node.position, this.position);
public Line LineTo(DirectedNode node) => new Line(this.position, node.position);
public float3 Dir(DirectedNode node) => node.position - this.position;
public void insertNode(DirectedNode node) { nodes.Add(node); size++; }