• Nenhum resultado encontrado

Estimation of gas costs

4.2 Loop analysis

4.2.1 Tarjan’s reducibility test algorithm

Tarjan’s algorithm is described in pseudocode in Algorithm 2. N D(v) is the number of descendants ofv inT. In the description of the algorithm and subsequent analysis we will drop the references to the data structure in the functions makeset,f ind, and unionsince that will always be clear. Initially, the data structure is empty; during the first for loop of Algorithm 2 the structure S

v∈V

({v}, v) is built; after that it is only modified by the functionsf indandunionand it is assumed that they operate over the current structure in each call.

In the original description of the algorithm, Tarjan also considerstree edges in line 14. Tree edges are the edges of Gthat are also edges of T. Since these can also be classified as forward edges, we do not distinguish between them.

Proof of correction

We will now prove that this algorithm is correct. We follow the structure of the paper where the algorithm was presented [17] and write the proofs in more detail. In order to do that, we will need some auxiliary results. The following lemma allows us to check if there is a path between two vertices in T using the functionN D:

Lemma 2. For every v, w ∈ G, there exists a path v → w in T if and only if ord(v) ≤ ord(w) ≤ ord(v) +N D(v).

Proof. We start by noticing that the result holds trivially if|V|= 1. Assume now that|V|>1. Suppose that ord(v)≤ord(w)≤ord(v) +N D(v). Then wis first visited after the first visit to v. Between the

Algorithm 2 Tarjan’s algorithm

Input: control flow graphG= (V, E) such that|V|=n

1: construct a DFST ofG, obtaining the preorder number ord(v) of each vertex v, from 1 ton, and calculatingN D(v) for each vertexv

2: forvsuch thatord(v) = 1 until ord(v) =ndo

3: make lists of cycle edges, forward edges and cross edges that enterv 4: makeset(v)

5: end for

6: forwsuch thatord(w) =nstep−1 untilord(w) = 1do

7: P =∅

8: foreach cycle edge (u, w)do 9: addf ind(u) toP

10: end for

11: Q=P

12: whileQ6=∅ do

13: select a vertexxfromQand delete it fromQ 14: foreach forward edge or cross edge (y, x)do 15: y0=f ind(y)

16: if ord(w)> ord(y0) or ord(w) +N D(w)≤ord(y0)then 17: return false

18: end if

19: if y0 6∈P andy0 6=wthen 20: addy0 to P and to Q

21: end if

22: end for 23: end while 24: forx∈P do 25: union(x, w) 26: end for 27: end for 28: return true

first and the last visits to v, only descendants of v were visited, and every descendant ofv was visited.

This means that the value of count when v is pushed to the stack for the last time by Algorithm 1 is ord(v) +N D(v). Then,wis in the subtree rooted atv, which means that there is a path from vtowin T.

Suppose now that there exists v →win T. Then wis a descendant of v and, so,w belongs to the subtree rooted at v, which means that w will be visited between the first and the last times that v is pushed to the stack. The value ofcountwhenvis pushed to the stack for the last time isord(v) +N D(v).

Therefore,ord(v)≤ord(w)≤ord(v) +N D(v).

The following results, that lead to Theorem 1, are attributed to Hecht and Ullman and until that point we follow the proofs in [9], after which we return to Tarjan’s proof. We should note that the definition of reducibility used by Hecht and Ullman also considers the transformation T1: If (v, v) is an edge ofG, remove it from G. In this case, a graph is reducible if the successive application of T1 and T2, in any order, results in a graph with a single vertex. We will consider that, if Ghas edges of the form (v, v), they are removed before any other operation. Edges of the form (v, v) that are added to the graph by application ofT2are ignored by Tarjan’s algorithm when the vertices ofP are merged intow. Therefore, the definitions can be seen as equivalent and the results obtained in both papers are compatible.

An important characterization of reducibility is given by a particular type of irreducible graph. The family of graphs represented in Figure 4.2 is known in the literature as (*). eis the entry node and can be the same node asa. The wavy arrows represent disjoint paths – paths that do not share nodes except possibly the first or the last. Formally, two paths u1, . . . , uk, v1, . . . , vl aredisjoint ifui 6=vj for every (i, j)∈ {1, . . . , k} × {1, . . . , l} \ {(1,1),(1, l),(k,1),(k, l)}.

e

a



b ++

kk c

Figure 4.2: Irreducible graph (*)

Proposition 1. Gis irreducible if and only if it contains a graph of the family (*) as a subgraph.

Proof. (⇐) Assume that Ghas a subgraph of the family (*). The proof is by induction on the number of vertices, n, which is at least 3.

