Exemple #1
0
    def test_SpecifyThatTwoGraphEdgesCanBeCompared(self):
        edge1 = GraphEdge(fromNodeIndex=10, toNodeIndex=20, cost=30)
        edge2 = GraphEdge(fromNodeIndex=10, toNodeIndex=20, cost=30)
        edge3 = GraphEdge(fromNodeIndex=70, toNodeIndex=80, cost=90)

        self.assertTrue(edge1 == edge2, "comp 1")
        self.assertTrue(edge1 == edge1, "comp 2")
        self.assertTrue(edge1 != edge3, "comp 3")
Exemple #2
0
    def test_SpecifyThatGraphEdgePropertiesCanBeModified(self):
        graphEdge = GraphEdge()

        graphEdge.From = 100
        graphEdge.To = 200
        graphEdge.Cost = 300

        self.assertEqual(100, graphEdge.From, "From")
        self.assertEqual(200, graphEdge.To, "To")
        self.assertEqual(300, graphEdge.Cost, "Cost")
    def test_SpecifyThatAnEdgeCanBeAddedBetweenNodesOnADirectedGraph(self):
        sparseGraph = SparseGraph(isDigraph=True)

        node0 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node0)

        node1 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node1)

        edge = GraphEdge(fromNodeIndex=node0.Index, toNodeIndex=node1.Index)
        sparseGraph.AddEdge(edge)

        self.assertEqual(2, len(sparseGraph._nodes), "_nodes")
        self.assertEqual(2, len(sparseGraph._edgeListVector),
                         "_edgeListVector")
        self.assertEqual(2, sparseGraph._nextNodeIndex, "_nextNodeIndex")

        self.assertEqual(1, len(sparseGraph._edgeListVector[0]),
                         "_edgeListVector[0]")
        self.assertEqual(0, sparseGraph._edgeListVector[0][0].From,
                         "_edgeListVector[0][0].From")
        self.assertEqual(1, sparseGraph._edgeListVector[0][0].To,
                         "_edgeListVector[0][0].To")

        self.assertEqual(0, len(sparseGraph._edgeListVector[1]),
                         "_edgeListVector[1]")
Exemple #4
0
    def Load(self, fileName):
        """
        Methods for loading and saving graphs from an open file stream or from a file name
        :param fileName:
        """
        try:
            with open(fileName, 'r') as file:
                self.Clear()

                self._isDigraph = bool(file.readline())

                # Get the number of nodes and read them in
                numberOfNodes = int(file.readline())

                for nodeIndex in range(numberOfNodes):
                    newNodeData = file.readline()
                    newNodeIndex = int(newNodeData.split(":")[1])
                    newNode = self._createNode(newNodeIndex)

                    # When editing graphs it's possible to end up with a situation where some
                    # of the nodes have been invalidated (their id's set to invalid_node_index). Therefore
                    # when a node of index invalid_node_index is encountered, it must still be added.

                    if newNode.Index != NodeType.InvalidNodeIndex.value:
                        self.AddNode(newNode)
                    else:
                        self._nodes.append(newNode)

                        # Make sure an edgeList is added for each node
                        self._edgeListVector.append([])

                        self._nextNodeIndex += 1

                # Get the number of nodes and read them in
                numberOfEdges = int(file.readline())

                # now add the edges
                for edgeIndex in range(numberOfEdges):
                    newEdgeData = file.readline().split("|")
                    fromIndex = int(newEdgeData[0].split(":")[1])
                    toIndex = int(newEdgeData[1].split(":")[1])
                    cost = float(newEdgeData[2].split(":")[1])
                    nextEdge = GraphEdge(fromIndex, toIndex, cost)

                    self._edgeListVector[nextEdge.From].append(nextEdge)

            return True

        except IOError:
            raise Exception("Cannot open file: " + fileName)
    def test_SpecifyThatNodeCanHaveEdgeOnToItselfOnAnUndirectedGraph(self):
        sparseGraph = SparseGraph(isDigraph=False)

        node0 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node0)

        edge = GraphEdge(fromNodeIndex=node0.Index, toNodeIndex=node0.Index)
        sparseGraph.AddEdge(edge)

        self.assertEqual(1, len(sparseGraph._nodes), "_nodes")
        self.assertEqual(1, len(sparseGraph._edgeListVector),
                         "_edgeListVector")
        self.assertEqual(1, sparseGraph._nextNodeIndex, "_nextNodeIndex")

        self.assertEqual(1, len(sparseGraph._edgeListVector[0]),
                         "_edgeListVector[0]")
        self.assertEqual(0, sparseGraph._edgeListVector[0][0].From,
                         "_edgeListVector[0][0].From")
        self.assertEqual(0, sparseGraph._edgeListVector[0][0].To,
                         "_edgeListVector[0][0].To")
    def _search(self):
        """
        This method performs the DFS search
        """
        # Create a standard stack of edges (LIFO)
        stack = []

        # Create a dummy edge and put on the stack
        dummyEdge = GraphEdge(self._sourceNodeIndex, self._sourceNodeIndex, cost=0)

        stack.append(dummyEdge)

        # While there are edges in the stack keep searching
        while len(stack) > 0:
            # Grab the next edge and remove the edge from the stack
            nextEdge = stack.pop()

            # Make a note of the parent of the node this edge points to
            self._routeNodeToParent[nextEdge.To] = nextEdge.From

            # Put it on the tree. (making sure the dummy edge is not placed on the tree)
            if nextEdge != dummyEdge:
                self._spanningTree.append(nextEdge)

            # and mark it visited
            self._visited[nextEdge.To] = NodeState.Visited

            # if the target has been found the method can return success
            if nextEdge.To == self._targetNodeIndex:
                return True

            # Push the edges leading from the node this edge points to onto the
            # stack (provided the edge does not point to a previously visited node)
            edgeList = self._graph.GetNodeEdges(nextEdge.To)

            for edge in edgeList:
                if self._visited[edge.To] == NodeState.Unvisited:
                    stack.append(edge)

        # No path to target
        return False
