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.product;
006    
007    import graphlab.library.BaseEdge;
008    import graphlab.library.BaseGraph;
009    import graphlab.library.BaseVertex;
010    import graphlab.library.util.Pair;
011    
012    import java.io.FileWriter;
013    import java.io.IOException;
014    
015    
016    /**
017     * @author Mohammad Ali Rostami
018     * @email ma.rostami@yahoo.com
019     */
020    public abstract class GProduct
021            <VertexType extends BaseVertex,
022                    EdgeType extends BaseEdge<VertexType>,
023                    GraphType extends BaseGraph<VertexType, EdgeType>> {
024        protected GraphType g1;
025        protected GraphType g2;
026    
027        public final GraphType multiply(GraphType g1
028                , GraphType g2) {
029            GraphType g = (GraphType) g1.createEmptyGraph();
030            for (VertexType v1 : g1) {
031                for (VertexType v2 : g2) {
032                    VertexType vnew = (VertexType) v1.getCopy();
033                    vnew.getProp().obj = new Pair<VertexType, VertexType>(v1, v2);
034                    g.insertVertex(vnew);
035                }
036            }
037    
038            this.g1 = g1;
039            this.g2 = g2;
040    
041            for (VertexType v1 : g) {
042                for (VertexType v2 : g) {
043                    if (compare(((Pair<VertexType, VertexType>) v1.getProp().obj).first
044                            , ((Pair<VertexType, VertexType>) v2.getProp().obj).first
045                            , ((Pair<VertexType, VertexType>) v1.getProp().obj).second
046                            , ((Pair<VertexType, VertexType>) v2.getProp().obj).second)) {
047                        g.insertEdge((EdgeType) g1.edgeIterator().next().getCopy(v1, v2));
048                    }
049    
050                }
051            }
052    
053            try {
054                FileWriter fw = new FileWriter("ggg.graph");
055                String s = "";
056                double[][] m = g.getAdjacencyMatrix().getArray();
057                s += m.length + "\n";
058                for (int i = 0; i < m.length; i++)
059                    for (int j = i; j < m.length; j++)
060                        if (m[i][j] == 1) {
061                            s += i + " " + j + "\n";
062                        }
063    //            System.out.println(s);
064                fw.write(s);
065                fw.close();
066            } catch (IOException e) {
067                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
068            }
069    
070            return g;
071        }
072    
073        public abstract boolean compare(VertexType v1OfFirstG
074                , VertexType v2OfFirstG
075                , VertexType v1OfSecondG
076                , VertexType v2OfSecondG);
077    }