Title: Graph Programs for Graph Algorithms
1Graph Programs for Graph Algorithms
- Sandra Steinert and Detlef Plump
- The University of York
- 22/03/05
2Example Graph Algorithm in C (Sedgewick)
Graph ADT include Graph.h typedef struct node
link struct node int v double wt link next
Struct graph int V int E link adj
link NEW(int v, double wt, link next)
link x malloc(sizeof x) x-gtv v
x-gtwt wt x-gtnext next return x
Graph GRAPHinit(int V) int i Graph
G malloc(sizeof G) G-gtadj
malloc(Vsizeof(link)) G-gtV V G-gtE
0 for (i 0 i lt V i) G-gtadji
NULL return G Void GRAPHinsertE(Graph
G, Edge e) link t int v e.v, w
e.w if (v w) return G-gtadjv
NEW(w, e.wt, G-gtadjv) G-gtE
Priority Queue Implementation include
PQindex.h Typedef int Item static int N,
pqmaxPQ1, qpmaxPQ1 void exch(int i, int
j) int t t qpi qpi qpj
qpj t pqqpi i pqqpj j
void PQinit() N 0 int PQempty() return
!N void PQinser(int k) qpk N pqN
k fixUp(pq, N) int PQdelmax() exch
(pq1, pqN) fixDown(pq, 1, - - N)
return pqN1 void PQchange(int k)
fixUp(pq, qpk) fixDown(pq, qpk, N)
Graph Algorithm define GRAPHpfs GRAPHspt define
P (wtv t-gtwt) void GRAPHpfs (Graph G, int s,
double wt ) int v, w link t PQinit()
priority wt for (v 0 v lt G-gtV v)
stv -1 wtv maxWT
PQinsert(v) wts 0.0 PQdec(s)
while (!PQempty()) if (wtv PQdelmin()
! maxWT) for (t G-gtadjv t !
NULL t t-gtnext)
if (P lt wtw t-gtv) wtw
P PQdec(w)
What problem does it solve?
Dijkstras single-source shortest path algorithm!
3Graph Program Simple_Dijkstra
4(Conditional) Rule Schemata
Conditional Rule Schema
Instance (DPO rule with relabelling)
Transformation Step
5Graph Programs
6Graph Program Simple_Dijkstra
7Results for Simple_Dijkstra
- Proposition
- Simple_Dijkstra always terminates
- (follows easily from rules)
- (2) Correctness started from a graph containing
an unique node s with tag _0, Simple_Dijkstra
produces the graph which is obtained by labelling
each node v with the shortest distance from s to
v
8Complexity
- worst case exponential in the number of rule
applications
start node
- best derivation n-1 applications of S_Reduce
9Graph Programs
10Graph Program Dijkstra
11Conclusion
- GP is a simple, semantics-based language
- Dijkstras single-source shortest-path algorithm
as case study succinct programs, easy to
understand simple semantics supports formal
reasoning - Outlook
- example-driven development of GP procedures,
graph types, - more case studies in graph algorithms (e.g.
Floyd-Warshalls shortest path algorithm, vertex
colouring) and other domains - development of general proof patterns for
termination, correctness and complexity - implementation of GP (Greg Manning, The
University of York)