3. How to?
1st: we measure the real, straight-line distance from nodes
(cities) to destination.
(Theoretically, it can be done using the Euclidean distance
which finds the straight-line distance between two points)
Now, technology can help by using more accurate distance
using GPS.
These distances are called Heuristics (i.e. estimated values
of the straight line distance)
4. Example – Cont.
Irbid
Rmth
Jers
Zrqa
amm
Krk
Maan
AQA
MFR
5 2
3
2
6
9
4
10
3
2
4
4
3
2
3
7
14 (Straight line distance)
11
15
11
5
8
9
4
When we compare distances,
We take combined heuristics
Instead of weight. Example:
Distance when we arrive Zrqa
Will be 3 (weight) + 11 (straight-
Line distance) = 14
This can be represented by the
Equation: f(n)=g(n)+h(n)
Let’s say: g is the graph weight,
And h is the heuristics distance
5. Example – Cont.
Irbi
d
Rmt
h
Jers
Zrq
a
am
m
Krk
Maan
AQ
A
MF
R
5 2
3
2
6
9
4
10
3
2
4
4
3
2
3
7
14
11
15
11
5
8
9
4
Starting from Rmth, we assign
The following to it: Rmth | 0 |14
Where the 0 means the path cost
To Rmth, and the 14 is the distnace
To AQA.
Now we start to expand the path
Now Irbid: Irbid | 5 |16 where 5 is
The g distance and 16 is the 5+11
Add Irbid to the queue
Then Zrqa Zrqa | 3 |14 , we add it
To the queue. By comparing the 16
And 14, Zrqa is less (14), it goes to
The top of the queue.
Now MFR MFR| 2 |17 , we add it
To the queue. By comparing 16
14, and 17, the queue will have Zrqa
Then Irbid, then MFR
6. Example – Cont.
Irbi
d
Rmt
h
Jers
Zrq
a
am
m
Krk
Maan
AQ
A
MF
R
5 2
3
2
6
9
4
10
3
2
4
4
3
2
3
7
14
11
15
11
5
8
9
4
Our queue
Zrqa | 3 |14
Irbid | 5 |16
MFR | 2 |17
We can add a flag that
represents where the path is
coming from
Zrqa | 3 |14 | Rmth
Irbid | 5 |16 |Rmth
MFR | 2 |17 |Rmth
Now we choose Zrqa, BUT!
From Zrqa we see Irbid and MFR,
so we should now pay attention
to graph cost (just like Dikstra’s):
From Rmth to Irbid via Zrqa =
3+6 >5, so we don’t update
From Rmth to MFR via Zrqa is
3+3=6>2, so we don’t update
7. Example – Cont.
Irbi
d
Rmt
h
Jers
Zrq
a
am
m
Krk
Maan
AQ
A
MF
R
5 2
3
2
6
9
4
10
3
2
4
4
3
2
3
7
14
11
15
11
5
8
9
4
Now from
Zrqa | 3 |14 |Rmth
We can go to amm, with a
Graph cost of 3+4 (7), and
Heuristic of 9.
7+9=16
So we add to the queue:
Amm|7|16|Zrqa
Just like before, from amm
we can go to Irbid which we
visited before, considering
the graph cost to decide to
update or not.
Rmth > Zrqa >Amma >
Irbid = 3+4+3 = 10 which is
> 5, so again we don’t
update amm.
8. Example – Cont.
Irbi
d
Rmt
h
Jers
Zrq
a
am
m
Krk
Maan
AQ
A
MF
R
5 2
3
2
6
9
4
10
3
2
4
4
3
2
3
7
14
11
15
11
5
8
9
4
Since Zrqa has visited
all, we remove it from
the queue. The queue
now:
Irbid | 5 | 16 | Rmth
amm| 7 | 16 | Zrqa
MFR | 2 |17 |Rmth
Since Irbid is in the top of
the queue, we choose it.
From Irbid we can go to
Zrqa but path is 6+3 (9),
greater than 3 (no update).
And to amm (5+3) greater
than 7, so no update.
No to Jers with 5+2 (7) Plus
8
So Jers: Jers| 7 | 15 | Irbid
This puts Jers on top of the queue, so we choose it.
9. Example – Cont.
Irbi
d
Rmt
h
Jers
Zrq
a
am
m
Krk
Maan
AQ
A
MF
R
5 2
3
2
6
9
4
10
3
2
4
4
3
2
3
7
14
11
15
11
5
8
9
4
From Jers| 7 | 15 |
Irbid we can go to
amm but 5+2+2 is
greater than 3+4 (no
update)
And Krk.
To Krk:
Krk | 9 (5+2+2) | 13 (9+4)
Krk | 9 | 13 | Jers
Again, we see if we can update path to
amm since Krk has a path to it, but this
path costs 5+2+2+4 which is greater than
3+4 (no update)
Now Krk has a combined heuristic of 13, which
puts it on the top of the queue.
From Krk we can go to AQA (destination)
AQA g(n) = 9+3 = 12
AQA h(n) = 0
AQA | 12 | 12| Krk IS THE SHORTEST PATH