Base: n= 3. ThenGis the graph shown in Figure 4.1a (wheree=a), or the same graph with the edge (1,2), or (2,3), or both. No transformation can be applied to any of those graphs, so they are all irreducible.

Induction hypothesis: IfGis a graph withn−1 vertices, wheren≥4, with (*) as a subgraph, then Gis irreducible.

Step: LetGbe a graph withn≥4 vertices containing (*) as a subgraph. Suppose thatGis reducible.

Then there is a sequence of T1, T2 transformations that result in a graph with 1 vertex. At some point, the transformed graphG0 hasn−1 vertices. G0 still contains (*) as a subgraph: the final edges of the paths a →b, a → c, b →c, c → b cannot be contracted by T2. Then, by induction hypothesis, G0 is irreducible, which is a contradiction.

(⇒) Suppose now that Gis irreducible. The proof is, again, by induction on the number of vertices n.

Base: n= 3. Let ebe the entry node and let a, b be the other two vertices ofG. e does not have entering edges and there exist pathse→a,e→b. It is easy to verify that, if every vertex has at most one entering edge, the graph is reducible. Ifaandb have two entering edges each, then it is not possible to apply T2 and G is irreducible. (*) is obviously a subgraph of this graph. If one vertex, a, has two entering edges and the other, b, has only one, we can applyT2 and b is collapsed into aor into e. The resulting graph is always reducible.

Induction hypothesis: Ifn≥4 and the irreducible graphGhask < nvertices, then (*) is a subgraph ofG.

Step: LetGbe an irreducible graph with n≥4 vertices. IfT2 can be applied to a vertex ofG, the resulting graphG0is an irreducible graph withn−1 vertices, so by induction hypothesis (*) is a subgraph ofG0and, so, ofG. Assume now that it is not possible to applyT2 to any vertex ofG. LetT be a DFST of G andy the rightmost child of the entry node ein T. Since T2 cannot be applied, y has two edges entering it in G: one from e and other from a vertex x6=e. (x, y) is not a forward edge: x6→y in T sincey is a child of the root. By contradiction, if (x, y) is a cross edge, ord(y)< ord(x). Consequently, sincey is the rightmost child of the root,xis a descendant ofy, and thus there isy→xin T. Yet this cannot hold by definition of cross edge. Then (x, y) is a cycle edge, and so there isy→xinT.

Consider the subgraphH of Ginduced by the vertices x, y, and all verticesz such that there exists z →xin Gthat does not containy. Ife∈H, theny does not dominatex. There exist an edge (e, y), a path e→xwithouty, an edge (x, y) and a path y→x. Noticing that the paths are disjoint and that the edges ofT are contained in the edges ofG, we conclude thatGhas a subgraph of the family (*).

Ife /∈H, theny dominatesx. Furthermore, it dominates every vertex of H. Letz ∈H\ {x}. If y did not dominate z, there would exist e→z not containing y and, so,e→xnot containingy, which is a contradiction. Then H is a directed graph with entry nodey andk < n vertices and so by induction hypothesis it contains (*), soGalso contains (*).

This property, although useful to prove other results about reducible flow graphs, does not provide a direct algorithm to find out if a graph is reducible, since determining whether a graph has a given subgraph is an NP-complete problem.

Theorem 1. Gis reducible if and only if for everyv, w∈V such that(v, w)is a cycle edge,wdominates v in G.

Proof. (⇐) Assume thatG is reducible and that (v, w) is a cycle edge ofG. Ifv=wor if w=e, then w dominatesv. Let us now assume thatv 6=wandw6=e. Suppose thatwdoes not dominatev. Then

there is a pathe→vthat does not containw. Letpbe this path, and letqbe a pathe→wandrbe the pathw→vthat exists because (v, w) is a cycle edge. We can assume thatqandrdo not share edges.

Letp∩qbe the set of vertices that are both inpand inq. This set is not empty: e∈p∩q. Letabe the vertex of p∩q which is “closest” to w: there existsa→w containingk vertices and, if a0 ∈p∩q, then all pathsa0→whave kor more vertices. Similarly, letp∩rbe the set of vertices that are both in pand inrand letbbe the vertex ofp∩rthat is “closest” tow: there existsw→bcontainingk0 vertices and, if b0 ∈p∩r, then all pathsw→b0 have k0 or more vertices. Thena,b, andwcorrespond to a, b, andc of the irreducible graph (*) shown in Figure 4.2. a→b anda→w are disjoint by definition ofa.

