|
8 | 8 | /** |
9 | 9 | * Created by sherxon on 1/1/17. |
10 | 10 | */ |
11 | | -public class UndirectedGraph<V, E extends Number> implements Graph<V, E> { |
12 | | - private Map<V, Vertex<V>> vertexMap = new HashMap<>(); |
| 11 | +public class UndirectedGraph implements Graph { |
| 12 | + private Map<Integer, Set<Integer>> vertexMap = new HashMap<>(); |
13 | 13 |
|
14 | 14 |
|
15 | 15 | @Override |
16 | | - public void addVertex(V v) { |
17 | | - vertexMap.put(v, new Vertex<>(v)); |
| 16 | + public boolean addVertex(Integer v) { |
| 17 | + vertexMap.put(v, new HashSet<>()); |
| 18 | + return true; |
18 | 19 | } |
19 | 20 |
|
20 | 21 |
|
21 | 22 | @Override |
22 | | - public Set<Vertex<V>> getVertices() { |
23 | | - return new HashSet<>(vertexMap.values()); |
| 23 | + public Set<Integer> getVertices() { |
| 24 | + return new HashSet<>(vertexMap.keySet()); |
24 | 25 | } |
25 | 26 |
|
26 | 27 |
|
27 | 28 | @Override |
28 | | - public void addEdge(V v1, V v2) { |
29 | | - if(!vertexMap.containsKey(v1))return; |
30 | | - if(!vertexMap.containsKey(v2))return; |
31 | | - Vertex<V> from=vertexMap.get(v1); |
32 | | - Vertex<V> to=vertexMap.get(v2); |
33 | | - if (from == to) return; |
34 | | - if (from.getNeighbors().contains(to) || to.getNeighbors().contains(from)) return; |
35 | | - from.addNeighbor(to); |
36 | | - to.addNeighbor(from); |
| 29 | + public Double addEdge(Integer v1, Integer v2) { |
| 30 | + if (!vertexMap.containsKey(v1)) return -1d; |
| 31 | + if (!vertexMap.containsKey(v2)) return -1d; |
| 32 | + vertexMap.get(v1).add(v2); |
| 33 | + return 0d; |
37 | 34 | } |
38 | 35 |
|
39 | 36 | @Override |
40 | | - public void addEdge(V v1, V v2, E weight) { |
| 37 | + public boolean addEdge(Integer v1, Integer v2, Double weight) { |
41 | 38 | // not supported |
42 | 39 | // use weighted graph |
| 40 | + return false; |
43 | 41 | } |
44 | 42 |
|
45 | 43 | @Override |
46 | | - public void removeVertex(V v) { |
47 | | - Vertex<V> vertex = vertexMap.get(v); |
48 | | - Set<Vertex<V>> neis = vertex.getNeighbors(); |
49 | | - for (Vertex<V> nei : neis) { |
50 | | - nei.removeNeighrbor(vertex); |
51 | | - } |
52 | | - |
53 | | - vertex.getNeighbors().clear(); |
54 | | - vertexMap.remove(v); |
| 44 | + public boolean removeVertex(Integer v) { |
| 45 | + return false; |
55 | 46 | } |
56 | 47 |
|
57 | 48 | @Override |
58 | | - public void removeEdge(V v1, V v2) { |
59 | | - if(!vertexMap.containsKey(v1))return; |
60 | | - if(!vertexMap.containsKey(v2))return; |
61 | | - Vertex<V> from=vertexMap.get(v1); |
62 | | - Vertex<V> to=vertexMap.get(v2); |
63 | | - from.removeNeighrbor(to); |
64 | | - to.removeNeighrbor(from); |
| 49 | + public boolean removeEdge(Integer v1, Integer v2) { |
| 50 | + return false; |
65 | 51 | } |
66 | 52 |
|
67 | 53 | @Override |
68 | 54 | public int size() { |
69 | 55 | return vertexMap.size(); |
70 | 56 | } |
71 | 57 |
|
72 | | - @Override |
73 | | - public Vertex<V> getVertex(V v) { |
74 | | - return vertexMap.get(v); |
75 | | - } |
76 | | - |
77 | 58 | } |
0 commit comments