def read_graph_from_file(filename): '''Parse edges from the file and load them into a graph object.''' edges = read_edges_from_file(filename) g = Graph() for v, u in edges: g.add_edge(v, u) return g
def read_weighted_undirected_graph(filename): g = Graph() with open(filename) as f: for line in f: try: v1, v2, w = line.split() g.add_edge(v1, v2, {'weight': int(w)}) except: pass return g
def read_weighted_undirected_graph(filename): """A helper to read the edges of a graph described in the file into a graph object.""" g = Graph() with open(filename) as f: for line in f: try: v1, v2, w = line.split() g.add_edge(v1, v2, {'weight': int(w)}) except: pass return g
def main(): graph = Graph("tineG.txt") source = 0 search = DepthFirstSearch(graph, source) for v in range(graph.v()): if search.marked(v): print v, print if search.count != graph.v(): print "Not ", print "connected"
def setUp(self): self.graph = Graph({ "a": ["d", "g"], "b": ["c"], "c": ["b", "c", "d", "e"], "d": ["a", "c"], "e": ["c"], "f": [], "g": ["a"] }) self.another_graph = Graph({ "a": ["c"], "b": ["c", "e", "f"], "c": ["d", "e"], "d": ["c"], "e": ["b", "c", "f"], "f": ["b", "e"] })
class TestGraphMehods(unittest.TestCase): def setUp(self): self.graph = Graph({ "a": ["d", "g"], "b": ["c"], "c": ["b", "c", "d", "e"], "d": ["a", "c"], "e": ["c"], "f": [], "g": ["a"] }) self.another_graph = Graph({ "a": ["c"], "b": ["c", "e", "f"], "c": ["d", "e"], "d": ["c"], "e": ["b", "c", "f"], "f": ["b", "e"] }) def test_find_path(self): self.assertEqual(self.graph.find_path("a", "f"), []) self.assertEqual(self.graph.find_path("a", "g"), ["a", "g"]) self.assertEqual(self.graph.find_path("a", "d"), ["a", "d"]) self.assertEqual(self.graph.find_path("a", "c"), ["a", "d", "c"]) self.assertEqual(self.graph.find_path("d", "e"), ["d", "c", "e"]) self.assertEqual(self.graph.find_path("c", "g"), ["c", "d", "a", "g"]) self.assertEqual(self.graph.find_path("e", "b"), ["e", "c", "b"]) def test_vertex_degree(self): self.assertEqual(self.graph.vertex_degree("a"), 2) self.assertEqual(self.graph.vertex_degree("c"), 5) self.assertEqual(self.graph.vertex_degree("f"), 0) def test_is_connected(self): self.assertEqual(self.graph.is_connected(), False) self.assertEqual(self.another_graph.is_connected(), True) def test_diameter(self): self.assertEqual(self.another_graph.diameter(), 3)
# please feel free to create helpers, modify provided code, create new helper files, etc. # Whatever you turn in is what we will grade (ie we won't provide any files or overwrite # any of yours) # Have fun! def compute_mst(filename): '''Use Prim's algorithm to compute the minimum spanning tree of the weighted undirected graph described by the contents of the file named filename.''' tree_edges = [] # TODO compute the edges of a minimum spanning tree write_tree_edges_to_file(tree_edges, filename + '.mst') if __name__ == '__main__': #读取信息 g = Graph() with open("5000 input") as f: for line in f: try: v1, v2, w = line.split() g.add_edge(v1, v2, {'weight': int(w)}) except: pass node_sets = initialize_disjoint_set(g.get_nodes()) node_count = len(node_sets) #edges = [(g.attributes_of(v, u)['weight'], v, u) for u, v in g.get_edges()] #edges.sort() tree_edges = [] #查看读取信息 #print("node_sets is: ",node_sets)
# any of yours) # Have fun! def compute_mst(filename): '''Use Prim's algorithm to compute the minimum spanning tree of the weighted undirected graph described by the contents of the file named filename.''' tree_edges = [] # TODO compute the edges of a minimum spanning tree write_tree_edges_to_file(tree_edges, filename + '.mst') if __name__ == '__main__': #读取信息 #时间换空间 保存当前最优解 start_time = time.process_time() g = Graph() with open("5 input") as f: for line in f: try: v1, v2, w = line.split() g.add_edge(v1, v2, {'weight': int(w)}) except: pass node_dist = initialize_disjiont_set(g.get_nodes()) print(g.get_nodes()) print(node_dist) # O(n) first_node = list(g.get_nodes())[0] #O(1) node_sets = set([first_node]) #O(1) tree_edges = [] print(g.neighbors(first_node))