public BenchPerformance(String fileName, Graph graph) { r = Runtime.getRuntime(); forceGC(); long used1 = r.totalMemory() - r.freeMemory(); g = graph; try { g.read(fileName); } catch (Exception e) { e.printStackTrace(); System.exit(0); } System.out.println( "Graph read: " + g.getNodeCount() + " nodes and " + g.getEdgeCount() + " edges"); for (Node n : g) n.clearAttributes(); for (Edge e : g.getEachEdge()) e.clearAttributes(); forceGC(); long used2 = r.totalMemory() - r.freeMemory(); measureValues = new EnumMap<Measures, Long>(Measures.class); measureValues.put(Measures.MEMORY, used2 - used1); nodeIds = new ArrayList<String>(g.getNodeCount()); for (Node n : g) nodeIds.add(n.getId()); // sort them to be sure that we always work with the same nodes Collections.sort(nodeIds); edgeIds = new ArrayList<String>(g.getEdgeCount()); for (Edge e : g.getEachEdge()) edgeIds.add(e.getId()); Collections.sort(edgeIds); }
/** * Clone a given graph with same node/edge structure and same attributes. * * @param g the graph to clone * @return a copy of g */ public static Graph clone(Graph g) { Graph copy; try { Class<? extends Graph> cls = g.getClass(); copy = cls.getConstructor(String.class).newInstance(g.getId()); } catch (Exception e) { logger.warning(String.format("Cannot create a graph of %s.", g.getClass().getName())); copy = new AdjacencyListGraph(g.getId()); } copyAttributes(g, copy); for (int i = 0; i < g.getNodeCount(); i++) { Node source = g.getNode(i); Node target = copy.addNode(source.getId()); copyAttributes(source, target); } for (int i = 0; i < g.getEdgeCount(); i++) { Edge source = g.getEdge(i); Edge target = copy.addEdge( source.getId(), source.getSourceNode().getId(), source.getTargetNode().getId(), source.isDirected()); copyAttributes(source, target); } return copy; }
public void execute() { // Toolkit.c final Graph graph = new SingleGraph("roberto"); graph.setStrict(false); graph.addAttribute("ui.stylesheet", "node.important {fill-color: red;}"); graph.addAttribute("ui.quality"); graph.addAttribute("ui.antialias"); BaseGenerator gen = null; // Between 1 and 3 new links per node added. // gen = new BarabasiAlbertGenerator(1);//talvez // gen = new DorogovtsevMendesGenerator();//talvez // gen = new PointsOfInterestGenerator();//gera desconexo // gen = new PreferentialAttachmentGenerator();//parece com lobster // gen = new RandomEuclideanGenerator(2);//+- // gen = new RandomFixedDegreeDynamicGraphGenerator(200, 9.0, // 0.5);//acho que não // gen = new WattsStrogatzGenerator(200, 4, 0.1);//+- // gen = new ClusterGeneratorOld(200); int n = 100; // gen = new LobsterGenerator(5, 5);// sim // gen = new CustomLobsterGenerator(500, 3, 5);// sim // gen = new CustomFlowerSnarkGenerator(100);//sim // gen = new ClusterGenerator(n, 3, 3); //sim // gen = new CustomGridGenerator(110, true, 0.9F, 15, 5);//usado para // gen = new WattsStrogatzGenerator(20, 10, 0.1);//pode ser mas tem que // rodar 2 vezes // gen = new BarabasiAlbertGenerator(1);//talvez - parecido com lobster // gen = new DorogovtsevMendesGenerator();//talvez gen.setRandomSeed(12345); gen.addSink(graph); gen.begin(); while (gen.nextEvents()) ; gen.end(); System.out.println("" + graph.getNodeCount() + " " + graph.getEdgeCount()); // gen.configureNodes(graph); // System.out.println("Amount of important: " + gen.countImportant); Viewer viewer = graph.display(true); // Layout layoutAlgorithm = new SpringBox(); // viewer.enableAutoLayout(layoutAlgorithm); viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.HIDE_ONLY); ViewerListener vl = new MyViewListener(graph, n, 2, gen); ViewerPipe fromViewer = viewer.newViewerPipe(); fromViewer.addViewerListener(vl); fromViewer.addSink(graph); while (loop) { fromViewer.pump(); // or fromViewer.blockingPump(); try { Thread.sleep(100); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } // try { // Thread.sleep(10000); // } catch (InterruptedException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } }
/* * (non-Javadoc) * * @see org.graphstream.algorithm.Algorithm#compute() */ @SuppressWarnings("unchecked") public void compute() { Node source = graph.getNode(this.source_id); // Step 1: Initialize graph for (Node n : graph) { if (n == source) n.addAttribute(identifier + ".distance", 0.0); else n.addAttribute(identifier + ".distance", Double.POSITIVE_INFINITY); // n.addAttribute(identifier+".predecessors",(Object)null); } // Step 2: relax edges repeatedly for (int i = 0; i < graph.getNodeCount(); i++) { for (Edge e : graph.getEachEdge()) { Node n0 = e.getNode0(); Node n1 = e.getNode1(); Double d0 = (Double) n0.getAttribute(identifier + ".distance"); Double d1 = (Double) n1.getAttribute(identifier + ".distance"); Double we = (Double) e.getAttribute(weightAttribute); if (we == null) throw new NumberFormatException( "org.graphstream.algorithm.BellmanFord: Problem with attribute \"" + weightAttribute + "\" on edge " + e); if (d0 != null) { if (d1 == null || d1 >= d0 + we) { n1.addAttribute(identifier + ".distance", d0 + we); ArrayList<Edge> predecessors = (ArrayList<Edge>) n1.getAttribute(identifier + ".predecessors"); if (d1 != null && d1 == d0 + we) { if (predecessors == null) { predecessors = new ArrayList<Edge>(); } } else { predecessors = new ArrayList<Edge>(); } if (!predecessors.contains(e)) { predecessors.add(e); } n1.addAttribute(identifier + ".predecessors", predecessors); } } } } // Step 3: check for negative-weight cycles for (Edge e : graph.getEachEdge()) { Node n0 = e.getNode0(); Node n1 = e.getNode1(); Double d0 = (Double) n0.getAttribute(identifier + ".distance"); Double d1 = (Double) n1.getAttribute(identifier + ".distance"); Double we = (Double) e.getAttribute(weightAttribute); if (we == null) { throw new NumberFormatException( String.format( "%s: Problem with attribute \"%s\" on edge \"%s\"", BellmanFord.class.getName(), weightAttribute, e.getId())); } if (d1 > d0 + we) { throw new NumberFormatException( String.format( "%s: Problem: negative weight, cycle detected on edge \"%s\"", BellmanFord.class.getName(), e.getId())); } } }