简体   繁体   English

使用算法A *查找路径

[英]Finding path using algorithm A*

hello i'd like to ask some javascript code in finding path using algorithm A*. 您好,我想问一些JavaScript代码,以使用算法A *查找路径。 the path will have source node and target node , and here's the example code 该路径将具有source nodetarget node ,这是示例代码

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

the problem is im using position.push and it is array but i can't implement the pointer, if its has move next or move previous when finding path to the target node. 问题是即时通讯使用position.push并且它是数组,但是如果找到目标节点的路径时指针已经移到下一个或移到了上一个,则我无法实现该指针。 thanks 谢谢

maybe you don't need push position.push(_e.target); 也许您不需要push position.push(_e.target); , just let the cursor in to the end , after the cursor hasn't more target , you can initial the cursor to back to the first node and don't forget each node you visit has to be set to be true ,只需将光标移到末尾,在没有更多目标之后,就可以将光标初始返回第一个节点,并且不要忘记将访问的每个节点都设置为true

i have the answer for this, initial the cursor from the first node until the target node and don't forget each node you visit has to be set to be true if the node has visited, then go back again try to finding the first edges from the nodes, if the node hasn't true go to the next node, if the node has true find other node. 我有答案,首先将光标从第一个节点一直移动到目标节点,并且不要忘记您访问的每个节点都必须设置为true(如果该节点已访问过),然后再次返回以尝试找到第一个边缘如果该节点不是true,则从该节点转到下一个节点;如果该节点为true,则找到另一个节点。 here's my example code 这是我的示例代码

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
            }

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM