code: def determine_network_topology(self):
          
  
          adjacency_matrix = self.adjacency_matrix(self.passive_branch_components)
          n_components, labels = csgraph.connected_components(adjacency_matrix, directed=False)
  
          # remove all old sub_networks
          for sub_network in self.sub_networks.index:
              obj = self.sub_networks.at[sub_network,"obj"]
              self.remove("SubNetwork", sub_network)
              del obj
  
          for i in np.arange(n_components):
              # index of first bus
              buses_i = (labels == i).nonzero()[0]
              carrier = self.buses.carrier.iat[buses_i[0]]
  
              if carrier not in ["AC","DC"] and len(buses_i) > 1:
                  logger.warning("Warning, sub network {} is not electric but contains multiple buses\n"
                                  "and branches. Passive flows are not allowed for non-electric networks!".format(i))
  
              if (self.buses.carrier.iloc[buses_i] != carrier).any():
                  logger.warning("Warning, sub network {} contains buses with mixed carriers! Value counts:\n{}".format(i),
                                  self.buses.carrier.iloc[buses_i].value_counts())
  
              self.add("SubNetwork", i, carrier=carrier)
  
          #add objects
          self.sub_networks["obj"] = [SubNetwork(self, name) for name in self.sub_networks.index]
  
          self.buses.loc[:, "sub_network"] = labels.astype(str)
  
          for c in self.iterate_components(self.passive_branch_components):
              c.df["sub_network"] = c.df.bus0.map(self.buses["sub_network"])
docs: Build sub_networks from topology.