Skip to content

Instantly share code, notes, and snippets.

@jayesh15111988
Created December 2, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayesh15111988/5939a1cc0196707af2df0313e0f47210 to your computer and use it in GitHub Desktop.
Save jayesh15111988/5939a1cc0196707af2df0313e0f47210 to your computer and use it in GitHub Desktop.
Find virus originator
let graphNodes = [(1, 2), (2, 3), (2, 4), (4, 5), (4, 6), (5, 4)]
var nodeToIncomingEdgesCountMapping: [Int: Int] = [:]
for node in graphNodes {
if nodeToIncomingEdgesCountMapping[node.0] == nil {
nodeToIncomingEdgesCountMapping[node.0] = 0
}
if nodeToIncomingEdgesCountMapping[node.1] == nil {
nodeToIncomingEdgesCountMapping[node.1] = 0
}
nodeToIncomingEdgesCountMapping[node.1]! += 1
}
for (node, incomingEdgesCount) in nodeToIncomingEdgesCountMapping {
if incomingEdgesCount == 0 {
print("Node is \(node)")
}
}
@jayesh15111988
Copy link
Author

jayesh15111988 commented Dec 12, 2020

print statement should read,
print("The node where the virus originated is \(node)")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment