KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > alg > VertexCovers


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -----------------
26  * VertexCovers.java
27  * -----------------
28  * (C) Copyright 2003-2006, by Linda Buisman and Contributors.
29  *
30  * Original Author: Linda Buisman
31  * Contributor(s): Barak Naveh
32  * Christian Hammer
33  *
34  * $Id: VertexCovers.java 504 2006-07-03 02:37:26Z perfecthash $
35  *
36  * Changes
37  * -------
38  * 06-Nov-2003 : Initial revision (LB);
39  * 07-Jun-2005 : Made generic (CH);
40  *
41  */

42 package org.jgrapht.alg;
43
44 import java.util.*;
45
46 import org.jgrapht.*;
47 import org.jgrapht.alg.util.*;
48 import org.jgrapht.graph.*;
49
50
51 /**
52  * Algorithms to find a vertex cover for a graph. A vertex cover is a set of
53  * vertices that touches all the edges in the graph. The graph's vertex set is a
54  * trivial cover. However, a <i>minimal</i> vertex set (or at least an
55  * approximation for it) is usually desired. Finding a true minimal vertex cover
56  * is an NP-Complete problem. For more on the vertex cover problem, see <a
57  * HREF="http://mathworld.wolfram.com/VertexCover.html">
58  * http://mathworld.wolfram.com/VertexCover.html</a>
59  *
60  * @author Linda Buisman
61  * @since Nov 6, 2003
62  */

63 public abstract class VertexCovers
64 {
65
66     //~ Methods ---------------------------------------------------------------
67

68     /**
69      * Finds a 2-approximation for a minimal vertex cover of the specified
70      * graph. The algorithm promises a cover that is at most double the size of
71      * a minimal cover. The algorithm takes O(|E|) time.
72      *
73      * <p>For more details see Jenny Walter, CMPU-240: Lecture notes for
74      * Language Theory and Computation, Fall 2002, Vassar College, <a
75      * HREF="http://www.cs.vassar.edu/~walter/cs241index/lectures/PDF/approx.pdf">
76      * http://www.cs.vassar.edu/~walter/cs241index/lectures/PDF/approx.pdf</a>.
77      * </p>
78      *
79      * @param g the graph for which vertex cover approximation is to be found.
80      *
81      * @return a set of vertices which is a vertex cover for the specified
82      * graph.
83      */

84     public static <V, E> Set<V> find2ApproximationCover(Graph<V, E> g)
85     {
86         // C <-- {}
87
Set<V> cover = new HashSet<V>();
88
89         // G'=(V',E') <-- G(V,E)
90
Subgraph<V, E> sg = new Subgraph<V, E>(g, null, null);
91
92         // while E' is non-empty
93
while (sg.edgeSet().size() > 0) {
94             // let (u,v) be an arbitrary edge of E'
95
E e = sg.edgeSet().iterator().next();
96
97             // C <-- C U {u,v}
98
V u = g.getEdgeSource(e);
99             V v = g.getEdgeTarget(e);
100             cover.add(u);
101             cover.add(v);
102
103             // remove from E' every edge incident on either u or v
104
sg.removeVertex(u);
105             sg.removeVertex(v);
106         }
107
108         return cover; // return C
109
}
110
111     /**
112      * Finds a greedy approximation for a minimal vertex cover of a specified
113      * graph. At each iteration, the algorithm picks the vertex with the highest
114      * degree and adds it to the cover, until all edges are covered.
115      *
116      * <p>The algorithm works on undirected graphs, but can also work on
117      * directed graphs when their edge-directions are ignored. To ignore edge
118      * directions you can use {@link org.jgrapht.Graphs#undirectedGraph(Graph)}
119      * or {@link org.jgrapht.graph.AsUndirectedGraph}.</p>
120      *
121      * @param g the graph for which vertex cover approximation is to be found.
122      *
123      * @return a set of vertices which is a vertex cover for the specified
124      * graph.
125      */

126     public static <V, E> Set<V> findGreedyCover(UndirectedGraph<V, E> g)
127     {
128         // C <-- {}
129
Set<V> cover = new HashSet<V>();
130
131         // G' <-- G
132
UndirectedGraph<V, E> sg = new UndirectedSubgraph<V, E>(g, null, null);
133
134         // compare vertices in descending order of degree
135
VertexDegreeComparator<V, E> comp =
136             new VertexDegreeComparator<V, E>(sg);
137
138         // while G' != {}
139
while (sg.edgeSet().size() > 0) {
140             // v <-- vertex with maximum degree in G'
141
V v = Collections.max(sg.vertexSet(), comp);
142
143             // C <-- C U {v}
144
cover.add(v);
145
146             // remove from G' every edge incident on v, and v itself
147
sg.removeVertex(v);
148         }
149
150         return cover;
151     }
152 }
153
Popular Tags