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.goperators;
006    
007    import graphlab.library.BaseEdge;
008    import graphlab.library.BaseGraph;
009    import graphlab.library.BaseVertex;
010    
011    import java.util.HashMap;
012    
013    /**
014     * @author Mohammad Ai Rostami
015     * @email ma.rostami@yahoo.com
016     */
017    
018    public class GraphComplement {
019        public static <VertexType extends BaseVertex,
020                EdgeType extends BaseEdge<VertexType>>
021        BaseGraph<VertexType, EdgeType>
022        complement(BaseGraph<VertexType, EdgeType> g1) {
023            {
024                BaseGraph<VertexType, EdgeType> g = g1.createEmptyGraph();
025                HashMap<VertexType, VertexType> hm = new HashMap<VertexType, VertexType>();
026    
027                for (VertexType v : g1) {
028                    VertexType t = (VertexType) v.getCopy();
029                    hm.put(v, t);
030                    g.insertVertex(t);
031                }
032    
033                for (VertexType v : g1)
034                    for (VertexType u : g1) {
035                        if (!g1.isEdge(v, u)) {
036                            EdgeType e = (EdgeType) g1.edgeIterator().next()
037                                    .getCopy(hm.get(v), hm.get(u));
038                            g.insertEdge(e);
039                        }
040                    }
041                return g;
042            }
043        }
044    }