Created
December 2, 2020 14:02
-
-
Save jayesh15111988/5939a1cc0196707af2df0313e0f47210 to your computer and use it in GitHub Desktop.
Find virus originator
This file contains hidden or 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
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
print statement should read,
print("The node where the virus originated is \(node)")