Graph Programs for Graph Algorithms Sandra Steinert and

  • Slides: 11
Download presentation
Graph Programs for Graph Algorithms Sandra Steinert and Detlef Plump The University of York

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

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 GRAPHinsert. E(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 Graph Algorithm #include “PQindex. h” #define GRAPHpfs GRAPHspt Typedef int Item; #define P (wt[v] + t->wt) static int N, pq[max. PQ+1], qp[max. PQ+1]; void GRAPHpfs (Graph G, int s, double void exch(int i, int j) wt[ ]) { int t; { int v, w; link t; t = qp[i]; qp[i] = qp[j]; qp[j] = t; PQinit(); priority = wt; pq[qp[i]] = i; pq[qp[j]] = j; for (v = 0; v < G->V; v++) } void PQinit() { N = 0; } { st[v] = -1; wt[v] = max. WT; int PQempty() { return !N; } PQinsert(v); } void PQinser(int k) wt[s] = 0. 0; PQdec(s); { qp[k] = ++N; pq[N] = k; fix. Up(pq, N); } while (!PQempty()) int PQdelmax() if (wt[v = PQdelmin()] != max. WT) { exch (pq[1], pq[N]); for (t = G->adj[v]; t != NULL; fix. Down(pq, 1, - - N); return pq[N+1]; t = t->next) } if (P < wt[w = t->v]) void PQchange(int k) { wt[w] = P; PQdec(w) } { fix. Up(pq, qp[k]); fix. Down(pq, qp[k], N); } } What problem does shortest it solve? Dijkstra‘s single-source path algorithm! 2

Graph Program Simple_Dijkstra 3

Graph Program Simple_Dijkstra 3

(Conditional) Rule Schemata Conditional Rule Schema y x 1 z x 2 1 y

(Conditional) Rule Schemata Conditional Rule Schema y x 1 z x 2 1 y y x 2 1 x+y 2 where (x+y) < z instantiation Instance 1 (DPO rule 1 with relabelling) 2 5 1 2 1 instantiation 2 2 (PO) Transformation 1 Step 2 1 5 42 1 3 2 (PO) 2 1 5 2 2 1 1 2 5 42 2 1 1 3 2 5 42 4

Graph Programs non-deterministic one-step application apply R “as long of a set R of

Graph Programs non-deterministic one-step application apply R “as long of a set R of conditional rule schemata as possible” sequential composition Semantics: 5

Graph Program Simple_Dijkstra 6

Graph Program Simple_Dijkstra 6

Results for Simple_Dijkstra Proposition: (1) Simple_Dijkstra always terminates (follows easily from rules) (2) Correctness:

Results for Simple_Dijkstra Proposition: (1) 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 7

Complexity • worst case: exponential in the number of rule applications start node -

Complexity • worst case: exponential in the number of rule applications start node - S_Reduce can be applied times - best derivation: n-1 applications of S_Reduce 8

Graph Programs non-deterministic one-step application apply R “as long of a set R of

Graph Programs non-deterministic one-step application apply R “as long of a set R of conditional rule schemata as possible” sequential composition Semantics: Language Extension While: , where B is a graph {(B B B)} with B B identity morphism on B 9

Graph Program Dijkstra Complexity: n + (n-1)² + e + (n-1) + 1 rule

Graph Program Dijkstra Complexity: n + (n-1)² + e + (n-1) + 1 rule applications = O(n² + e) (O(n²) if parallel edges are forbidden) 10

Conclusion • GP is a simple, semantics-based language • Dijkstra’s single-source shortest-path algorithm as

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) 11