/// <summary> /// Calculate node depths in an acyclic network. /// </summary> public NetworkDepthInfo CalculateNodeDepths(INetworkDefinition networkDef) { // Clear any existing state (allow reuse of this class). _nodeDepthById.Clear(); // Get and store connectivity data for the network. _networkConnectivityData = networkDef.GetConnectivityData(); // Loop over all input (and bias) nodes; Perform a depth first traversal of each in turn. // Set of nodes visited in the current traversal (reset before each individual depth first traversal). HashSet <uint> visitedNodeSet = new HashSet <uint>(); int inputAndBiasCount = networkDef.InputNodeCount + 1; for (int i = 0; i < inputAndBiasCount; i++) { visitedNodeSet.Clear(); TraverseNode(_networkConnectivityData.GetNodeDataByIndex(i), visitedNodeSet, 0); } // Extract node depths from _nodeDepthById into an array of depths (node depth by node index). // Note. Any node not in the dictionary is in an isolated sub-network and will be assigned to // layer 0 by default. INodeList nodeList = networkDef.NodeList; int nodeCount = nodeList.Count; int[] nodeDepthArr = new int[nodeCount]; int maxDepth = 0; // Loop over nodes and set the node depth. Skip over input and bias nodes, they are defined as // being in layer zero. for (int i = inputAndBiasCount; i < nodeCount; i++) { // Lookup the node's depth. If not found depth remains set to zero. int depth; if (_nodeDepthById.TryGetValue(nodeList[i].Id, out depth)) { nodeDepthArr[i] = depth; // Also determine maximum depth, that is, total depth of the network. if (depth > maxDepth) { maxDepth = depth; } } } // Return depth analysis info. return(new NetworkDepthInfo(maxDepth + 1, nodeDepthArr)); }
/// <summary> /// Calculate node depths in an acyclic network. /// </summary> public NetworkDepthInfo CalculateNodeDepths(INetworkDefinition networkDef) { // Clear any existing state (allow reuse of this class). _nodeDepthById.Clear(); // Get and store connectivity data for the network. _networkConnectivityData = networkDef.GetConnectivityData(); // Loop over all input (and bias) nodes; Perform a depth first traversal of each in turn. // Set of nodes visited in the current traversal (reset before each individual depth first traversal). HashSet<uint> visitedNodeSet = new HashSet<uint>(); int inputAndBiasCount = networkDef.InputNodeCount + 1; for(int i=0; i<inputAndBiasCount; i++) { visitedNodeSet.Clear(); TraverseNode(_networkConnectivityData.GetNodeDataByIndex(i), visitedNodeSet, 0); } // Extract node depths from _nodeDepthById into an array of depths (node depth by node index). // Note. Any node not in the dictionary is in an isolated sub-network and will be assigned to // layer 0 by default. INodeList nodeList = networkDef.NodeList; int nodeCount = nodeList.Count; int[] nodeDepthArr = new int[nodeCount]; int maxDepth = 0; // Loop over nodes and set the node depth. Skip over input and bias nodes, they are defined as // being in layer zero. for(int i=inputAndBiasCount; i<nodeCount; i++) { // Lookup the node's depth. If not found depth remains set to zero. int depth; if(_nodeDepthById.TryGetValue(nodeList[i].Id, out depth)) { nodeDepthArr[i] = depth; // Also determine maximum depth, that is, total depth of the network. if(depth > maxDepth) { maxDepth = depth; } } } // Return depth analysis info. return new NetworkDepthInfo(maxDepth+1, nodeDepthArr); }