graph programs for graph algorithms
DESCRIPTION
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; }; - PowerPoint PPT PresentationTRANSCRIPT
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
4
(Conditional) Rule Schemata
instantiation instantiation
(PO) (PO)
1 21 2 5 1 2 3
1 2 1 2 1 2
11 2 3
42
521
1 2 5
42
52 1
1 2
52
42
ConditionalRule Schema
Instance(DPO rulewith relabelling)
TransformationStep
x+yyx y z x y x
1 2 1 2 1 2
where (x+y) < z
5
Graph Programs
non-deterministic one-step applicationof a set R of conditional rule schemata
apply R “as longas possible”
sequentialcomposition
Semantics:
6
Graph Program Simple_Dijkstra
7
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
8
Complexity
• worst case: exponential in the number of rule applications
- S_Reduce can be applied times
- best derivation: n-1 applications of S_Reduce
start node
9
Graph Programs
non-deterministic one-step applicationof a set R of conditional rule schemata
apply R “as longas possible”
sequentialcomposition
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)
11
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)