Sie sind auf Seite 1von 22

rboles generadores mnimos (MST)

comp-420

rboles generadores mnimos (MST)


Sea G=(V,E) una grca conectada y no-dirigida con V vrtices y E aristas. Cada arista (u,v)E tiene un peso w(u,v), que es el costo de conectar u y v. La meta es encontrar un subconjunto acclico TE que conecte todos los vrtices y que minimice el costo total

w(T ) =
(u,v)T

w(u, v).

Qu tipo de estructura debe ser T para ser acclico y conectar todos los nodos? un rbol (llamado rbol generador)

En ingls minimum spanning trees.

rboles generadores mnimos (MST)


8 b 4 2 a 8 h 1 g 2 f 11 7 i 6 10 4 14 c 7 d 9 e

arbol generador mnimo con un costo de 37. arbol que no es nico. Podemos cambiar por ejemplo (b,c) por (a,h) y tendr el mismo costo.

13.1

Introduction

rboles generadores mnimos (MST)


eT

Por simplicidad supondremos that all the edge weights are tienen pesos distintos w(e) w(e) To keep things simple, Ill assume que todas las aristas distinct: w(e) = w(e ) for any pair of edges e and e Distinct weights guarantee that the minimum spanning tree of the graph para cualquier par. de aristas e y e.
is unique. Without this condition, there may be several dierent minimum spanning trees. For example, if all the edges have weight 1, then every spanning tree is a Pesos distintos garantizan que el MST de una grcaminimum spanning tree with sea nica. weight V 1.
8 10 2 18 12 14 4 26 3 30 16 5

Suppose we are given a connected, undirected, weighted graph. This is a graph G = (V, E) together with a function w : E IR that assigns a weight w(e) to each edge e. For this lecture, well assume that the weights are real numbers. Our task is to nd the minimum spanning tree of G, i.e., the spanning tree T minimizing the function w(T ) = w(e).

A weighted graph and its minimum spanning tree.

Podemos have an algorithm that assumes thesuponga pesos iguales en ciertas aristas en pero If we utilizar un algoritmo que edge weights are unique, we can still use it on graphs where mtodo de desempate. con un multiple edges have the same weight, as long as we have a consistent method for breaking
ties. One way to break ties consistently is to use the following algorithm in place of a simple comparison. ShorterEdge takes as input four integers i, j, k, l, and decides which of the two Un ejemploand (k, l) has smaller weight. edges (i, j) de mtodo de desempate es SHORTEREDGE(i,j,k,l) que decide, entre

dos

aristas (i,j) y (k,l) cul tiene el menor peso.

ShorterEdge(i, j, k, l) if w(i, j) < w(k, l) return (i, j)

14

rboles generadores mnimos (MST)


A weighted graph and its minimum spanning tree.

26

If we have an algorithm that assumes the edge weights are unique, we can still use it on graph ere multiple edges have the same weight, as long as we have a consistent method for breakin . One way to break ties consistently is to use the following algorithm in place of a simp mparison. ShorterEdge takes as input four integers i, j, k, l, and decides which of the tw es (i, j) and (k, l) has smaller weight. ShorterEdge(i, j, k, l) if w(i, j) < w(k, l) return (i, j) if w(i, j) > w(k, l) return (k, l) if min(i, j) < min(k, l) return (i, j) if min(i, j) > min(k, l) return (k, l) if max(i, j) < max(k, l) return (i, j) if max(i,j) < max(k,l) return (k, l)

The Only MST Algorithm

ere are several dierent ways to compute minimum spanning trees, but really they are all in nces of the following generic algorithm. The situation is similar to the previous lecture, where w that depth-rst search and breadth-rst search were both instances of a single generic traversa orithm.

rboles generadores mnimos (MST)


Cmo encontraran el rbol generador mnimo? IDEA: algoritmo de fuerza bruta. calcular todos los rboles generadores. encontrar el mnimo.

PROBLEMAS: no es fcil de implementar. pueden ser muchsimos rboles generadores.

rboles generadores mnimos (MST)


Existen varias maneras de calcular el MST pero todas son instancias de un algoritmos genrico. La situacin es similar a la de los algoritmos de recorridos de grcas (dfs y bfs). El algoritmo genrico para encontrar el MST mantiene una subgrca acclica A de la grca de entrada G. Esta subgrca se llama bsque generador intermedio (intermediate spanning forest) A es una subgrca del rbol generador mnimo de G. Cada componente de A es un rbol generador mnimo de sus vrtices.

