Shortest Path Algorithm. Need help.

Hey, Im having trouble coming up with an algorithm for a shortest path. I have an NxN weighted matrix and need to get from the upper left corner to the lower right corner. The length of the path must be the shortest possible for the number of moves and total weight (ie. a 3X3 matrix would use 4 moves, a 4X4 uses 6 and a 5X5 uses 8).
im using a 2-dimensional array like matrix[x][y] with the storage at this location being a random integer.
I came up with some guidelines:
1) the path can never move left because it would break the shortest moves requirement.
2) the path can never move up for the same reason.
when traversing through the matrix if x == N then go down to N,N. If y == n go right until N,N.
I can visualize how to do it. Its just putting my algorithm into a program is beyond me right now.
Any help would be appreciated greatly.

Dijkstra's algorithm only works on a weighted graph.
I have a weighted matrix.
Its like a chess board where every square has a
number on it. I have to get from the upper left to
the lower right using as little squares as possible
and then amongst those paths find the one with the
smallest weight or total.So something where individual squares have a certain toughness to get through, then?
Yeah, A* will own that...
I really don't want to write a tutorial on how to find the shortest path. But basically, you need to have a queue of paths. For each iteration until you find your goal, you pop the path with the least cost and you find the four paths you could get from it (move up, down, left, and right).
The key here is the cost function (heuristic). It must approximate the actual cost of a path WITHOUT over-approximating.
For your problem, you should probably factor in cost of the path so far, and the lowest possible expectable distance from the end of the path you're considering to the goal (think "distance formula").
Also, since these are general algorithms, you should probably code them up to work with an arbitrary start/end, instead of concrete values.
~Cheers

Similar Messages

  • Best shortest path algorithm ??

    Hi
    I am developing an application to guide a user from one point to another. I need to work out the shortest path between these two points.
    Is Dijkstra's algorithm the best to use? or should I be looking for a different one? I am looking for speed.
    Thanks

    Choosing a "single source shortest path" algorithm depends on if you are going to use a weighted or not weighted graph, if all weights are non-negative or negative etc. If negative -weight edges are allowed, Djikstra's algorithm can not be used. Instead you can use the Bellman-Ford algorithm.
    The running time of Djikstra's algorithm is lower than that of the Bellman-Ford algorithm. Runningtime for the Bellman-Ford algorithm is in general O(V^2E). Because Djikstra's algorithm always chooses the "closest" vertex (in a weigthed directed graph G=(V,E)) , we say it uses a greedy strategy. This strategy does not always yield optimal result. How fast the Djiktra algorithm is, depends how the min-priority queue is implemented. f.example: if you use the linear array implementation of the min-priority queue, the runningtime for Djikstra's algorithm is O(v^3) while a binary min-heap implementation of min-priority queue gives a runningtime of O(V E lg V).

  • Shortest Path Algorithm:

    Hi,
    I have about 20,000 (double) points and trying to find a shortest path between any two. I know that the best algorithm for doing that is Dijkstra's one. What is the best data structure for this? I read that usually heaps but the time it needs to sort the list is too long, maybe some one knows a better way? Maybe there is another algorithm that solves this problem? Please give me any suggestion about anything that I said above.
    Thank you in advance.

    I don't know no Dijkstra's algorithm for this problem. The only algorithm of Dijkstra for something like that that I know is to find the shortest path between two nodes in a weight network.
    I think that the lower bound for what you are trying to do is the same lower bound for sorting the points, so I think that the best you can do is to apply a good sorting algorithm (like quicksort, mergesort, heapsort or shellsort).
    The best data structure depends about how much info do you have previously about the number of points and memory that you have available. If you know that the max number of points that you'll work can be fit in the available memory, then the best data holder to use is an array (it's aways the fastest). If you'll see it as an array or as stack (for example) depends on the algorithm that you'll choose to sort the points.
    One last tip: if you choose using a List, prefer the ArrayList over LinkedList. LinkedList is slower than ArrayList for almost all kind of operations. If you'll work with a lot of elemts, do some testing before pick one of the two.
    Hope it helps,
    RGB

  • Shortest Path Algorithm with Djikstra

    Hi all,
    im developing a shortest path searching techniques by using Djikstra's algorithm. My search space is very huge and it is about 100,000 nodes or we can call it Vertex. I store all the nodes and weight of edges in MySql as follows:
    From | To | PathCost
    A B 2
    B A 2
    A D 9
    D A 9
    B C 7
    C B 7
    B E 5
    E B 5
    C D 5
    D C 9
    Whenever the class receive any request from someone who want to go from point A to point Z, it will load the data from mysql by using "Select * FROM table WHERE From='Variable' ", and declare it as new Vertex, and perform the search. The reason i load it from MySql everytime i receive a new request is because any new node can be added to database anytime.
    In a nut shell, im developing a system to search for a shortest path in real world, i represent buildings as a Vertex, and road as edges, any idea of what can i improve my current djikstra searching method or any new developement to reduce the running time to lowest??
    regards,
    howsen

    minimal spanning tree != shortest path...
    Mr. Pike is indeed correct. Consider this simple example. arrange a bunch of dots around the rim of a circle. Consider the distance between them to be just the standard measure of distance in the plane. Assume that the points are fairly evenly spaced, but not exactly evenly spaced.
    The minimal spanning tree, which you got by first joining the two closest points and then the next two, and then by joining more points to that structure, will end up looking like a letter C going around the circle and leaving a gap between the two points that were the furthest apart on the circle.
    Every point connects to every other point, possibly by passing through other points and it is minimal because it left out the widest gap.
    On the other hand, the quickest way to get from one tip of the C to the other tip is to go straight from the one to the other, not to walk all the way around the circle following a path that goes through every single other point in the system.
    Reducing a graph to a minimal spanning tree, gets you a single subgraph of the original grpah that has the least total distance. That, unfortunately, has nothing to do with telling you the fastest way to get from point A to point B traveling along the original graph.
    hence minimal spanning tree != shortest path...

  • Cost Factor in Shortest Path

    Hi,
    I need a little help.
    I am performing routing on geodatic data (i.e., locations with Lat/Long).
    I think the AStar shortest path algorithms are finding the shortest path on the basis of distance betwwen two nodes.
    I am not quite sure whether they are taking care of the cost column in the link table.
    for eg., say A-B-C and A-D-C are two alternative routes from A to C .
    such that A-B-C is the shortest path on the basis of geodatic distance between the locations A an C.
    But may be the cost-wise A-D-C would have been a shorter path.
    So, which path will the algo return?
    Also I do not want to implement AStarCostFunction class and provide a distance function.
    Please help me out.
    Regards.

    Hi,
    Any/all cost-based analysis in the network data model is done using the cost column. If the cost column contains geodetic distances then that is what will be used.
    Hope this helps,
    Dan

  • Shortest path code

    hi
    if nyone cud give me the code for finding the shortest path between 2 points but in this case all the paths have equal weights i.e. the shortest path would be the involvement of minimum number of nodes between the start & the end node.
    Please am stuck up if anyone can provide me with the code.
    Thanks in advance

    Related to Dijkstra's shortest path algorithm if all edges have equal
    weight, just the heap management (priority queue) will be much simpler.
    If the graph to be traversed isn't too big, a simple linear array could do the
    job fine where entry i indexes paths of length i. Nothing much else is to
    be gained though: it stil will be a permanent graph labeling algoritm.
    kind regards,
    Jos

  • Help needed in a shortest path search problem..

    Basically the problem is that there is a network of nodes with different paths to other nodes and I am supposed to find a shortest path from a random node to another random node.There are other criteria like weight, capacity etc. but that shouldnt affect the search algorithm... What is the best way to do this type of problem? Example would be great,thanks!

    what about Breadth first search? Also for Dijkstra's, it seems to be dependent only on the weight of paths but if the paths have same weight, how do i go back to the previous node if a path wasn't found?

  • Fitness for Shortest path using a genetic algorithm

    Hi.
    I have this problem:
    I'm doing a program that has a map and two points. the map has wall that are not valid positions.
    I have used a A* algorithm to calculate the shortest path and is working fine.
    now i'm implementing a genetic algorithm to solve the problem but i'm not having sucess.
    My genetic operator are directions (left, right, up and down).
    I'm not being able to find a goodfitness function to solve the problem.
    can someone tell me a god function or here I can found information about that?
    I have been searching on google and I have not yet had sucess.
    I have tryed two diferent fitness tecniques:
    - add 1 for each step and if it finds a wall or gets out of the map add the maximum value (mapsize*mapsize).
    - the other one is add the valid steps and if it finds a wall or gets out of the map add the number of necessary steps to gets to destination. the valid steps has a weight of 30% and the necessary steps has a weight of 70% (i have tryed to use diferent weight).
    Can someone help me?
    thanks

    How about adapting your TSP code as follows. Your graph G(V, E) has two special vertices: v_s and v_d (start and destination). Let a candidate solution consist of a permutation of V\{v_s, v_d}u{v_m} where v_m is a new marker vertex whose purpose is to allow you to treat part of the candidate solution as irrelevant. Given candidate solution v_0 v_1 v_2 ... v_n v_m v_n+1 ... the weight is c(v_s, v_0) + c(v_0, v_1) + ... + c(v_n-1, v_n) where c(v_a, v_b) is the cost of the edge ab.

  • Hi, i'm new to java. need help setting the path in win XP

    hi all,
    i'm new to java technology. i've just downloaded the JDK and ran my first java program (hello world). i love it. java's gr8. i need help. i run win XP and how can i setup the path sothat i can execute my programs from the root dir??? any help in this direction will be greatly appreciated. please email me @ [email protected]
    Best regards
    Mrinal

    Go to Start menu and select Control Panel. In the Control Panel, double click on System. In the System dialogue, choose the Advanced tab. Then click on Environmental Variables. Select Path and Edit. Put ;c:\j2sdk1.4.0\bin at the end of the Path (or c:\j2sdk1.4.0\bin;) at the start of the Path. That's it.

  • Need help with my ps cs4 path stroke

    when i use " Rounded rectangle tool (U) " to make a path
    then i stroke it with the brush
    i used to get an even stroke, but now it's always heavy stroke at beginning and light stroke in the end
    i reset brush again and again but always get the same!!!!        
    i need help!!!!!!!!!!!!

    i reset the brush, resize it, but still the same problem
    wierd thing is, when the brush is set to default instead of my stitchy one, dots brush or brush set to highly spacing
    it seems no uneven problem

  • Need help in algorithm

    Hello everyOne,
    I am trying to make an algorithm which can detect the solar cells of solar module one by one, declare the active area by co-ordinate system like where is Xmin,Xmax, Ymin, Ymax are and then  calculate the area covered by the solar module. For this I made a program and at the moment, I am stuck badly and need help. 
    What I am trying to do is, find the white pixel and convert it to gray. I managed to do it on cell however it perform only 3/4 of the cell area. For the last two days I am trying to find the error but could not locate it. Can someone please look at my code and guide me through, Please. 
    You can download all my Sub VIs on my other thread which is http://forums.ni.com/t5/Machine-Vision/Detect-the-​rectangular/td-p/2370836.  
    Many Thanks 
    Lazer 
    Attachments:
    Cell detection - main.vi ‏181 KB
    convert 16Bits to 8Bits.vi ‏23 KB
    Jiangs_SSC_test_V130308_SJ204_POS6_10m_25deg_34_6m.jpg ‏9 KB

    Hey Lazer,
    I'm not entirely sure what you are trying to do, I'm guessing it is the big while loop which is not giving the correct results? What exactly are you trying to do, e.g. what is going into the loop, and what is supposed to come out? Is the pixel array going in going to look like the jpg you attached? It sounds like you are trying to search for particular colours (which will be a certain value in U8) and look around them, or change some colours... maybe if we get a better idea of what is trying to be achieved, we could help a little.
    In general, it's worth posting questions about vision in the machine vision forum, where there are a lot of other vision customers who will have experience. It may be easier to do this all with the vision functions, there are tools for colour matching among other things.
    Also, if you have a lot of vi's, its easier to pack them all into a zipped folder so that people can download the whole lot at once.
    There is lots of information about different functions in labview help, if you are stuck about what something does, or what to see if there is a function to do what you are looking for, you can have context help open while you look around the palette, and it will give information about each function.
    Ian S
    Applications Engineer CLD
    National Instruments UK&Ireland

  • Finding the shortest path router for the router tracking purpose

    Hi all,
    A Question asking you regarding to routers' tracking information.
    We keeps all the router infomation of our subnet in a file name "routers.txt" in this format:
    1 2 1
    2 4 1
    4 3 1
    1 3 5
    This states there are four routers, the distance between routers 1 and 2 is 1, between 2 and 4 is 1, etc.
    I need to write a Java program to keep track the shortest path between routers, I would understand that we can get this done easily in Java, but I am not a Java Savvy. I'm new in Java, would somebody help me to the right direction?
    In order to keep track the routers in our subnet easily,the output would look something like:
    Router 1
    To Router Distance Vector
    2 1 2
    3 3 2
    4 2 2
    Thanks very much,
    Cait.

    Hi kksenji,
    Well, because of the webform, it's not obvious to see. The output would be simple. From the input, for router 1 to router 2, the shortest distance is 1. For router 1 to router 3, the shortest distance is 3, for router 1 to router 4, the shortest distance is 2 and so on. The middle vector that it went through is 2 for every route. Hope this makes sense.
    Just try to solve the problem with the shortest distance, and I have a hard time to figure out the algorithm for this as well as how to get this started.
    Thanks, Cait.

  • Aco  implemenataion for shortest paths.

    has anyone used ant colony optimization algorithm for finding shortest paths other than for the tsp? please i need some help and advice. thanks

    sorry for not being specific and the insubsequent reply. am just doing some research on aco, i want to try and implement it for best bath planning on a map/graph. I've been looking around for some example on such and noticed that this algorithm is mainly used for tsp problem which got me questioning the possibility of my task. I just want to know if it is definitely possible with aco as i will give it a go in this case. Implementing this algorthm successfully is the main challenge of my project so i cannot contemplate another algorithm option. Thanks for your time.

  • Need help finalizing breadth-first search function...

    I have a map with bidirectional links and I am to find the shortest path so the first path found will be good for me. I tried follow the algorithm for BFS and it shows below. However, I am not sure when/where/what I should return per loop. I would like to get a list of Nodes (map represented by numeric node numbers). I dont think returning a list is right for this recursion so i put down int for now... I think the steps are right but just not sure how to get the result.... Please help! thanks!
    public int(?) getShortestPath() {
              int currentNode = bfsQueue.remove();//already has Root from start
              if (currentNode == destNode) {
                   shortestPathFound = true;
              } else {
                   ArrayList<Integer> temp = getChildrenLinks(currentNode);
                   for (int x=0; x< temp.size(); x++) {
                        bfsQueue.add(temp.get(x));
              if (bfsQueue.size() == 0) {
                   return null;
              } else if (shortestPathFound == false){
                   getShortestPath();
              return null;
         }

    You have almost all variable defined outside your method. You "should" declare them inside. Also, there is no need to make a recursive call (which makes it easier to return your path).
    Here's a debugged version of a BFS:
    Say you have the following edges:
    1 <--> 2
    1 <--> 3
    2 <--> 5
    2 <--> 6
    3 <--> 4
    4 <--> 5
    5 <--> 6
    5 <--> 7
    7 <--> 8
    7 <--> 9
    8 <--> 9
    8 <--> 10A picture of this would look like:
    1 --- 2 --- 6
    |      \___ |     9
    |          \|    /|
    3 --- 4 --- 5   / |
                |  /  |
                | /   |
                7 --- 8 --- 10And you want to find the shortest path from 1 to 8.
    Create a stack that holds all "layers" (a list of numbers) that you are discovering. So you start with an empty STACK and push the start node (in a list) on it:
    STACK {
      {1} // layer 1
    }Pop the last "layer" from your STACK and collect all it's unvisited neighbours. All these unvisited neighbours are the next layer which should be pushed on your STACK. Now your STACK looks like:
    STACK {
      {1}   // layer 1
      {2,3} // layer 2
    }Again pop the last layer from your stack (this is now {2,3}) and collect all it's unvisited neighbours and push these on your STACK. Now your STACK looks like:
    STACK {
      {1}     // layer 1
      {2,3}   // layer 2
      {4,5,6} // layer 3
    }Keep pushing new "layers" on your STACK until you stumble upon your destination node (8 in this example). So, when you get to your destination node, your STACK looks like:
    STACK {
      {1}     // layer 1
      {2,3}   // layer 2
      {4,5,6} // layer 3
      {7}     // layer 4
      {8,9}   // layer 5
    }Now that you have gotten to your destination, it's time to find the actual path by back-tracking through your "layers" where you must "jump" from "layer" N to a "layer" N-1 (you can only "jump" to a lower "layer"):
    The back-tracking will look like:
    STACK {
      {1}     // layer 1
       | 
      {2,3}   // layer 2
      {4,5,6} // layer 3
      {7}     // layer 4
       |
      {8,9}   // layer 5
    - 8 (in layer 5) is connected to 7 (in layer 4)
    - 7 (in layer 4) is connected to 5 (in layer 3)
    - 5 (in layer 3) is connected to 2 (in layer 2)
    - 2 (in layer 2) is connected to 1 (in layer 1)So, your shortest path is: [1,2,5,7,8]
    HTH

  • Shortest path between two arbitrary point in the network

    Hi All,
    In oracle NDM, it's possible to find shortest path between two nodes (e.g. using SDO_NET_MEM.NETWORK_MANAGER.SHORTEST_PATH), but I need to find shortest path between 2 points which are on the network edges. I suppose I should use (Interface SubPath) in network java apis. However I want to do it via PLSQL api. Should I code it myself or there exists a function?
    Any help is appreciated.
    Edited by: Fa on Dec 15, 2011 2:51 AM

    pritamg wrote:
    I have to build an application in which the user will draw the graph by creating nodes and edges.and then the start node will be marked.Then the shortest paths to other nodes will be displayed.Give me any possible clue how to start.I am in deep deep trouble.I have to use Dijkstra's Algorithm.
    please help some one...pleaseDo you know Dijkstra's Algorithm for shortest path? I believe that one was also called the traveling salesman problem. You can easily Google to find out what it is.
    Did you listen to your instructor when he/she did his/her lectures on recursion and halting contitions? If not, then please go talk to him/her and read your book, the forum is not a place to try to learn a basic concept that you should have paid attention in class for the first time.
    If you have code and you have specific questions post them and we will be glad to help, but we are not here to develop your homework solutions for you, no matter how that may affect your future.

Maybe you are looking for

  • Adobe Acrobat 7.1.0 won't read

    For some reason, after I downloaded Adobe Acrobat 7.1.0 and installed it, it won't read documents from several different websites. At first, I thought the explanation for the problem was that government websites weren't set up for 7.1.0., ----so Adob

  • BI 4.0 How to configure the server properties in CMC

    The BI 4.0 administration guide only gives information about core service ( in the appendix ). Where to find the explanation of properties for other servers like dashboard design / federation servers. Thesehave some specific parameters and there is n

  • Artist not displayed on my "artist menu"

    When I click on artists on the iPod, certain artists do not appear. When the iPod is connected to my computer and I click on artists on my iPod, they appear. Why are the songs/albums/artist on my iPod but not displayed? Make sense?

  • Internal Error Occured with time split...please help

    Hi All, During delta data load to the cube I am getting error 'Internal Error Occured with time split' in processing and data is coming till PSA and got error in PSA.There is no field found that is time dependent and I am unable to fix it in PSA by e

  • Print Boilerplate once per page

    How can I print boilerplate once per page in between parent and child records? I tried using a summary column with Reset-At:Page, but I can't reference a Page-level column within a Format trigger. Any other ideas? (thanks in advance) pat