a→b andw→b are disjoint by definition ofb. Then all paths are disjoint and (*) is a subgraph ofG, so Gis irreducible; contradiction.

(⇒) Assume that G is irreducible. Then by Proposition 1 G has a graph of the family (*) as a subgraph. Lete, a,b andc be as in Figure 4.2, and let dbe the vertex such that (d, b) is the last edge of the path c→b. There exists a DFSTT ofGsuch that the paths a→b,b→c, andc→dare inT.

(d, b) is a cycle edge since there isb →d in T. b does not dominate d: the concatenation of the paths e→a, a→c, andc→dyields a path from the entry point todthat does not contain b.

Lemma 3. Gis reducible if and only if for all wand for allv∈P(w), there existsw→v in T. Proof. (⇐) Assume that Gis not reducible. Then by Theorem 1 there exists a cycle edge (v, w) in G such thatwdoes not dominatev, which means that there is a pathe→v inGthat does not containw.

Since (v, w) is a cycle edge,v∈C(w). By definition ofP(w), we havee, w∈P(w), and by definition of root vertex, we havew6→ein T.

(⇒) Suppose now that there existswand v∈P(w) such that there is no path fromwto v inT. So there is a path e→v in T that does not containw. Since v ∈P(w), there existsz ∈ C(w) such that there is v →z in T that does not contain w. Combining both paths, there existse→z in T, and, so, also in G, that does not containw, which means thatwdoes not dominatez, but (z, w) is a cycle edge, which is a contradiction with Theorem 1.

Consider the set of vertices with entering cycle edges. In the following three lemmas, w represents the element of that set with the highest value oford(w). Suppose that, for allv∈P(w), there is a path w → v in T. This condition means that there are no jumps to the middle of the loop headed at w.

Consider the graphG0 obtained fromGby collapsing all vertices ofP(w) into w.

Lemma 4. Every edge (v0, w0)of the graphG0 corresponds to an edge(v, w0)of the graphGwhere v is such that there exists v0→v in T.

Proof. If (x0, y0) is an edge ofG0, either (x0, y0) was an edge ofG, in which case the result trivially holds, or it is the result of a contraction of a vertex of P(w) intow, so eitherx0=wory0 =w.

Ify0 =w, then the edge (x0, w) of G0 is the result of the contraction of (x, y) of Gwhere y∈P(w), so there exists z∈C(w) such that there is y →z that does not containw. Then x∈P(w) combining (x, y) withy→z, soxwas contracted towandx0=w. (x0, y0) = (w, w) is not a proper edge ofG0.

Ifx0=w, then the edge (w, y0) ofG0 is the result of the contraction of (x, y) ofGwherey /∈P(w): if y ∈P(w) theny0 =wand the edge (w, y0) would not be considered. Soy0 =y. Sincexwas contracted to w,x∈P(w) and, by the previous assumption, we havew→xinT.

LetT0 be the subgraph of G0 whose vertices are those ofG0 and whose edges are corresponding to the edges ofT.

Lemma 5. (T0,e) is a DFST of G0. Cycle edges of G0 correspond to cycle edges of G, forward edges of G0 correspond to forward edges or cross edges ofG, and cross edges ofG0 correspond to cross edges ofG.

Proof. We start by noticing that e was not collapsed since it has no ingoing edges. T0 is a tree: the correspondence between edges ensures that there is one and only one path frometo any vertexv6=eof G0.

Let (v0, w0) be a cycle edge of G0. Then there isw0 →v0 in T0. SinceT0 ⊆T, it is also a path of T.

By Lemma 4, (v0, w0) corresponds to an edge (v, w0) ofGsuch that there isv0 →vinT. Combining both paths, we conclude that there exists a pathw0→v in T, so (v, w0) is a cycle edge.

Let (v0, w0) be a forward edge ofG0: v0→w0 is in T0. By Lemma 4 this edge corresponds to an edge (v, w0) ofG0 such thatv0→v is inT. We want to show that (v, w0) is a forward edge or a cross edge or, equivalently, that it is not a cycle edge. Assume by contradiction that it is: there exists a pathw0→vin T. Then we can combine these paths: v0→w0→v. Ifv0=vthis situation is impossible inT. Ifv06=v, the edge (v, w0) was replaced by (v0, w0) during the construction ofG0, which means thatv0=w, by the proof of the previous lemma. Thereforew0 is a vertex with an entering cycle edge that is visited after w in the traversal of T, so ord(w)< ord(w0), which contradicts the definition ofw.

