code: def build_graph(num_nodes, edge_path):
      filelist = []
      if os.path.isfile(edge_path):
          filelist = [edge_path]
      elif os.path.isdir(edge_path):
          filelist = [
              os.path.join(dp, f)
              for dp, dn, filenames in os.walk(edge_path)
              for f in filenames
          ]
      else:
          raise ValueError(edge_path + " not supported")
      edges, edge_weight = [], []
      for name in filelist:
          with open(name) as inf:
              for line in inf:
                  slots = line.strip("\n").split()
                  edges.append([slots[0], slots[1]])
                  edges.append([slots[1], slots[0]])
                  if len(slots) > 2:
                      edge_weight.extend(
                          [float(slots[2]), float(slots[2])]
                      )
      edges = np.array(edges, dtype="int64")
      assert (
          num_nodes > edges.max()
      ), "Node id in any edges should be smaller then num_nodes!"
  
      edge_feat = dict()
      if len(edge_weight) == len(edges):
          edge_feat["weight"] = np.array(edge_weight)
  
      graph = Graph(num_nodes, edges, edge_feat=edge_feat)
      log.info("Build graph done")
  
      graph.outdegree()
  
      del edges, edge_feat
  
      log.info("Build graph index done")
      if "weight" in graph.edge_feat:
          (
              graph.node_feat["alias"],
              graph.node_feat["events"],
          ) = graph_alias_sample_table(graph, "weight")
          log.info("Build graph alias sample table done")
      return graph
  
file_path: PGL/legacy/examples/distribute_deepwalk/utils.py
imports: from __future__ import division
  from __future__ import absolute_import
  from __future__ import print_function
  from __future__ import unicode_literals
  import os
  import time
  import numpy
  from pgl.utils.logger import log
  from pgl.graph import Graph
  from pgl.sample import graph_alias_sample_table
  from reader import DeepwalkReader
  import mp_reader
label_desc: Use image node_id 2 for calling a global function with example usage: build_graph(num_nodes, edge_path) and returns: graph