rboles generadores mnimos (MST)

Inicialmente, la subgrca A consta de n rboles de un nodo cada uno. El algoritmo genrico une los rboles agregando ciertas aristas entre esos nodos. Cuando el algoritmo se detiene, A consta de un slo arbol de n vrtices: el rbol generador mnimo.

MST: algoritmo genrico


Supongamos una grca conectada y no-dirigida G=(V,E) con una funcin de peso w: ER, que asigna un peso w(e) a cada arista e. Queremos encontrar el arbol mnimo generador para G. El algoritmo maneja una serie de aristas (subgrca de G) A, manteniendo la invariante al cclo siguiente: Antes de cada iteracin, A es un subconjunto de un MST En cada paso determinamos la arista (u,v) que pueda agregarse sin violar esta invariante: A {(u,v)} ser tambin un subconjunto de MST. La arista (u,v) se conoce como arista segura o safe edge.

MST: algoritmo genrico


CS 373 Lecture 13: Minimum Spanning Trees Fall 2002

and every component of F is a minimum spanning A induce dos Initially, de aristas: El bosque generador intermedio tree of its vertices. tipos F consists of n one-node trees. The generic MST algorithm merges trees together by adding certain edges between them. When the algorithm halts, F consists of a single n-node tree, which must be the minimum spanning tree. Obviously, we have to beno son aristas edges A pero the evolving forest, extremos aristas inservibles si careful about which de we add to ambos puntos since not every edge is in the eventual minimum spanning tree. These encuentran en el induces two special types of edges. A. edge is useless if it is intermediate spanning forest F mismo componente de An not an edge of F , but both its endpoints are in the same component of F . For each component of F , we associate a safe edgethe minimum-weight edge with exactly one endpoint in that component. 1 aristas seguras si sus puntos extremos conectan, con el costo mnimo Dierent components might or might not have dierent safe edges. Some edges are neither safe nor uselesswe call these edges undecided. A. dos componentes de All minimum spanning tree algorithms are based on two simple observations.

Componentes distintas pueden every tener aristas seguras Lemma 1. The minimum spanning tree contains o no safe edge and no useless edges.

diferentes.

Proof: Let T be the minimum spanning tree. Suppose F has a bad component whose safe edge Algunas aristas pueden no ser ni servibles ni seguras, las least e = (u, v) is not in T . Since T is connected, it contains a unique path from u to v, and atllamaremos one edge e on this path has exactly one endpoint in the bad component. Removing e from the indecisas. minimum spanning tree and adding e gives us a new spanning tree. Since e is the bad components safe edge, we have w(e ) > w(e), so the the new spanning tree has smaller total weight than T . But thisMST contienethe minimum spanning tree.seguras contain every safe edge. inservible. Un is impossibleT is todas las aristas So T must y ninguna arista Adding any useless edge to F would introduce a cycle.

e v e

Proving that every safe edge is in the minimum spanning tree. The bad component of F is highlighted.

CS 373

and every component of F is a minimum spanning tree of its vertices. Initially, F consists of n one-node trees. The generic MST algorithm merges trees together by adding certain edges between them. When the algorithm halts, F consists of a single n-node tree, which must be the minimum spanning tree. Obviously, we have to be careful about which edges we add to the evolving forest, since not every edge is in the eventual minimum spanning tree. The intermediate spanning forest F induces two special types of edges. An edge is useless if it is not an edge El we associateof F ,genrico minimum-weightthe same componentuna. Formscomponent ofseguras algoritmo but both its endpoints arerepetidamenteone endpoint in that component. 1, agrega in edge with exactly of F o each aristas F a safe edgethe bosque A que evoluciona. not have dierent safe edges. Some edges are neither safe nor Dierent components might or might uselesswe call these edges undecided. All minimum spanning tree algorithms are based on two simple observations.

MST: algoritmo genrico


Lecture 13: Minimum Spanning Trees

Fall 2002

al

Cuando agregamos nuevas aristas a A, algunas aristas indecisas se Lemma 1. conviertenThe minimum spanning tree en inservibles. and no useless edges. en seguras, otras contains every safe edge

