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.traversal.BreadthFirstSearch; 011 import graphlab.library.exceptions.InvalidGraphException; 012 import graphlab.library.exceptions.InvalidVertexException; 013 014 import java.util.Iterator; 015 016 /** 017 * @author Omid Aladini 018 */ 019 public class ConnectivityChecker { 020 021 /** 022 * Checks whether the current graph is a connected graph. 023 * 024 * @return True if graph is connected and false otherwise. 025 * @throws InvalidGraphException 026 */ 027 public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 028 boolean isGraphConnected(BaseGraph<VertexType, EdgeType> graph) 029 throws InvalidGraphException { 030 try { 031 Iterator<VertexType> it = graph.iterator(); 032 if (!it.hasNext()) 033 return true; 034 035 new BreadthFirstSearch<VertexType, EdgeType>(graph).doSearch(it.next(), null); 036 037 } catch (InvalidVertexException e) { 038 throw new InvalidGraphException(); 039 } 040 041 for (VertexType v : graph) 042 if (!v.getMark()) 043 return false; 044 045 return true; 046 } 047 048 049 }