class TestSearch(unittest.TestCase):
    def setUp(self):
        self.randG = RandomGraph()

    def testBidirectional(self):
        for i in range(50):
            adjList = self.randG.randomGnmGraph(n=50,
                                                M=1000,
                                                isWeighted=False,
                                                isDirected=False)
            src = random.choice(adjList.keys())
            dest = random.choice(adjList.keys())
            dist1, prev1 = bfs(adjList, src)
            dist2, prev2 = bidirectional(adjList, src, dest)
            self.assertEqual(dist1[dest], dist2)

    def testIterativeDeepening(self):
        for i in range(50):
            adjList = self.randG.randomGnmGraph(n=50,
                                                M=1000,
                                                isWeighted=False,
                                                isDirected=False)
            src = random.choice(adjList.keys())
            dest = random.choice(adjList.keys())
            dist1, prev1 = bfs(adjList, src)
            dist2, prev2 = iterative_deepening(adjList, src, dest)
            self.assertEqual(dist1[dest], dist2[dest])
Beispiel #2
0
def main(script, n='10', *args):

    # create n Vertices
    n = int(n)
    labels = string.ascii_lowercase + string.ascii_uppercase
    vs = [Vertex(c) for c in labels[:n]]

    # Create complete graph
    # g = Graph(vs)
    # g.add_all_edges()

    # Create regular graph
    # g = Graph(vs)
    # g.add_regular_edges(4)

    # Create random graph
    g = RandomGraph(vs)
    g.add_random_edges(0.5)

    layout = CircleLayout(g)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(g, layout)
    gw.mainloop()
class TestShortest(unittest.TestCase):
    def setUp(self):
        self.randG = RandomGraph()

    def testUnweighted(self):
        for i in range(50):
            adjList = self.randG.randomGnmGraph(n=50,
                                                M=1000,
                                                isWeighted=False,
                                                isDirected=False)
            src = random.choice(list(adjList.keys()))
            dist1, prev1 = bfs(adjList, src)
            adjList = self.randG.toWeighted(adjList)
            dist3, prev3 = dijkstra(adjList, src)
            self.assertEqual(dist1, dist3)

    def testWeighted(self):
        for i in range(50):
            adjList = self.randG.randomGnmGraph(n=50, M=1000, isWeighted=True)
            nodes, edges = self.randG.toNodeEdges(adjList)
            src = random.choice(list(adjList.keys()))
            dist1, prev1 = dijkstra(adjList, src)
            dist2, prev2 = bellman_ford(nodes, edges, src)
            dist3, prev3 = shortest_path_faster(adjList, src)
            self.assertEqual(dist1, dist2)
            self.assertEqual(dist2, dist3)

    def testFloydWarshall(self):
        for i in range(50):
            adjList = self.randG.randomGnmGraph(n=50, M=1000, isWeighted=True)
            nodes, edges = self.randG.toNodeEdges(adjList)
            src = random.choice(list(adjList.keys()))
            dist1, prev1 = dijkstra(adjList, src)
            dist2, prev2 = floyd_warshall(nodes, edges)
            self.assertEqual(dist1, dist2[src])

    def testAllPairs(self):
        for i in range(50):
            adjList = self.randG.randomGnmGraph(n=50, M=1000, isWeighted=True)
            nodes, edges = self.randG.toNodeEdges(adjList)
            src = random.choice(list(adjList.keys()))
            dist1, prev1 = floyd_warshall(nodes, edges)
            dist2, prev2 = johnson(nodes, edges)
            self.assertEqual(dist1, dist2)
                    default=50,
                    type=int,
                    help="Number of nodes in Erdos-Renyi Graph")
parser.add_argument(
    "--p",
    default=0.25,
    type=float,
    help="Probability of edge being present in Erdos-Renyl Graph")
parser.add_argument("--n_iter",
                    default=1000,
                    type=int,
                    help="Number of iterations for simulation to run")
args = parser.parse_args()

if __name__ == "__main__":
    randomGraph = RandomGraph(n_nodes=args.nodes, p=args.p)
    randomGraph.gen_RandomPartitions()
    randomGraph.calculateSpectrum()
    randomGraph.showPartitions(args.savefigures)
    randomGraph.SwapNodes(getSwapNodes(randomGraph))

    fig, [[axis1, axis2], [axis3, axis4]] = plt.subplots(2,
                                                         2,
                                                         figsize=(16, 16))
    history = np.array(randomGraph.history, dtype=np.float)
    axis1.plot(history[1:, 1], ":r*", label='Component 0')
    axis1.axhline(y=history[0][1],
                  color='r',
                  label='Initial value(Component 0)',
                  linestyle="-")
    axis1.plot(history[1:, 2], ":g^", label='Component 1')
 def setUp(self):
     self.randG = RandomGraph()