Proof: Let T be the minimum spanning tree. Suppose F has a bad component whose safe edge e = especicar Since T is connected, contains a unique path from decidir at least Para (u, v) is not in T .un algoritmo enit particular debemos u to v, and qu aristas one edge e on this path has exactly one endpoint in the bad component. Removing e from the agregar y cmo identicargives us a new spanning segurase e the bad components cada minimum spanning tree and adding e nuevas aristas tree. Since is inservibles en safe edge, we have w(e ) > w(e), so the the new spanning tree has smaller total weight than T . iteracinis del algoritmo. minimum spanning tree. So T must contain every safe edge. But this impossibleT is the Adding any useless edge to F would introduce a cycle.

e v e

Proving that every safe edge is in the minimum spanning tree. The bad component of F is highlighted.

So our generic minimum spanning tree algorithm repeatedly adds one or more safe edges to

MST: algoritmo genrico


Generic-MST(G, w) 1 A 2 while A does not form a spanning tree 3 do nd and edge (u, v) that is safe for A 4 A A {(u, v)} 5 returnA
Usamos la invariante al ciclo siguiente: Inicializacin: depus de la lnea 1, el conjunto A satisface la invariante trivialmente. Mantenimiento: el cclo de las lneas 2 a 4 mantiene la invariante agregando solamente aristas seguras. Terminacin: Todas las aristas que se agregan a A estn en el MST y por lo tanto, al regresar A despus de la lnea 5, este es un MST.

MST: algoritmo genrico


Problema: identicar aristas seguras (que respeten la invariante) Un corte (cut) (S, V-S) de una grca no dirigida es una particin de V.
8 b 4 c 2 a 8 h 1 g 2 f 11 7 i 6 10 7 d 9 4 14 e

S V S

S V S

Decimos que la arista (u,v) E cruza el corte (S, V-S) si uno de sus extremos est en S y el otro en V-S. Qu arista cruza el corte aqu?

MST: algoritmo genrico


Decimos que un corte respeta un conjunto A de aristas si ninguna arista de A cruza el corte. Una arista que cruza un corte se llama arista clara (light edge) si su costo es el mnimo entre cualquier otra arista que cruza el corte. Puede haber ms de una arista clara en el caso que el peso sea el mismo entre estas aristas. La regla para reconocer aristas seguras es entonces:

Sea G=(V,E) una grca conectada, no dirigida con una funcin real w de costos denida en E. Sea A un subconjunto de E incluido en un MST para G, sea (S,V-S) cualquier corte de G que respete A, y sea (u,v) una arista clara que cruce (S,V-S). Entonces, la arista (u,v) es segura para A.

MST: aristas seguras

Sea T un MST que incluye A y supongamos que T no contiene a la arista clara (u,v). Queremos construir otro MST, T que incluya A {(u,v)} mostrando que (u,v) es una arista segura. La arista (u,v) forma un cclo con las aristas del camino p desde u hasta v en T. Ya que u y v estn en lados opuestos del corte (S,V-S), hay al menos una arista en T en el camino p que tambin cruza el corte. (x,y) no est en A porque sabemos que el corte respeta A.

x (x,y) p T u (u,v) v V-S A y

MST: aristas seguras

x (x,y) p T u (u,v) v V-S A y

Ya que (x,y) est en el nico camino p desde u hasta v en T, remover (x,y) desconecta T en dos componentes. Agregar (u,v) le reconecta para formar un nuevo MST: T = T-{(x,y)} {(u,v)}.

MST: aristas seguras


Probamos que T es un MST: Ya que (u,v) es una arista clara que cruza (S, V-S) y (x,y) tambin cruza el corte, w(u,v) w(x,y). Entonces: w(T) = w(T) - w(x,y) + w(u,v) w(T) Como T es un MST, w(T) w(T), entonces T debe ser tambin un MST. Ya que T es un MST, (u,v) es una arista segura para A.

MST: algoritmo genrico


Mientras el algoritmo avanza, el conjunto A es siempre acclico, de otra forma el MST que incluye A contendra un cclo, lo que es contradictorio. En cualquier momento de la ejecucin del algoritmo, la grca GA = (V,A) es un bosque, en donde cada componente de GA es un rbol. Algunos de estos rboles pueden contener un solo vrtice (al inicio del algoritmo por ejemplo). Cualquier arista segura conecta dos componentes de GA. El cclo de las lneas 2 a 4 de GENERIC-MST se ejecuta, cuntas veces? V-1 veces.

