Help with Dijkstra's algorithm


O

Ook

This is probably a bit OT, as I'm not looking for a c++ implementaiton of
Dijkstra's algorithm, rather I'm just trying to understand it (is there a
better place then here to ask this question?). I've reviewed several sites
and the faq (no, I'm not asking you to do my homework for me) and the
examples don't give me the information I need to continue on. I'm working
with a complex tree, and am not sure how to logically apply Dijkstra's
algorithm to it. This is how I understand it in my own words:

Let's say I start at A, and the adjacent vertex with the least weight is
A-B, and the weight of the edge is 5.

A--5 --B

From here, there are only 3 places I can go as follows:

A--6--C
A--5--B--2--D
A--5--B--3--E

From A-C is 6, from A-B-D is 7, from A-B-E is 8. The next shortest path is
from A-C for a total of 6, not A-B-D or A-B-E.
D for a total of 7 or 8. Here is where I'm confused: does my second step
have to be a continuation of the first step, IOW can I only go from B to the
next unused vertex, or should I select the next unused vertex adjacent to a
used vertex that gives me the leasted cumulative weight? If the latter, then
I should choose A-C for 6, then A-B-D for 7, and lastly A-B-E for 8. At each
stop, I look at all adjacent nodes and fill in the lessor of the already
figured distance to the node, and the distance from the node I stopped at to
that node. If I do this, I eventually get what I believe is the shortest
path to my destination, but I'm not sure I did it right.
 
Ad

Advertisements

M

Mark P

Ook said:
This is probably a bit OT, as I'm not looking for a c++ implementaiton of
Dijkstra's algorithm, rather I'm just trying to understand it (is there a
better place then here to ask this question?).

comp.programming comes to mind as one option

I've reviewed several sites
and the faq (no, I'm not asking you to do my homework for me) and the
examples don't give me the information I need to continue on. I'm working
with a complex tree, and am not sure how to logically apply Dijkstra's
algorithm to it. This is how I understand it in my own words:

Let's say I start at A, and the adjacent vertex with the least weight is
A-B, and the weight of the edge is 5.

A--5 --B

From here, there are only 3 places I can go as follows:

A--6--C
A--5--B--2--D
A--5--B--3--E

From A-C is 6, from A-B-D is 7, from A-B-E is 8. The next shortest path is
from A-C for a total of 6, not A-B-D or A-B-E.
D for a total of 7 or 8. Here is where I'm confused: does my second step
have to be a continuation of the first step, IOW can I only go from B to the
next unused vertex,

no

or should I select the next unused vertex adjacent to a
used vertex that gives me the leasted cumulative weight?

yes

If the latter, then
I should choose A-C for 6, then A-B-D for 7, and lastly A-B-E for 8.

Certaintly choose A-C for 6. Then you update the distances based on
having made that choice and choose the next closest item (which may be
A-B-D for 7 or may be something else which becomes closer by virtue of
the path through C).

At each
stop, I look at all adjacent nodes and fill in the lessor of the already
figured distance to the node, and the distance from the node I stopped at to
that node. If I do this, I eventually get what I believe is the shortest
path to my destination, but I'm not sure I did it right.

That is the basic idea of the algorithm. (But bear in mind that it
doesn't work if any edge has negative length.)
 
M

Matthew L Reed

comp.programming comes to mind as one option

Thanks, I'll try there :)
or should I select the next unused vertex adjacent to a

yes

That is what I was missing. Most of the explanations I've seen seem to
assume you already know how it works, and don't clearly explain it. :p
 
Ad

Advertisements

M

Mark P

Matthew said:
Thanks, I'll try there :)




That is what I was missing. Most of the explanations I've seen seem to
assume you already know how it works, and don't clearly explain it. :p

I'd say find a better explanation, as there are good ones out there.
"Introduciton to Algorithms" by Cormen et al. is one example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top