1 / 13

Lecture 14 Sort Quicksort Shortest Paths

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()

Télécharger la présentation

Lecture 14 Sort Quicksort Shortest Paths

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 14SortQuicksortShortest Paths COMP 201 Formal Specification using ASML. (Examples)

  2. SORT Algorithm (simple specification of the one-swap-at-a-time sorting algorithm)

  3. 4 1 5 2 3 1 2 3 4 5 Sorting

  4. 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)

  5. 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

  6. Example Initial array

  7. 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

  8. 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.

  9. What is the shortest distance from SeaTac to Redmond? 11 13 SeaTac Seattle 11 5 5 9 13 9 5 Bellevue Redmond 5

  10. 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")

  11. 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

  12. S(n) := min({S(m) + D(m,n) | m in N}) m S(m) D(m,n) s n ?

  13. 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.

More Related