Sort Quicksort Shortest Paths - PowerPoint PPT Presentation

About This Presentation
Title:

Sort Quicksort Shortest Paths

Description:

Sort Quicksort Shortest Paths COMP 201 Formal Specification with ASML. (Examples) Hoare s quicksort Quicksort was discovered by Tony Hoare (published in 1962). – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 14
Provided by: IgorPo3
Category:

less

Transcript and Presenter's Notes

Title: Sort Quicksort Shortest Paths


1
SortQuicksortShortest Paths
  • COMP 201
  • Formal Specification with ASML.
  • (Examples)

2
SORT Algorithm
(simple specification of the one-swap-at-a-time
sorting algorithm)
3
Sorting
4
var A as Seq of Integer swap() choose i in
0..length(A)-1, j in 0..length(A)-1 where i lt
j and

A(i) gt A(j) A(j) A(i)
A(i) A(j) sort() step until
fixpoint 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
Hoares 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

6
Example
4 1 3 8 0 2 11 9 5
1 3 0 2 4 8 11 9 5
0 1 3 2 4 5 8 11 9
0 1 2 3 4 5 8 9 11

Initial array
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 lt
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 gt pivot
parition 1 items lt 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
N SeaTac, Seattle, Bellevue, Redmond
D (SeaTac, SeaTac) -gt 0,
(SeaTac, Seattle) -gt 11,
(SeaTac, Bellevue) -gt 13, (SeaTac,
Redmond) -gt infinity, // to be calculated
(Seattle, SeaTac) -gt 11,
(Seattle, Seattle) -gt 0, (Seattle,
Bellevue) -gt 5, (Seattle, Redmond)
-gt 9, (Bellevue, SeaTac) -gt 13,
(Bellevue, Seattle) -gt 5,
(Bellevue, Bellevue) -gt 0,
(Bellevue, Redmond) -gt 5, (Redmond,
SeaTac) -gt infinity, // to be calculated
(Redmond, Seattle) -gt 9,
(Redmond, Bellevue) -gt 5, (Redmond,
Redmond) -gt 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, D as Map of (Node, Node) to
Integer) as Map of Node to
Integer var S s -gt 0 merge n -gt
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.
Write a Comment
User Comments (0)
About PowerShow.com