Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Social Network Connectivity Algorithm

View README.md

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.

View README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
<script>
var users = {},
groups = {};
d3.csv("links2.csv", function(error, links) {
var sources = _(links).pluck("source");
var targets = _(links).pluck("target");
_(sources.concat(targets))
.uniq()
.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[link.target]
? { target: link.target, source: link.source}
: { source: link.target, target: link.source}
var targetGroup = users[merge.target];
var sourceGroup = users[merge.source];
groups[sourceGroup].forEach(function(d) {
counter++;
users[d] = targetGroup;
});
groups[targetGroup] = groups[targetGroup].concat( groups[sourceGroup] );
delete groups[sourceGroup];
if (d3.keys(groups).length == 1) {
found = true;
console.log(links[index]);
console.log(counter);
return links[index];
}
index++;
};
});
</script>
View README.md
1 2 3 4 5 6 7 8 9 10 11
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.