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)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
print statement should read,
print("The node where the virus originated is \(node)")