Example #1
0
 def test_getVertex(self): # Tests error alert when getting non-existent vertex
     g = LinkedDirectedGraph()
     g.addVertex("A")
     g.addVertex("B")
     g.addVertex("C")
     g.addEdge("A", "B", 2.5)
     with self.assertRaises(AttributeError):
         g.getVertex("F")
Example #2
0
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"),
      g.containsEdge("b", "a"), g.containsEdge("a", "a"))
g.addEdge("a","c", 2)
Example #3
0
# Create a directed graph using an adjacency list
g = LinkedDirectedGraph()

# Add vertices labeled A, B, and C to the graph and print it
g.addVertex("A")
g.addVertex("B")
g.addVertex("C")
print("Expect vertices ABC and no edges: \n" + str(g))

# Insert edges with weight 2.5 and print the graph
g.addEdge("A", "B", 2.5)
g.addEdge("B", "C", 2.5)
g.addEdge("C", "B", 2.5)
print("Expect same vertices and edges AB BC CB all with weight 2.5: \n" +
      str(g))

# Mark all the vertices
for vertex in g.vertices():
    vertex.setMark()

# Print the vertices adjacent to vertex B
print("Expect vertices adjacent to B, namely C:")
v = g.getVertex("B")
for neighbor in g.neighboringVertices(v.getLabel()):
    print(neighbor)

# Print the edges of vertex B
print("Expect edges incident to B, namely BC:")
for edge in g.incidentEdges(v.getLabel()):
    print(edge)
Example #4
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)
Example #5
0
# Create a directed graph using an adjacency list
g = LinkedDirectedGraph()

# Add vertices labeled A, B, and C to the graph and print it      
g.addVertex("A")
g.addVertex("B")
g.addVertex("C")
print("Expect vertices ABC and no edges: \n" + str(g))

# Insert edges with weight 2.5 and print the graph
g.addEdge("A", "B", 2.5)
g.addEdge("B", "C", 2.5)
g.addEdge("C", "B", 2.5)
print("Expect same vertices and edges AB BC CB all with weight 2.5: \n" + str(g))

# Mark all the vertices
for vertex in g.vertices():
    vertex.setMark()

# Print the vertices adjacent to vertex B
print("Expect vertices adjacent to B, namely C:")
v = g.getVertex("B")
for neighbor in g.neighboringVertices(v.getLabel()):
    print(neighbor)

# Print the edges of vertex B
print("Expect edges incident to B, namely BC:")
for edge in g.incidentEdges(v.getLabel()):
    print(edge)
chart.addEdge("CSCI111", "CSCI210", 1)
chart.addEdge("CSCI111", "CSCI112", 1)
chart.addEdge("CSCI210", "CSCI312", 1)
chart.addEdge("CSCI112", "CSCI209", 1)
chart.addEdge("CSCI112", "CSCI211", 1)
chart.addEdge("CSCI112", "CSCI312", 1)
chart.addEdge("MATH121", "CSCI211", 1)
chart.addEdge("MATH121", "MATH102", 1)
chart.addEdge("MATH121", "MATH122", 1)
chart.addEdge("MATH121", "CSCI313", 1)
chart.addEdge("MATH121", "CSCI312", 1)

print("The chart: \n" + str(chart))

# Print the vertices adjacent to vertex CSCI112.
v = chart.getVertex("CSCI112")
print("Expect vertices adjacent to CSCI112:")
for neighbor in chart.neighboringVertices(v.getLabel()):
    print(neighbor)

# Print the edges incident to CSCI112.
print("Expect edges incident to CSCI112:")
for edge in chart.incidentEdges(v.getLabel()):
    print(edge)

def traverseFromVertex(graph, startVertex, process, collection = LinkedStack()):
    chart.clearVertexMarks()
    collection.add(startVertex)
    while not collection.isEmpty():
        v = collection.pop()
        if not v.isMarked():
Example #7
0
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"), \
      g.containsEdge("b", "a"), g.containsEdge("a", "a")
g.addEdge("a","c", 2)
Example #8
0
chart.addEdge("CSCI111", "CSCI210", 1)
chart.addEdge("CSCI111", "CSCI112", 1)
chart.addEdge("CSCI210", "CSCI312", 1)
chart.addEdge("CSCI112", "CSCI209", 1)
chart.addEdge("CSCI112", "CSCI211", 1)
chart.addEdge("CSCI112", "CSCI312", 1)
chart.addEdge("MATH121", "CSCI211", 1)
chart.addEdge("MATH121", "MATH102", 1)
chart.addEdge("MATH121", "MATH122", 1)
chart.addEdge("MATH121", "CSCI313", 1)
chart.addEdge("MATH121", "CSCI312", 1)

print("The chart: \n" + str(chart))

# Print the vertices adjacent to vertex CSCI112.
v = chart.getVertex("CSCI112")
print("Expect vertices adjacent to CSCI112:")
for neighbor in chart.neighboringVertices(v.getLabel()):
    print(neighbor)

# Print the edges incident to CSCI112.
print("Expect edges incident to CSCI112:")
for edge in chart.incidentEdges(v.getLabel()):
    print(edge)


def traverseFromVertex(graph, startVertex, process, collection=LinkedStack()):
    chart.clearVertexMarks()
    collection.add(startVertex)
    while not collection.isEmpty():
        v = collection.pop()
Example #9
0
# Add vertices labeled A, B, and C to the graph and print it
g.addVertex("A")
g.addVertex("B")
g.addVertex("C")
print("Expect vertices ABC and no edges: \n" + str(g))

# Insert edges with weight 2.5 and print the graph
g.addEdge("A", "B", 2.5)
g.addEdge("B", "C", 2.5)
g.addEdge("C", "B", 2.5)
print("Expect same vertices and edges AB BC CB all with weight 2.5: \n" +
      str(g))

#g.addVertex("A")

#g.addEdge("A", "D", 2.5)

#g.addEdge("A", "B", 2.5)

print("Expect A: ", g.getVertex("A"))
#print(g.getVertex("D"))

print("Edge from A to B:", g.getEdge("A", "B"))
print("Edge from B to A:", g.getEdge("B", "A"))
#print(g.getEdge("D", "A"))

print("Incident edges of A:", list(map(str, g.incidentEdges("A"))))
#print(list(map(str, g.incidentEdges("D"))))
#print(list(map(str, g.neighboringVertices("D"))))
Example #10
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)
Example #11
0
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"), \
      g.containsEdge("b", "a"), g.containsEdge("a", "a")
g.addEdge("a", "c", 2)