Download Graph Theory and Dijkstra's Algorithm: Solving Shortest Path Problems and more Slides Aeronautical Engineering in PDF only on Docsity!
3
Today
• Problem Formulation
- Problem solving as state space search
• Definition of Graphs
• Shortest Path problems
4
Complex missions must carefully:
- Plan complex sequences of actions
- Schedule tight resources
- Monitor and diagnose behavior
- Repair or reconfigure hardware.
Ö Most AI problems, like these, may be formulated as state space search.
5
Simple Trivial
Astronaut Goose Grain Fox
Rover
- Astronaut + 1 item allowed in the rover.
- Goose alone eats Grain
- Fox alone eats Goose
Can the astronaut get its produce safely across the Martian canal?
6
Problem Solving as
State Space Search
• Formulate Goal
- State
- Astronaut, Fox, Goose & Grain across river
• Formulate Problem
- States
- Location of Astronaut, Fox, Goose & Grain at top or bottom river bank
- Operators
- Move rover with astronaut & 1 or 0 items to other bank
• Generate Solution
- Sequence of States
- Move(goose,astronaut), Move(astronaut),...
9
Today
• Problem Formulation
- Problem solving as state space search
• Definition of Graphs
• Shortest Path problems
10
Graph
• A graph is a generalization of the simple
concept of a set of dots (called vertices
or nodes) connected by links (called
edges or arcs)
- Example: graph with 6 vertices and 7 edges
1 2
4 5
3
6
11
Examples of Graphs
SFO
Boston
LA (^) Dallas
Wash DC
Airline Routes
A B C
A B
C
A B
C
A
B C
Put C on B
Put C on A
Put B on C
Put C on A A
B
Put A on C C
Planning Actions
(graph of possible states of the world)
12
Graphs
• A graph G = (V, E) is a finite nonempty
set of vertices and a set of edges
• An empty graph is the graph whose
edge set is empty
• The null graph is the graph whose
edge set and vertex set are empty
V = {1, 2, 3}
E = {(1, 2)}
1 3 V = {1, 2, 3, 4}
E = {(1,2)(2,3)(1,4)(2,4)}
1 V = {1}
E = {∅}
V = {∅}
E = {∅}
15
Graphs
• A multigraph has two or more edges
that connect the same pair of vertices
• A cycle is a path that begins and ends
with the same vertex
- A cycle of length 1 is a loop
- (1, 2, 3, 5, 4, 2, 1) is a cycle of length 6
1 2
4 5
3
6
16
Vertices
• Two vertices, u and v in an
undirected graph G are called
adjacent (or neighbors) in G, if
{( u,v) } is an edge of G.
• The degree of a vertex in an
undirected graph is the number of
edges incident with it, except that
a loop at a vertex contributes
twice to the degree of that vertex.
1 2
4 5
3
6
17
Adjacency Matrix
• A finite graph is often represented by
its adjacent matrix.
- An entry in row I and column j gives the number of edges from the i th^ to the j th vertex.
1 2
4 5
3
6
18
Layout of Graphs
S
21
• An Eulerian path in a graph is a path
that uses each edge precisely once.
- If such path exists, the graph is called traversable
• Euler showed that an Eulerian cycle
exists if and only if all vertices in the
graph are of even degree.
“The 1 st^ problem in Graph Theory”
Seven Bridges of Königsberg
22
Weighted Graph
• A weighted graph associates a value
(weight) to every edge in the graph.
- A weight of a path in a weighted graph is the sum of the weights of the traversed edges.
• Directed graph (digraph) is a graph
with one-way edges
A B
D E
C
F
2
1 1
1
3 7
2
23
Today
• Problem Formulation
- Problem solving as state space search
• Definition of Graphs
• Shortest Path problems
24
Shortest Path Problems
• The shortest path from v 1 to v 2
- Is the path of the smallest weight between the two vertices
- Shortest may be least number of edges, least total weight, etc.
- The weight of that path is called the distance between them
27
Dijkstra’s Algorithm
• Dijkstra(G,w,s)
Init_Source(G,s) S := empty set Q := set of all vertices
while Q is not an empty set loop u := Extract_Min(Q) S := S union {u} for each vertex v which is a neighbor of u loop Relax(u,v,w)
28
Dijkstra’s Algorithm
- Init_Source(G,s) for each vertex v in V[G] loop d[v] := infinite previous[v] := 0 d[s] := 0
- v = Extract_Min(Q) searches for the vertex v in the vertex set Q that has the least d[v] value. That vertex is removed from the set Q and then returned.
- Relax(u,v,w) if d[v] > d[u] + w(u,v) then d[v] := d[u] + w(u,v) previous[v] := u
29
Dijkstra’s Algorithm
s
a b
c d
V = {a, b, c, d, s} E = {(s,c), (c,d), (d,b), (b,d), (c,b), (a,c), (c,a), (a,b), (s,a)}
S = {∅}
Q = {s, a, b, c, d}
0 ∞ ∞ ∞ ∞
d = prev =
30
Dijkstra’s Algorithm
s
a b
c d
S = {s} Q = {a, b, c, d}
Extract_Min (Q) Æ s Neighbors of s = a, c
Relax (s,c,5) Relax (s,a,10)
d =
prev =
Æ
Æ
s
s
33
Dijkstra’s Algorithm
s
a b
c d
S = {s, c, d, a} Q = {b}
Extract_Min (Q) Æ a Neighbors of a = b, c Relax (a,b,1) Relax (a,c,3)
5 7 0 c d s c
d =
prev =
0 8 7 0 c c
Æ
Æ
a s
34
Dijkstra’s Algorithm
s
a b
c d
S = {s, c, d, a, b} Q = {}
Extract_Min (Q) Æ b Neighbors of b = d Relax (b, d, 4)
0 8 9 5 7 0 c a s c
d =
prev =
0 8 9 5 0 c a s
Æ
Æ
c