|
| 1 | +package graph; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +/** |
| 6 | + * Represents a generic graph. |
| 7 | + * |
| 8 | + * @param <T> the type of the node data |
| 9 | + */ |
| 10 | +public interface Graph<T> { |
| 11 | + |
| 12 | + List<Node<T>> getNodes(); |
| 13 | + |
| 14 | + /** |
| 15 | + * Adds a new node with the specified data to the graph. |
| 16 | + * |
| 17 | + * @param data the data of the new node |
| 18 | + * @return the newly added node |
| 19 | + */ |
| 20 | + Node<T> addNode(T data); |
| 21 | + |
| 22 | + /** |
| 23 | + * Adds a new node with the specified data and name to the graph. |
| 24 | + * |
| 25 | + * @param data the data of the new node |
| 26 | + * @param name the name of the new node |
| 27 | + * @return the newly added node |
| 28 | + */ |
| 29 | + Node<T> addNode(T data, String name); |
| 30 | + |
| 31 | + /** |
| 32 | + * Connects two nodes in the graph with a specified weight. |
| 33 | + * |
| 34 | + * @param fromNode the starting node |
| 35 | + * @param toNode the ending node |
| 36 | + * @param weight the weight of the connection |
| 37 | + * @throws IllegalArgumentException if either node is not part of the graph |
| 38 | + */ |
| 39 | + void connect(Node<T> fromNode, Node<T> toNode, double weight); |
| 40 | + |
| 41 | + /** |
| 42 | + * Connects two nodes in the graph with a default weight of 1.0. |
| 43 | + * |
| 44 | + * @param fromNode the starting node |
| 45 | + * @param toNode the ending node |
| 46 | + */ |
| 47 | + default void connect(Node<T> fromNode, Node<T> toNode) { |
| 48 | + this.connect(fromNode, toNode, 1.0); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Connects a node to multiple nodes with specified weights. |
| 53 | + * |
| 54 | + * @param fromNode the starting node |
| 55 | + * @param toNodes the list of ending nodes |
| 56 | + * @param weights the list of weights |
| 57 | + * @throws IllegalArgumentException if fromNode is null, or if toNodes or weights are null or different sizes |
| 58 | + */ |
| 59 | + default void connect(Node<T> fromNode, List<Node<T>> toNodes, List<Double> weights) { |
| 60 | + if (fromNode == null || toNodes == null || weights == null || toNodes.size() != weights.size()) { |
| 61 | + throw new IllegalArgumentException("Invalid nodes or weights"); |
| 62 | + } |
| 63 | + for (int i = 0; i < toNodes.size(); i++) { |
| 64 | + this.connect(fromNode, toNodes.get(i), weights.get(i)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Connects a node to multiple nodes with a default weight of 1.0. |
| 70 | + * |
| 71 | + * @param fromNode the starting node |
| 72 | + * @param toNodes the list of ending nodes |
| 73 | + * @throws IllegalArgumentException if fromNode or toNodes is null |
| 74 | + */ |
| 75 | + default void connect(Node<T> fromNode, List<Node<T>> toNodes) { |
| 76 | + if (fromNode == null || toNodes == null) { |
| 77 | + throw new IllegalArgumentException("Invalid nodes"); |
| 78 | + } |
| 79 | + for (Node<T> toNode : toNodes) { |
| 80 | + this.connect(fromNode, toNode, 1.0); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Disconnects two nodes in the graph. |
| 86 | + * |
| 87 | + * @param fromNode the starting node |
| 88 | + * @param toNode the ending node |
| 89 | + * @throws IllegalArgumentException if fromNode or toNode is null |
| 90 | + */ |
| 91 | + void disconnect(Node<T> fromNode, Node<T> toNode); |
| 92 | + |
| 93 | + /** |
| 94 | + * Disconnects a node from multiple nodes. |
| 95 | + * |
| 96 | + * @param fromNode the starting node |
| 97 | + * @param toNodes the list of ending nodes |
| 98 | + * @throws IllegalArgumentException if fromNode or toNodes is null |
| 99 | + */ |
| 100 | + default void disconnect(Node<T> fromNode, List<Node<T>> toNodes) { |
| 101 | + if (fromNode == null || toNodes == null) { |
| 102 | + throw new IllegalArgumentException("Invalid nodes"); |
| 103 | + } |
| 104 | + for (Node<T> toNode : toNodes) { |
| 105 | + this.disconnect(fromNode, toNode); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Updates data of a node |
| 111 | + * |
| 112 | + * @param node the node whose data needs to be updated |
| 113 | + * @param newData the updated new data |
| 114 | + * @throws IllegalArgumentException if node or newData is null or node is not part of this graph |
| 115 | + */ |
| 116 | + void updateNodeData(Node<T> node, T newData); |
| 117 | + |
| 118 | + /** |
| 119 | + * Updates weight of an edge |
| 120 | + * |
| 121 | + * @param fromNode the starting node of the edge |
| 122 | + * @param toNode the ending node of the edge |
| 123 | + * @param newWeight the updated new weight |
| 124 | + * @throws IllegalArgumentException if either start and end nodes are not part of the graph or the edge doesn't exist between the two nodes |
| 125 | + */ |
| 126 | + void updateEdgeWeight(Node<T> fromNode, Node<T> toNode, double newWeight); |
| 127 | +} |
0 commit comments