650 likes | 766 Views
Outline. We will look at Dijkstra’s Shortest Path Algorithm. The issues in real world way-finding. The additional algorithms available in pgRouting OGC Location Services (OpenLS): Core Services. Permanent Labels Temporary Labels.
E N D
Outline • We will look at Dijkstra’s Shortest Path Algorithm. • The issues in real world way-finding. • The additional algorithms available in pgRouting • OGC Location Services (OpenLS): Core Services
Permanent Labels Temporary Labels V the set of vertices in the graph, xpaths are next steps F set of vertices whose shortest-path lengths remains to be calculated V – F (permanent labels) set of vertices whose shortest-path lengths have been calculated Each iteration moves one vertex from F to (V – F)
Dijkstra’s Shortest Path Algorithm • Dijkstra’s algorithm (Breath First Search (BFS)) for finding the shortest from a to v, involves assigning labels to vertices. Let L(v) denote the label of vertex v. At any point, some vertices have temporary labels and the rest have permanent labels, these are shown in red in the following slides. We let T denote the set of vertices having temporary labels.
Example Graph G(*) 2 b c 2 1 3 4 z 2 a 4 d e 1 6 7 3 f g 5
Initialized Graph G(*) 2 b c 2 1 0 4 3 z 2 a 4 d e 1 6 7 3 f g 5
After First Iteration Graph G(*) For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)} Where w(v,x) is distance from v to x. 2 2 b c T := set of all temporary vertices 2 1 3 4 z 2 a 4 1 d e Labels L(a)=min(,0) L(f) = min{,0+1} L(b) = min{,0+2} 6 7 3 f g 1 5 Chose (v= f)∊ T as minimum L(v)
After Second Iteration Graph G(*) For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)} Where w(v,x) is distance from v to x. 2 2 b c T := set of all temporary vertices 2 1 3 4 z 2 a Labels L(a)=min(,0) L(f) = min{,0+1} L(b) = min{,0+2} L(d) = min{,0+1+3} L(g) = min{,0+1+5} 1 4 4 d e 6 7 3 f g 1 6 5 Chose (v= b )∊ T as minimum L(v)
After Third Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)} Where w(v,x) is distance from v to x. 2 4 2 b c T := set of all temporary vertices 2 1 3 4 z 2 a Labels L(a)=min(,0) L(f) = min{,0+1} L(b) = min{,0+2} L(c) = min{,0+2+2} L(d) = min{,0+1+3} L(d) = min{4,0+2+2} --dif. path L(e) = min{,0+2+4} L(g) = min{,0+1+5} 6 1 4 4 d e 6 7 3 f g 6 5 1 Chose (v= c)∊ T as minimum L(v)
Fourth Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)} Where w(v,x) is distance from v to x. 2 4 2 b c T := set of all temporary vertices 2 1 3 4 z 5 2 a Labels L(a)=min(,0) L(f) = min{,0+1} L(b) = min{,0+2} L(c) = min{,0+2+2} L(d) = min{,0+1+3} L(d) = min{4,0+2+2} dif. Path L(e) = min{,0+2+4} L(z) = min{,0+2+2+1} L(g) = min{,0+1+5} L(e) = min{6,0+2+2+3} dif. Path 6 4 1 4 d e 6 7 3 f g 6 5 1 Chose (v= d )∊ T as minimum L(v)
Fourth Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)} Where w(v,x) is distance from v to x. 2 4 2 b c T := set of all temporary vertices 2 1 3 4 z 5 2 a Labels L(a)=min(,0) L(f) = min{,0+1} L(b) = min{,0+2} L(c) = min{,0+2+2} L(d) = min{4,0+2+2} L(z) = min{,0+2+2+1} L(e) = min{,0+2+4} L(g) = min{,0+1+5} L(e) = min{6,0+2+2+3} dif. Path a b c e 6 4 1 4 d e 6 7 3 f g 6 5 1 Chose (v= z)∊ T as minimum L(v)
Fourth Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x),L(v)+w(v,x)} Where w(v,x) is distance from v to x. 2 4 2 b c T := set of all temporary vertices 2 1 3 4 z 5 2 a 6 4 1 Labels L(a)=min(,0) L(f) = min{,0+1} L(b) = min{,0+2} L(c) = min{,0+2+2} L(d) = min{,0+1+3} L(d) = min{4,0+2+2} dif. Path L(z) = min{,0+2+2+1} L(e) = min{,0+2+4} L(g) = min{,0+1+5} L(e) = min{6,0+2+2+3} dif. Path 4 d e 6 7 3 f g 6 5 1 Terminate zT Path to Z shorter than any unexplored path
Another Example: 1 a.2 3 b c 2 2 0 1 z a 1 Labels L(b) = min{,0+2} L(d) = min{,0+1} 2 1 d e a,1 Choose v ∊ T with minimum L(v)=d
Another Example: 2 a,2 3 b c 2 2 0 1 z a 1 Labels L(b) = min{,0+2} L(d) = min{,0+1} L(e)=min{,0+1+1} 2 1 d e d.2 a.1 Choose v ∊ T with minimum L(v)=b
Another Example: 3 a,2 3 b,5 b c 2 2 0 1 z a 1 Labels L(b) = min{,0+2} L(d) = min{,0+1} L(e)=min{,0+1+1} L(c)=min{,0+2+3} L(e)=min{2,0+2+1} diff path 2 1 d e d.2 a.1 Choose v ∊ T with minimum L(v) =e
Another Example: 4 a,2 3 b,5 b c 2 2 0 1 a z e,4 1 Labels L(b) = min{,0+2} L(d) = min{,0+1} L(e)=min{,0+1+1} L(c)=min{,0+2+3} L(e)=min{2,0+2+1} diff path L(z)=min{,0+1+1+2} Each node is labelled with its source node and the distance from the start node 2 1 d e d.2 a.1 Shortest path: a,d,e,z Choose v ∊ T with minimum L(v)=z
Find shortest path a-f 7 d b 2 10 5 2 f 4 3 e a 6 7 c
Find shortest path a-f 7 d b 2 10 5 2 f 4 3 e a(0,a) a 6 7 c
Find shortest path a-f 7 d b 2 10 5 2 f 4 3 e Labels L(b) = min{,0+5} L(c) = min{,0+6} Each node is labelled with its source node and the distance from the start node a(0,a) a 6 7 c
Find shortest path a-f 7 d b 2 10 5 2 f 4 3 e Labels L(b) = min{,0+5} L(c) = min{,0+6} L(d) = min{,0+5+7} L(f) = min{,0+5+10} L(e) = min{,0+5+2} L(c) = min{,0+5+3} Each node is labelled with its source node and the distance from the start node a(0,a) a 6 7 c c
Find shortest path a-f 7 d b 2 10 5 2 f 4 3 e Labels L(b) = min{,0+5} L(c) = min{,0+6} L(d) = min{,0+5+7} L(f) = min{,0+5+10} L(e) = min{,0+5+2} L(c) = min{,0+5+3} Each node is labelled with its source node and the distance from the start node a(0,a) a 6 7 c
Find shortest path a-f 7 d b 2 10 5 2 f 4 3 e Labels L(b) = min{,0+5} L(c) = min{,0+6} L(d) = min{,0+5+7} L(f) = min{,0+5+10} L(e) = min{,0+5+2} L(c) = min{,0+5+3} L(e) = min{,0+6+7} Each node is labelled with its source node and the distance from the start node a(0,a) a 6 7 c
Find shortest path a-f 7 d b Minimum Paths 2 10 5 2 f 4 3 e a(0,a) Labels L(b) = min{,0+5} –from a L(c) = min{,0+6} –from a L(d) = min{,0+5+7} –from b L(f) = min{,0+5+10} – from b L(e) = min{,0+5+2} –from b L(c) = min{6,0+5+3} –from b L(c) = min{6,0+5+2 +7} –from e L(f) = min{, 0+5+2 +4} – from e d not explored because (5+2+4)<12 a 6 7 c Shortest path: a,b,e,f Length of path =11
Shortest Distance from A to all other vertices. Path from A to F = <A,D,E,F> Len = 6
Shortest Path Example 5 v2 v3 3 6 3 0 1 2 v6 v1 1 6 3 4 v4 v5
5 v2 v3 3 6 3 1 2 v6 v1 0 1 6 3 4 v4 v5 Find the shortest distance from v1 to all other vertices The shortest distance to each vertex can be read off the bottom row.
Path = <A,D,E,F> Len = 6
Yet another example Each node is labelled with total distance and predecessor node Find the shortest path from a to f in the graph G
All paths example Find the shortest distance from v1 to all other vertices The shortest distance to each vertex can be read off the bottom row.
Computer Graph Representation 1 • Adjacency matrix • Rows and columns are labeled with ordered vertices write a 1 if there is an edge between the row vertex and the column vertex and 0 if no edge exists between them
Incidence matrix Label rows with vertices Label columns with edges 1 if an edge is incident to a vertex, 0 otherwise Computer Graph Representation 2
Issues when implementing networks and route finding in GIS(*) • Real world networks (e.g. roads) tend to have many intermediate non-branching vertices (e.g. polylines) which contribute to large adjacency matrices. • Networks often multi leveled (e.g walk, bus , train, bus, ferry, car). • In some cases shortest path problems are subject to additional ‘thematic’ or ‘semantic’ constraints. (e.g. road type or quality of transmission line) Constrained Shortest Path Problem (CSPP).
Issues when implementing networks and route finding in GIS(*) • Basic Dijkstra-type algorithms provide inconsistent run-times on real-world networks, principally because such networks have very limited vertex connectivity (their adjacency matrices are very sparse)1. • Most network algorithms are 2-D, route on map maybe be in 3-D, (e.g. cross mountains).
Issues when implementing networks and route finding in GIS • The OGC Simple Feature (SFSQL) standard used in PostGIS does not include any network representation, therefore an additional Network ADT must be provided, which must integrate with the existing standard. Ideally we would want to avoid two distinct non-compatible ADTs. The existing OGC data structures and algorithms for 2-dimensional objects contain polygons, edges, and vertices and must be augmented with connectivity information. Also, the OGC SFSQL must integrate with the 1-D route finding algorithms and network data structures. The Geographic Markup Language (GML1) provides network support.
Issues when using networks and route finding in GIS(*) • The user must be able to understand how to use the Network ADT (e.g. pgRouting). • User must make sure that the data in the appropriate format for the network ADT. This may require manual or software conversion of the data.
pgRouting: preparation • A simple line graph needs to be transformed to include network topology, with connective nodes and edges, by issuing this command in the SQL Shell or pgAdmin SQL Query tool: SELECT assign_vertex_id('edges', 0.001, 'the_geom', 'gid'); • assign_vertex_id() is a function included with the pgRouting extension. This is explained in the tutorial. • In general, we need to update a table by adding a numerical value to a column that we previously created when issued the CREATE TABLE statement. UPDATE edges SET length = length(the_geom); • This creates a length field to hold the length value of network edges. This can be a cost. We would also use the same statement to update a “cost” column.
pgRouting • Now we can use any of the GIS functions provided by PostGIS and pgRouting. • Dijkstra’s algorithm is a good single-source routing algorithm provided by pgRouting.
Methods for Analysis • To run the query and get an answer in WKT (Well-Known-Text), you can issue this command in pgAdmin’s SQL Query tool: SELECT gid, AsText(the_geom) AS the_geom FROM dijkstra_sp('edges', 1, 8); Starting node = 1 Destination node = 8 ‘edges’ = the table containing the network
Results • Dijkstra’s results expressed in WKT: • The “gid” is synonymous with “FID” in ArcGIS, and “the_geom” in this case is expressed in WKT. The numbers shown represent coordinate pairs that make up points along a polyline. • WKT is an OGC (Open Geospatial Consortium) standard. • In order to view the result in OpenJump we usually include the additional steps of making a result table or using an SQL view. Details are in the labs.
Dijkstra, A*, Shooting* Anton Patrushev Shooting * includes turn restrictions, not covered on this course.
Dijkstra, A*, Shooting* Anton Patrushev (Orkney, Inc.)
Dijkstra, A*, Shooting* Anton Patrushev (Orkney, Inc.)
A heuristic algorithm • A heuristic algorithm is an algorithm that is able to produce an acceptable solution to a problem in many practical scenarios, but for which there is no formal proof of its correctness. The algorithm uses a “rule of thumb” or “educated guess” to help find a solution.
A-Star a heuristic algorithm • A* takes uses the distance so far plus cost heuristic. • Two distinctions between Dijkstra and A*. • A* uses a heuristic e.g. remaining distance. • A* uses the distance already traveled and not simply the local cost from the previously expanded node. This in contrast to Dijkstra which is a greedy algorithm. Dijkstra can be used when the estimate to the goal is difficult or impossible to calculate.
A-Star a heuristic algorithm • A* takes the distance already traveled into account and not simply the local cost from the previously expanded node. For each node x traversed, it maintains 3 values: • g(x) : the actual shortest distance traveled from initial node to current node x. • h(x) : the estimated (or "heuristic") distance from current node to goal • f(x) : the sum of g(x) and h(x).
A-Star a heuristic algorithm • The heuristics h(x) must not be an overestimate of the distance to the goal. The Euclidean (or straight line) distance is always less than or equal to the road distance.
pgRouting data structure For further details of the data pgRouting structures see http://pgrouting.postlbs.org/wiki/WorkshopFOSS4G2007 http://pgrouting.postlbs.org/wiki