|
| 1 | +package net.coderodde.graph; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | + |
| 5 | +/** |
| 6 | + * This class defines the API for graph data structures. The actual nodes are |
| 7 | + * represented as integers. The client programmer should always be able to map |
| 8 | + * his domain specific nodes to the integers. |
| 9 | + * |
| 10 | + * @author Rodion "rodde" Efremov |
| 11 | + * @version 1.6 (Jan 10, 2016) |
| 12 | + */ |
| 13 | +public abstract class AbstractGraph { |
| 14 | + |
| 15 | + protected int edges; |
| 16 | + |
| 17 | + /** |
| 18 | + * Returns the number of nodes in this graph. |
| 19 | + * |
| 20 | + * @return the size of this graph. |
| 21 | + */ |
| 22 | + public abstract int size(); |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns the number of edges in this graph. |
| 26 | + * |
| 27 | + * @return the number of edges. |
| 28 | + */ |
| 29 | + public abstract int getNumberOfEdges(); |
| 30 | + |
| 31 | + /** |
| 32 | + * Adds the node with ID {@code nodeId} to this graph. |
| 33 | + * |
| 34 | + * @param nodeId the ID of the node to add. |
| 35 | + * @return {@code true} if the structure of this graph has changed, which is |
| 36 | + * the same as that the added node was not present in the graph. |
| 37 | + */ |
| 38 | + public abstract boolean addNode(int nodeId); |
| 39 | + |
| 40 | + /** |
| 41 | + * Checks whether the given node is present in this graph. |
| 42 | + * |
| 43 | + * @param nodeId the query node. |
| 44 | + * @return {@code true} if the query node is in this graph. {@code false} |
| 45 | + * otherwise. |
| 46 | + */ |
| 47 | + public abstract boolean hasNode(int nodeId); |
| 48 | + |
| 49 | + /** |
| 50 | + * Removes the argument node from this graph. |
| 51 | + * |
| 52 | + * @param nodeId the node to remove. |
| 53 | + * @return {@code true} only if the node was present in the graph which |
| 54 | + * means that the structure of the graph has changed. |
| 55 | + */ |
| 56 | + public abstract boolean removeNode(int nodeId); |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates an edge between {@code tailNodeId} and {@code headNodeId} with |
| 60 | + * weight {@code weight}. It depends on the concrete implementation of this |
| 61 | + * abstract class what an edge {@code (tailNodeId, headNodeId)}. Two |
| 62 | + * possible cases are an undirected edge and a directed edge. Refer to the |
| 63 | + * documentation of the implementing graph type. |
| 64 | + * <p> |
| 65 | + * If some of the input nodes are not present in this graph, it will be |
| 66 | + * created silently. |
| 67 | + * |
| 68 | + * @param tailNodeId the tail node of the edge. |
| 69 | + * @param headNodeId the head node of the edge. |
| 70 | + * @param weight the weight of the edge. |
| 71 | + * @return {@code true} only if the edge was not present in the graph, or |
| 72 | + * the weight of the edge has changed. |
| 73 | + */ |
| 74 | + public abstract boolean addEdge(int tailNodeId, |
| 75 | + int headNodeId, |
| 76 | + double weight); |
| 77 | + |
| 78 | + /** |
| 79 | + * Creates an edge between {@code tailNodeId} and {@code headNodeId} with |
| 80 | + * the default weight of 1.0. This method is a shortcut for constructing |
| 81 | + * (virtually) unweighted graphs. |
| 82 | + * |
| 83 | + * @param tailNodeId the tail node of the edge. |
| 84 | + * @param headNodeId the head node of the edge. |
| 85 | + * @return {@code true} only if the edge was not present in the graph, or |
| 86 | + * the weight of the edge has changed. |
| 87 | + */ |
| 88 | + public boolean addEdge(int tailNodeId, int headNodeId) { |
| 89 | + return addEdge(tailNodeId, headNodeId, 1.0); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns a boolean value indicating whether this graph contains an edge |
| 94 | + * from {@code tailNodeId} to {@code headNodeId}. |
| 95 | + * |
| 96 | + * @param tailNodeId the tail node of the query edge. |
| 97 | + * @param headNodeId the head node of the query edge. |
| 98 | + * @return {@code true} only if the query edge is in this graph. |
| 99 | + */ |
| 100 | + public abstract boolean hasEdge(int tailNodeId, int headNodeId); |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the weight of the edge {@code (tailNodeId, headNodeId)}. If the |
| 104 | + * query edge does not exist, returns {@link java.lang.Double#NaN}. |
| 105 | + * |
| 106 | + * @param tailNodeId the tail node of the query edge. |
| 107 | + * @param headNodeId the head node of the query edge. |
| 108 | + * @return the weight of the edge. |
| 109 | + */ |
| 110 | + public abstract double getEdgeWeight(int tailNodeId, int headNodeId); |
| 111 | + |
| 112 | + /** |
| 113 | + * Removes the edge from {@code tailNodeId} and {@code headNodeId}. |
| 114 | + * |
| 115 | + * @param tailNodeId the tail node of the edge to remove. |
| 116 | + * @param headNodeId the head node of the edge to remove. |
| 117 | + * @return {@code true} if the target edge was in this graph, and thus is |
| 118 | + * removed. |
| 119 | + */ |
| 120 | + public abstract boolean removeEdge(int tailNodeId, int headNodeId); |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the set of all nodes that are children of the node |
| 124 | + * {@code nodeId}. It depends on the actual graph implementation what is |
| 125 | + * understood by the termin <i>child</i>. In unweighted graphs, every child |
| 126 | + * is also a parent of a node, which is not necessarily true in directed |
| 127 | + * graphs. |
| 128 | + * |
| 129 | + * @param nodeId the query node. |
| 130 | + * @return set of nodes that are children on the argument node. |
| 131 | + */ |
| 132 | + public abstract Set<Integer> getChildrenOf(int nodeId); |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns the set of all nodes that are parents of the node {@code nodeId}. |
| 136 | + * |
| 137 | + * @see #getChildrenOf(int) |
| 138 | + * @param nodeId the query node. |
| 139 | + * @return set of nodes that are parent of the arugment node. |
| 140 | + */ |
| 141 | + public abstract Set<Integer> getParentsOf(int nodeId); |
| 142 | + |
| 143 | + /** |
| 144 | + * Returns the set of all nodes stored in this graph. |
| 145 | + * |
| 146 | + * @return the set of all nodes. |
| 147 | + */ |
| 148 | + public abstract Set<Integer> getAllNodes(); |
| 149 | + |
| 150 | + /** |
| 151 | + * Removes all nodes and edges from this graph. |
| 152 | + */ |
| 153 | + public abstract void clear(); |
| 154 | +} |
0 commit comments