Javascript 用A算法寻找路径*

Javascript 用A算法寻找路径*,javascript,algorithm,a-star,gephi,Javascript,Algorithm,A Star,Gephi,您好,我想询问一些使用算法A*查找路径的javascript代码。路径将包含源节点和目标节点,下面是示例代码 do{ var cursor = nodes.node1 //this is source node for (var i in GexfJS.graph.edgeList) { // search all edgeList on graph var _e = GexfJS.graph.edgeList[i] //each list has variabl

您好,我想询问一些使用算法A*查找路径的javascript代码。路径将包含源节点和目标节点,下面是示例代码

do{
    var cursor = nodes.node1 //this is source node
    for (var i in GexfJS.graph.edgeList) { // search all edgeList on graph
        var _e = GexfJS.graph.edgeList[i] //each list has variable _e
        var position = []; // where i put the pointer
        if ( _e.source == cursor ) { //when some edge source is match from the cursor(source node/initial node)
            var _n = GexfJS.graph.nodeList[_e.target]; // edge target from the node which match with the cursor
            _str += 'Found '; // just give string if it's works
            position.push(_e.target); // for pointer?
            cursor = _e.target // go to the next node, but how can i move to previos node?
            }
    }
} while(cursor!=nodes.node2); //until find the target node
pathed={};
            ketemu=false;
            cursor1="";
            if(path==true){
            while(ketemu!=true){
                var cursor = nodes.node1;
                var lala = new Array();
                var visit = new Array();
                var j = 0;
                for (var i in GexfJS.graph.edgeList) { // relasi yang dituju 
                    var _e = GexfJS.graph.edgeList[i];
                    if ( _e.source == cursor ) {
                        var _n = GexfJS.graph.nodeList[_e.target];
                        if(pathed[_n.label] != true){
                            if(_e.target==nodes.node2){

                            cursor = _e.target;
                            cursor1 = _e.target;
                            lala[j] = cursor;
                            visit[j] = cursor;
                            j++;

                            ketemu=true;
                            break;
                            }

                             if(_e.target !=nodes.node2){

                                cursor = _e.target;
                                lala[j] = cursor;
                                visit[j] = cursor;
                                j++;
                            }


                            //alert(cursor);
                            //alert(lala[j]);

                        }

                    }
                    //else if(cursor1==_e.target){
                    //cursor = nodes.node1;
                    //alert(cursor);
                    //}


                }
                if(ketemu!=true){
                i=0;
                for(var j=lala.length-1;j>=0;j--){
                var test = lala.length-1;
                var ujung = lala[test];
                var koyong = nodes.node1;

                i++;

                }

                ujung = GexfJS.graph.nodeList[ujung];
                pathed[ujung.label] = true;
                cursor = koyong;

                }

            }

                for(var k=0;k<visit.length;k++){
                var visitednya = visit[k];
                var _n = GexfJS.graph.nodeList[visitednya];
                _str += '<li><div class="smallpill" style="background: ' + _n.color.base +'"></div><a href="#" onmouseover="GexfJS.params.activeNode = ' + _e.source + '" onclick="displayNode(' + _e.source + ', true); return false;">' + _n.label + '</a>' + ( GexfJS.params.showEdgeWeight && _e.weight ? ' [' + _e.weight + ']' : '') + '</li>';
                }
                visit=[];



            //end
            }

问题是im使用position.push,它是数组,但我无法实现指针,如果它在查找到目标节点的路径时具有move-next或move-previous。谢谢

也许你不需要推位,只需将光标放在末尾,在光标没有更多目标后,您可以将光标初始化回第一个节点,不要忘记您访问的每个节点都必须设置为true,将光标从第一个节点初始化到目标节点,不要忘记,如果节点已访问,则您访问的每个节点都必须设置为true,然后再次返回尝试从节点查找第一条边,如果节点未为true,则转到下一个节点,如果节点为true,则查找其他节点。下面是我的示例代码

do{
    var cursor = nodes.node1 //this is source node
    for (var i in GexfJS.graph.edgeList) { // search all edgeList on graph
        var _e = GexfJS.graph.edgeList[i] //each list has variable _e
        var position = []; // where i put the pointer
        if ( _e.source == cursor ) { //when some edge source is match from the cursor(source node/initial node)
            var _n = GexfJS.graph.nodeList[_e.target]; // edge target from the node which match with the cursor
            _str += 'Found '; // just give string if it's works
            position.push(_e.target); // for pointer?
            cursor = _e.target // go to the next node, but how can i move to previos node?
            }
    }
} while(cursor!=nodes.node2); //until find the target node
pathed={};
            ketemu=false;
            cursor1="";
            if(path==true){
            while(ketemu!=true){
                var cursor = nodes.node1;
                var lala = new Array();
                var visit = new Array();
                var j = 0;
                for (var i in GexfJS.graph.edgeList) { // relasi yang dituju 
                    var _e = GexfJS.graph.edgeList[i];
                    if ( _e.source == cursor ) {
                        var _n = GexfJS.graph.nodeList[_e.target];
                        if(pathed[_n.label] != true){
                            if(_e.target==nodes.node2){

                            cursor = _e.target;
                            cursor1 = _e.target;
                            lala[j] = cursor;
                            visit[j] = cursor;
                            j++;

                            ketemu=true;
                            break;
                            }

                             if(_e.target !=nodes.node2){

                                cursor = _e.target;
                                lala[j] = cursor;
                                visit[j] = cursor;
                                j++;
                            }


                            //alert(cursor);
                            //alert(lala[j]);

                        }

                    }
                    //else if(cursor1==_e.target){
                    //cursor = nodes.node1;
                    //alert(cursor);
                    //}


                }
                if(ketemu!=true){
                i=0;
                for(var j=lala.length-1;j>=0;j--){
                var test = lala.length-1;
                var ujung = lala[test];
                var koyong = nodes.node1;

                i++;

                }

                ujung = GexfJS.graph.nodeList[ujung];
                pathed[ujung.label] = true;
                cursor = koyong;

                }

            }

                for(var k=0;k<visit.length;k++){
                var visitednya = visit[k];
                var _n = GexfJS.graph.nodeList[visitednya];
                _str += '<li><div class="smallpill" style="background: ' + _n.color.base +'"></div><a href="#" onmouseover="GexfJS.params.activeNode = ' + _e.source + '" onclick="displayNode(' + _e.source + ', true); return false;">' + _n.label + '</a>' + ( GexfJS.params.showEdgeWeight && _e.weight ? ' [' + _e.weight + ']' : '') + '</li>';
                }
                visit=[];



            //end
            }

谢谢

有很多非常详细的教程介绍了这一点-您尝试过几种不同的来源吗?您可能希望使用优先级队列,并根据启发式值+成本值对其进行索引。我是sry,我在这里使用javascript和我是新手。是的,问题是我如何实现在javascript中搜索路径的指针。我一直在使用一个数组,但我不知道插入什么值来生成一些pointer@Marty你是什么意思?是的,我在wiki上读过A*算法,但是实现pointeryeah似乎很困难,我以前没有读过,但是它似乎不起作用,因为在循环中搜索edlist之后,条件会处理