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.util; 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.goperators.EdgeInduced; 012 import graphlab.library.algorithms.goperators.GraphComplement; 013 import graphlab.library.algorithms.goperators.GraphUnion; 014 import graphlab.library.algorithms.goperators.VertexInduced; 015 016 import java.util.Collection; 017 import java.util.Iterator; 018 019 /** 020 * @see graphlab.plugins.main.core.AlgorithmUtils 021 */ 022 public class LibraryUtils { 023 024 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 025 boolean falsifyEdgeMarks(BaseGraph<VertexType, EdgeType> g) { 026 boolean flag = false; 027 EdgeType e; 028 Iterator<EdgeType> iet = g.edgeIterator(); 029 while (iet.hasNext()) { 030 e = iet.next(); 031 flag = e.getMark() == true; 032 e.setMark(false); 033 } 034 return flag; 035 } 036 037 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 038 boolean falsifyVertexMarks(BaseGraph<VertexType, EdgeType> g) { 039 boolean flag = false; 040 for (VertexType v : g) { 041 flag = v.getMark() == true; 042 v.setMark(false); 043 } 044 return flag; 045 } 046 047 /** 048 * @see graphlab.library.algorithms.goperators.GraphComplement#complement(graphlab.library.BaseGraph) 049 */ 050 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 051 BaseGraph<VertexType, EdgeType> 052 complement(BaseGraph<VertexType, EdgeType> g1) { 053 return GraphComplement.complement(g1); 054 } 055 056 /** 057 * @see graphlab.library.algorithms.goperators.EdgeInduced#edgeInduced(graphlab.library.BaseGraph,java.util.Collection) 058 */ 059 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 060 BaseGraph<VertexType, EdgeType> edgeInduced(BaseGraph<VertexType, EdgeType> g, Collection<EdgeType> S) { 061 return EdgeInduced.edgeInduced(g, S); 062 } 063 064 /** 065 * @see graphlab.library.algorithms.goperators.VertexInduced#induced(graphlab.library.BaseGraph,java.util.Collection) 066 */ 067 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 068 BaseGraph<VertexType, EdgeType> induced(BaseGraph<VertexType, EdgeType> g, Collection<VertexType> S) { 069 return VertexInduced.induced(g, S); 070 } 071 072 /** 073 * @see graphlab.library.algorithms.goperators.GraphUnion#join(graphlab.library.BaseGraph,graphlab.library.BaseGraph) 074 */ 075 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 076 BaseGraph<VertexType, EdgeType> join(BaseGraph<VertexType, EdgeType> g1, BaseGraph<VertexType, EdgeType> g2) { 077 return GraphUnion.union(g1, g2); 078 } 079 080 /** 081 * @see graphlab.library.algorithms.util.EventUtils#algorithmStep(graphlab.library.algorithms.Algorithm,String) 082 */ 083 public static void algorithmStep(Algorithm a, String message) { 084 EventUtils.algorithmStep(a, message); 085 } 086 087 /** 088 * returns all vertex marks in a array 089 */ 090 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 091 boolean[] getVertexMarks(BaseGraph<VertexType, EdgeType> g) { 092 boolean ret[] = new boolean[g.getVerticesCount()]; 093 int i = 0; 094 for (BaseVertex v : g) { 095 ret[i++] = v.getMark(); 096 } 097 return ret; 098 } 099 100 /** 101 * sets all the vertex marks 102 */ 103 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 104 void setVertexMarks(BaseGraph<VertexType, EdgeType> g, boolean verexMarks[]) { 105 int i = 0; 106 for (BaseVertex v : g) { 107 v.setMark(verexMarks[i++]); 108 } 109 } 110 111 }