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

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

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;

public class MutableUndirectedWeightedGraph implements Graph> {

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

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

	public void insertVertex(V v) {
		vertices.insert(v);
	}

	public void addEdge(V v1, V v2, W weight) {
		assertVertexExist(v1);
		assertVertexExist(v2);
		edges.addToLast(SimpleUndirectedWeightedEdge.create(v1, v2, weight));
	}

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

	@Override
	public Collection getVertices() {
		return vertices;
	}

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

	@Override
	public String toString() {
		return GraphToString.toString(this);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy