Sort Quicksort Shortest Paths COMP 201 Formal Specification

  • Slides: 13
Download presentation
Sort Quicksort Shortest Paths COMP 201 Formal Specification with ASML. (Examples)

Sort Quicksort Shortest Paths COMP 201 Formal Specification with ASML. (Examples)

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

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

Sorting 4 1 5 2 3 1 2 3 4 5

Sorting 4 1 5 2 3 1 2 3 4 5

var A as Seq of Integer swap() choose i in {0. . length(A)-1}, j

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(i) : = A(j) sort() step until fixpoint swap() Main() step A : = [-4, 6, 9, 0, 2, -12, 7, 3, 5, 6] step Write. Line(“Sequence A : ") step sort() step Write. Line("after sorting: " + A)

Hoare’s quicksort • Quicksort was discovered by Tony Hoare (published in 1962). • Here

Hoare’s quicksort • Quicksort was 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 parition 1: items <= pivot partition: items > pivot

Example Initial array 4 1 0 0 1 3 1 1 3 0 3

Example Initial array 4 1 0 0 1 3 1 1 3 0 3 2 8 2 2 3 0 4 4 4 2 8 5 5 11 9 5 8 11 9 8 9 11

Here is Hoare's quicksort using sequence comprehensions: qsort(s as Seq of Integer) as Seq

Here is Hoare's quicksort using sequence comprehensions: qsort(s as Seq of Integer) as Seq of Integer if s = [] then return [] else Partition 2: parition 1: items <= pivot items > 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() Write. Line(qsort([7, 8, 2, 42]))

Shortest Paths Algorithm • Specification of Shortest Paths from a given node s. •

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 Sea. Tac to Redmond? 11 Sea. Tac Seattle

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

Graph declaration N = {Sea. Tac, Seattle, Bellevue, Redmond} D = {(Sea. Tac, Sea.

Graph declaration N = {Sea. Tac, Seattle, Bellevue, Redmond} D = {(Sea. Tac, Sea. Tac) -> 0, (Sea. Tac, Seattle) -> 11, (Sea. Tac, Bellevue) -> 13, (Sea. Tac, Redmond) -> infinity, // to be calculated structure Node s as String infinity = 9999 (Seattle, Sea. Tac) -> 11, (Seattle, Seattle) -> 0, (Seattle, Bellevue) -> 5, (Seattle, Redmond) -> 9, Sea. Tac = Node("Sea. Tac") (Bellevue, Sea. Tac) -> 13, Seattle = Node("Seattle“) (Bellevue, Seattle) -> 5, Bellevue = Node("Bellevue") (Bellevue, Bellevue) -> 0, Redmond = Node("Redmond") (Bellevue, Redmond) -> 5, (Redmond, Sea. Tac) -> infinity, // to be calculated (Redmond, Seattle) -> 9, (Redmond, Bellevue) -> 5, (Redmond, Redmond) -> 0}

shortest( s as Node, N as Set of Node, D as Map of (Node,

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,

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 … shortest. Paths. From. Sea. Tac

The main program Main() // … Graph specification … shortest. Paths. From. Sea. Tac = shortest(Sea. Tac, N, D) Write. Line("The shortest distance from Sea. Tac to Redmond is " shortest. Paths. From. Sea. Tac(Redmond) + " miles. ") The shortest distance from Sea. Tac to Redmond is 18 miles.