com.salesforce.jgrapht.generate.GridGraphGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AptSpringProcessor Show documentation
Show all versions of AptSpringProcessor Show documentation
This project contains the apt processor that implements all the checks enumerated in @Verify. It is a self contained, and
shaded jar.
The newest version!
/*
* (C) Copyright 2011-2018, by Assaf Mizrachi and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* See the CONTRIBUTORS.md file distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the
* GNU Lesser General Public License v2.1 or later
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
*
* SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
*/
package com.salesforce.jgrapht.generate;
import com.salesforce.jgrapht.*;
import java.util.*;
/**
* Generates a bidirectional grid graph of
* any size. A grid graph is a two dimensional graph whose vertices correspond to the points in the
* plane with integer coordinates, x-coordinates being in the range 0,..., n, y-coordinates being in
* the range 1,...m, and two vertices are connected by an edge whenever the corresponding points are
* at distance 1. Vertices are created from left to right and from top to bottom.
*
* @param the graph vertex type
* @param the graph edge type
*
* @author Assaf Mizrachi
*/
public class GridGraphGenerator
implements
GraphGenerator
{
/**
* Role for the vertices at the corners.
*/
public static final String CORNER_VERTEX = "Corner Vertex";
private final int rows;
private final int cols;
/**
* Creates a new GridGraphGenerator object with rows x cols dimension.
*
* @param rows the number of rows
* @param cols the number of columns
*/
public GridGraphGenerator(int rows, int cols)
{
if (rows < 2) {
throw new IllegalArgumentException(
"illegal number of rows (" + rows + "). there must be at least two.");
}
if (cols < 2) {
throw new IllegalArgumentException(
"illegal number of columns (" + cols + "). there must be at least two.");
}
this.rows = rows;
this.cols = cols;
}
/**
* {@inheritDoc}
*/
@Override
public void generateGraph(Graph target, Map resultMap)
{
Map map = new TreeMap<>();
// Adding all vertices to the set
int cornerCtr = 0;
for (int i = 0; i < rows * cols; i++) {
V vertex = target.addVertex();
map.put(i + 1, vertex);
boolean isCorner = (i == 0) || (i == (cols - 1)) || (i == (cols * (rows - 1)))
|| (i == ((rows * cols) - 1));
if (isCorner && (resultMap != null)) {
resultMap.put(CORNER_VERTEX + ' ' + ++cornerCtr, vertex);
}
}
// Iterating twice over the key set, for undirected graph edges are
// added from upper vertices to lower, and from left to right. The
// second addEdge call will return nothing; it will not add a the edge
// at the opposite direction. For directed graph, edges in opposite
// direction are also added.
for (int i : map.keySet()) {
for (int j : map.keySet()) {
if ((((i % cols) > 0) && ((i + 1) == j)) || ((i + cols) == j)) {
target.addEdge(map.get(i), map.get(j));
target.addEdge(map.get(j), map.get(i));
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy