Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tedigraphwithgs; import com.google.common.base.Objects; import com.google.common.collect.Iterables; import com.sun.org.apache.xpath.internal.operations.Mult; import java.awt.Event; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Stack; import javafx.scene.control.Tab; import javax.crypto.Mac; import javax.swing.tree.DefaultMutableTreeNode; import org.graphstream.graph.*; import org.graphstream.graph.implementations.*; import scala.collection.immutable.RedBlack; import sun.reflect.generics.tree.Tree; /** * * @author hamim ari */ public class TediGraph { private TreeDecomposition tree; private Graph graph; private Stack stack; private ArrayList<Node> localShortestPath; private ArrayList<Node> shortestPath; private List<String> listIdEdge; private List<String> listIdNode; private String GRAPH_NAME = "graph.dgs"; public TediGraph() { graph = new MultiGraph("Iface"); stack = new Stack(); listIdEdge = new ArrayList<String>(); listIdNode = new ArrayList<String>(); } //constructor untuk duplicate graph public TediGraph(TediGraph tg) { graph = tg.getGraph(); stack = new TediGraph().getStack(); listIdEdge = new TediGraph().getListIdEdge(); listIdNode = new TediGraph().getListIdNode(); GRAPH_NAME = tg.getGRAPH_NAME().substring(0, tg.getGRAPH_NAME().length() - 4) + "Copy.dgs"; } public void generateTree(Stack s, Graph g) throws IOException { tree = new TreeDecomposition(); TreeNode tn = new TreeNode(); int count = 0; //construct root dengan semua node graph yang tidak diremove pada proses graph reduction for (Node n : g.getEachNode()) { tn.getListVertex().add(n); } tree.setRoot(tn); //insert root ke list bag pada tree tree.getListBag().add(tn); //iterasi untuk mengeluarkan isi stack while (!s.empty()) { if (tree.getListBag().size() != 0) { //iterasi semua bag yang ada di list bag for (TreeNode treeNode : tree.getListBag()) { //iterasi semua node yang ada pada top stack for (Node n : Iterables.skip(((TreeNode) s.peek()).getListVertex(), 1)) { //jika bag mengandung node yg dimiliki oleh top stack if (treeNode.getListVertex().contains(n)) { count++; } //jika bag memiliki semua node -1 yg ada pada top stack if (count == treeNode.getListVertex().size() - 1) { //set parent ((TreeNode) s.peek()).setParent(treeNode); System.out.println("induk dari " + ((TreeNode) s.peek()).getListVertex() + " adalah " + treeNode.getListVertex()); count = 0; } } count = 0; } } tree.getListBag().add((TreeNode) s.pop()); } } public Stack graphReduction(int k, Graph g) throws IOException { for (int i = 1; i <= k; i++) { removeUpTo(i, g); } return stack; } public void computeLocalShortestPath(TreeDecomposition t, Graph g) { for (TreeNode tn : t.getListBag()) { for (int i = 0; i < tn.getListVertex().size(); i++) { for (int j = i + 1; j < tn.getListVertex().size(); j++) { //start bfs method bfs(g, tn.getListVertex().get(i), tn.getListVertex().get(j)); //save shortest path tn.getLocalShortestPath().put(localShortestPath, localShortestPath.size() - 2); } } } } public List<Node> Shortestpath(TreeDecomposition t, Node u, Node v) { return Collections.unmodifiableList(shortestPath); } public Graph getGraph() { return graph; } public Stack getStack() { return stack; } public void removeUpTo(int l, Graph g) throws IOException { List<Node> neighbour; TreeNode treeNode; for (Node node : g.getEachNode()) { //check node degree yang akan direduksi if (node.getDegree() <= l) { treeNode = new TreeNode(); treeNode.getListVertex().add(node); for (Iterator<Node> it = node.getNeighborNodeIterator(); it.hasNext();) { treeNode.getListVertex().add((Node) it.next()); } //construct clique for (int i = 0; i < treeNode.getListVertex().size(); i++) { for (int j = i + 1; j < treeNode.getListVertex().size(); j++) { if (!treeNode.getListVertex().get(i).hasEdgeBetween(treeNode.getListVertex().get(j))) { g.addEdge(String.valueOf(generateId(Edge.class)), treeNode.getListVertex().get(i), treeNode.getListVertex().get(j)); } } } stack.push(treeNode); g.removeNode(node); } else { break; } } } public Long generateId(Class<?> type) throws IOException { long id = -1; if (type == Edge.class) { //cek apakah array listId kosong if (listIdEdge.isEmpty()) { File file = new File(GRAPH_NAME); // cek apakah file kosong if (file.length() == 0) { id = id + 1; } else { //memasukkan seluruh edge di graph ke arraylist for (Edge edge : graph.getEachEdge()) { listIdEdge.add(edge.getId()); } //jika di file tidak ada id edge if (listIdEdge.size() == 0) { id = id + 1; } else { id = Long.parseLong(Collections.max(listIdEdge)) + 1; } } } else { //jika array list tidak kosong, maka add idEdge yang ada di graph ke Arraylist idEdge for (Edge edge : graph.getEachEdge()) { listIdEdge.add(edge.getId()); } id = Long.parseLong(Collections.max(listIdEdge)) + 1; } //tambahkan id yg sudah digenerate ke listIdEdge listIdEdge.add(String.valueOf(id)); } return id; } public String getGRAPH_NAME() { return GRAPH_NAME; } public TreeDecomposition getTree() { return tree; } public List<Node> getShortestPath() { return shortestPath; } public List<String> getListIdEdge() { return listIdEdge; } public List<String> getListIdNode() { return listIdNode; } public void setGRAPH_NAME(String GRAPH_NAME) { this.GRAPH_NAME = GRAPH_NAME; } public void setGraph(Graph graph) { this.graph = graph; } public void bfs(Graph g, Node source, Node dest) { ArrayDeque<Node> queue = new ArrayDeque<Node>(); ArrayList<Node> visited = new ArrayList<Node>(); ArrayList<Node> path = new ArrayList<Node>(); ArrayList<Node> neighbour = new ArrayList<Node>(); ArrayList<Node> newPath = new ArrayList<Node>(); ArrayList<Node> validPath = new ArrayList<Node>(); localShortestPath = new ArrayList<Node>(); //memasukkan source ke queue queue.offer(source); //pop queue while (!queue.isEmpty()) { //memasukkan isi queue ke path path.add(queue.pollFirst()); //mengambil isi listpath Node vertex = path.get(path.size() - 1); //cek apa vertex adalah tujuan if (vertex.getId().equals(dest.getId())) { int i = path.size() - 1; boolean pathFound = true; while (pathFound) { if (!path.get(i).getId().equals(source.getId())) { localShortestPath.add(path.get(i)); } else { localShortestPath.add(path.get(i)); pathFound = false; } i--; } Collections.reverse(localShortestPath); System.out.println("Valid Path : " + localShortestPath); return; } else if (!visited.contains(vertex)) { neighbour = new ArrayList<Node>(); //mengambil semua tetangga for (Iterator<Node> it = g.getNode(vertex.getId()).getNeighborNodeIterator(); it.hasNext();) { neighbour.add(it.next()); } newPath = new ArrayList<Node>(); for (Node n : neighbour) { for (Node nd : path) { newPath.add(nd); } newPath.add(n); for (Node ndn : newPath) { queue.offer(ndn); } } //menandai node sudah dikunjungi visited.add(vertex); } } } }