Published
Edited
May 18, 2022
Importers
1 star
Insert cell
Insert cell
// Computes how many friends in common do two people have. Receives either the nodes and links or the connectionSets
function getCommonConnections({ links, connectionsSets }) {
const linksCC = [];
const mapCC = new Map();

if (links !== undefined && connectionsSets === undefined) {
console.log("building connection sets");
connectionsSets = getConnectionsSets({ links });
}

// function getKey(si, ti) {
// return si < ti ? `${si}~${ti}` : `${ti}~${si}`;
// }

const before = performance.now();
for (let [n1, s1] of connectionsSets) {
for (let [n2, s2] of connectionsSets) {
if (n1 === n2 || !n1 || !n2) continue;

const intersectionSet = new Set([...s1].filter((d) => s2.has(d)));

if (intersectionSet.size) {
linksCC.push({
source: n1,
target: n2,
value: intersectionSet.size,
commonConnections: intersectionSet
});
// mapCC.set(key, intersectionSet);
}
}
}
// console.log("Finished building the mapCC", performance.now() - before);

// for (let [k, s] of mapCC) {
// const [source, target] = k.split("~");

// linksCC.push({source, target, value: s.size, connectionSet: set })
// }

console.log("Finished creating the array", performance.now() - before);

return linksCC;
}
Insert cell
// Transforms a set of links from d3 link style
function getConnectionsSets({ links }) {
const mapConnections = new Map();

function getSet(user) {
let set = mapConnections.get(user);

if (!set) {
set = new Set();
}

return set;
}

for (let l of links) {
const set = getSet(l.source);
set.add(l.target);
mapConnections.set(l.source, set);
}

return mapConnections;
}
Insert cell
import {data} from "3fb4e59272163b09"
Insert cell
ccs = getCommonConnections(data)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more