@@ -27,51 +27,60 @@ def __init__(self, vertices: List[V] = []) -> None:
2727
2828 @property
2929 def vertex_count (self ) -> int :
30- return len (self ._vertices )
30+ return len (self ._vertices ) # Number of vertices
3131
3232 @property
3333 def edge_count (self ) -> int :
34- return sum (map (len , self ._edges ))
34+ return sum (map (len , self ._edges )) # Number of edges
3535
3636 # Add a vertex to the graph and return its index
3737 def add_vertex (self , vertex : V ) -> int :
3838 self ._vertices .append (vertex )
3939 self ._edges .append ([]) # add empty list for containing edges
4040 return self .vertex_count - 1 # return index of added vertex
4141
42- # this is an undirected graph,
42+ # This is an undirected graph,
4343 # so we always add edges in both directions
4444 def add_edge (self , edge : Edge ) -> None :
4545 self ._edges [edge .u ].append (edge )
4646 self ._edges [edge .v ].append (edge .reversed ())
4747
48+ # Add an edge using vertex indices (convenience method)
4849 def add_edge_by_indices (self , u : int , v : int ) -> None :
4950 edge : Edge = Edge (u , v )
5051 self .add_edge (edge )
5152
53+ # Add an edge by looking up vertex indices (convenience method)
5254 def add_edge_by_vertices (self , first : V , second : V ) -> None :
5355 u : int = self ._vertices .index (first )
5456 v : int = self ._vertices .index (second )
5557 self .add_edge_by_indices (u , v )
5658
59+ # Find the vertex at a specific index
5760 def vertex_at (self , index : int ) -> V :
5861 return self ._vertices [index ]
5962
63+ # Find the index of a vertex in the graph
6064 def index_of (self , vertex : V ) -> int :
6165 return self ._vertices .index (vertex )
6266
67+ # Find the vertices that a vertex at some index is connected to
6368 def neighbors_for_index (self , index : int ) -> List [V ]:
6469 return list (map (self .vertex_at , [e .v for e in self ._edges [index ]]))
6570
71+ # Lookup a vertice's index and find its neighbors (convenience method)
6672 def neighbors_for_vertex (self , vertex : V ) -> List [V ]:
6773 return self .neighbors_for_index (self .index_of (vertex ))
6874
75+ # Return all of the edges associated with a vertex at some index
6976 def edges_for_index (self , index : int ) -> List [Edge ]:
7077 return self ._edges [index ]
7178
79+ # Lookup the index of a vertex and return its edges (convenience method)
7280 def edges_for_vertex (self , vertex : V ) -> List [Edge ]:
7381 return self .edges_for_index (self .index_of (vertex ))
7482
83+ # Make it easy to pretty-print a Graph
7584 def __str__ (self ) -> str :
7685 desc : str = ""
7786 for i in range (self .vertex_count ):
@@ -112,7 +121,7 @@ def __str__(self) -> str:
112121
113122 # Reuse BFS from Chapter 2 on city_graph
114123 import sys
115- sys .path .insert (0 , '..' ) # so we can access the Chapter2 module in the parent directory
124+ sys .path .insert (0 , '..' ) # so we can access the Chapter2 package in the parent directory
116125 from Chapter2 .generic_search import bfs , Node , node_to_path
117126
118127 bfs_result : Optional [Node [V ]] = bfs ("Boston" , lambda x : x == "Miami" , city_graph .neighbors_for_vertex )
0 commit comments