Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package revisedversion;
- public class Edge<T> {
- private T destination;
- private int weight;
- public Edge(T destination, int weight) {
- this.destination = destination;
- this.weight = weight;
- }
- @Override
- public boolean equals(Object object) {
- return ((Edge) object).destination == this.destination;
- }
- public void setDestination(T destination) {
- this.destination = destination;
- }
- public void setWeight(int weight) {
- this.weight = weight;
- }
- public T getDestination() {
- return destination;
- }
- public int getWeight() {
- return weight;
- }
- }
- >>
- import java.util.LinkedList;
- public class Vertex <T> {
- private T item;
- private LinkedList<Edge> edgeList;
- private int inDegree;
- private int outDegree;
- public Vertex(T item) {
- this.item = item;
- this.edgeList = new LinkedList<>();
- this.inDegree = 0;
- this.outDegree = 0;
- }
- @Override
- public boolean equals(Object object) {
- Vertex<T> vertex2 = (Vertex<T>) object;
- return this.item.equals(vertex2.item);
- }
- public T getItem() {
- return item;
- }
- public void setItem(T item) {
- this.item = item;
- }
- public LinkedList<Edge> getEdgeList() {
- return edgeList;
- }
- public int getInDegree() {
- return inDegree;
- }
- public void setInDegree(int inDegree) {
- this.inDegree = inDegree;
- }
- public int getOutDegree() {
- return outDegree;
- }
- public void setOutDegree(int outDegree) {
- this.outDegree = outDegree;
- }
- }
- >>
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeSet;
- public class DiGraph<T> {
- Map<T, Vertex<T>> vertexMap;
- public DiGraph() {
- vertexMap = new HashMap<>();
- }
- boolean addEdge(T sourceItem, T destinationItem, int weight) {
- if (containsVertex(sourceItem) && containsVertex(destinationItem)) {
- if (containsEdge(sourceItem, destinationItem)) {
- return false;
- }
- Vertex<T> sourceVertex = vertexMap.get(sourceItem);
- Vertex<T> destinationVertex = vertexMap.get(destinationItem);
- List sourceEdgeList = sourceVertex.getEdgeList();
- sourceEdgeList.add(new Edge(destinationItem, weight));
- sourceVertex.setOutDegree(sourceVertex.getOutDegree() + 1);
- destinationVertex.setInDegree(destinationVertex.getInDegree() + 1);
- return true;
- }
- return false;
- }
- boolean addVertex(T item) {
- if (!containsVertex(item)) {
- Vertex<T> vertex = new Vertex<>(item);
- vertexMap.put(item, vertex);
- return true;
- }
- return false;
- }
- int getNumberOfEdges() {
- int numberOfEdges = 0;
- for (Vertex<T> vertex : vertexMap.values()) {
- numberOfEdges += vertex.getEdgeList().size();
- }
- return numberOfEdges;
- }
- int getNumberOfVertices() {
- return vertexMap.size();
- }
- boolean isEmpty() {
- return getNumberOfVertices() == 0;
- }
- boolean containsVertex(T item) {
- return vertexMap.containsKey(item);
- }
- public String toString() {
- String output = "";
- for (Vertex<T> vertex : vertexMap.values()) {
- output += vertex.getItem() + ":\tin-degree " + vertex.getInDegree()
- + "\tout-degree " + vertex.getOutDegree() + "\n";
- output += "\tEdges: ";
- for (Edge edge : vertex.getEdgeList()) {
- output += edge.getDestination() + "(" + edge.getWeight() + ")\t";
- }
- output += "\n";
- }
- return output;
- }
- int getInDegree(T item) {
- if (containsVertex(item)) {
- Vertex<T> vertex = vertexMap.get(item);
- return vertex.getInDegree();
- }
- return -1;
- }
- int getOutDegree(T item) {
- if (containsVertex(item)) {
- Vertex<T> vertex = vertexMap.get(item);
- return vertex.getOutDegree();
- }
- return -1;
- }
- private Edge getEdge(T sourceItem, T destinationItem) {
- if (containsVertex(sourceItem)) {
- Vertex<T> sourceVertex = vertexMap.get(sourceItem);
- for (Edge edge : sourceVertex.getEdgeList()) {
- if (edge.getDestination().equals(destinationItem)) {
- return edge;
- }
- }
- }
- return null;
- }
- boolean containsEdge(T sourceItem, T destinationItem) {
- return getEdge(sourceItem, destinationItem) != null;
- }
- int getWeight(T sourceItem, T destinationItem) {
- Edge edge = getEdge(sourceItem, destinationItem);
- if (edge != null) {
- return edge.getWeight();
- }
- return -1;
- }
- boolean setWeight(T sourceItem, T destinationItem, int weight) {
- Edge edge = getEdge(sourceItem, destinationItem);
- if (edge != null) {
- edge.setWeight(weight);
- return true;
- }
- return false;
- }
- Set getNeighbors(T item) {
- Set neighborsSet = new TreeSet<>();
- if (containsVertex(item)) {
- List<Edge> vertexEdgeList = vertexMap.get(item).getEdgeList();
- for (Edge edge : vertexEdgeList) {
- neighborsSet.add(edge.getDestination());
- }
- return neighborsSet;
- } else {
- throw new IllegalArgumentException("Vertex " + item + " is not member of graph");
- }
- // exception belum berjalan
- }
- boolean removeEdge(T sourceItem, T destinationItem) {
- Edge removedEdge = getEdge(sourceItem, destinationItem);
- if (removedEdge != null) {
- Vertex<T> sourceVertex = vertexMap.get(sourceItem);
- Vertex<T> destinationVertex = vertexMap.get(destinationItem);
- sourceVertex.getEdgeList().remove(removedEdge);
- sourceVertex.setOutDegree(sourceVertex.getOutDegree() - 1);
- destinationVertex.setInDegree(destinationVertex.getInDegree() - 1);
- return true;
- }
- return false;
- }
- boolean removeVertex(T item) {
- if (containsVertex(item)) {
- Vertex<T> removedVertex = vertexMap.get(item);
- List<Edge> removedVertexEdgeList = removedVertex.getEdgeList();
- if (!removedVertexEdgeList.isEmpty()) {
- for (Edge edge : removedVertexEdgeList) {
- T destination = (T) edge.getDestination();
- Vertex<T> destinationVertex = vertexMap.get(destination);
- destinationVertex.setInDegree(destinationVertex.getInDegree() - 1);
- }
- }
- for (T key : vertexMap.keySet()) {
- Vertex<T> vertex = vertexMap.get(key);
- Edge edgeToRemovedVertex = getEdge(key, item);
- if (edgeToRemovedVertex != null) {
- vertex.getEdgeList().remove(edgeToRemovedVertex);
- }
- }
- vertexMap.remove(item);
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement