// Finds and returns the path between source and target indices // and the spanning tree of the search. // // Returns true if a path exists, returns false otherwise. // // TODO Replace ref with in if one day Unity supports C# 7+ protected override bool FindPath( int sourceIndex, int targetIndex, AbstractGraph <NodeType, EdgeType> sourceGraph, ref List <int> path, ref List <HelperEdge> spanningTree ) { HelperEdge startEdge = new HelperEdge(sourceIndex, sourceIndex, EdgeCommons.DefaultEdgeCost); searchQueue.Enqueue(startEdge); visitingList[sourceIndex] = GraphSearchCommons.SearchFlags.visited; // Search while (searchQueue.Count > 0) { // Get the next element in the queue HelperEdge next = searchQueue.Dequeue(); // Mark for the route route[next.ToNode] = next.FromNode; // Add into the spanning tree if (next != startEdge) { spanningTree.Add(next); } // The target has found if (next.ToNode == targetIndex) { GetTargetPath(sourceIndex, targetIndex, ref path); return(true); } List <HelperEdge> edgeList = sourceGraph.GetLeadingEdges(next.ToNode); if (edgeList.Count > 0) { foreach (HelperEdge edge in edgeList) { // Push each path leading to an unvisited node if (visitingList[edge.ToNode] == GraphSearchCommons.SearchFlags.unvisited) { // Push each path leading to an unvisited node searchQueue.Enqueue(edge); // And mark it as visited visitingList[edge.ToNode] = GraphSearchCommons.SearchFlags.visited; } } } } // End of while // No path to the target return(false); }
public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } HelperEdge edge = (HelperEdge)obj; return(this == edge); }
// Returns the all possible paths from given node // Serves to path finding algorithms public override List <HelperEdge> GetLeadingEdges(int nodeIndex) { List <HelperEdge> edgeList = new List <HelperEdge>(); foreach (var edgeId in adjacencyList[nodeIndex]) { HelperEdge newEdge = new HelperEdge(nodeIndex, edgeId, 0f); edgeList.Add(newEdge); } return(edgeList); }
// Returns the all possible paths from given node // Serves to path finding algorithms public override List <HelperEdge> GetLeadingEdges(int nodeIndex) { List <HelperEdge> edgeList = new List <HelperEdge>(); foreach (EdgeType edge in this.edges[nodeIndex]) { HelperEdge newEdge = new HelperEdge(edge.FromNode, edge.ToNode, 0f); edgeList.Add(newEdge); } return(edgeList); }
// Finds and returns the path between source and target indices. // // Returns true if a path exists, returns false otherwise. // // TODO Replace ref with in if one day Unity supports C# 7+ protected override bool FindPath( int sourceIndex, int targetIndex, AbstractGraph <NodeType, EdgeType> sourceGraph, ref List <int> path ) { HelperEdge startEdge = new HelperEdge(sourceIndex, sourceIndex, EdgeCommons.DefaultEdgeCost); searchStack.Push(startEdge); // Search while (searchStack.Count > 0) { // Get the top element of the stack //EdgeType next = searchStack.Peek(); HelperEdge next = searchStack.Peek(); searchStack.Pop(); // Mark for the route route[next.ToNode] = next.FromNode; // And mark as visited visitingList[next.ToNode] = GraphSearchCommons.SearchFlags.visited; // The target has found if (next.ToNode == targetIndex) { GetTargetPath(sourceIndex, targetIndex, ref path); return(true); } List <HelperEdge> edgeList = sourceGraph.GetLeadingEdges(next.ToNode); if (edgeList.Count > 0) { foreach (HelperEdge edge in edgeList) { // Push each path leading to an unvisited node if (visitingList[edge.ToNode] == GraphSearchCommons.SearchFlags.unvisited) { searchStack.Push(edge); } } } } // End of while() // No path to the target return(false); }