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

graph.impl.EdgeListGraph Maven / Gradle / Ivy

The newest version!
package graph.impl;

import graph.core.Edge;
import graph.core.Graph;
import graph.core.InvalidVertexException;
import graph.core.Vertex;
import graph.util.LinkedList;
import graph.util.List;
import graph.util.Position;

import java.util.Iterator;



public class EdgeListGraph implements Graph {
	private class EdgeListVertex implements Vertex {
		Position position;
		V element;
		
		public EdgeListVertex(V element) {
			this.element = element;
		}
		
		@Override
		public V element() {
			return element;
		}
		
		public String toString() {
			return element.toString();
		}
	}
	
	private class EdgeListEdge implements Edge {
		Position position;
		E element;
		EdgeListVertex start, end;
		
		public EdgeListEdge(EdgeListVertex start, EdgeListVertex end, E element) {
			this.start = start;
			this.end = end;
			this.element = element;
		}
		
		@Override
		public E element() {
			return element;
		}
		
		public String toString() {
			return element.toString();
		}
	}
	
	private List vertices;
	private List edges;
	
	public EdgeListGraph() {
		vertices = new LinkedList();
		edges = new LinkedList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Vertex[] endVertices(Edge e) {
		EdgeListEdge edge = (EdgeListEdge) e;
		Vertex[] endpoints = (Vertex[]) new Vertex[2];
		endpoints[0] = edge.start;
		endpoints[1] = edge.end;
		return endpoints;
	}

	@Override
	public Vertex opposite(Vertex v, Edge e) {
		Vertex[] endpoints = endVertices(e);
		if (endpoints[0].equals(v)) {
			return endpoints[1];
		} else if (endpoints[1].equals(v)) {
			return endpoints[0];
		}
		throw new InvalidVertexException();
	}

	@Override
	public boolean areAdjacent(Vertex v, Vertex w) {
		for (EdgeListEdge edge: edges) {
			if ((edge.start.equals(v)) && (edge.end.equals(w))) return true;
			if ((edge.end.equals(v)) && (edge.start.equals(w))) return true;
		}
		return false;
	}

	@Override
	public V replace(Vertex v, V x) {
		EdgeListVertex vertex = (EdgeListVertex) v;
		V temp = vertex.element;
		vertex.element = x;
		return temp;
	}

	@Override
	public E replace(Edge e, E x) {
		EdgeListEdge edge = (EdgeListEdge) e;
		E temp = edge.element;
		edge.element = x;
		return temp;
	}

	@Override
	public Vertex insertVertex(V v) {
		EdgeListVertex vertex = new EdgeListVertex(v);
		Position position = vertices.insertLast(vertex);
		vertex.position = position;
		return vertex;
	}

	@Override
	public Edge insertEdge(Vertex v, Vertex w, E o) {
		EdgeListEdge edge = new EdgeListEdge((EdgeListVertex) v, (EdgeListVertex) w, o);
		Position position = edges.insertLast(edge);
		edge.position = position;
		return edge;
	}

	@Override
	public V removeVertex(Vertex v) {
		Iterator> it = incidentEdges(v).iterator();
		while (it.hasNext()) it.remove();
		
		EdgeListVertex vertex = (EdgeListVertex) v;
		vertices.remove(vertex.position);
		return vertex.element;
	}

	@Override
	public E removeEdge(Edge e) {
		EdgeListEdge edge = (EdgeListEdge) e;
		edges.remove(edge.position);
		return edge.element;
	}

	@Override
	public List> incidentEdges(Vertex v) {
		LinkedList> list = new LinkedList>();
		
		for (EdgeListEdge edge : edges) {
			if (edge.start.equals(v)) list.insertLast(edge);
			if (edge.end.equals(v)) list.insertLast(edge);
		}
		
		return list;
	}

	@Override
	public List> vertices() {
		LinkedList> list = new LinkedList>();
		for (EdgeListVertex vertex : vertices) {
			list.insertLast(vertex);
		}
		return list;
	}

	@Override
	public List> edges() {
		LinkedList> list = new LinkedList>();
		for (EdgeListEdge edge : edges) {
			list.insertLast(edge);
		}
		return list;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy