org.apache.giraph.graph.Vertex.addEdge()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(154)

本文整理了Java中org.apache.giraph.graph.Vertex.addEdge()方法的一些代码示例,展示了Vertex.addEdge()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vertex.addEdge()方法的具体详情如下:
包路径:org.apache.giraph.graph.Vertex
类名称:Vertex
方法名:addEdge

Vertex.addEdge介绍

[英]Add an edge for this vertex (happens immediately)
[中]为该顶点添加边(立即发生)

代码示例

代码示例来源:origin: org.apache.giraph/giraph-core

  1. /**
  2. * Add edges to the Vertex.
  3. *
  4. * @param vertex Vertex to add edges to
  5. * @param vertexChanges contains edges to add
  6. */
  7. protected void addEdges(Vertex<I, V, E> vertex,
  8. VertexChanges<I, V, E> vertexChanges) {
  9. if (vertex == null) {
  10. return;
  11. }
  12. if (hasEdgeAdditions(vertexChanges)) {
  13. for (Edge<I, E> edge : vertexChanges.getAddedEdgeList()) {
  14. vertex.addEdge(edge);
  15. }
  16. }
  17. }

代码示例来源:origin: org.apache.giraph/giraph-core

  1. /**
  2. * Add an edge to an existing vertex
  3. *
  4. * @param vertexId Edge origin
  5. * @param edgePair The edge
  6. * @return this
  7. */
  8. public TestGraph<I, V, E> addEdge(I vertexId, Entry<I, E> edgePair) {
  9. if (!vertices.containsKey(vertexId)) {
  10. Vertex<I, V, E> v = conf.createVertex();
  11. v.initialize(vertexId, conf.createVertexValue());
  12. vertices.put(vertexId, v);
  13. }
  14. vertices.get(vertexId)
  15. .addEdge(EdgeFactory.create(edgePair.getKey(),
  16. edgePair.getValue()));
  17. return this;
  18. }

代码示例来源:origin: org.apache.giraph/giraph-core

  1. /**
  2. * Add an edge to an existing vertex
  3. *
  4. * @param vertexId Edge origin
  5. * @param toVertex Edge destination
  6. * @param edgeValue Edge value
  7. * @return this
  8. */
  9. public TestGraph<I, V, E> addEdge(I vertexId, I toVertex, E edgeValue) {
  10. if (!vertices.containsKey(vertexId)) {
  11. Vertex<I, V, E> v = conf.createVertex();
  12. v.initialize(vertexId, conf.createVertexValue());
  13. vertices.put(vertexId, v);
  14. }
  15. vertices.get(vertexId)
  16. .addEdge(EdgeFactory.create(toVertex, edgeValue));
  17. return this;
  18. }
  19. /**

代码示例来源:origin: org.apache.giraph/giraph-core

  1. /**
  2. * Add vertex
  3. *
  4. * @param vertex Vertex
  5. * @return this
  6. */
  7. public TestGraph<I, V, E> addVertex(Vertex<I, V, E> vertex) {
  8. Vertex<I, V, E> previousVertex = vertices.get(vertex.getId());
  9. if (previousVertex != null) {
  10. vertexValueCombiner.combine(previousVertex.getValue(), vertex.getValue());
  11. for (Edge<I, E> edge : vertex.getEdges()) {
  12. previousVertex.addEdge(edge);
  13. }
  14. } else {
  15. vertices.put(vertex.getId(), vertex);
  16. }
  17. return this;
  18. }

代码示例来源:origin: grafos-ml/okapi

  1. @Override
  2. public void compute(Vertex<I, V, E> vertex,
  3. Iterable<MessageWrapper<I,M1>> messages) throws IOException {
  4. for (MessageWrapper<I,M1> msg : messages) {
  5. E edgeValue = vertex.getEdgeValue(msg.getSourceId());
  6. if (edgeValue == null) {
  7. I clonedId = null;
  8. E clonedEdgeValue = null;
  9. try {
  10. clonedId = (I)msg.getSourceId().getClass().newInstance();
  11. clonedEdgeValue = (E)msg.getMessage().getClass().newInstance();
  12. } catch (InstantiationException e) {
  13. throw new IOException(e);
  14. } catch (IllegalAccessException e) {
  15. throw new IOException(e);
  16. }
  17. ReflectionUtils.copy(
  18. getContext().getConfiguration(), msg.getSourceId(), clonedId);
  19. ReflectionUtils.copy(
  20. getContext().getConfiguration(), msg.getMessage(), clonedEdgeValue);
  21. Edge<I, E> edge = EdgeFactory.create(clonedId, clonedEdgeValue);
  22. vertex.addEdge(edge);
  23. }
  24. }
  25. }
  26. }

代码示例来源:origin: grafos-ml/okapi

  1. public void sampleIrrelevantEdges(
  2. Vertex<CfLongId, FloatMatrixWritable, BooleanWritable> vertex) {
  3. if (vertex.getId().isUser()){//only users
  4. Iterable<Edge<CfLongId, BooleanWritable>> edges = vertex.getEdges();
  5. HashSet<CfLongId> relevant = new HashSet<CfLongId>();
  6. for (Edge<CfLongId, BooleanWritable> e : edges) {
  7. relevant.add(e.getTargetVertexId());
  8. }
  9. for (int i=0; i<numberSamples; i++){
  10. CfLongId random = getRandomItemId(relevant);
  11. vertex.addEdge(EdgeFactory.create(random, new BooleanWritable(false)));
  12. }
  13. }
  14. }

