130 likes | 146 Views
Lecture 14 Sort Quicksort Shortest Paths. COMP 201 Formal Specification using ASML. (Examples). SORT Algorithm. (simple specification of the one-swap-at-a-time sorting algorithm). 4. 1. 5. 2. 3. 1. 2. 3. 4. 5. Sorting. var A as Seq of Integer swap()
E N D
Lecture 14SortQuicksortShortest Paths COMP 201 Formal Specification using ASML. (Examples)
SORT Algorithm (simple specification of the one-swap-at-a-time sorting algorithm)
4 1 5 2 3 1 2 3 4 5 Sorting
var A as Seq of Integer swap() choose i in {0..length(A)-1}, j in {0..length(A)-1} where i < j and A(i) > A(j) A(j) := A(i) A(i) := A(j) sort() stepuntilfixpoint swap() Main() step A := [-4,6,9,0, 2,-12,7,3,5,6] step WriteLine(“Sequence A : ") step sort() step WriteLine("after sorting: " + A)
pivot parition 1: items <= pivot partition: items > pivot Hoare’s quicksort • Quicksortwas discovered by Tony Hoare (published in 1962). • Here is the outline • Pick one item from the array--call it the pivot • Partition the items in the array around the pivot so all elements to the left are smaller than the pivot and all elements to the right are greater than the pivot • Use recursion to sort the two partitions
Example Initial array
Here is Hoare's quicksort using sequence comprehensions: qsort(s as Seq of Integer) as Seq of Integer if s = [] thenreturn [] else pivot = Head(s) rest = Tail(s) return qsort([y | y ∈ rest where y < pivot]) + [pivot] + qsort([y | y ∈ rest where y ≥ pivot]) A sample main program sorts the Sequence [7, 8, 2, 42] and prints the result: Main() WriteLine(qsort([7, 8, 2, 42])) Partition 2: items > pivot parition 1: items <= pivot pivot
Shortest Paths Algorithm • Specification of Shortest Paths from a given node s. • The nodes of the graph are given as a set N. • The distances between adjacent nodes are given by a map D, where D(n,m)=infinity denotes that the two nodes are not adjacent.
What is the shortest distance from SeaTac to Redmond? 11 13 SeaTac Seattle 11 5 5 9 13 9 5 Bellevue Redmond 5
N = {SeaTac, Seattle, Bellevue, Redmond} D = {(SeaTac, SeaTac) -> 0, (SeaTac, Seattle) -> 11, (SeaTac, Bellevue) -> 13, (SeaTac, Redmond) -> infinity, // to be calculated (Seattle, SeaTac) -> 11, (Seattle, Seattle) -> 0, (Seattle, Bellevue) -> 5, (Seattle, Redmond) -> 9, (Bellevue, SeaTac) -> 13, (Bellevue, Seattle) -> 5, (Bellevue, Bellevue) -> 0, (Bellevue, Redmond) -> 5, (Redmond, SeaTac) -> infinity, // to be calculated (Redmond, Seattle) -> 9, (Redmond, Bellevue) -> 5, (Redmond, Redmond) -> 0} Graph declaration structure Node s as String infinity = 9999 SeaTac = Node("SeaTac") Seattle = Node("Seattle“) Bellevue = Node("Bellevue") Redmond = Node("Redmond")
shortest( s as Node, N as Set of Node, D as Map of (Node, Node) to Integer) as Map of Node to Integer var S = {s -> 0} merge {n -> infinity | n in N where n ne s} step until fixpoint forall n in N where n ne s S(n) := min({S(m) + D(m,n) | m in N}) step return S min(s as Set of Integer) as Integer require s ne {} return any x | x in s where forall y in s holds x lte y
S(n) := min({S(m) + D(m,n) | m in N}) m S(m) D(m,n) s n ?
The main program Main() // … Graph specification … shortestPathsFromSeaTac = shortest(SeaTac, N, D) WriteLine("The shortest distance from SeaTac to Redmond is " shortestPathsFromSeaTac(Redmond) + " miles.") The shortest distance from SeaTac to Redmond is 18 miles.