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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more