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

graph.impl.AdjacencyMatrixGraph Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show 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 AdjacencyMatrixGraph implements Graph {
	private class AdjacencyMatrixVertex implements Vertex {
		Position position;
		V element;
		int index;
		
		public AdjacencyMatrixVertex(V element, int index) {
			this.element = element;
			this.index = index;
		}
		
		@Override
		public V element() {
			return element;
		}
		
		public String toString() {
			return "{" + element.toString() + ", " + index + "}";
		}
	}
	
	private class AdjacencyMatrixEdge implements Edge {
		Position position;
		E element;
		AdjacencyMatrixVertex start, end;
		
		public AdjacencyMatrixEdge(AdjacencyMatrixVertex start, AdjacencyMatrixVertex end, E element) {
			this.start = start;
			this.end = end;
			this.element = element;
		}
		
		@Override
		public E element() {
			return element;
		}
		
		public String toString() {
			return element.toString();
		}
	}

	@SuppressWarnings("unchecked")
	private Edge[][] matrix = new Edge[10][10];
	
	private List vertices;
	private List edges;
	
	public AdjacencyMatrixGraph() {
		vertices = new LinkedList();
		edges = new LinkedList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Vertex[] endVertices(Edge e) {
		AdjacencyMatrixEdge edge = (AdjacencyMatrixEdge) 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 (AdjacencyMatrixEdge 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) {
		AdjacencyMatrixVertex vertex = (AdjacencyMatrixVertex) v;
		V temp = vertex.element;
		vertex.element = x;
		return temp;
	}

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

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

	@Override
	public Edge insertEdge(Vertex v, Vertex w, E o) {
		AdjacencyMatrixEdge edge = new AdjacencyMatrixEdge((AdjacencyMatrixVertex) v, (AdjacencyMatrixVertex) w, o);
		Position position = edges.insertLast(edge);
		edge.position = position;
		matrix[((AdjacencyMatrixVertex) v).index][((AdjacencyMatrixVertex) w).index] = edge;
		matrix[((AdjacencyMatrixVertex) w).index][((AdjacencyMatrixVertex) v).index] = edge;
		return edge;
	}

	@Override
	public V removeVertex(Vertex v) {
		Iterator> it = incidentEdges(v).iterator();
		while (it.hasNext()) it.remove();
		
		AdjacencyMatrixVertex vertex = (AdjacencyMatrixVertex) v;
		Position pos = vertices.next(vertex.position);
		
		int index = vertex.index;
		while (index < vertices.size()) {
			for (int i = 0; i < vertices.size(); i++) {
				matrix[index][i] = matrix[index+1][i];
			}
			index++;
		}
		
		index = vertex.index;
		while (index < vertices.size()) {
			for (int i = 0; i < vertices.size(); i++) {
				matrix[i][index] = matrix[i][index+1];
			}
			index++;
		}
		
		vertices.remove(vertex.position);
		
		while (!pos.equals(vertices.last())) {
			pos.element().index--;
			pos = vertices.next(pos);
		}
		pos.element().index--;
		
		return vertex.element;
	}

	@Override
	public E removeEdge(Edge e) {
		AdjacencyMatrixEdge edge = (AdjacencyMatrixEdge) e;
		matrix[edge.start.index][edge.end.index] = null;
		matrix[edge.end.index][edge.start.index] = null;
		edges.remove(edge.position);
		return edge.element;
	}

	@Override
	public List> incidentEdges(Vertex v) {
		LinkedList> list = new LinkedList>();
		
		for (int i=0; i> vertices() {
		LinkedList> list = new LinkedList>();
		for (AdjacencyMatrixVertex vertex : vertices) {
			list.insertLast(vertex);
		}
		return list;
	}

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy