• Aucun résultat trouvé

Depth-first search and strong connectivity in Coq

N/A
N/A
Protected

Academic year: 2022

Partager "Depth-first search and strong connectivity in Coq"

Copied!
29
0
0

Texte intégral

(1)

1

Depth-first search and

strong connectivity in Coq

Fran¸cois Pottier

January 9, 2015

(2)

2

The problem

Finding the strongly connected components of a directed graph.

I Pedagogical value:

The first nontrivial graph algorithm.

I Practical value:

Applications in program analysis, constraint solving, model-checking, etc.

(3)

3

Known algorithms

Several known algorithms run in linear time:

I Tarjan (1972).

I One pass. Maintains auxiliary data (“lowpoint values”, etc.).

I Kosaraju (unpublished, 1978) and Sharir (1981).

I Two passes. Maintains no auxiliary data.

I Described in Cormen/Leiserson/Rivest’s textbook.

I Explained by Wegener (2002).

I Gabow(2000), improving on Purdom (1968) and Munro (1971).

I One pass. Maintains a union-find data structure.

(4)

4

Kosaraju’s algorithm

The algorithm is as follows:

1. Perform a DFS traversal of the graphE, producing a forestf1.

2. Perform a DFS traversal of the reverse graphE,¯ visiting the roots in the reverse post-orderof f1, producing a forestf2.

Then,f2 is a list of the strongly connected components. Magic!

– Note: the second traversal does not have to be depth-first.

Really Easyto implement if you have done DFS already.

(5)

5

Why does this work?

(6)

6

Complete discovery

The left side of every dashed boundary is closed w.r.t. E.

The right side of every dashed boundary is closed w.r.t. E.¯ Every component is contained within some tree.

(7)

7

f2

r scc(r)

�v2 r

w2

r f1

scc(r)

r

w1

�v1

w1

Letrbe the root of thelasttree inf1. The component ofrmust be E¯?(r).

So it is exactly thefirsttree inf2. Furthermore,

it is aprefixof the last tree inf1. So, if weremoveit by thought... we end up where we started, ... only with asmallergraph. (Induction!)

(8)

8

f2

r scc(r)

�v2 r

w2

r f1

scc(r)

r

w1

�v1

w1

Letrbe the root of thelasttree inf1. The component ofrmust be E¯?(r).

So it is exactly thefirsttree inf2.

Furthermore,

it is aprefixof the last tree inf1. So, if weremoveit by thought... we end up where we started, ... only with asmallergraph. (Induction!)

(9)

9

f2

r scc(r)

�v2 r

w2

r f1

scc(r)

r

w1

�v1

w1

Letrbe the root of thelasttree inf1. The component ofrmust be E¯?(r).

So it is exactly thefirsttree inf2. Furthermore,

it is aprefixof the last tree inf1.

So, if weremoveit by thought... we end up where we started, ... only with asmallergraph. (Induction!)

(10)

10

f2

r scc(r)

�v2 r

w2

r f1

scc(r)

r

w1

�v1

w1

Letrbe the root of thelasttree inf1. The component ofrmust be E¯?(r).

So it is exactly thefirsttree inf2. Furthermore,

it is aprefixof the last tree inf1. So, if weremoveit by thought...

we end up where we started, ...

only with asmallergraph. (Induction!)

(11)

11

Now, in Coq (briefly)

(12)

12

Forests

A non-empty forest:

w

w �v

Forests form an inductive type:

f, ~v, ~w::=| w

~ w ::~v

(13)

13

DFS forests

We define an inductive predicatedfs (i)~v(o).

I It has a certaindeclarative flavor:

~v is a DFS forest.

I It still has a certain imperativeflavor:

if the vertices ini are marked at the beginning, then a DFS algorithm may construct~v,

and the vertices ino are marked at the end.

(14)

14

DFS forests

DFS-Empty dfs (i)(i)

(15)

15

DFS forests

DFS-NonEmpty w6∈i dfs ({w} ∪i)w~ (m) roots(~w)E({w})

E({w})m dfs (m)~v(o) dfs (i) w

~

w ::~v(o)

wwas not initially marked

after markingw, the DFS forestw~ was built every root ofw~ is a successor of w every successor ofwwas marked at this point