代码示例来源:origin: grafos-ml/okapi

  1. @Override
  2. public void compute(
  3. Vertex<LongWritable, VertexValue, EdgeValue> vertex,
  4. Iterable<LongWritable> messages) throws IOException {
  5. for (LongWritable other : messages) {
  6. EdgeValue edgeValue = vertex.getEdgeValue(other);
  7. if (edgeValue == null) {
  8. edgeValue = new EdgeValue();
  9. edgeValue.setWeight((byte) 1);
  10. Edge<LongWritable, EdgeValue> edge = EdgeFactory.create(
  11. new LongWritable(other.get()), edgeValue);
  12. vertex.addEdge(edge);
  13. } else {
  14. edgeValue = new EdgeValue();
  15. edgeValue.setWeight(edgeWeight);
  16. vertex.setEdgeValue(other, edgeValue);
  17. }
  18. }
  19. }

代码示例来源:origin: org.apache.giraph/giraph-core

  1. @Override
  2. public boolean putOrCombine(Vertex<I, V, E> vertex) {
  3. Vertex<I, V, E> originalVertex = vertexMap.get(vertex.getId());
  4. if (originalVertex == null) {
  5. originalVertex =
  6. vertexMap.putIfAbsent(vertex.getId(), vertex);
  7. if (originalVertex == null) {
  8. return true;
  9. }
  10. }
  11. synchronized (originalVertex) {
  12. // Combine the vertex values
  13. getVertexValueCombiner().combine(
  14. originalVertex.getValue(), vertex.getValue());
  15. // Add the edges to the representative vertex
  16. for (Edge<I, E> edge : vertex.getEdges()) {
  17. originalVertex.addEdge(edge);
  18. }
  19. }
  20. return false;
  21. }

代码示例来源:origin: org.apache.giraph/giraph-core

  1. /**
  2. * Combine two vertices together and store the serialized bytes
  3. * in the vertex map.
  4. *
  5. * @param representativeVertex existing vertex
  6. * @param representativeCombinerVertex new vertex to combine
  7. */
  8. private void combine(Vertex<I, V, E> representativeVertex,
  9. Vertex<I, V, E> representativeCombinerVertex) {
  10. getVertexValueCombiner().combine(representativeVertex.getValue(),
  11. representativeCombinerVertex.getValue());
  12. // Add the edges to the representative vertex
  13. for (Edge<I, E> edge : representativeCombinerVertex.getEdges()) {
  14. representativeVertex.addEdge(edge);
  15. }
  16. vertexMap.put(representativeCombinerVertex.getId(),
  17. WritableUtils.writeVertexToByteArray(
  18. representativeVertex, useUnsafeSerialization, getConf()));
  19. }

代码示例来源:origin: grafos-ml/okapi

  1. @Override
  2. public void compute(Vertex<CfLongId, FloatMatrixWritable, FloatWritable> vertex,
  3. Iterable<FloatMatrixMessage> messages) throws IOException {
  4. FloatMatrixWritable vector =
  5. new FloatMatrixWritable(getContext().getConfiguration().getInt(
  6. VECTOR_SIZE, VECTOR_SIZE_DEFAULT));
  7. Random randGen = new Random();
  8. for (int i=0; i<vector.length; i++) {
  9. vector.put(i, 0.01f*randGen.nextFloat());
  10. }
  11. vertex.setValue(vector);
  12. for (FloatMatrixMessage msg : messages) {
  13. DefaultEdge<CfLongId, FloatWritable> edge =
  14. new DefaultEdge<CfLongId, FloatWritable>();
  15. edge.setTargetVertexId(msg.getSenderId());
  16. edge.setValue(new FloatWritable(msg.getScore()));
  17. vertex.addEdge(edge);
  18. }
  19. // The score does not matter at this point.
  20. sendMessageToAllEdges(vertex,
  21. new FloatMatrixMessage(vertex.getId(), vertex.getValue(), 0.0f));
  22. vertex.voteToHalt();
  23. }
  24. }

代码示例来源:origin: grafos-ml/okapi

  1. @Override
  2. public void compute(Vertex<CfLongId, FloatMatrixWritable,
  3. FloatWritable> vertex, Iterable<FloatMatrixMessage> messages)
  4. throws IOException {
  5. FloatMatrixWritable vector =
  6. new FloatMatrixWritable(getContext().getConfiguration().getInt(
  7. VECTOR_SIZE, VECTOR_SIZE_DEFAULT));
  8. Random randGen = new Random();
  9. for (int i=0; i<vector.length; i++) {
  10. vector.put(i, 0.01f*randGen.nextFloat());
  11. }
  12. vertex.setValue(vector);
  13. for (FloatMatrixMessage msg : messages) {
  14. DefaultEdge<CfLongId, FloatWritable> edge =
  15. new DefaultEdge<CfLongId, FloatWritable>();
  16. edge.setTargetVertexId(msg.getSenderId());
  17. edge.setValue(new FloatWritable(msg.getScore()));
  18. vertex.addEdge(edge);
  19. }
  20. // The score does not matter at this point.
  21. sendMessageToAllEdges(vertex,
  22. new FloatMatrixMessage(vertex.getId(), vertex.getValue(), 0.0f));
  23. vertex.voteToHalt();
  24. }
  25. }

代码示例来源:origin: org.apache.giraph/giraph-examples

  1. " on superstep " + getSuperstep());
  2. vertex.addEdge(newEdge);
  3. sendMessage(edge.getTargetVertexId(),
  4. new VerifiableMessage(

代码示例来源:origin: org.apache.giraph/giraph-examples

  1. " to vertex " + edge.getTargetVertexId() +
  2. " on superstep " + getSuperstep());
  3. vertex.addEdge(newEdge);
  4. sendMessage(edge.getTargetVertexId(), newEdgeValue);

代码示例来源:origin: grafos-ml/okapi

  1. edge.setTargetVertexId(msg.getSenderId());
  2. edge.setValue(new FloatWritable(msg.getScore()));
  3. vertex.addEdge(edge);

相关文章