001 /* 002 * www.ti.bfh.ch 003 * 004 * Copyright 2007, Berne University of Applied Sciences, 005 * School of Engineering and Information Technology 006 * and individual contributors as indicated by the @authors tag. 007 * 008 * This is free software; you can redistribute it and/or modify it under the terms of the 009 * GNU Lesser General Public License as published by the Free Software Foundation; 010 * either version 3 of the License, or (at your option) any later version. 011 * 012 * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 013 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 014 * See the GNU Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License along with this software; 017 * if not, see <http://www.gnu.org/licenses/>. 018 * 019 */ 020 package ch.bfh.algo.graph; 021 022 import java.util.Collection; 023 import java.util.HashMap; 024 import java.util.Map; 025 026 import ch.bfh.algo.Direction; 027 import ch.bfh.algo.Graph; 028 import ch.bfh.algo.Position; 029 import ch.bfh.algo.core.graph.GenericAdjacencyListGraph; 030 031 public class DefaultGraph<E,V> extends GenericAdjacencyListGraph<E,V,Edge<E,V>,Vertex<E,V>>{ 032 033 public DefaultGraph(GraphFactory<Edge<E,V>,Vertex<E,V>> factory, boolean directed){ 034 super(factory,directed); 035 } 036 037 public DefaultGraph(boolean directed){ 038 this(new DefaultGraphFactory<E,V>(),directed); 039 } 040 041 public DefaultGraph(GraphFactory<Edge<E,V>,Vertex<E,V>> factory){ 042 this(factory,true); 043 } 044 045 public DefaultGraph(){ 046 this(new DefaultGraphFactory<E,V>(),true); 047 } 048 049 public DefaultGraph(Collection<? extends V> c){ 050 this(); 051 this.addAll(c); 052 } 053 054 public DefaultGraph(Graph<E,V> g){ 055 this(); 056 this.addGraph(g); 057 } 058 059 public DefaultGraph(DefaultGraph<E,V> g){ 060 this(g.getFactory()); 061 this.addGraph(g); 062 } 063 064 public DefaultGraph(GraphFactory<Edge<E,V>,Vertex<E,V>> factory, Graph<? extends E,? extends V> g){ 065 this(factory); 066 this.addGraph(g); 067 } 068 069 private <VV extends V,EE extends E> Map<Position<VV>,Position<V>> addGraph(Graph<EE,VV> g){ 070 Map<Position<VV>,Position<V>> map=new HashMap<Position<VV>,Position<V>>(g.size()); 071 for(Position<VV> v:g.vertices()) map.put(v,this.insert(v.element())); 072 for(Position<EE> edge:g.edges(Direction.DIRECTED)) 073 this.setDirected(this.insertEdge(edge.element(),map.get(g.origin(edge)),map.get(g.destination(edge)))); 074 for(Position<EE> edge:g.edges(Direction.UNDIRECTED)) 075 this.setUndirected(this.insertEdge(edge.element(),map.get(g.origin(edge)),map.get(g.destination(edge)))); 076 return map; 077 } 078 079 } 080