Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Programs for Graph Algorithms

Similar presentations


Presentation on theme: "Graph Programs for Graph Algorithms"— Presentation transcript:

1 Graph Programs for Graph Algorithms
Sandra Steinert and Detlef Plump The University of York 22/03/05

2 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!

3 Graph Program Simple_Dijkstra
IMPORTANT (point to): Two new concepts, labels in rules are terms over a fixed algebra, rules with conditions. Here explained only intuitively, later in detail (POINT TO): x is a variable, x+y is a term, where... Is a condition START NODE: we assume that our start loop has an attached loop with label *. Mention: terms as labels and conditions. New concepts are introduced later. Here only intuition

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

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

6 Graph Program Simple_Dijkstra
IMPORTANT (point to): Two new concepts, labels in rules are terms over a fixed algebra, rules with conditions. Here explained only intuitively, later in detail (POINT TO): x is a variable, x+y is a term, where... Is a condition START NODE: we assume that our start loop has an attached loop with label *. Mention: terms as labels and conditions. New concepts are introduced later. Here only intuition

7 Results 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

8 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 We measure time complexity in the number of rule applications and ignore the cost of matching It turns out that Simple_Dijkstra is exponential. The reason is that Simple_Dijkstra is highly non-deterministic. Example a bit slower Don’t tell the sum: we this term here… Best derivation: linear The Problem here is the non-determinism. Thus, we reduced the non-determinism and show an improved program The non-determinism is responsible for the problem of exponential complexity

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

10 Graph Program Dijkstra
Complexity: n + (n-1)² + e + (n-1) + 1 rule applications = O(n² + e) (O(n²) if parallel edges are forbidden) New concepts: edge predicate in a condition; prepare is executed iff such an edge does not exist New concepts: while loop; Program is executed as long as graph B occurs as a subgraph N-1 applications of while because in each step, a box loop is deleted by next At most n-1 applications of min (comparing one node with every other node) Reduce can only be applied once to each edge Next applied n-1 times because there are n-1 box labelled nodes and one of them is deleted in each iteration IMPORTANT: explain the program first, Prepare…, rules are given here…, Prepare adds a box-loop to every node! IMPORTANT: n^2 because then e is bound by n^2 (n square)

11 Conclusion Outlook 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) SAY: Our approach is similar to the one proposed by Schied in 1992 but we allow… Schied also has a condition concept: he allows arbitrary term equations SAY: types: for example type concepts of previous talk… Schied: propositional formulas over term equations


Download ppt "Graph Programs for Graph Algorithms"

Similar presentations


Ads by Google