title: IndexError: list index out of range graph diameter
body: <p>this is my code:</p>
  
  <pre><code>class Graph(object):
  
  def __init__(self, graph_dict={}):
      self.__graph_dict = graph_dict
  
  def vertices(self):
      return list(self.__graph_dict.keys())
  
  
  def find_all_paths(self, start_vertex, end_vertex, path=[]):
      graph = self.__graph_dict
      path = path + [start_vertex]
      if start_vertex == end_vertex:
          return [path]
      if start_vertex not in graph:
          return []
      paths = []
      for vertex in graph[start_vertex]:
          if vertex not in path:
              extended_paths = self.find_all_paths(vertex, 
                                               end_vertex, 
                                               path)
              for p in extended_paths:
                  paths.append(p)
  
  
      return paths
  
  def diameter(self):
  
  
      v = self.vertices() 
      pairs = [ (v[i],v[j]) for i in range(len(v)) for j in range(i+1, len(v)-1)]
      smallest_paths = []
      for (s,e) in pairs:
          paths = self.find_all_paths(s,e)
          smallest = sorted(paths, key=len)[0]
          smallest_paths.append(smallest)
  
      smallest_paths.sort(key=len)
  
  
      diameter = len(smallest_paths[-1])
      return diameter    
  </code></pre>
  
  <p>When I run the comand:</p>
  
  <pre><code>if __name__ == "__main__":
  
  from collections import defaultdict
  g = defaultdict(list)
  with open("/Volumes/City_University/data_mining/Ecoli.txt") as f:
      for line in f:
          key, val = line.split()
          g[key].append(val)
  for k, v in g.items():
      print(k, v)
  
      graph = Graph(g)
  
  print("Vertices of graph:")
  print(graph.vertices())
  print(graph.find_all_paths("0", "3"))
  
  print(graph.diameter())
  </code></pre>
  
  <p>I get this error:
  File "", line 70, in 
      print(graph.diameter())</p>
  
  <p>File "", line 40, in diameter
      smallest = sorted(paths, key=len)[0]</p>
  
  <p>IndexError: list index out of range</p>
  
  <p>The graph is a very big one. It has 3633 edges.
  Any reason why I get this error?
  Is there another function I can use to calcute the diameter of this graph?</p>