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.homomorphism;
006    
007    import graphlab.library.BaseEdge;
008    import graphlab.library.BaseGraph;
009    import graphlab.library.BaseVertex;
010    
011    import java.util.HashMap;
012    import java.util.Iterator;
013    
014    /**
015     * @author Soroush Sabet
016     */
017    public class Homomorphism<VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> {
018    
019        private BaseGraph<VertexType, EdgeType> domain;
020        private BaseGraph<VertexType, EdgeType> range;
021        HashMap<VertexType, VertexType> homomorphism;
022    
023        Homomorphism(BaseGraph<VertexType, EdgeType> domain, BaseGraph<VertexType, EdgeType> range,
024                     HashMap<VertexType, VertexType> map) {
025            this.domain = domain;
026            this.range = range;
027            homomorphism.putAll(map);
028        }
029    
030        public BaseGraph<VertexType, EdgeType> getDomain() {
031            return domain;
032        }
033    
034        public BaseGraph<VertexType, EdgeType> getRange() {
035            return range;
036        }
037    
038        public boolean isValid() {
039            boolean res = true;
040            for (VertexType v : domain) {
041                if (!(homomorphism.containsKey(v)) || (homomorphism.get(v) == null)) {
042                    res = false;
043                }
044    
045            }
046            if (res) {
047                Iterator<EdgeType> i = domain.edgeIterator();
048                EdgeType e;
049                while (i.hasNext()) {
050                    e = i.next();
051                    res = range.isEdge(homomorphism.get(e.source), homomorphism.get(e.target));
052                }
053            }
054    
055            return res;
056        }
057    
058    
059    }