Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14 Sort Quicksort Shortest Paths

Similar presentations


Presentation on theme: "Lecture 14 Sort Quicksort Shortest Paths"— Presentation transcript:

1 Lecture 14 Sort Quicksort Shortest Paths
COMP 201 Formal Specification using ASML. (Examples)

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

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

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() step until fixpoint 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 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 partition: items > pivot pivot parition 1: items <= pivot

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

7 Here is Hoare's quicksort using sequence comprehensions:
qsort(s as Seq of Integer) as Seq of Integer if s = [] then return [] 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 Graph declaration structure Node s as String infinity = 9999
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} 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,
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})
?

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.


Download ppt "Lecture 14 Sort Quicksort Shortest Paths"

Similar presentations


Ads by Google