// For adding Edge To Graph public void AddEdgeToGraph( String First_IP, String Second_IP, String edge_details, boolean IsSwitch_1, boolean IsSwitch_2) { try { String temp_1 = "OpenVSwitch"; String temp_2 = "OpenVSwitch"; if (!IsSwitch_1) { temp_1 = "EndHost"; } if (!IsSwitch_2) { temp_2 = "EndHost"; } String node_name_1 = temp_1 + " [" + First_IP + "]"; String node_name_2 = temp_2 + " [" + Second_IP + "]"; network_graph.addEdge(edge_details, node_name_1, node_name_2); Edge edge = network_graph.getEdge(edge_details); edge.addAttribute("ui.label", edge.getId()); } catch (Exception e) { } }
@SuppressWarnings("unchecked") private void pathSetShortestPath_facilitate(Node current, Path path, List<Path> paths) { Node source = graph.getNode(this.source_id); if (current != source) { Node next = null; ArrayList<? extends Edge> predecessors = (ArrayList<? extends Edge>) current.getAttribute(identifier + ".predecessors"); while (current != source && predecessors.size() == 1) { Edge e = predecessors.get(0); next = e.getOpposite(current); path.add(current, e); current = next; predecessors = (ArrayList<? extends Edge>) current.getAttribute(identifier + ".predecessors"); } if (current != source) { for (Edge e : predecessors) { Path p = path.getACopy(); p.add(current, e); pathSetShortestPath_facilitate(e.getOpposite(current), p, paths); } } } if (current == source) { paths.add(path); } }
public int testGraphIterators() { int foo = 0; // Iterating on all nodes start = System.currentTimeMillis(); Iterator<Node> nodeIt = g.getNodeIterator(); while (nodeIt.hasNext()) { Node n = nodeIt.next(); if (n.hasAttribute("foo")) foo++; } end = System.currentTimeMillis(); measureValues.put(Measures.GRAPH_NODE_IT, end - start); // iterating on all edges start = System.currentTimeMillis(); Iterator<Edge> edgeIt = g.getEdgeIterator(); while (edgeIt.hasNext()) { Edge e = edgeIt.next(); if (e.hasAttribute("foo")) foo++; } end = System.currentTimeMillis(); measureValues.put(Measures.GRAPH_EDGE_IT, end - start); return foo; }
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; }
private void defaultVisit(CFrame frame) { MultiGraph graph = this.view.getGraph(); graph.getNodeSet().clear(); graph.getEdgeSet().clear(); // this.baseIRIList = new HashSet<String>(); // Получение всех элементов и формирование из них визуального части. for (OWLNamedIndividual owlName : frame.getContent().getConcepts()) { String label = this.model.getLabel(owlName); if (label.equals("_EMPTY_")) { label = owlName.getIRI().getFragment().toString(); } Node node = graph.addNode(owlName.getIRI().toString()); node.addAttribute("OWLNamedIndividual", owlName); node.addAttribute("ui.label", label); if (owlName.getIRI().equals(frame.getTrgConcept().getIRI())) { node.setAttribute("ui.class", "concept"); } } // Связывает все элементы линиями. for (Branch br : frame.getContent().getBranches()) { Node nodeSub = null; Node nodeObj = null; for (Node node : graph.getNodeSet()) { if (node.getId().equals(br.getSubject().getIRI().toString())) { nodeSub = node; continue; } else { if (node.getId().equals(br.getObject().getIRI().toString())) { nodeObj = node; continue; } } if (nodeSub != null && nodeObj != null) { break; } } // Создает элемент линия для двух node. Edge edge = graph.addEdge(br.getBrachIndIRI().toString(), nodeSub, nodeObj, true); String labelEdge = this.model.getAnnotationValue(br.getPrp().getIRI()); if (labelEdge.equals("_EMPTY_")) { labelEdge = br.getPrp().getIRI().getFragment().toString(); } edge.addAttribute("ui.label", labelEdge); } }
public <T extends Edge> T getEdge(int index) throws IndexOutOfBoundsException { Edge e; elementLock.lock(); e = wrappedElement.getEdge(index); elementLock.unlock(); return e == null ? null : this.<T>getEdge(e.getId()); }
@SuppressWarnings("all") public <T extends Edge> T getEdgeToward(String id) { List<? extends Edge> edges = mygraph.connectivity.get(this); for (Edge edge : edges) { if (edge.getOpposite(this).getId().equals(id)) return (T) edge; } return null; }
/** * Like {@link #edgeLength(Graph,String)} but use an existing edge as argument. * * @param edge * @return The edge length or -1 if the nodes of the edge have no positions. */ public static double edgeLength(Edge edge) { double xyz0[] = nodePosition(edge.getNode0()); double xyz1[] = nodePosition(edge.getNode1()); if (xyz0 == null || xyz1 == null) return -1; xyz0[0] = xyz1[0] - xyz0[0]; xyz0[1] = xyz1[1] - xyz0[1]; xyz0[2] = xyz1[2] - xyz0[2]; return Math.sqrt(xyz0[0] * xyz0[0] + xyz0[1] * xyz0[1] + xyz0[2] * xyz0[2]); }
SynchronizedGraph(Graph g) { super(g); elementLock = new ReentrantLock(); synchronizedNodes = new HashMap<String, Node>(); synchronizedEdges = new HashMap<String, Edge>(); for (Node n : g.getEachNode()) synchronizedNodes.put(n.getId(), new SynchronizedNode(this, n)); for (Edge e : g.getEachEdge()) synchronizedEdges.put(e.getId(), new SynchronizedEdge(this, e)); }
@Override public void computeNode(Node node) { /* * Recall and update the node current community and previous score */ Object previousCommunity = node.getAttribute(marker); Double previousScore = (Double) node.getAttribute(marker + ".score"); super.computeNode(node); /* * Update the node label score */ // Handle first iteration if (previousCommunity == null) { previousCommunity = node.getAttribute(marker); previousScore = (Double) node.getAttribute(marker + ".score"); } /* * The node is the originator of the community and hasn't changed * community at this iteration (or we are at the first simulation step): * keep the maximum label score */ if ((node.getAttribute(marker).equals(previousCommunity)) && (previousScore.equals(1.0))) node.setAttribute(marker + ".score", 1.0); /* * Otherwise search for the highest score amongst neighbors and reduce * it by decreasing factor */ else { Double maxLabelScore = Double.NEGATIVE_INFINITY; for (Edge e : node.getEnteringEdgeSet()) { Node v = e.getOpposite(node); if (v.hasAttribute(marker) && v.getAttribute(marker).equals(node.getAttribute(marker))) { if ((Double) v.getAttribute(marker + ".score") > maxLabelScore) maxLabelScore = (Double) v.getAttribute(marker + ".score"); } } node.setAttribute(marker + ".score", maxLabelScore - delta); } }
/** * Compute the scores for all relevant communities for the selected node using Leung algorithm. * * @param u The node for which the scores computation is performed * @complexity O(DELTA) where DELTA is is the average node degree in the network */ @Override protected void communityScores(Node u) { /* * Reset the scores for each communities */ communityScores = new HashMap<Object, Double>(); /* * Iterate over the nodes that this node "hears" */ for (Edge e : u.getEnteringEdgeSet()) { Node v = e.getOpposite(u); /* * Update the count for this community */ if (v.hasAttribute(marker)) { // Compute the neighbor node current score Double score = (Double) v.getAttribute(marker + ".score") * Math.pow(v.getInDegree(), m); /* * The rest of the formula depends on the weighted status of the * network */ Double weight; if (e.hasAttribute(weightMarker)) if (e.isDirected()) { Edge e2 = v.getEdgeToward(u.getId()); if (e2 != null && e2.hasAttribute(weightMarker)) weight = (Double) e.getAttribute(weightMarker) + (Double) e2.getAttribute(weightMarker); else weight = (Double) e.getAttribute(weightMarker); } else weight = (Double) e.getAttribute(weightMarker); else weight = 1.0; // Update the score of the according community if (communityScores.get(v.getAttribute(marker)) == null) communityScores.put(v.getAttribute(marker), score * weight); else communityScores.put( v.getAttribute(marker), communityScores.get(v.getAttribute(marker)) + (score * weight)); } } }
/** * Returns the shortest path between the source node and one given target. If multiple shortest * paths exist, one of them is returned at random. * * @param target the target of the shortest path starting at the source node given in the * constructor. * @return A {@link org.graphstream.graph.Path} object that constrains the list of nodes and edges * that constitute it. */ @SuppressWarnings("unchecked") public Path getShortestPath(Node target) { Path p = new Path(); if (target == source) { return p; } boolean noPath = false; Node v = target; while (v != source && !noPath) { ArrayList<? extends Edge> list = (ArrayList<? extends Edge>) v.getAttribute(identifier + ".predecessors"); if (list == null) { noPath = true; } else { Edge parentEdge = list.get(0); p.add(v, parentEdge); v = parentEdge.getOpposite(v); } } return p; }
public int testAccessById() { int foo = 0; // access each node by id start = System.currentTimeMillis(); for (String id : nodeIds) { Node n = g.getNode(id); if (n.hasAttribute("foo")) foo++; } end = System.currentTimeMillis(); measureValues.put(Measures.NODE_BY_ID, end - start); // access each edge by id start = System.currentTimeMillis(); for (String id : edgeIds) { Edge e = g.getEdge(id); if (e.hasAttribute("foo")) foo++; } end = System.currentTimeMillis(); measureValues.put(Measures.EDGE_BY_ID, end - start); return foo; }
@Override protected void makeTree() { if (treeEdges == null) treeEdges = new LinkedList<Edge>(); else treeEdges.clear(); int n = graph.getNodeCount(); Data[] data = new Data[n]; FibonacciHeap<Double, Node> heap = new FibonacciHeap<Double, Node>(); for (int i = 0; i < n; i++) { data[i] = new Data(); data[i].edgeToTree = null; data[i].fn = heap.add(Double.POSITIVE_INFINITY, graph.getNode(i)); } treeWeight = 0; while (!heap.isEmpty()) { Node u = heap.extractMin(); Data dataU = data[u.getIndex()]; data[u.getIndex()] = null; if (dataU.edgeToTree != null) { treeEdges.add(dataU.edgeToTree); edgeOn(dataU.edgeToTree); treeWeight += dataU.fn.getKey(); dataU.edgeToTree = null; } dataU.fn = null; for (Edge e : u) { Node v = e.getOpposite(u); Data dataV = data[v.getIndex()]; if (dataV == null) continue; double w = getWeight(e); if (w < dataV.fn.getKey()) { heap.decreaseKey(dataV.fn, w); dataV.edgeToTree = e; } } } }
public int testFindEdge() { int foo = 0; // for each pair of nodes (n1, n2) find the edge between n1 and n2 long start = System.currentTimeMillis(); for (String id1 : nodeIds) { Node n1 = g.getNode(id1); for (String id2 : nodeIds) { Edge e = n1.getEdgeBetween(id2); if (e != null && e.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.EDGE_BETWEEN, end - start); // for each pair of nodes (n1, n2) find the edge from n1 to n2 start = System.currentTimeMillis(); for (String id1 : nodeIds) { Node n1 = g.getNode(id1); for (String id2 : nodeIds) { Edge e = n1.getEdgeToward(id2); if (e != null && e.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.EDGE_TOWARD, end - start); // for each pair of nodes (n1, n2) find the edge from n2 to n1 start = System.currentTimeMillis(); for (String id1 : nodeIds) { Node n1 = g.getNode(id1); for (String id2 : nodeIds) { Edge e = n1.getEdgeFrom(id2); if (e != null && e.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.EDGE_FROM, end - start); return foo; }
/* * (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())); } } }
/** * Get the style of a given edge. * * @param edge The edge to search for. * @return The edge style. */ public StyleGroup getStyleFor(Edge edge) { String gid = byEdgeIdGroups.get(edge.getId()); return groups.get(gid); }
protected void coupling() { for (Edge edge : getEnteringEdgeSet()) { KuramotoNode neighbor = edge.getOpposite(this); nextKTheta += Parameters.COUPLING_STRENGTH * Math.sin(neighbor.tmpTheta - tmpTheta); } }
public int testNodeIterators() { int foo = 0; // For each node n, iterating on all edges of n start = System.currentTimeMillis(); Iterator<Node> nodeIt = g.getNodeIterator(); while (nodeIt.hasNext()) { Node n = nodeIt.next(); Iterator<Edge> edgeIt = n.getEdgeIterator(); while (edgeIt.hasNext()) { Edge e = edgeIt.next(); if (e.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.NODE_EDGE_IT, end - start); // For each node n, iterating on all entering edges of n start = System.currentTimeMillis(); nodeIt = g.getNodeIterator(); while (nodeIt.hasNext()) { Node n = nodeIt.next(); Iterator<Edge> edgeIt = n.getEnteringEdgeIterator(); while (edgeIt.hasNext()) { Edge e = edgeIt.next(); if (e.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.NODE_ENTERING_EDGE_IT, end - start); // For each node n, iterating on all leaving edges of n start = System.currentTimeMillis(); nodeIt = g.getNodeIterator(); while (nodeIt.hasNext()) { Node n = nodeIt.next(); Iterator<Edge> edgeIt = n.getLeavingEdgeIterator(); while (edgeIt.hasNext()) { Edge e = edgeIt.next(); if (e.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.NODE_LEAVING_EDGE_IT, end - start); // For each node n, iterating on all neighbors of n start = System.currentTimeMillis(); nodeIt = g.getNodeIterator(); while (nodeIt.hasNext()) { Node n = nodeIt.next(); Iterator<Node> neighborIt = n.getNeighborNodeIterator(); while (neighborIt.hasNext()) { Node neighbor = neighborIt.next(); if (neighbor.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.NODE_NEIGHBOR_IT, end - start); // For each node n, iterating on all edges of n using n.getEdge(i) start = System.currentTimeMillis(); nodeIt = g.getNodeIterator(); while (nodeIt.hasNext()) { Node n = nodeIt.next(); for (int i = 0; i < n.getDegree(); i++) { Edge e = n.getEdge(i); if (e.hasAttribute("foo")) foo++; } } end = System.currentTimeMillis(); measureValues.put(Measures.NODE_GET_EDGE, end - start); return foo; }