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_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