Created
June 14, 2015 20:52
-
-
Save fetchTe/8f0d877b10e779866e45 to your computer and use it in GitHub Desktop.
Traversing the social network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Dict(elements) { | |
this.elements = elements || {}; | |
} | |
Dict.prototype.has = function (key) { | |
return {}.hasOwnProperty.call(this.elements, key); | |
}; | |
Dict.prototype.get = function (key) { | |
return this.has(key) ? this.elements[key] : undefined; | |
}; | |
Dict.prototype.set = function (key, val) { | |
return this.elements[key] = val; | |
}; | |
Dict.prototype.remove = function (key) { | |
delete this.elements[key]; | |
}; | |
//In order to pick an arbitrary element of the set, we need a new method | |
//for the Dict class: | |
Dict.prototype.pick = function () { | |
for (var key in this.elements) { | |
if (this.has(key)) { | |
return key; | |
} | |
} | |
throw new Error('empty dictionary'); | |
}; | |
function WorkSet () { | |
this.entries = new Dict(); | |
this.count = 0; | |
} | |
WorkSet.prototype.isEmpty = function () { | |
return this.count === 0; | |
}; | |
WorkSet.prototype.add = function (key, val) { | |
if (this.entries.has(key)) { | |
return; | |
} | |
this.entries.set(key, val); | |
this.count++; | |
}; | |
WorkSet.prototype.get = function (key) { | |
return this.entries.get(key); | |
}; | |
WorkSet.prototype.remove = function (key) { | |
if (!this.entries.has(key)) { | |
return; | |
} | |
this.entries.remove(key); | |
this.count--; | |
}; | |
WorkSet.prototype.pick = function () { | |
return this.entries.pick(); | |
}; | |
// Friends association method | |
// use a while loop to implement , each selected from a set of | |
// arbitrary elements , and from the work-set is removed in | |
Member.prototype.inNetwork = function (other) { | |
var visited = {}; | |
var workset = new WorkSet(); | |
// caller's name when the key, is used to locate the caller friends | |
workset.add(this.name, this); | |
while(!workset.isEmpty()){ | |
var name = workset.pick(); | |
var member = workset.get(name); | |
workset.remove(name); | |
//don't revisit members | |
if (name in visited) { | |
continue; | |
} | |
visited[name] = member; | |
if (member === other) { | |
//found | |
return true; | |
} | |
member.friends.forEach(function (friend) { | |
workset.add(friend.name, friend); | |
}); | |
} | |
return false; | |
}; | |
// associate friend Method | |
// save the work item to the array rather than a collection (set) in , | |
// in exactly the same order to tour social network routes | |
Member.prototype.inNetworkList = function (other) { | |
var visited = {}; | |
var worklist = [this]; | |
while (worklist.length > 0) { | |
var member = worklist.pop(); | |
if (member.name in visited) { | |
continue; | |
} | |
visited[member.name] = member; | |
if (member === other) { | |
return true; | |
} | |
member.friends.forEach(function (friend) { | |
worklist.push(friend); | |
}); | |
} | |
return false; | |
}; | |
function Member(name) { | |
this.name = name; | |
this.friends = []; | |
} | |
var Alice = new Member("Alice"), | |
Bob = new Member("Bob"), | |
Carol = new Member("Carol"), | |
Dexter = new Member("Dexter"), | |
Eli = new Member("Eli"), | |
Fatima = new Member("Fatima"); | |
Alice.friends.push(Bob); | |
Bob.friends.push(Carol); | |
Carol.friends.push(Eli); | |
Dexter.friends.push(Bob); | |
Eli.friends.push(Dexter, Fatima, Bob); | |
console.log(Alice.inNetwork(Fatima)) | |
console.log(Bob.inNetwork(Alice)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment