com.googlecode.blaisemath.graph.GraphSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blaise-graphtheory Show documentation
Show all versions of blaise-graphtheory Show documentation
Link graph definitions, algorithms, and visualization.
The newest version!
/*
* GraphSupport.java
* Created Oct 29, 2011
*/
package com.googlecode.blaisemath.graph;
/*
* #%L
* BlaiseGraphTheory
* --
* Copyright (C) 2009 - 2016 Elisha Peterson
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.google.common.collect.Sets;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import com.googlecode.blaisemath.util.Edge;
/**
* Implements the methods of {@link Graph} that can be inferred from other methods.
* The nodes are maintained as a {@link LinkedHashSet}.
*
* @param graph node type
* @author Elisha Peterson
*/
public abstract class GraphSupport implements Graph {
/** Whether graph is directed */
protected final boolean directed;
/** The nodes of the graph */
protected final Set nodes;
/**
* Constructs with the set of nodes. The nodes are copied from the supplied
* collection into a new data structure.
* @param directed if graph is directed
* @param nodes graph's nodes
*/
public GraphSupport(boolean directed, Iterable nodes) {
this.directed = directed;
this.nodes = Collections.unmodifiableSet(Sets.newLinkedHashSet(nodes));
}
@Override
public String toString() {
return GraphUtils.printGraph(this);
}
@Override
public boolean isDirected() {
return directed;
}
//
// NODES
//
@Override
public Set nodes() {
return Collections.unmodifiableSet(nodes);
}
@Override
public int nodeCount() {
return nodes.size();
}
@Override
public boolean contains(V x) {
return nodes.contains(x);
}
//
// EDGES
//
@Override
public int edgeCount() {
return edges().size();
}
//
// ADJACENCY
//
@Override
public boolean adjacent(V x, V y) {
for (Edge e : edgesAdjacentTo(x)) {
if (y.equals(e.opposite(x))) {
return true;
}
}
return false;
}
@Override
public Set outNeighbors(V x) {
if (!directed) {
return neighbors(x);
} else {
Set result = new HashSet();
for (Edge e : edgesAdjacentTo(x)) {
if (x.equals(e.getNode1())) {
result.add(e.getNode2());
}
}
return result;
}
}
@Override
public Set inNeighbors(V x) {
if (!directed) {
return neighbors(x);
} else {
Set result = new HashSet();
for (Edge e : edgesAdjacentTo(x)) {
if (x.equals(e.getNode2())) {
result.add(e.getNode1());
}
}
return result;
}
}
@Override
public Set neighbors(V x) {
Set result = new HashSet();
for (Edge e : edgesAdjacentTo(x)) {
result.add(e.opposite(x));
}
return result;
}
@Override
public int outDegree(V x) {
if (!directed) {
return degree(x);
} else {
int result = 0;
for (Edge e : edgesAdjacentTo(x)) {
if (x.equals(e.getNode1())) {
result++;
}
}
return result;
}
}
@Override
public int inDegree(V x) {
if (!directed) {
return degree(x);
} else {
int result = 0;
for (Edge e : edgesAdjacentTo(x)) {
if (x.equals(e.getNode2())) {
result++;
}
}
return result;
}
}
@Override
public int degree(V x) {
int result = 0;
for (Edge e : edgesAdjacentTo(x)) {
// permit double counting if both vertices of edge are x
if (x.equals(e.getNode1())) {
result++;
}
if (x.equals(e.getNode2())) {
result++;
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy