110 likes | 131 Views
Graph Programs implementation of Dijkstra's single-source shortest path algorithm, exploring termination, correctness, and complexity. Language extensions and outlook on future developments shared.
E N D
Graph Programs for Graph Algorithms Sandra Steinert and Detlef Plump The University of York 22/03/05
Example: 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->v = v; x->wt = wt; x->next = next; return x; } Graph GRAPHinit(int V) { int i; Graph G = malloc(sizeof *G); G->adj = malloc(V*sizeof(link)); G->V = V; G->E = 0; for (i = 0; i < V; i++) G->adj[i] = NULL; return G; } Void GRAPHinsertE(Graph G, Edge e) { link t; int v = e.v, w = e.w; if (v == w) return; G->adj[v] = NEW(w, e.wt, G->adj[v]); G->E++ } Priority Queue Implementation #include “PQindex.h” Typedef int Item; static int N, pq[maxPQ+1], qp[maxPQ+1]; void exch(int i, int j) { int t; t = qp[i]; qp[i] = qp[j]; qp[j] = t; pq[qp[i]] = i; pq[qp[j]] = j; } void PQinit() { N = 0; } int PQempty() { return !N; } void PQinser(int k) { qp[k] = ++N; pq[N] = k; fixUp(pq, N); } int PQdelmax() { exch (pq[1], pq[N]); fixDown(pq, 1, - - N); return pq[N+1]; } void PQchange(int k) { fixUp(pq, qp[k]); fixDown(pq, qp[k], N); } Graph Algorithm #define GRAPHpfs GRAPHspt #define P (wt[v] + t->wt) void GRAPHpfs (Graph G, int s, double wt[ ]) { int v, w; link t; PQinit(); priority = wt; for (v = 0; v < G->V; v++) { st[v] = -1; wt[v] = maxWT; PQinsert(v); } wt[s] = 0.0; PQdec(s); while (!PQempty()) if (wt[v = PQdelmin()] != maxWT) for (t = G->adj[v]; t != NULL; t = t->next) if (P < wt[w = t->v]) { wt[w] = P; PQdec(w) } } What problem does it solve? Dijkstra‘s single-source shortest path algorithm!
y y 2 y 2 2 2 2 x x x+y 1 1 1 x 1 1 z 5 3 5 3 1 1 1 1 1 1 2 2 2 2 2 2 where (x+y) < z instantiation instantiation 2 1 (PO) (PO) 5 1 1 1 2 2 2 5 5 42 42 42 (Conditional) Rule Schemata Conditional Rule Schema Instance (DPO rule with relabelling) Transformation Step
non-deterministic one-step application of a set R of conditional rule schemata sequential composition apply R “as long as possible” Semantics: Graph Programs
Results for Simple_Dijkstra Proposition: • Simple_Dijkstraalways 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
- S_Reduce can be applied times Complexity • worst case: exponential in the number of rule applications start node • - best derivation: n-1 applications of S_Reduce
non-deterministic one-step application of a set R of conditional rule schemata sequential composition apply R “as long as possible” Semantics: Language Extension While: , where B is a graph {(B B B)} with B B identity morphism on B Graph Programs
Complexity: n + (n-1)² + e + (n-1) + 1 rule applications = O(n² + e) (O(n²) if parallel edges are forbidden) Graph Program Dijkstra
Conclusion • GP is a simple, semantics-based language • Dijkstra’s 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-Warshall’s 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)