1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| package com.shg.graph;
import java.util.*;
public class ListGraph<K, E> implements Graph<K, E> { private Map<K, Vertex<K, E>> vertices; private Set<Edge<K, E>> edges;
public ListGraph() { vertices = new HashMap<>(); edges = new HashSet<>(); }
public void print() { vertices.forEach((k, vertex) -> { System.out.println(k + "->" + vertex); });
edges.forEach(System.out::println); }
@Override public int edgesSize() { return edges.size(); }
@Override public int verticesSize() { return vertices.size(); }
@Override public void addVertex(K k) { vertices.put(k, new Vertex<>(k)); }
@Override public void addEdge(K from, K to) { addEdge(from, to, null); }
@Override public void addEdge(K from, K to, E weight) { Vertex<K, E> fromVertex = vertices.get(from); if (fromVertex == null) { fromVertex = new Vertex<>(from); vertices.put(from, fromVertex); }
Vertex<K, E> toVertex = vertices.get(to); if (toVertex == null) { toVertex = new Vertex<>(to); vertices.put(to, toVertex); }
Edge<K, E> edge = new Edge<>(weight, fromVertex, toVertex); if (fromVertex.outEdges.remove(edge)) { toVertex.inEdges.remove(edge); } fromVertex.outEdges.add(edge); toVertex.inEdges.add(edge); edges.add(edge); }
@Override public void removeVertex(K k) { Vertex<K, E> vertex = vertices.remove(k); if (vertex == null) return; Iterator<Edge<K, E>> iterator = vertex.inEdges.iterator(); while (iterator.hasNext()) { Edge<K, E> edge = iterator.next(); edge.from.outEdges.remove(edge); iterator.remove(); edges.remove(edge); } iterator = vertex.outEdges.iterator(); while (iterator.hasNext()) { Edge<K, E> edge = iterator.next(); edge.to.inEdges.remove(edge); iterator.remove(); edges.remove(edge); } }
@Override public void removeEdge(K from, K to) { Vertex<K, E> fromVertex = vertices.get(from); if (fromVertex == null) return; Vertex<K, E> toVertex = vertices.get(to); if (toVertex == null) return;
Edge<K, E> edge = new Edge<>(fromVertex, toVertex); if (edges.remove(edge)) { fromVertex.outEdges.remove(edge); toVertex.inEdges.remove(edge); } }
private static class Edge<K, E> { E weight; Vertex<K, E> from; Vertex<K, E> to;
public Edge(Vertex<K, E> from, Vertex<K, E> to) { this(null, from, to); }
public Edge(E weight, Vertex<K, E> from, Vertex<K, E> to) { this.weight = weight; this.from = from; this.to = to; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Edge<?, ?> edge = (Edge<?, ?>) o; return Objects.equals(from, edge.from) && Objects.equals(to, edge.to); }
@Override public int hashCode() { return Objects.hash(from, to); }
@Override public String toString() { return "Edge{" + "weight=" + weight + ", from=" + from + ", to=" + to + '}'; } }
private static class Vertex<K, E> { K key; Set<Edge<K, E>> inEdges; Set<Edge<K, E>> outEdges;
public Vertex(K key) { this.key = key; this.inEdges = new HashSet<>(); this.outEdges = new HashSet<>(); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Vertex<?, ?> vertex = (Vertex<?, ?>) o; return Objects.equals(key, vertex.key); }
@Override public int hashCode() { return Objects.hash(key); }
@Override public String toString() { return key.toString(); } }
@Override public String toString() { return "ListGraph{" + "vertices=" + vertices + ", edges=" + edges + '}'; } }
|