001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU Lesser General Public License (LGPL): http://www.gnu.org/licenses/ 004 005 package graphlab.library.algorithms.vertexcover; 006 007 import graphlab.library.BaseEdge; 008 import graphlab.library.BaseGraph; 009 import graphlab.library.BaseVertex; 010 import graphlab.library.algorithms.Algorithm; 011 import graphlab.library.algorithms.AutomatedAlgorithm; 012 import graphlab.library.algorithms.util.EventUtils; 013 import graphlab.library.event.VertexEvent; 014 import graphlab.library.event.typedef.BaseGraphRequest; 015 import graphlab.library.exceptions.InvalidGraphException; 016 import graphlab.library.exceptions.InvalidVertexException; 017 import graphlab.library.genericcloners.BaseEdgeVertexCopier; 018 import graphlab.library.genericcloners.EdgeVertexConverter; 019 020 import java.util.Iterator; 021 import java.util.Vector; 022 023 /** 024 * @author Soroush Sabet 025 */ 026 public class AppVertexCover<VertexType extends BaseVertex, 027 EdgeType extends BaseEdge<VertexType>> 028 extends Algorithm implements AutomatedAlgorithm { 029 /** 030 * 031 */ 032 final BaseGraph<VertexType, EdgeType> graph; 033 034 private EdgeVertexConverter<VertexType, VertexType, EdgeType, EdgeType> gc; 035 036 /** 037 * @param graph 038 * @param gc 039 */ 040 public AppVertexCover(BaseGraph<VertexType, EdgeType> graph, 041 EdgeVertexConverter<VertexType, VertexType, EdgeType, EdgeType> gc) { 042 // if (gc == null || graph == null) 043 // throw new NullPointerException(); 044 045 this.graph = graph; 046 this.gc = gc; 047 } 048 049 050 /** 051 * @return 052 * @throws InvalidGraphException 053 * @throws InvalidVertexException 054 */ 055 056 public Vector<VertexType> findAppCover() 057 throws InvalidGraphException, InvalidVertexException { 058 059 // BaseGraph<VertexType,EdgeType> gCopy = graph.copy(gc); 060 Vector<VertexType> C = new Vector<VertexType>(); 061 Vector<VertexType> D = new Vector<VertexType>(); 062 Vector<VertexType> marked = new Vector<VertexType>(); 063 Iterator<EdgeType> i; 064 //cleat marks 065 for (Iterator<EdgeType> ie = graph.edgeIterator(); ie.hasNext();) 066 ie.next().setMark(false); 067 EdgeType e; 068 069 Iterator<EdgeType> iet = graph.edgeIterator(); 070 while (iet.hasNext()) { 071 e = iet.next(); 072 if (!(C.contains(e.source) || C.contains(e.target))) { 073 e.setMark(true); 074 // dispatchEvent(new EdgeEvent<VertexType, EdgeType>(graph, e, EdgeEvent.EventType.MARK)); 075 EventUtils.algorithmStep(this, ""); 076 C.add(e.source); 077 C.add(e.target); 078 // i = graph.edgeIterator(e.source); 079 // while(i.hasNext()){ 080 // gCopy.removeEdge(i.next()); 081 // 082 // } 083 084 // i= gCopy.edgeIterator(e.target); 085 // while(i.hasNext()){ 086 // gCopy.removeEdge(i.next()); 087 // } 088 // iet = gCopy.edgeIterator(); 089 } 090 } 091 092 for (VertexType v : C) { 093 // int j = v.getId(); 094 // for (VertexType u : graph){ 095 // if (u.getId() == j){ 096 // dispatchEvent(new VertexEvent(graph, v, VertexEvent.EventType.MARK)); 097 v.setMark(true); 098 EventUtils.algorithmStep(this, ""); 099 D.add(v); 100 // } 101 // } 102 } 103 104 return D; 105 106 } 107 108 public void doAlgorithm() { 109 BaseGraphRequest gr = new BaseGraphRequest(); 110 dispatchEvent(gr); 111 BaseGraph<BaseVertex, BaseEdge<BaseVertex>> graph = gr.getGraph(); 112 113 // BaseVertexRequest vr = new BaseVertexRequest(graph); 114 // dispatchEvent(vr); 115 116 AppVertexCover vc = new AppVertexCover(graph, new BaseEdgeVertexCopier()); 117 vc.acceptEventDispatcher(getDispatcher()); 118 119 Vector<VertexType> appCover = vc.findAppCover(); 120 for (VertexType v : appCover) { 121 dispatchEvent(new VertexEvent<BaseVertex, BaseEdge<BaseVertex>>(graph, v, VertexEvent.EventType.MARK)); 122 } 123 // BaseGraph<BaseVertex,BaseEdge<BaseVertex>> output = 124 // prim.findMinimumSpanningTree(vr.getVertex()); 125 126 // dispatchEvent(new BaseGraphEvent(output)); 127 } 128 }