in this code in the init function we initialize the i.first elements as zero but the nodes that have 0 outdegree do not appear in i.first part of adj list how are we increasing their indegree in the next function when nodes like webd were never initialised as 0 in the first place?
Graphs Topological Sort Using BFS
Hello @rachitbansal2500
Let’s say we have
#include<iostream>
using namespace std;
int main() {
map<string, int> m;
cout << m["key"]; // This will print zero even if it was never initialized
}
This works because even though the map “m” is empty (No elements were ever initialized). The operator [] will insert the key into the map and define its value as the default for the type (in this case integers are zero by default).
So even if you comment out the Line 50 (“indegree[node] = 0”), it will work because by default they are initially zero.