Let (v0, w0) be a cross edge ofG0: w0 6→v0 in T0,v0 6→w0 in T0 andw0 < v0. Letv be the source of the edge given by Lemma 4 and recall thatv0 →vis inT. Suppose that there existsv→w0 inT. Then there is alsov0 →w0 inT, which is a contradiction. Suppose now that there existsw0 →v in T. Then, sincev has indegree 1, either w0 is an ancestor ofv0 or v0 is an ancestor ofw0, that is, eitherw0 →v0 is in T orv0→w0 is in T, but both options are impossible.

Lemma 6. For any vertexxsuch thatord(x)< ord(w), letP0(x)andC0(x)be defined in G0 relative to T0 asP(x)andC(x)were defined inG relative toT. Then x→y inT0 for ally∈P0(x)if and only if x→y in T for ally∈P(x).

Proof. (⇐) Assume by contrapositive that there exist x, y∈ V such that y ∈ P0(x) and x6→y in T0. Then it also holds thatx6→y inT. Since y ∈P0(x), there existsz0 ∈C0(x) such that there is y →z0 in G0 not containingx. By Lemma 4 and Lemma 5, the cycle edge (z0, x) of G0 corresponds to a cycle edge (z, x) of G. Moreover, there exists a pathy →z in Gcontaining edges from the path y→z0 and possibly some deleted edges with vertices in P(w)∪ {w}. We are assuming that there isw→vin T for every v∈P(w), so by Lemma 2 this path cannot containxsinceord(x)< ord(w). So y∈P(x).

(⇒) Assume again by contrapositive that there exist x, y∈V such thaty∈P(x) andx6→y inT.

• Ify6∈P(w), theny∈P0(x) andx6→y in T0;

• Ify∈P(w), then it was collapsed towand, recalling the correspondence between the edges before and after the transformation of the graph,x6→winT0. Sincey∈P(x),w∈P0(x).

Let us now return to the algorithm. Every vertexvof Gis analysed once inside the for loop in lines 6–27. Notice that if v is not the target of a cycle edge, the algorithm skipsv and starts analysing the next vertex, sinceQis empty when the while loop starts at line 12. The proof of this proposition is only sketched in [17]; herein we present the entire proof.

Ifwis the vertex of Gwith entering cycle edges with the highest value oford, then a contraction of Gis the operation that consists of collapsing all vertices ofP(w) into w. LetG(0) =GandG(k)be the result of applying a contraction toG(k−1). Also, letT(k)the DFST of G(k)and P(k)(w), for any vertex w of G(k), be defined relative to G(k), T(k) as P(w) was defined relative toG, T. Similarly to what we did previously, we may writeG0 instead ofG(1).

Proposition 2. Algorithm 2 returns true if and only ifGis reducible.

Proof. Let {w1, . . . , wl} be the set of vertices of G with entering cycle edges, numbered such that ord(w1)>· · ·> ord(wl).

(⇐) We will prove that, ifGis reducible, then the algorithm returnstrueby induction on the number of vertices with entering cycle edges,l.

Base: IfGdoes not have cycle edges, then it is a tree and it is reducible by repeated application of T2: every vertex has indegree 1 so, starting from the leaves, it is always possible to collapse a leaf into its parent (the only modification is the removal of one vertex and one edge) until the resulting graph is only the entry node. The algorithm, when applied to a tree, scans every vertex without modifying the tree, eventually reaching line 28 where it returnstrue.

Step: We start by observing that the set of vertices ofG(k)with entering cycle edges is{wi:i≥k+1}, for all k≤l. By Lemma 4 and Lemma 5, which are applicable sinceGis reducible, every cycle edge of G(k)corresponds to a cycle edge ofG, so the set of vertices with entering cycle edges ofG(k)corresponds to the set of vertices with entering cycle edges of G. f ind(wi) for i ≤ k+ 1 does not have entering cycle edges since they were all collapsed in the previous steps. Finally, wj is a vertex of G(k) for all j ≥k+ 1, that is, f ind(wj) =wj: assume thatwj was previously collapsed; thenwj ∈ P(i−1)(wi) for somei≤k+ 1, so there iswi →wj inT(i)and also inT sincewi andwj were not already collapsed, but this is a contradiction withord(wi)> ord(wj).

We will show that, ifG is reducible, thenG0 is reducible. By Lemma 3∀x∈V ∀v∈P(x), there is x→vinT. By Lemma 6,∀x∈V:ord(x)< ord(w1)∀v∈P0(x) there isx→vinT0. Since every vertex xofG0 with entering cycle edges is such that ord(x)< ord(w1),∀x∈V0 ∀v∈P0(x) there isx→v and G0 is reducible.

