code: def find_tree(sub_network, weight='x_pu'):
      
  
      branches_bus0 = sub_network.branches()["bus0"]
      branches_i = branches_bus0.index
      buses_i = sub_network.buses_i()
  
      graph = sub_network.graph(weight=weight, inf_weight=1.)
      sub_network.tree = nx.minimum_spanning_tree(graph)
  
      #find bus with highest degree to use as slack
      tree_slack_bus, slack_degree = max(degree(sub_network.tree), key=itemgetter(1))
      logger.info("Tree slack bus is %s with degree %d.", tree_slack_bus, slack_degree)
  
      #determine which buses are supplied in tree through branch from slack
  
      #matrix to store tree structure
      sub_network.T = dok_matrix((len(branches_i),len(buses_i)))
  
      for j,bus in enumerate(buses_i):
          path = nx.shortest_path(sub_network.tree,bus,tree_slack_bus)
          for i in range(len(path)-1):
              branch = next(iterkeys(graph[path[i]][path[i+1]]))
              branch_i = branches_i.get_loc(branch)
              sign = +1 if branches_bus0.iat[branch_i] == path[i] else -1
              sub_network.T[branch_i,j] = sign
docs: Get the spanning tree of the graph, choose the node with the
      highest degree as a central "tree slack" and then see for each
      branch which paths from the slack to each node go through the
      branch.