public override SearchState CycleOne() { if (isHavePath) { return(SearchState.Search_Complete); } if (pq.isEmpty()) { return(SearchState.Search_Fail); } int index = pq.DequeueIndex(); spt[index] = searchFrontier[index]; DebugController.instance.AddPathLine(graph.GetNode(spt[index].From).Pos, graph.GetNode(spt[index].To).Pos); if (index == target) { isHavePath = true; return(SearchState.Search_Complete); } foreach (var e in graph.GetAdjoinEdges(index)) { double costNode = costToNode[e.From] + e.Cost; double costEnd = costNode + heuristic(e.To, target); if (searchFrontier[e.To] == null) { costToNode[e.To] = costNode; costToEnd[e.To] = costEnd; searchFrontier[e.To] = e; pq.ChangePriority(e.To, costEnd); } else { if (costNode < costToNode[e.To] && spt[e.To] == null) { costToEnd[e.To] = costEnd; costToNode[e.To] = costNode; searchFrontier[e.To] = e; pq.ChangePriority(e.To, costEnd); } } } return(SearchState.Searching); }
public DijkstraSP(EdgeWeightedDigraph G, int s) { edgeTo = new DirectedEdge[G.V]; distTo = new double[G.V]; pq = new IndexPriorityQueue<double>(G.V); for (int v = 0; v < G.V; v++) distTo[v] = Double.PositiveInfinity; distTo[s] = 0.0; pq.Insert(s, 0.0); while (!pq.isEmpty()) relax(G, pq.Del()); }
private IndexPriorityQueue<Double> pq; // eligible crossing edges #endregion Fields #region Constructors public PrimMST(EdgeWeightedGraph G) { edgeTo = new Edge[G.V]; distTo = new double[G.V]; marked = new bool[G.V]; for (int v = 0; v < G.V; v++) distTo[v] = Double.PositiveInfinity; pq = new IndexPriorityQueue<double>(G.V); distTo[0] = 0.0; pq.Insert(0, 0.0); // Initialize pq with 0, weight 0. while (!pq.isEmpty()) visit(G, pq.Del()); // Add closest vertex to tree. }
private bool search(int source, int target) { //int num = 0; if (source == target) { //当起点和终点相同时 Debug.Log("目标和起点节点相同" + target); return(true); } //Debug.Log("起始点是"+source+"终点是"+target); costToNode[source] = 0; costToEnd[source] = 0; IndexPriorityQueue <double> pq = new IndexPriorityQueue <double>(costToEnd, (i, j) => (float)(i - j), PriorityOrder.MinFirst); searchFrontier[source] = new GraphEdge(source, source); while (!pq.isEmpty()) { //Debug.Log("A*"); int index = pq.DequeueIndex(); spt[index] = searchFrontier[index]; //Debug.Log("最小花费的点是"+index+"花费是"+costToEnd[index]); DebugController.instance.AddPathLine(graph.GetNode(spt[index].From).Pos, graph.GetNode(spt[index].To).Pos); if (index == target) { //Debug.Log("A*总的搜索次数是"+num); return(true); } //Debug.Log("" + graph.GetAdjoinEdges(index).Count); foreach (var e in graph.GetAdjoinEdges(index)) { //num++; double costNode = costToNode[e.From] + e.Cost; double costEnd = costNode + heuristic(e.To, target); //Debug.Log("边的总花费是" + costEnd+"边的花费是"+e.Cost+"上一个节点的花费是"+costToNode[e.From]+"启发因子花费是"+ heuristic(e.To, target)); if (searchFrontier[e.To] == null) { costToNode[e.To] = costNode; costToEnd[e.To] = costEnd; searchFrontier[e.To] = e; pq.ChangePriority(e.To, costEnd); } else { //Debug.Log("哈哈哈哈"+costNode+",,"+costToNode[e.To]); if (costNode < costToNode[e.To] && spt[e.To] == null) { //否则只有当spt上没有这个节点 //Debug.Log("啦啦啦啦"); costToEnd[e.To] = costEnd; costToNode[e.To] = costNode; searchFrontier[e.To] = e; pq.ChangePriority(e.To, costEnd); } } } } return(false); }
private bool search(int source, int target) { int num = 0; if (source == target) { spt[target] = new GraphEdge(target, target); Debug.Log("一样"); return(true); } IndexPriorityQueue <float> queue = new IndexPriorityQueue <float>(costToNode, (i, j) => (i - j), PriorityOrder.MinFirst); searchFrontier[source] = new GraphEdge(source, source); //searchFrontier[] //int t = queue.TopIndex(); //Debug.Log("起点处理完后最小的点是" + t+"花费是"+costToNode[t]); while (!queue.isEmpty()) { var index = queue.DequeueIndex(); spt[index] = searchFrontier[index]; DebugController.instance.AddPathLine(graph.GetNode(spt[index].From).Pos, graph.GetNode(spt[index].To).Pos); if (index == target) { //Debug.Log("DJ总的搜索次数是"+num); return(true); } //queue.ChangePriority(index,float.PositiveInfinity); #region 邻接边的寻找 foreach (var e in graph.GetAdjoinEdges(index)) { num++; float costNode = costToNode[e.From] + e.Cost; //float costEnd = costNode + heuristic(e.To, target); if (searchFrontier[e.To] == null) { costToNode[e.To] = costNode; searchFrontier[e.To] = e; queue.ChangePriority(e.To, costNode); } else if (costNode < costToNode[e.To] && spt[e.To] == null) { //否则只有当spt上没有这个节点 Debug.Log("lalalal"); costToNode[e.To] = costNode; searchFrontier[e.To] = e; queue.ChangePriority(e.To, costNode); } } #endregion } return(false); }