def createGraphFromFile(): """Creates a graph from a file. Returns the graph if it was successfully created or an error message otherwise.""" fileName = input("Enter the file name: ") theFile = open(fileName, 'r') graph = LinkedDirectedGraph() rep = theFile.read() edgeList = rep.split() for edge in edgeList: if not '>' in edge: # A disconnected vertex if not graph.containsVertex(edge): graph.addVertex(edge) else: graph = None return "Duplicate vertex" else: # Two vertices and an edge bracketPos = edge.find('>') colonPos = edge.find(':') if bracketPos == -1 or colonPos == -1 or \ bracketPos > colonPos: self._graph = None return "Problem with > or :" fromLabel = edge[:bracketPos] toLabel = edge[bracketPos + 1:colonPos] weight = edge[colonPos + 1:] if weight.isdigit(): weight = int(weight) if not graph.containsVertex(fromLabel): graph.addVertex(fromLabel) if not graph.containsVertex(toLabel): graph.addVertex(toLabel) if graph.containsEdge(fromLabel, toLabel): graph = None return "Duplicate edge" graph.addEdge(fromLabel, toLabel, weight) return graph
result += str(vertex) + " " print("Expect 10 :", g.sizeVertices()) print("Expect a thru j :", result) print("Expect True False :", g.containsVertex("a"), end = " ") print(g.containsVertex("x")) print("Expect a b True False :", g.getVertex("a"), end = " ") print(g.getVertex("b"), " ", g.removeVertex("j")) print(g.containsVertex("j")) vertex = g.getVertex("a") vertex.setLabel("aaa", g) print("Expect vertices aaa thru j :", g) vertex.setLabel("a", g) # Test some edge methods print("Expect False False:", g.containsEdge("a", "b"), g.containsEdge("a", "a")) g.addEdge("a","b", 1) print("Expect True False False :", g.containsEdge("a", "b"), g.containsEdge("b", "a"), g.containsEdge("a", "a")) g.addEdge("a","c", 2) g.addEdge("e","c", 4) g.addEdge("e","d", 5) g.addEdge("e","f", 6) g.addEdge("d","a", 7) print("Expect True False True :", g.removeEdge("a","c"), g.containsEdge("a","c"), g.containsEdge ("d","a")) edge = g.getEdge("e", "d") edge.setWeight(55)
class GraphDemoModel(object): """The model class for the application.""" def __init__(self): self._graph = None self._startLabel = None def createGraph(self, rep, startLabel): """Creates a graph from rep and startLabel. Returns a message if the graph was successfully created or an error message otherwise.""" self._graph = LinkedDirectedGraph() self._startLabel = startLabel edgeList = rep.split() for edge in edgeList: if not '>' in edge: # A disconnected vertex if not self._graph.containsVertex(edge): self._graph.addVertex(edge) else: self._graph = None return "Duplicate vertex" else: # Two vertices and an edge bracketPos = edge.find('>') colonPos = edge.find(':') if bracketPos == -1 or colonPos == -1 or \ bracketPos > colonPos: self._graph = None return "Problem with > or :" fromLabel = edge[:bracketPos] toLabel = edge[bracketPos + 1:colonPos] weight = edge[colonPos + 1:] if weight.isdigit(): weight = int(weight) if not self._graph.containsVertex(fromLabel): self._graph.addVertex(fromLabel) if not self._graph.containsVertex(toLabel): self._graph.addVertex(toLabel) if self._graph.containsEdge(fromLabel, toLabel): self._graph = None return "Duplicate edge" self._graph.addEdge(fromLabel, toLabel, weight) vertex = self._graph.getVertex(startLabel) if vertex is None: self._graph = None return "Start label not in graph" else: vertex.setMark() return "Graph created successfully" def getGraph(self): """Returns the string rep of the graph or None if it is unavailable""" if not self._graph: return None else: return str(self._graph) def getStartLabel(self): return self._startLabel def run(self, algorithm): """Runs the given algorithm on the graph and returns its result, or None if the graph is unavailable.""" if self._graph is None: return None else: return algorithm(self._graph, self._startLabel)
result += str(vertex) + " " print "Expect 10 :", g.sizeVertices() print "Expect a thru j :", result print "Expect true false :", g.containsVertex("a"), print g.containsVertex("x") print "Expect a b true false :", g.getVertex("a"), print g.getVertex("b"), g.removeVertex("j") print g.containsVertex("j") vertex = g.getVertex("a") vertex.setLabel("aaa", g) print "Expect vertices aaa thru j :", g vertex.setLabel("a", g) # Test some edge methods print "Expect false false:", g.containsEdge("a", "b"), \ g.containsEdge("a", "a") g.addEdge("a","b", 1) print "Expect true true false :", g.containsEdge("a", "b"), \ g.containsEdge("b", "a"), g.containsEdge("a", "a") g.addEdge("a","c", 2) g.addEdge("e","c", 4) g.addEdge("e","d", 5) g.addEdge("e","f", 6) g.addEdge("d","a", 7) print "Expect true false true :", g.removeEdge("a","c"), \ g.containsEdge("a","c"), g.containsEdge ("d","a") edge = g.getEdge("e", "d") edge.setWeight(55)
result += str(vertex) + " " print "Expect 10 :", g.sizeVertices() print "Expect a thru j :", result print "Expect true false :", g.containsVertex("a"), print g.containsVertex("x") print "Expect a b true false :", g.getVertex("a"), print g.getVertex("b"), g.removeVertex("j") print g.containsVertex("j") vertex = g.getVertex("a") vertex.setLabel("aaa", g) print "Expect vertices aaa thru j :", g vertex.setLabel("a", g) # Test some edge methods print "Expect false false:", g.containsEdge("a", "b"), \ g.containsEdge("a", "a") g.addEdge("a", "b", 1) print "Expect true true false :", g.containsEdge("a", "b"), \ g.containsEdge("b", "a"), g.containsEdge("a", "a") g.addEdge("a", "c", 2) g.addEdge("e", "c", 4) g.addEdge("e", "d", 5) g.addEdge("e", "f", 6) g.addEdge("d", "a", 7) print "Expect true false true :", g.removeEdge("a","c"), \ g.containsEdge("a","c"), g.containsEdge ("d","a") edge = g.getEdge("e", "d") edge.setWeight(55)