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]")
def test_SpecifyThatTwoNodesCanBeAddedToTheGraph(self): sparseGraph = SparseGraph(isDigraph=False) sparseGraph.AddNode( GraphNode(index=sparseGraph.GetNextFreeNodeIndex())) sparseGraph.AddNode( GraphNode(index=sparseGraph.GetNextFreeNodeIndex())) self.assertEqual(2, len(sparseGraph._nodes), "_nodes") self.assertEqual(2, len(sparseGraph._edgeListVector), "_edgeListVector") self.assertEqual(0, len(sparseGraph._edgeListVector[0]), "_edgeListVector") self.assertEqual(2, sparseGraph._nextNodeIndex, "_nextNodeIndex")
def test_SpecifyThatRetrievingAnInvalidNodeRaisesAnException(self): sparseGraph = SparseGraph(isDigraph=False) node = GraphNode(index=sparseGraph.GetNextFreeNodeIndex()) sparseGraph.AddNode(node) self.assertRaises(Exception, sparseGraph.GetNode, -1) self.assertRaises(Exception, sparseGraph.GetNode, 1)
def test_SpecifyThatANodeCanBeRetrievedFromTheGraph(self): sparseGraph = SparseGraph(isDigraph=False) node = GraphNode(index=sparseGraph.GetNextFreeNodeIndex()) sparseGraph.AddNode(node) retrievedNode = sparseGraph.GetNode(0) self.assertEqual(node, retrievedNode)
def test_SpecifyThatANodeCanAddedInplaceOfACurrentlyDeactivatedNode(self): sparseGraph = SparseGraph(isDigraph=False) nodeIndex = sparseGraph.GetNextFreeNodeIndex() node = GraphNode(nodeIndex) sparseGraph.AddNode(node) self.assertEqual(1, len(sparseGraph._nodes), "_nodes 1") self.assertEqual(0, sparseGraph._nodes[nodeIndex].Index, "node.Index 1") # Deactivate the node node.Index = NodeType.InvalidNodeIndex self.assertEqual(NodeType.InvalidNodeIndex.value, sparseGraph._nodes[nodeIndex].Index, "node.Index 2") # Add a new node in place of a deactivate node in the graph newNode = GraphNode(nodeIndex) sparseGraph.AddNode(newNode) self.assertEqual(1, len(sparseGraph._nodes), "_nodes 3") self.assertEqual(0, sparseGraph._nodes[nodeIndex].Index, "node.Index 3")
def test_SpecifyThatAnExceptionIsRaiseWhenAddingANodeInplaceOfACurrentlyActivatedNode( self): sparseGraph = SparseGraph(isDigraph=False) nodeIndex = sparseGraph.GetNextFreeNodeIndex() node = GraphNode(nodeIndex) sparseGraph.AddNode(node) self.assertEqual(1, len(sparseGraph._nodes), "_nodes 1") self.assertEqual(0, sparseGraph._nodes[nodeIndex].Index, "node.Index 1") # Add a new node in place of a active node in the graph newNode = GraphNode(nodeIndex) self.assertRaises(Exception, sparseGraph.AddNode, newNode)
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 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
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