Skip to content

Latest commit

 

History

History
58 lines (45 loc) · 2.05 KB

pgx_builtin_s2_adamic_adar_counting.md

File metadata and controls

58 lines (45 loc) · 2.05 KB

Adamic-Adar index

The Adamic-Adar index is meant for undirected graphs, since it is computed using the degree of the shared neighbors by two vertices in the graph. This implementation computes the index for every pair of vertices connected by an edge and associates it with that edge.

Signature

Input Argument Type Comment
G graph the graph.
Output Argument Type Comment
adamic_adar edgeProp edge property holding the Adamic-Adar index of each edge in the graph.
Return Value Type Comment
void None

Code

/*
 * Copyright (C) 2013 - 2025 Oracle and/or its affiliates. All rights reserved.
 */
package oracle.pgx.algorithms;

import oracle.pgx.algorithm.EdgeProperty;
import oracle.pgx.algorithm.annotations.GraphAlgorithm;
import oracle.pgx.algorithm.PgxGraph;
import oracle.pgx.algorithm.PgxVertex;
import oracle.pgx.algorithm.annotations.Out;

import static java.lang.Math.log10;

@GraphAlgorithm
public class AdamicAdar {
  public void adamicAdar(PgxGraph g, @Out EdgeProperty<Double> aa) {
    g.getEdges().forEach(e -> {
      PgxVertex src = e.sourceVertex();
      PgxVertex dst = e.destinationVertex();

      double value = src.getOutNeighbors()
          .filter(n -> n.hasEdgeFrom(dst))
          .sum(n -> 1 / log10(n.getDegree()));

      aa.set(e, value);
    });
  }
}