Skip to content

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Social Network Connectivity Algorithm

Solution to this prompt, built at a JS Study Group

Social network connectivity. Given a social network containing N members and a log file containing M timestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member is a friend of a friend of a friend ... of a friend). Assume that the log file is sorted by timestamp and that friendship is an equivalence relation. The running time of your algorithm should be MlogN or better and use extra space proportional to N.

<!DOCTYPE html>
<meta charset="utf-8">
.node {
stroke: #fff;
stroke-width: 1.5px;
.link {
stroke: #999;
stroke-opacity: .6;
<script src=""></script>
<script src=""></script>
var users = {},
groups = {};
d3.csv("links2.csv", function(error, links) {
var sources = _(links).pluck("source");
var targets = _(links).pluck("target");
.forEach(function(d,i) {
users[d] = i;
_(users).each(function(v,k) {
groups[v] = [k];
var found = false;
var index = 0;
var counter = 0;
while (!found) {
var link = links[index];
var merge = users[link.source] > users[]
? { target:, source: link.source}
: { source:, target: link.source}
var targetGroup = users[];
var sourceGroup = users[merge.source];
groups[sourceGroup].forEach(function(d) {
users[d] = targetGroup;
groups[targetGroup] = groups[targetGroup].concat( groups[sourceGroup] );
delete groups[sourceGroup];
if (d3.keys(groups).length == 1) {
found = true;
return links[index];
time source target
1 a b
2 c d
3 e f
4 g h
5 a d
6 h e
7 g f
8 e d
9 c b
10 a h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.