org.apache.giraph.graph.Vertex类的使用及代码示例

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

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

Vertex介绍

[英]Class which holds vertex id, data and edges.
[中]类,该类保存顶点id、数据和边。

代码示例

代码示例来源: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: org.apache.giraph/giraph-core

  1. /**
  2. * The backing store of the current vertex id is now released.
  3. * Further calls to getCurrentVertexId () without calling next()
  4. * will return null.
  5. *
  6. * @return Current vertex id that was released
  7. */
  8. public I releaseCurrentVertexId() {
  9. I releasedVertexId = vertex.getId();
  10. vertex.initialize(null, vertex.getValue());
  11. return releasedVertexId;
  12. }

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

  1. @Override
  2. public void compute(
  3. Vertex<LongWritable, LongWritable, DoubleWritable> vertex,
  4. Iterable<DoubleWritable> messages) throws IOException {
  5. LongWritable vertexValue = vertex.getValue();
  6. vertexValue.set(vertex.getNumEdges());
  7. vertex.setValue(vertexValue);
  8. vertex.voteToHalt();
  9. }
  10. }

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

  1. @Override
  2. public void compute(Vertex<WritableComparable, Writable, Writable> vertex,
  3. Iterable<WritableComparable> messages) throws IOException {
  4. int count = 0;
  5. for (WritableComparable msg : messages) {
  6. // If this vertex has a neighbor with this ID, then this means it
  7. // participates in a triangle.
  8. if (vertex.getEdgeValue(msg)!=null) {
  9. count++;
  10. }
  11. }
  12. if (count>0) {
  13. vertex.setValue(new IntWritable(count));
  14. }
  15. vertex.voteToHalt();
  16. }
  17. }

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

  1. @Override
  2. public void compute(Vertex<CfLongId, FloatMatrixWritable, FloatWritable> vertex, Iterable<FloatMatrixMessage> messages) throws IOException {
  3. vertex.setValue(new FloatMatrixWritable(FloatMatrix.rand(DIM)));
  4. if(getSuperstep() == 0){
  5. Iterable<Edge<CfLongId, FloatWritable>> edges = vertex.getEdges();
  6. sendMessage(vertex.getId(), emptyMsg); //send message to myself in order to be executed in the next super step
  7. for (Edge<CfLongId, FloatWritable> edge : edges) {
  8. sendMessage(edge.getTargetVertexId(), new FloatMatrixMessage(vertex.getId(), emptyList, edge.getValue().get()));
  9. }
  10. }
  11. vertex.voteToHalt();
  12. }

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

  1. @Override
  2. public void compute(Vertex<LongWritable, Writable, DoubleWritable> vertex,
  3. Iterable<LongWritable> messages) throws IOException {
  4. for (LongWritable msg : messages) {
  5. assert(msg.compareTo(vertex.getId())<0); // This can never happen
  6. double weight = vertex.getEdgeValue(msg).get();
  7. // This means there is an edge:
  8. // 1) FROM vertex with ID=msg.get()
  9. // 2) TO vertex with ID=vertex.getId().get()
  10. // 3) with the specified weight.
  11. SimpleEdge t = new SimpleEdge(msg.get(), vertex.getId().get(), weight);
  12. for (Edge<LongWritable, DoubleWritable> edge: vertex.getEdges()) {
  13. if (vertex.getId().compareTo(edge.getTargetVertexId()) < 0) {
  14. sendMessage(edge.getTargetVertexId(), t);
  15. }
  16. }
  17. }
  18. vertex.voteToHalt();
  19. }
  20. }

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

  1. @Override
  2. protected Text convertVertexToLine(
  3. Vertex<CfLongId, FloatMatrixWritable, BooleanWritable> vertex)
  4. throws IOException {
  5. if (outputEdge.equals(vertex.getId())){
  6. return new Text(vertex.getValue().toString());
  7. }
  8. return null;
  9. }
  10. };

代码示例来源:origin: apache/giraph

  1. for (Vertex<I, V, E> vertex : partition) {
  2. Iterable messages =
  3. internalApi.takeMessages(vertex.getId());
  4. if (vertex.isHalted() && !Iterables.isEmpty(messages)) {
  5. vertex.wakeUp();
  6. if (!vertex.isHalted()) {
  7. localLogic.compute(vertex, messages);
  8. vertex.unwrapMutableEdges();
  9. if (!vertex.isHalted()) {
  10. anyCurVertexAlive = true;

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

  1. protected void initFactorsIfNeeded(Vertex<CfLongId, FloatMatrixWritable, FloatWritable> vertex) {
  2. if (null == vertex.getValue() || vertex.getValue().columns != d){
  3. vertex.setValue(new FloatMatrixWritable(FloatMatrix.rand(d)));
  4. }
  5. }

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

  1. /**
  2. * Writes Vertex data to output stream.
  3. *
  4. * @param output the output stream
  5. * @param vertex The vertex to serialize
  6. * @param conf Configuration
  7. * @param <I> Vertex id
  8. * @param <V> Vertex value
  9. * @param <E> Edge value
  10. * @throws IOException
  11. */
  12. @SuppressWarnings("unchecked")
  13. public static <I extends WritableComparable, V extends Writable,
  14. E extends Writable> void writeVertexToDataOutput(
  15. DataOutput output,
  16. Vertex<I, V, E> vertex,
  17. ImmutableClassesGiraphConfiguration<I, V, E> conf)
  18. throws IOException {
  19. vertex.getId().write(output);
  20. vertex.getValue().write(output);
  21. ((OutEdges<I, E>) vertex.getEdges()).write(output);
  22. output.writeBoolean(vertex.isHalted());
  23. }

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

  1. @Override
  2. public void compute(
  3. Vertex<LongWritable, VertexValue, EdgeValue> vertex,
  4. Iterable<PartitionMessage> messages) throws IOException {
  5. short partition = vertex.getValue().getCurrentPartition();
  6. if (partition == -1) {
  7. partition = (short) rnd.nextInt(numberOfPartitions);
  8. }
  9. aggregate(loadAggregatorNames[partition],
  10. new LongWritable(vertex.getNumEdges()));
  11. vertex.getValue().setCurrentPartition(partition);
  12. vertex.getValue().setNewPartition(partition);
  13. PartitionMessage message = new PartitionMessage(vertex.getId()
  14. .get(), partition);
  15. sendMessageToAllEdges(vertex, message);
  16. }

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

  1. @Override
  2. public void compute(Vertex<WritableComparable, Writable, Writable> vertex,
  3. Iterable<Writable> messages) throws IOException {
  4. for (Edge<WritableComparable, Writable> edge: vertex.getEdges()) {
  5. if (edge.getTargetVertexId().compareTo(vertex.getId()) > 0) {
  6. sendMessage(edge.getTargetVertexId(), vertex.getId());
  7. }
  8. }
  9. vertex.voteToHalt();
  10. }
  11. }

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

  1. /**
  2. * We override this function as we need a special treatment for item biases. See the class documentation for the explanation.
  3. */
  4. protected void initFactorsIfNeeded(Vertex<CfLongId,FloatMatrixWritable,FloatWritable> vertex) {
  5. if (null == vertex.getValue() || vertex.getValue().columns != d+1){
  6. vertex.setValue(new FloatMatrixWritable(FloatMatrix.rand(d + 1)));
  7. }
  8. if (vertex.getId().isUser()){//In BPR the first factor of the user is always 1, its to have item baselines
  9. vertex.getValue().put(0, 1f);
  10. }
  11. }

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

  1. private void processUpdates(Vertex<LongWritable, IntWritable, MBMEdgeValue> vertex, Iterable<MBMMessage> messages) throws AssertionError {
  2. Set<LongWritable> toRemove = new HashSet<LongWritable>();
  3. int numIncluded = 0;
  4. for (MBMMessage msg : messages) {
  5. MBMEdgeValue edgeValue = vertex.getEdgeValue(msg.getId());
  6. if (edgeValue == null) {
  7. // edge has already been removed, do nothing
  8. if (LOG.isDebugEnabled())
  9. LOG.debug(String.format("Superstep %d Vertex %d: message for removed edge from vertex %d", getSuperstep(), vertex.getId().get(), msg
  10. .getId().get()));
  11. } else {
  12. if (msg.getState() == State.PROPOSED && edgeValue.getState() == State.PROPOSED) {
  13. edgeValue.setState(State.INCLUDED);
  14. numIncluded++;
  15. } else if (msg.getState() == State.REMOVED) {
  16. toRemove.add(msg.getId());
  17. }
  18. }
  19. }
  20. // update capacity
  21. vertex.getValue().set(vertex.getValue().get() - numIncluded);
  22. // remove edges locally
  23. for (LongWritable e : toRemove)
  24. vertex.removeEdges(e);
  25. if (LOG.isDebugEnabled())
  26. LOG.debug(String.format("Superstep %d Vertex %d: included %d edges, removed %d edges", getSuperstep(), vertex.getId().get(), numIncluded,
  27. toRemove.size()));
  28. }

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

  1. @Override
  2. public Text convertVertexToLine(Vertex<LongWritable, IntWritable, MBMEdgeValue> vertex) throws IOException {
  3. StringBuffer sb = new StringBuffer(vertex.getId().toString());
  4. sb.append(delimiter);
  5. sb.append(vertex.getValue());
  6. for (Edge<LongWritable, MBMEdgeValue> edge : vertex.getEdges()) {
  7. sb.append(delimiter).append(edge.getTargetVertexId());
  8. sb.append(delimiter).append(edge.getValue().getWeight());
  9. // skip the state, which is always INCLUDED
  10. }
  11. return new Text(sb.toString());
  12. }
  13. }

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

  1. @Override
  2. public void writeVertex(
  3. Vertex<WritableComparable, Writable, Writable> vertex)
  4. throws IOException, InterruptedException {
  5. StringBuilder sb = new StringBuilder(vertex.getNumEdges() * 10);
  6. for (Edge<WritableComparable, Writable> edge : vertex.getEdges()) {
  7. addEdge(sb, vertex.getId(), edge);
  8. }
  9. addNodeInfo(vertex, sb);
  10. getRecordWriter().write(new Text(sb.toString()), null);
  11. }
  12. }

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

  1. @Override
  2. public void compute(Vertex<LongWritable, NullWritable, DoubleBooleanPair> vertex,
  3. Iterable<LongWritable> messages) throws IOException {
  4. // handle set-semimetric-label messages
  5. for (LongWritable trg: messages) {
  6. if (vertex.getEdgeValue(trg) != null) {
  7. vertex.setEdgeValue(trg, vertex.getEdgeValue(trg).setSemimetric(true));
  8. }
  9. }
  10. for (Edge<LongWritable, DoubleBooleanPair> edge : vertex.getEdges()) {
  11. if (edge.getValue().isSemimetric()) {
  12. // remove edge
  13. removeEdgesRequest(vertex.getId(), edge.getTargetVertexId());
  14. }
  15. }
  16. }
  17. }

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

  1. /**
  2. * Writes vertex data (Id, value and halted state) to stream.
  3. *
  4. * @param output The output stream
  5. * @param vertex The vertex to serialize
  6. * @throws IOException
  7. */
  8. private void writeVertexData(DataOutput output, Vertex<I, V, E> vertex)
  9. throws IOException {
  10. vertex.getId().write(output);
  11. V value = vertex.getValue();
  12. if (value != null) {
  13. output.writeBoolean(false);
  14. value.write(output);
  15. } else {
  16. output.writeBoolean(true);
  17. }
  18. output.writeBoolean(vertex.isHalted());
  19. }

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

  1. @Override
  2. public void compute(
  3. Vertex<LongWritable, NullWritable, NullWritable> vertex,
  4. Iterable<LongWritable> messages) throws IOException {
  5. HashSet<LongWritable> toDelete = new HashSet<LongWritable>();
  6. for (LongWritable id : messages) {
  7. toDelete.add(new LongWritable(id.get()));
  8. }
  9. Iterator<MutableEdge<LongWritable,NullWritable>> edgeIterator =
  10. vertex.getMutableEdges().iterator();
  11. while (edgeIterator.hasNext()) {
  12. if (toDelete.contains(edgeIterator.next().getTargetVertexId())) {
  13. edgeIterator.remove();
  14. }
  15. }
  16. if (vertex.getNumEdges()<getConf().getInt(K_VALUE, K_VALUE_DEFAULT)) {
  17. sendMessageToAllEdges(vertex, vertex.getId());
  18. removeVertexRequest(vertex.getId());
  19. }
  20. vertex.voteToHalt();
  21. }
  22. }

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

  1. @Override
  2. public void compute(Vertex<IntWritable, IntWritable, NullWritable> vertex,
  3. Iterable<IntWritable> messages) throws IOException {
  4. boolean changed = false;
  5. for (IntWritable message : messages) {
  6. if (vertex.getValue().get() < message.get()) {
  7. vertex.setValue(message);
  8. changed = true;
  9. }
  10. }
  11. if (getSuperstep() == 0 || changed) {
  12. sendMessageToAllEdges(vertex, vertex.getValue());
  13. }
  14. vertex.voteToHalt();
  15. }
  16. }

相关文章