All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.psjava.ds.graph.MutableBipartiteGraph Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
package org.psjava.ds.graph;

import org.psjava.ds.Collection;
import org.psjava.ds.array.DynamicArray;
import org.psjava.ds.set.MutableSet;
import org.psjava.goods.GoodMutableSetFactory;
import org.psjava.util.AssertStatus;
import org.psjava.util.IterableToString;

public class MutableBipartiteGraph implements BipartiteGraph {

	public static  MutableBipartiteGraph create() {
		return new MutableBipartiteGraph();
	}

	private MutableSet left = GoodMutableSetFactory.getInstance().create();
	private MutableSet right = GoodMutableSetFactory.getInstance().create();
	private DynamicArray> edges = DynamicArray.create();

	public void insertLeftVertex(V v) {
		AssertStatus.assertTrue(!right.contains(v));
		left.insert(v);
	}

	public void insertRightVertex(V v) {
		AssertStatus.assertTrue(!left.contains(v));
		right.insert(v);
	}

	public void addEdge(final V leftv, final V rightv) {
		assertVertexExist(leftv, left);
		assertVertexExist(rightv, right);
		edges.addToLast(SimpleBipartiteGraphEdge.create(leftv, rightv));
	}

	private void assertVertexExist(V v, MutableSet set) {
		AssertStatus.assertTrue(set.contains(v), "vertex is not in graph");
	}

	@Override
	public Collection getLeftVertices() {
		return left;
	}

	@Override
	public Collection getRightVertices() {
		return right;
	}

	@Override
	public Iterable> getEdges() {
		return edges;
	}

	@Override
	public String toString() {
		return this.getClass().getSimpleName() + ":" + IterableToString.toString(getEdges());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy