Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.kth.graph;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.NoSuchElementException;
- /**
- * A graph with a fixed number of vertices implemented using adjacency maps.
- * Space complexity is Θ(n + m) where n is the number of vertices and m
- * the number of edges.
- *
- * @author [Name]
- * @version [Date]
- */
- public class HashGraph implements Graph {
- /**
- * The map edges[v] contains the key-value pair (w, c) if there is an edge
- * from v to w; c is the cost assigned to this edge. The maps may be null
- * and are allocated only when needed.
- */
- private final Map<Integer, Integer>[] edges;
- private final static int INITIAL_MAP_SIZE = 4;
- /** Number of edges in the graph. */
- private int numEdges;
- /**
- * Constructs a HashGraph with n vertices and no edges. Time complexity:
- * O(n)
- *
- * @throws IllegalArgumentException
- * if n < 0
- */
- public HashGraph(int n) {
- if (n < 0)
- throw new IllegalArgumentException("n = " + n);
- // The array will contain only Map<Integer, Integer> instances created
- // in addEdge(). This is sufficient to ensure type safety.
- @SuppressWarnings("unchecked") Map<Integer, Integer>[] a = new HashMap[n];
- edges = a;
- }
- /**
- * Add an edge without checking parameters.
- */
- private void addEdge(int from, int to, int cost) {
- if (edges[from] == null)
- edges[from] = new HashMap<Integer, Integer>(INITIAL_MAP_SIZE);
- if (edges[from].put(to, cost) == null)
- numEdges++;
- }
- /**
- * {@inheritDoc Graph} Time complexity: O(1).
- */
- @Override
- public int numVertices() {
- return edges.length;
- }
- /**
- * {@inheritDoc Graph} Time complexity: O(1).
- */
- @Override
- public int numEdges() {
- return numEdges;
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public int degree(int v) throws IllegalArgumentException {
- if(v < 0 || v >= edges.length){
- throw new IllegalArgumentException();
- }
- Map<Integer, Integer> gren
- gren = edges[v];
- if(edges[v] != null) {
- return gren.length;
- }
- return 0;
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public VertexIterator neighbors(int v) {
- if(v < 0 && v >= edges.length) {
- throw new IllegalArgumentException();
- }
- return new HashVertexIterator(v);
- }
- private class HashVertexIterator implements VertexIterator {
- iterator<Integer> iterator;
- public HashVertexIterator(int v){
- if(edges[v] == null) {
- iterator = new HashMap<Integer, Integer>().keySet().iterator();
- }
- else{iterator = edges[v].keyset().iterator();
- }
- }
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public boolean hasEdge(int from, int to) {
- if(edges[from] != null) {
- if(edges[from].containsKey(to))
- return true;
- }
- return false;
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public int cost(int from, int to) throws IllegalArgumentException {
- if(from >= 0 && to >= 0 && edges[to].get(w) != null) {
- cost = edges[from].get(w);
- return cost;
- }
- return NO_COST;
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public void add(int from, int to) {
- addEdge(from, to, NO_COST);
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public void add(int from, int to, int c) {
- addEdge(from, to, c);
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public void addBi(int v, int w) {
- addEdge(v, w, NO_COST);
- if(v == w){
- return;
- }
- addEdge(w, v, NO_COST);
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public void addBi(int v, int w, int c) {
- addEdge(v, w, c)
- if(v == w){
- return;
- }
- addEdge(w, v, c);
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public void remove(int from, int to) {
- if(hasEdge(from, to)) {
- edges[from].remove(to);
- numEdges--;
- }
- }
- /**
- * {@inheritDoc Graph}
- */
- @Override
- public void removeBi(int v, int w) {
- if(v >= 0 && w >= 0) {
- if(edes[v] != null) {
- remove(v, w);
- numEdges--;
- }
- if(edges[w] != null) {
- remove(w, v);
- numEdges--;
- }
- }
- }
- /**
- * Returns a string representation of this graph.
- *
- * Should represent the graph in terms of edges (order does not matter).
- * For example, if a graph has edges (2,3) and (1,0) with NO_COST, the
- * representaiton should be:
- *
- * "{(1,0), (2,3)}" or "{(2,3), (1,0)}"
- *
- * If an edge has a cost (say, 5), it is added as a third value, like so:
- *
- * "{(1,0,5)}"
- *
- * An empty graph is just braces:
- *
- * "{}"
- *
- * @return a String representation of this graph
- */
- @Override
- public String toString() {
- if(edges.length != 0){
- for(int i = edges.length; i <= 0; i++){
- if(edges[i] != null){
- StringBuilder sb = new StringBuilder();
- String S = "";
- sb.append(edges[i].keySet().toString());
- System.out.println(s);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement