Esempio n. 1
0
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
Esempio n. 2
0
g.addVertex("c")
g.addVertex("d")
g.addVertex("e")
g.addVertex("f")
g.addVertex("g")
g.addVertex("h")
g.addVertex("i")
g.addVertex("j")
      
# Test some vertex methods
result = ""
for vertex in g.vertices():
    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"),
Esempio n. 3
0
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)
Esempio n. 4
0
g.addVertex("c")
g.addVertex("d")
g.addVertex("e")
g.addVertex("f")
g.addVertex("g")
g.addVertex("h")
g.addVertex("i")
g.addVertex("j")
      
# Test some vertex methods
result = ""
for vertex in g.vertices():
    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"), \
Esempio n. 5
0
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)
Esempio n. 6
0
g.addVertex("c")
g.addVertex("d")
g.addVertex("e")
g.addVertex("f")
g.addVertex("g")
g.addVertex("h")
g.addVertex("i")
g.addVertex("j")

# Test some vertex methods
result = ""
for vertex in g.vertices():
    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"), \