Let us now assume that the firstk < lcontractions were already made, so the current graph isG(k). We will show that the next iteration of the for loop that changes the graph (w=wk+1) transformsG(k) in G(k+1).

During the iteration wherew =wk+1, Q⊆ P ⊆P(k)(wk+1): The first inclusion is trivial. For the second inclusion, notice that (u, w) is a cycle edge ofGif and only if (f ind(u), w) is a cycle edge ofG(k), by Lemma 4 and Lemma 5; for every x∈V,f ind(x)→xinG; (y, x) is a forward, tree or cross edge if and only if (f ind(y), x) is a forward, tree or cross edge; andwk+16∈P. A vertexz0 =f ind(z) is added to P if (z0, wk+1) is a cycle edge or if (z0, x), for somex∈Q ⊆P, is an edge of G(k). Thus, for every z∈P there exists someu0 such that (u0, wk+1) is a cycle edge ofG(k)there exists a pathz→u0 inG(k) that does not contain wk+1.

After the while loop,P =P(k)(wk+1): Ifz∈P(k)(wk+1) then there existsusuch that (u, wk+1) is a cycle edge in G(k)and there is z→uin G(k) that does not contain wk+1. u∈P by construction of P.

Line 14 builds, backwards, every path of G(k) ending in uthat does not contain wk+1. Notice that in line 14xhas no entering edges in G(k)sinceord(x)> ord(wk+1) was verified before adding xtoP. In particular,z is added toP at some point in the execution, so, in the end of the while loop,z∈P.

So the last for loop, in lines 24–26, collapses every vertex of P(wk+1) into wk+1 and the result is G(k+1).

We conclude that, ifGis a reducible graph withlvertices, the algorithm successively transformsGin reducible graphs, reducing the number of vertices with entering cycle edges by 1 in each iteration, until G(l)is a tree, and the program returnstrue.

(⇒) Assume that G is an irreducible graph. Then there exists k such that ∃v ∈ P(wk) :wk 6→ v in T. Since, for all v, there is f ind(v) → v in T, this means that wk 6→ f ind(v) in T, considering the function f indapplied to any of the graphs G(j), j ≤l. By Lemma 2, ord(wk) > ord(f ind(v)) or ord(wk) +N D(wk)≤ord(f ind(v)). In iterationk,P eventually analyses all elements ofP(k−1)(wk), by the results obtained in the proof of the previous implication, so it will scanv and returnfalse.

Complexity analysis

Although Tarjan stated in his 1974 paper ([17]) that the time complexity of this algorithm wasO(|E|log|E|), where log is the iterated logarithm, this bound has been later reduced due to results that the same au-thor obtained for the complexity of the operations of the disjoint-set data structure. We will now show that, for nelements, a sequence ofm f ind,union, andmakesetoperations of whichnaremakesethas worst-case running timeO(m α(n)), whereαis related to the inverse Ackermann function.

The Ackermann function was defined in 1928 by William Ackermann ([1]) and it was the first example of a computable function that is not primitive recursive. Since then, several functions that share the same property have been constructed for various purposes. These functions grow faster than any multiple exponential, which means that their inverses grow very slowly – for every practical input, they are always below 5. Our proof and, so, our version of the Ackermann function, follows [4].

We will use amortized analysis to prove the desired time bound. Amortized analysis was first used in the 1980s to prove upper bounds on the time complexity of several algorithms, and it was described in more detail in 1985 by Tarjan in [18]. It is a technique that allows us to obtain tighter bounds

on the execution time of an algorithm by noticing that the worst-case running time of a sequence of operations may be smaller than the sum of the worst-case running times of each operation. One of the ways to perform amortized analysis is to use a potential function. IfDrepresents all possible states of the data structure used by the algorithm during the execution, then any Φ :D →Nis a potential function.

The choice of Φ, for each algorithm, strongly depends on the data structure and on the operations that are performed. Having chosen Φ, the amortized time of an operation, relative to Φ, is defined as a=t+ Φ(D0)−Φ(D), wheretis the actual time of the operation andD, D0 ∈ Dare the data structure states before and after the operation, respectively. Therefore, a sequence ofk operations has amortized time

are the initial and the final states of the data structure andai,ti are the amortized and actual times of theith operation. If Φ(D0)<Φ(Dk), then the amortized time is an upper bound on the actual time of the sequence of operations.

As it was already described, the disjoint-set data structure models subsets of a given set. For

As it was already described, the disjoint-set data structure models subsets of a given set. For

Documentos relacionados