then, the DFS forest~vwas built the DFS forest w

~

w ::~vwas built

(16)

16

Complete discovery

(17)

17

Complete discovery

Lemma (Complete discovery)

dfs(i)~v(o) andE(i)⊆iimplyE(o)⊆o.

Easy. (The paper summary of the proof is a few lines long.)

(18)

18

f2

r scc(r)

�v2 r

w2

r f1

scc(r)

r

w1

�v1

w1

(19)

19

Kosaraju’s algorithm

Theorem (Kosaraju’s algorithm is correct)

Let(V, E) be a directed graph. If the following hypotheses hold,

dfsE (∅)f1 (V) dfsE¯ (∅)f2 (V) rev(post(f1))orders f2

then the toplevel trees off2 are the components of the graph E.

Slightly involved. (The paper summary of the proof is two pages.)

(20)

20

Towards an executable

?

DFS in Coq

(?executable = extractible)

(21)

21

Parameters

A setVof vertices.

Vmust be finite.

– Slightly too strong an assumption, but OK for now.

(22)

22

Parameters

A mathematical graphE.

A runtime functionsuccessor v

producing an iterator on the successors of v.

(23)

23

Parameters

A runtime representation of sets of vertices.

Record SET (V : Type) := MkSET { repr : Type;

meaning : repr -> (V -> Prop);

void : repr;

mark : V -> repr -> repr;

marked : V -> repr -> bool;

... // 3 more hypotheses about void, mark, marked }.

(24)

24

A recursive formulation

Notation state := (repr * forest V)%type.

A state records the marked vertices and the forest built so far.

(25)

25

A recursive formulation

One would like to write something like this:

Definition visitf : state -> V -> state := ...

Thiscannot work, though.

Because the recursive call sits in a loop, the proof of termination must use the fact thata vertex, once marked, remains marked.

So, we must build this information into the postcondition...

(26)

26

A recursive formulation

This states thats1 has at least as many marked vertices as s0:

Definition visitf_dep:

forall s0 : state, V -> { s1 | lift le s0 s1 }.

Proof.

eapply (Fix (...) (...)).

...

Defined.

Works. Unpleasant.

(27)

27

A tail-recursive formulation

Work in progress.

Termination is relatively easy to prove. (Generic library: Loop.) Parameterized by user hooks (on entry,on exit,on rediscovery).

Nice (?) most general (?) specification:

Theorem dfs_main_spec:

exists vs,

rev roots = rrootsl vs /\

rdfs E (marked base) (marked dfs_main) vs /\

dfs_main = rfold dfs_init_spec vs.

Running the iterative DFS algorithm is equivalent toguessinga DFS forest and recursivelyfolding over this forest.

(28)

28

Conclusion

(29)

29

Conclusion

Contributions:

I Proofs of basic properties of DFS.

I A proof of (the principle of) Kosaraju’s algorithm.

I Embryo of a certified DFS library. (More to come.) Lessons:

I Separation between mathematicsandcodeis desirable, and quite easy to achieve in Coq.

I Writing, specifying, provinggeneric executable code is a lot of work!

I We need a certified library of basic graph algorithms!

Références

Documents relatifs

Disclaimer: layout of this document may differ from the published version. 1

For instance, here is the definition of a bool predicate Is_true, such that (Is_true true) is definitionally equal to the true proposition True, whereas (Is_true false)

The forward and backward Brownian web paths have the following properties [24,8,9]: (1) from a deterministic point (x, t ) with unique forward path W (x,t ) , the backward path is

Given a set P of n points in the plane, the Oja depth of a point x ∈ R 2 is defined to be the sum of the areas of all triangles defined by x and two points from P , normalized

Musical review of Once on this Island directed by Michael Arden at Circle in the Square Theatre, New York, in December 2017. Critique de la comédie musicale Once on this Island

To support the mechanism outlined in this proposal the packet format for RIP Version 1 [1] is modified when using Commands Update Request (9), Update Response (10) and

It is the consensus of the IETF that IETF standard protocols MUST make use of appropriate strong security mechanisms. This document describes the history and rationale for

The time and cost for a brute force attack is approximately 2^(16) more than for TripleDES, and thus, under the assumption that 128 bits of strength is the desired security