简体   繁体   中英

BFS graph traversal - Append new node to adjacency matrix

I'm trying to create a program that let's you append and remove nodes from a graph, then run BFS and DFS traversals.

在此输入图像描述

So I originally add and connect nodes at runtime... then I want to allow users to hit a add child to parent button which appends a child appropriately.

AddButton.addActionListener(new ActionListener() {   
        public void actionPerformed(ActionEvent e)
        {   
            Nodes nX = new Nodes("X", nodeX, nodeY, nodeWidth, nodeHeight);
            appendNode(rootNode, nX);
        }
   });  

}

When I try to append a new node, it seems to overwrite the current adjacency matrix and replace it with the single new node X as a child of A .

在此输入图像描述

I think I know why it's doing that... because my appendNode() function tries to update the adjacency matrix by overwriting the old one and creating a new one with the new nodeList size:

public void appendNode(Nodes parent, Nodes child) {
    //add new node X to nodeList
    addNode(child);

    //loop through all nodes again to be connected, plus new one... then create new adjMatrix
    System.out.println(nodeList.size());
    size = nodeList.size();

    adjMatrix = null;
    adjMatrix = new int[size][size];

    int fromNode = nodeList.indexOf(parent);
    int toNode = nodeList.indexOf(child);
    adjMatrix[fromNode][toNode] = 1;
    adjMatrix[toNode][fromNode] = 0;

}

public void addNode(Nodes n) {
    nodeList.add(n);
}

But I don't know any other way to achieve what I want without overwriting it.

Any thoughts?

Thanks!


FYI, here's the connect node method:

public void connectNode(Nodes from, Nodes to)
{
    //if matrix is empty..
    if(adjMatrix == null)
    {
        //set matrix [row][col] size to size of nodesList list
        size = nodeList.size();
        //set dimensions of adj matrix... (6x6 nodesList)

        adjMatrix = new int[size][size];
    }

    int fromNode = nodeList.indexOf(from);
    int toNode = nodeList.indexOf(to);

    //connect node A to B and B to A, set that i,j position = 1
    adjMatrix[fromNode][toNode] = 1;
    adjMatrix[toNode][fromNode] = 0;

}

EDIT:

public void appendNode(Nodes parent, Nodes child) {
    //add new node X to nodeList
    addNode(child);

    //loop through all nodes again to be connected, plus new one... then create new adjMatrix
    int newSize = nodeList.size();

    //make a new adj matrix of the new size...
    int[][] adjMatrixCopy = new int[newSize][newSize];

    int fromNode = nodeList.indexOf(parent);
    int toNode = nodeList.indexOf(child);
    adjMatrixCopy[fromNode][toNode] = 1;
    adjMatrixCopy[toNode][fromNode] = 0;


    //copy adjMatrix data to new matrix...
    for (int i = 0; i < adjMatrix.length; i++) {    
        for (int j = 0; j < adjMatrix[i].length; j++) {
            adjMatrixCopy[i][j] = adjMatrix[i][j];
        }
    }

    //still need to add newly added node 

    //adjMatrix = null;
}

By blanking the adjacency matrix and replacing it with a new one with the updated size, all cells except (from, to) and (to, from) will be zero. You need to make a new adjacency matrix with the new size, copy the data from the old one, and only then overwrite the old one.

Why not an adjacency list? or an adjacency matrix using collections which can be incremented on the fly?

I think using an array will lead to this perennial problem. You should use a collection if you want to use an adjacency matrix. that said, if you are just doing a BFS, an adjacency list would work faster so I would suggest that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM