0

So, I have successfully visualized a graph using JGraphX purely. Now, I would like to create an adjacency matrix. One method i could think of is get all the cells a particular cell targets (or points to) and also all of it's parent cells. However, I could not seem to find an appropriate JGraphX method to use from the documentation.

Are there actually any JGraphX method for that? Otherwise, are there any alternative ways or approaches you could suggest?

Thank you.

I already figured a way, somehow. But i'll be leaving this question in case other's need it.

My approach: I create two ArrayList , matrixListFrom and matrixListTo. Everytime I connect edges, I would add the label of vertex one to matrixFrom and the label of vertex two to matrixTo. And then I associated it with this method:

public int createAdjacencyMatrix(int adMatrix,  String strFromVertex,  String strToVertex)
    {
        for(int k = 0;k<matrixListFrom.size();++k)
        {
            if((matrixListFrom.get(k)).equals(strFromVertex) && (matrixListTo.get(k)).equals(strToVertex))
            {
                adMatrix = 1;
                break;
            }
            else if((matrixListFrom.get(k)).equals(strToVertex) && (matrixListTo.get(k)).equals(strFromVertex))
            {
                adMatrix = -1;
                for(int j = 0;j<matrixListFrom.size();++j)
                {
                    if((matrixListFrom.get(j)).equals(strFromVertex) && (matrixListTo.get(j)).equals(strToVertex))
                    {
                        adMatrix = 1;
                        break;
                    } 
                }
                break;
            }
            else if(!(matrixListFrom.get(k)).equals(strFromVertex) && !(matrixListTo.get(k)).equals(strToVertex))
            {
                adMatrix = 0;
            }

            else if(strFromVertex.equals(strToVertex))
            {
                adMatrix = 0;
                break;
            }

            else
            {
                adMatrix = 0;
            }
        }

        return adMatrix;
    }

Suppose we have vertices A, B, C, D, E stored in an ArrayList, named vertices

matrixListFrom consists of {A, C, D E} matrixListTo consists of {D, E, C, B}

Which means A -->D, C -->E, D -->C, E-->B

Thus, using the method, those two lists' contents are checked one by one. strFromVertex is equal to the vertex in column 0 and strToVertex is the corresponding vertex to be compared from the succeeding columns using a for loop. And then, using the both of the matrix lists, it is checked if the strFromVertex and strToVertex is equal to the matrixListFrom and matrixListTo respectively. If they are perfectly equal it returns a 1, if they are inversely equal (the second if) it returns a -1 (which means the vertex in column 0 is the terminal instead of source) and 0 if there's no connection at all.

However I think the method is not really efficient especially when deleting vertices. But somehow I managed to make it work.

1 Answers1

1

As far as I see, JGraphX has no built in option for that, but you can use this simple Method:

public static Set<mxICell> getTargetCells(mxICell input)
{
    if(input == null)
        return Collections.emptySet();
    int count = input.getEdgeCount();
    Set<mxICell> result = new HashSet<mxICell>(count, 1);
    for(int i = 0; i < count; i++)
    {
        mxICell tmp = input.getEdgeAt(i);
        if(tmp instanceof mxCell && ((mxCell)tmp).getSource() == input)
        {
            result.add(tmp);
        }
    }
    return result;
}
F. Lumnitz
  • 688
  • 4
  • 11