Cundo termina el algoritmo? cuando el bosque contiene slo un rbol.

MST: Algoritmo de Borvka


Ideado por Otakar Borvka (1899-1995) En su artculo On a certain minimal problem describi un algoritmo para encontrar el MST de una red elctrica (1926). Qu buscara Borvka en esta aplicacin? encontrar la construccin ms econmica de la red elctrica.

Problema concreto de ingeniera que se convirti en un modelo de problema para la optimizacin combinatoria. Es el algoritmo ms antiguo y posiblemente el ms simple para encontrar MST. Redescubierto por Choquet, Florek, Lukaziewicz, Perkal, Steinhaus, Zubrzycki y Sollin.

CS 373

Lecture 13: Minimum de Bor MST: Algoritmo Spanning Trees vka


Lecture 13: Minimum Spanning Trees

Fall 2002

Bor Add all the safe edges recurse. Boruvka:uvka: Add all thesafe edges andand recurse.
8 10 2 18 12 14
18

5
2

5 10 3 30 14 16 18 12 14 26

12

30
4

16
26

18

12

eginning of each phase of the Boruvka algorithm, each component elects a FindSafeEdges(V, E): Una vez elegidos los lderes encontramos las aristas seguras para cada componente, e. The por fuerza bruta y estas son agregadas a A. isfor each leader v search of F ; th simplest way to hold these elections a depth-rst Boruvka(V, that safe(v) leaders are elected, any component is E): components leader. Once the

El algoritmo elige arbitrariamente un vrtice lder. At the beginning of each phase of the Boruvka algorithm, each component elects an arbitrary Boruvkas algorithm run encontrar a este graph. Thick edges are in en. Lanode. The simplest way to holdthe example ldes depth-rst search of F ; F rst node manera ms simple para on these elections is a es con una bsqueda the leader Arrows point along isA. Elcomponents leader. OnceDashed edges are useless.nd the each components que edge. the cualquier componente profundidad (DFS) en that primer nodo safe visitamos enleaders are elected, we ser we visit in any component el lder del component, safe edges for each componente.essentially by brute force. Finally, we add these safe edges to F .

Boruvkas algorithm run on the example graph. Thick edges are in F . Arrows point along each components safe edge. Dashed edges are useless. 26 26

14

18

12 14

30

16

18

12 14

MST: Algoritmo de Borvka


4 26 26

Boruvkas algorithm run on the example graph. Thick edges are in F . Arrows point along each components safe edge. Dashed edges are useless.

At the beginning of each phase of the Boruvka algorithm, each component elects an arbitr Bor uvka(V, E) ader node. The simplest way to hold these elections is a depth-rst search of F ; the rst n 1 A = (V, ) visit in any component is that components leader. Once the leaders are elected, we nd 2 while component, than one by brute force. Finally, we add these safe edges to F fe edges for each A has more essentially component

3 choose leaders using DFS 4 FindSafeEdges(V, E) 5 for each leader v 6 add safe(v) a A Boruvka(V, E):

F = (V, ) while F has more than one component choose leaders using DFS FindSafeEdges(V, E) for each leader v add safe(v) to F

FindSafeEdges(V, E): for each leader v safe(v) for each edge (u, v) E u leader(u) v leader(v) if u = v if w(u, v) < w(safe(u)) safe(u) (u, v) if w(u, v) < w(safe(v)) safe(v) (u, v)

Each call to FindSafeEdges takes O(E) time, since it examines every edge. Since the grap

MST: Algoritmo de Borvka


Cada llamada a FindSafeEdges toma ... O(E) porque examina cada arista. E+1 vrtices. Como la grca es conectada, cuntos vrtices tiene a lo ms? Cada iteracin del ciclo while en Boruvka toma ... (suponiendo una representacin por listas de adyacencia) O(E). Cada iteracin reduce el nmero de componentes de A al menos en un factor de dos. Como al inicio hay V componentes, el cclo while itera...cuntas veces? O(log V) veces.

El tiempo total de ejecucin es entonces ... O(E log V).

Das könnte Ihnen auch gefallen