java - 使用 for 循环将节点项添加到 Java Graph

标签 java graph edges

我正在尝试将新创建的对象添加到网格和图表中。具体来说,如何使用 for 循环有效地将节点添加到图中。它们的网格已被设置为双数组,以便更新初始 View 。 (对象网格是模型并通过推送更新 View )。我还在 for 循环中使用 i 和 j 中的项目设置了一个 HashMap,以定义对象的键。但是,为了建立一个图以便稍后计算从节点到节点的最短路径,我需要将这些节点添加到图中。我想使用Djikstra算法来计算最短路径。我可以创建一个条件语句来定义网格中的角节点和边缘节点,并定义哪些项目将具有双向边缘,但这似乎是一个“长切”。有什么方法可以将节点添加到具有预先确定的矩阵大小的图形中,例如 20 X 20,类似于 double 组?

下面是关于如何创建前两项(创建双数组和 HashMap)的构造函数代码:

// **Constructor
// Construct a new Grid object. Sets up a HashMap of square object in order efficiently to get 
// and add Square objects later.
public ObjectGrid(Integer width, Integer height) 
{
    // View
    gui = new GUI();        // Instantiate GUI
    boardView = new BoardView(width,height);        // Instantiate BoardView

    // Initialize Gui, set time and add simulation event listener to model (ObjectGrid)
    gui.initGUI(BoardView);
    gui.addSimulationEventListener(this);

    // Initialize turnInSteps variable count to 0
    turnInSteps = 0;

    // Initialize numberOfDays variable count to 0
    numberOfDays = 1;

    // Instantiate HashMap
    objectHashMap = new HashMap();

    // Declare new object grid using double array of type objects.
    // Size determined by parameter Integer values in constructor
    myObjectGrid = new ObjectType[width][height];

    // Instantiate Graph object
    Graph graph = new ListGraph();

    // For loop sets up the initial grid with ObejctType using a double array. After
    // the completion of this loop, the grid will have XXX objects in the grid
    // each with a reference to an object. Objects are also added 
    // to HashMap using coordinates as keys and square objects as values.
    for(int i = 0; i < width; i++) 
    {  
        // Iterate through rows
        for(int j = 0; j < height; j++) 
        {  
            // Iterate through columns
            myObjectGrid[i][j] = new ObjectType();  // Instantiate a new Square at each row/column using default constructor
            gridView.addObjectView(myObjectGrid[i][j].getObjectView(), i, j);  // Add object view to each row/column placement
            String hashMapKey = (i + ", " + j); // Use values from i and j as Key for objects HashMap
            myObjectGrid[i][j].setID(hashMapKey);  // Add ID's for each ObjectView to display in object using values from i and j
            objectHashMap.add(hashMapKey, myObjectGrid[i][j]);  // Add object to HashMap using string value of coordinates as key
            listGraph.add(myObjectGrid[i][j]);

            // Pseudo code
            if (i != (width-height) && (j != height) etc) 
            {
                listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i][j+1]), 1);
                listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j]), 1);
                listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j+1]), 1);
            }
        }
    }
}

最佳答案

With the unit tests that I did the vertices in the graph return null.

原因如下:

listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i][j+1]), 1);
listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j]), 1);
listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j+1]), 1);

您正在初始化 mySquareGrid 并使用 future 的节点创建边。这些具有j+1i+1的节点此时为空。

尝试最简单的解决方案 - 网格初始化后,通过网格进行相同的循环并创建图形的边缘。

另外,您可以尝试将其更改为以下内容:

listGraph.addBidirectionalEdge(mySquareGrid[i][j-1], (mySquareGrid[i][j]), 1);
listGraph.addBidirectionalEdge(mySquareGrid[i-1][j], (mySquareGrid[i][j]), 1);
listGraph.addBidirectionalEdge(mySquareGrid[i-1][j-1], (mySquareGrid[i][j]), 1);

关于java - 使用 for 循环将节点项添加到 Java Graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016652/

相关文章:

python - 如何在 python 中使用 igraph 从边缘获取顶点?

java - 用 '.' 拆分 Java 字符串

java - 动画监听器不响应 Animatorset

java - HashMap 的替代方案

java - 字符串格式参数在 android 上的 string.xml 中不起作用

algorithm - 我们可以在修改后的图上运行 Dijkstra 算法并减去添加的权重以获得原始距离吗?

python - 有向图中的最大简单循环

python - 编辑 voronoi 类以返回 python 中的多边形点

three.js - 如何将边缘渲染为圆柱体?

python - 来自 networkx 的 MultiDiGraph 边使用 connectionStyle 绘制