Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Quickest Route script.

Name: Anonymous 2008-05-12 16:28

This isn't a "Give me the script please" request, but more of a...
What would I tell the computer to do so I get this result.
Basically I'm writing a quick web-based form that will give the quickest route between two nodes.
Each node is linked to eight others, and there are 16 nodes in total.
I'm looking to create a program that, when given the name of starting and ending nodes, would display the quickest way through the nodes, listing the name of each node.
There is no travel time between the nodes, I just need the shortest amount of nodes to travel between.
Planning on writing this in Javascript, as it's the language I know.
Basically, without writing the actual code for me (I can do that myself) how would I go about getting the computer to navigate all possible different ways through the "map" and then select the shortest route?
More of a logic puzzle than programming puzzle I guess, but this board suits.

Name: Anonymous 2008-05-12 16:32

*cough* Dykestraw *cough*

Name: Anonymous 2008-05-12 16:35

Good god, you're fucking stupid. Get out of /prog

Name: Anonymous 2008-05-12 16:39

Uhm, am I the only one who uses Kate?

Name: Anonymous 2008-05-12 16:41

Though I'm not sure I completely understand your problem.. I think what you need is dijkstra's pathfinding algoritm. Given a vertex in a graph it's able to find the shortest path to any other vertex. In your case you have a graph with a weight of 1 on each of the edges. It seems very lazy of me to hand you a wikipedia link but it contains some decent pseudocode.

In itself the algoritm is used to calculate the lenght of the shortest path, however you'll need the path itself as well. However it is fairly easy to save the path underway as well.

Name: Anonymous 2008-05-12 16:42

The length of the path isn't a problem, but the amount of nodes inbetween it, I can't get it around my head how to get the computer to check all routes possible, and then display the one with the shortest route.

Name: Anonymous 2008-05-12 16:47

Dijkstra's algoritm runs through all possible routes and finds the shortest, while running through the algoritm you allow each vertex to save a reference to the vertex 'behind it' (behind it meaning the shortest path back). Follow these vertex back to the original vertex and you'll have the shortest path vertex for vertex (actually you'll have them in reverse order but that shouldn't be a problem).

Name: Anonymous 2008-05-12 16:50

*cough* breadth-first *cough*
*cough* dumbfuck *cough*

Name: Anonymous 2008-05-12 16:53

>>8
M-x breadth-first

Name: Anonymous 2008-05-12 17:02

>>9
yes, match.

Name: Anonymous 2008-05-12 17:13

i'd have to agree with >>8 and speak out against dijkstra. implementing dijsktra is going to be waaay to much work for a noob like you; start with a http://en.wikipedia.org/wiki/Breadth-first_search and if it doesn't work you can optimize (but you'll have learned something in the meanwhile)

YOU WILL NOW THINK OF THE GAME WHENEVER YOU VISIT /prog/

Name: Anonymous 2008-05-12 17:43

>>8,11
A* ⊃ Dickstrap ⊃ Breadth-first

Name: Anonymous 2008-05-12 17:49

>>11
I think you're mistaking dijkstra for uniform-cost.

Name: >>13 2008-05-12 17:49

>>12
I think you're mistaking dijkstra for uniform-cost.

Name: Anonymous 2008-05-12 18:22

>>12
What sort of heuristic would you recommend for A*? I'm assuming we don't know the straight-line distance between nodes.

Name: Anonymous 2008-05-12 18:24

>>15
What do we know?

Name: >>15 2008-05-12 19:15

>>16
I don't know, I'm not any the other people in this thread.

Name: Anonymous 2008-05-12 19:50

>>16
This is getting deep.

Name: Anonymous 2008-05-12 19:55

Yeah, don't listen to these guys. Breadth-first is far too easy to be a worthwhile algorithm. And you'll have to do it every time somebody requests the path. Can your servers handle searching through up to 16 nodes every time someone requests a shortest path? I don't think so.
So what you'll need to do is run Floyd-Warshall on your graph, and fill in the results in a precomputed table. Do some work up front, then cruise past your competitor path-finding sites as they crumble under their load.

Name: Anonymous 2008-05-12 20:20

Forget it, it's NP-complete.

Name: Anonymous 2008-05-12 20:32

>>19
your servers
jabbascript

Name: Anonymous 2008-05-12 23:28

>>19 <-- This Wikipedia reader is correct.

Name: Anonymous 2008-05-13 4:40

>>22
"wikipedia reader?" fuck you, i've read my cormen

Name: Anonymous 2008-05-13 5:46

I can do this in O(1) time.

Make a lookup table with all the possible start and end points and the shortest path between them, there will only be 240 elements (16 * 15).

Name: Anonymous 2008-05-13 6:37

>>24
see >>19

Name: Anonymous 2008-05-13 8:44

Dijkstra's algorithm will be of use here.

Name: Anonymous 2008-05-13 9:12

Reading Wikipedia happens to be a running joke in the webcomic xkcd[citation needed].

Name: Anonymous 2008-05-13 9:16

Name: Anonymous 2008-05-13 14:43

>>28
It's funny because it's true.

Name: Anonymous 2008-05-13 15:49

this problem is np-complete...........

Name: Anonymous 2008-05-13 16:29

hax MY ANus

Name: Anonymous 2008-05-13 16:58

Forget it, its NP-complete.

Name: Anonymous 2008-05-13 17:16

Breadth First Search. All you use is a queue

parent[i] = -1 for all i
queue.push(end)
while(queue.top() != start) {
   cur = queue.pop()
   foreach node in neighbours(cur) {
      if (parent[node] == -1) {
         queue.push(node)
         parent[node] = cur
      }
   }
}

// prints path
cur = start
while(cur != -1) {
   print cur
   cur = parent[cur]
}

Name: Anonymous 2008-05-13 21:53

>>29
It's not funny despite its truth.

Name: Anonymous 2009-03-06 12:19

start index int const start index int   const end index.

Name: Anonymous 2010-11-02 6:57

Name: Anonymous 2010-12-06 9:17

Back to /b/, ``GNAA Faggot''

Name: Anonymous 2010-12-20 15:30

Name: Sgt.Kabukiman䳧೉ 2012-05-23 5:35

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List