Exemple #7
0
    def _search(self):
        # Create a standard queue of edges
        queue = deque()

        dummy = GraphEdge(self._sourceNodeIndex, self._sourceNodeIndex, 0)

        # Create a dummy edge and put on the queue
        queue.appendleft(dummy)

        # Mark the source node as visited
        self._visited[self._sourceNodeIndex] = NodeState.Visited

        # While there are edges in the queue keep searching
        while len(queue) > 0:
            # Grab the next edge
            nextEdge = queue.pop()

            # Mark the parent of this node
            self._routeNodeToParent[nextEdge.To] = nextEdge.From

            # Put it on the tree. (making sure the dummy edge is not placed on the tree)
            if nextEdge != dummy:
                self._spanningTree.append(nextEdge)

            # Exit if the target has been found
            if nextEdge.To == self._targetNodeIndex:
                return True

            # Push the edges leading from the node at the end of this edge onto the queue
            edgeList = self._graph.GetNodeEdges(nextEdge.To)

            for edge in edgeList:
                # If the node hasn't already been visited we can push the edge onto the queue
                if self._visited[edge.To] == NodeState.Unvisited:
                    queue.appendleft(edge)

                    # and mark it visited
                    self._visited[edge.To] = NodeState.Visited

        # No path to target
        return False
Exemple #8
0
    def CreateDigraph():
        from HorizonCore.Graph.SparseGraph import SparseGraph
        from HorizonCore.Graph.GraphEdge import GraphEdge
        from HorizonCore.Graph.GraphNode import GraphNode

        sparseGraph = SparseGraph(isDigraph=True)

        node0 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node0)
        node0.Index = -1

        node1 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node1)

        node2 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node2)

        node3 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node3)

        node4 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node4)

        node5 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node5)

        node6 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node6)

        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node1.Index,
                      toNodeIndex=node5.Index,
                      cost=2.9))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node1.Index,
                      toNodeIndex=node6.Index,
                      cost=1.0))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node2.Index,
                      toNodeIndex=node3.Index,
                      cost=3.1))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node3.Index,
                      toNodeIndex=node5.Index,
                      cost=0.8))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node4.Index,
                      toNodeIndex=node3.Index,
                      cost=3.7))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node5.Index,
                      toNodeIndex=node2.Index,
                      cost=1.9))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node5.Index,
                      toNodeIndex=node6.Index,
                      cost=3.0))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node6.Index,
                      toNodeIndex=node4.Index,
                      cost=1.1))

        return sparseGraph
Exemple #9
0
    def CreateUniGraph():
        from HorizonCore.Graph.SparseGraph import SparseGraph
        from HorizonCore.Graph.GraphEdge import GraphEdge
        from HorizonCore.Graph.GraphNode import GraphNode

        sparseGraph = SparseGraph(isDigraph=False)

        node0 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node0)
        node0.Index = -1

        node1 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node1)

        node2 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node2)

        node3 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node3)

        node4 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node4)

        node5 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node5)

        node6 = GraphNode(index=sparseGraph.GetNextFreeNodeIndex())
        sparseGraph.AddNode(node6)

        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node1.Index, toNodeIndex=node5.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node1.Index, toNodeIndex=node6.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node2.Index, toNodeIndex=node3.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node2.Index, toNodeIndex=node5.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node3.Index, toNodeIndex=node2.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node3.Index, toNodeIndex=node4.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node3.Index, toNodeIndex=node5.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node4.Index, toNodeIndex=node3.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node4.Index, toNodeIndex=node6.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node5.Index, toNodeIndex=node1.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node5.Index, toNodeIndex=node2.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node5.Index, toNodeIndex=node3.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node5.Index, toNodeIndex=node6.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node6.Index, toNodeIndex=node1.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node6.Index, toNodeIndex=node4.Index))
        sparseGraph.AddEdge(
            GraphEdge(fromNodeIndex=node6.Index, toNodeIndex=node5.Index))

        return sparseGraph
Exemple #10
0
    def test_SpecifyThatDefaultGraphEdgeCanBeConstructed(self):
        graphEdge = GraphEdge()

        self.assertEqual(NodeType.InvalidNodeIndex.value, graphEdge.From, "From")
        self.assertEqual(NodeType.InvalidNodeIndex.value, graphEdge.To, "To")
        self.assertEqual(1.0, graphEdge.Cost, "Cost")
Exemple #11
0
    def test_SpecifyThatGraphEdgeCanWriteOutItsContentsToConsole(self):
        graphEdge = GraphEdge(fromNodeIndex=10, toNodeIndex=20, cost=30)
        stringOutput = str(graphEdge)

        self.assertEqual(stringOutput, "From: 10 | To: 20 | Cost: 30")
Exemple #12
0
    def test_SpecifyThatGraphEdgeCanBeConstructed(self):
        graphEdge = GraphEdge(fromNodeIndex=10, toNodeIndex=20, cost=30)

        self.assertEqual(10, graphEdge.From, "From")
        self.assertEqual(20, graphEdge.To, "To")
        self.assertEqual(30, graphEdge.Cost, "Cost")