001package org.biopax.paxtools.pattern.util;
002
003import java.util.List;
004
005/**
006 * Adjacency matrix representation of a graph.
007 * @author Ozgun Babur
008 */
009public class AdjacencyMatrix
010{
011        /**
012         * Unique node names.
013         */
014        public String[] names;
015
016        /**
017         * Edges matrix. First index is source, second index is target. The matrix should be symmetrical
018         * if the graph is undirected.
019         */
020        public boolean[][] matrix;
021
022        /**
023         * Constructor with contents.
024         * @param matrix edges
025         * @param namesList nodes
026         */
027        public AdjacencyMatrix(boolean[][] matrix, List<String> namesList)
028        {
029                if (matrix.length != namesList.size())
030                {
031                        throw new IllegalArgumentException("Matrix row size not equal to nodes size");
032                }
033                for (boolean[] m : matrix)
034                {
035                        if (m.length != namesList.size())
036                        {
037                                throw new IllegalArgumentException("Matrix column size not equal to nodes size");
038                        }
039                }
040
041                this.matrix = matrix;
042                this.names = namesList.toArray(new String[namesList.size()]);
043        }
044
045        @Override
046        public String toString()
047        {
048                StringBuilder b = new StringBuilder();
049
050                for (String name : names)
051                {
052                        b.append("\t").append(name);
053                }
054                int i = 0;
055                for (boolean[] m : matrix)
056                {
057                        b.append("\n").append(names[i++]);
058
059                        for (boolean v : m)
060                        {
061                                b.append(v ? "\tX" : "\t");
062                        }
063                }
064
065                return b.toString();
066        }
067}