def FirstPlot(): algo_name = "DFS search" input_data = [] exec_time = [] for n in range(100, 1100, 100): graphList = [] graph = DFS.Graph(g=graphList) vertexList = [] DFS.createGraph(graph, vertexList, n) vertexNum = random.randint(0, n - 1) start_time = time.clock() DFS.DFS(graph) end_time = time.clock() exec_time.append((end_time - start_time) * 1000) input_data.append(n) CreatePlot(input_data, exec_time, algo_name)
import unittest import DFS location = '/Users/faiyamrahman/Desktop/personal improvement/' \ +'make_ money/programming/Design and Analysis of Algorithms I/SCC.txt' x = DFS.Graph(location) class TestDFS(unittest.TestCase): ## def setUp(self): ## self.location_DRone = '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRone.txt' ## self.location_DRtwo = '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRtwo.txt' ## self.location_DRthree = '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRthree.txt' ## self.location_DRfour = '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRfour.txt' ## self.location_DRfive = '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRfive.txt' ## self.location_DRsix = '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRsix.txt' ## self.location_DRseven = '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRseven.txt' ## self.location_DReight= '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DReight.txt' ## self.location_DRnine= '/Users/faiyamrahman/Desktop/personal improvement/' \ ## +'make_ money/programming/Design and Analysis of Algorithms I/DRnine.txt' ## self.location_DRten= '/Users/faiyamrahman/Desktop/personal improvement/' \
def test_DFS(self): """! Testing of DFS Method """ g1 = DFS.Graph(False) g1.add_edge((0, 1)) g1.add_edge((0, 2)) g1.add_edge((1, 2)) g1.add_edge((2, 0)) g1.add_edge((2, 3)) self.assertEqual(g1.DFS(0), [0, 1, 2, 3]) g2 = DFS.Graph(True) g2.add_edge((0, 1)) g2.add_edge((1, 2)) g2.add_edge((2, 3)) g2.add_edge((3, 4)) g2.add_edge((4, 0)) self.assertEqual(g2.DFS(0), [0, 1, 2, 3, 4]) g3 = DFS.Graph(True) g3.add_edge((0, 1)) g3.add_edge((1, 0)) g3.add_edge((0, 2)) g3.add_edge((2, 0)) g3.add_edge((1, 2)) g3.add_edge((2, 1)) g3.add_edge((2, 3)) g3.add_edge((3, 2)) self.assertEqual(g3.DFS(0), [0, 1, 2, 3]) g4 = DFS.Graph(False) g4.add_edge((0, 1)) g4.add_edge((0, 2)) g4.add_edge((0, 3)) g4.add_edge((0, 4)) g4.add_edge((1, 3)) g4.add_edge((2, 5)) self.assertEqual(g4.DFS(0), [0, 1, 3, 2, 5, 4]) g5 = DFS.Graph(False) g5.add_edge((0, 2)) g5.add_edge((6, 3)) g5.add_edge((6, 2)) g5.add_edge((4, 5)) g5.add_edge((6, 5)) g5.add_edge((4, 1)) self.assertEqual(g5.DFS(0), [0, 2, 6, 3, 5, 4, 1]) g6 = DFS.Graph(False) g6.add_edge((0, 1)) g6.add_edge((0, 2)) g6.add_edge((0, 3)) g6.add_edge((0, 4)) g6.add_edge((0, 5)) g6.add_edge((0, 6)) g6.add_edge((1, 6)) g6.add_edge((6, 5)) g6.add_edge((5, 4)) g6.add_edge((4, 3)) g6.add_edge((3, 2)) g6.add_edge((2, 1)) self.assertEqual(g6.DFS(0), [0, 1, 6, 5, 4, 3, 2])
def test_eq(self): """! Testing of __eq__ Method """ g1 = DFS.Graph(False) g1.add_edge((0, 1)) g1.add_edge((0, 2)) g1.add_edge((1, 2)) g1.add_edge((2, 0)) g1.add_edge((2, 3)) g2 = DFS.Graph(False) g2.add_edge((0, 1)) g2.add_edge((0, 2)) g2.add_edge((1, 2)) g2.add_edge((2, 0)) g2.add_edge((2, 3)) self.assertEqual(True, g1 == g2) g3 = DFS.Graph(True) g3.add_edge((0, 1)) g3.add_edge((1, 2)) g3.add_edge((2, 3)) g3.add_edge((3, 4)) g3.add_edge((4, 0)) g4 = DFS.Graph(True) g4.add_edge((0, 1)) g4.add_edge((1, 2)) g4.add_edge((2, 3)) g4.add_edge((3, 4)) g4.add_edge((4, 0)) self.assertEqual(True, g3 == g4) g5 = DFS.Graph(True) g5.add_edge((0, 1)) g5.add_edge((1, 0)) g5.add_edge((0, 2)) g5.add_edge((2, 0)) g5.add_edge((1, 2)) g5.add_edge((2, 1)) g5.add_edge((2, 3)) g5.add_edge((3, 2)) g6 = DFS.Graph(True) g6.add_edge((0, 1)) g6.add_edge((1, 0)) g6.add_edge((0, 2)) g6.add_edge((2, 0)) g6.add_edge((1, 2)) g6.add_edge((2, 1)) g6.add_edge((2, 3)) g6.add_edge((3, 2)) self.assertEqual(True, g5 == g6) g7 = DFS.Graph(False) g7.add_edge((0, 1)) g7.add_edge((0, 2)) g7.add_edge((0, 3)) g7.add_edge((0, 4)) g7.add_edge((1, 3)) g7.add_edge((2, 5)) g8 = DFS.Graph(False) g8.add_edge((0, 1)) g8.add_edge((0, 2)) g8.add_edge((0, 3)) g8.add_edge((0, 4)) g8.add_edge((1, 3)) g8.add_edge((2, 5)) self.assertEqual(True, g7 == g8) g9 = DFS.Graph(False) g9.add_edge((0, 2)) g9.add_edge((6, 3)) g9.add_edge((6, 2)) g9.add_edge((4, 5)) g9.add_edge((6, 5)) g9.add_edge((4, 1)) g10 = DFS.Graph(False) g10.add_edge((0, 2)) g10.add_edge((6, 3)) g10.add_edge((6, 2)) g10.add_edge((4, 5)) g10.add_edge((6, 5)) g10.add_edge((4, 1)) self.assertEqual(True, g9 == g10) g11 = DFS.Graph(False) g11.add_edge((0, 1)) g11.add_edge((0, 2)) g11.add_edge((0, 3)) g11.add_edge((0, 4)) g11.add_edge((0, 5)) g11.add_edge((0, 6)) g11.add_edge((1, 6)) g11.add_edge((6, 5)) g11.add_edge((5, 4)) g11.add_edge((4, 3)) g11.add_edge((3, 2)) g11.add_edge((2, 1)) g12 = DFS.Graph(False) g12.add_edge((0, 1)) g12.add_edge((0, 2)) g12.add_edge((0, 3)) g12.add_edge((0, 4)) g12.add_edge((0, 5)) g12.add_edge((0, 6)) g12.add_edge((1, 6)) g12.add_edge((6, 5)) g12.add_edge((5, 4)) g12.add_edge((4, 3)) g12.add_edge((3, 2)) g12.add_edge((2, 1)) self.assertEqual(True, g11 == g12)
def main(): files = [] if len(sys.argv) < 3: usage() else: datainputfile = sys.argv[1] traversaltype = sys.argv[2] # datainputfile = '11PointDFSBFS.tsp' # traversaltype = 'BFS' totalpoints, coordlist = load(datainputfile) if (traversaltype == 'BFS'): print( "Using Traversal type : Breadth First Search Algorithm to find the shortest path" ) print( "Below are the list of all possible traversal paths and we are going to find shortest route among them" ) # Creating a BFScl object and storing the graph data in the object property g = BFScl.Graphbfscl(11) g.addEdge(1, 2) g.addEdge(1, 3) g.addEdge(1, 4) g.addEdge(2, 3) g.addEdge(3, 4) g.addEdge(3, 5) g.addEdge(4, 5) g.addEdge(4, 6) g.addEdge(4, 7) g.addEdge(5, 7) g.addEdge(5, 8) g.addEdge(6, 8) g.addEdge(7, 9) g.addEdge(7, 10) g.addEdge(8, 9) g.addEdge(8, 10) g.addEdge(8, 11) g.addEdge(9, 11) # Generating all possible paths by invoking the object member function g.generatePaths(1, 11) # reusing project1 code to calculate distance for list of paths and find shortest of all shortroute = modlist(g.finalpaths, totalpoints, coordlist) print("Shortest path using BFS Algorithm: ", shortroute) if (traversaltype == 'DFS'): print( "Using Traversal type : Depth First Search Algorithm to find the shortest path" ) print( "Below are the list of all possible traversal paths and we are going to find shortest route among them" ) # Creating a DFS object and storing the graph data in the object property g = DFS.Graph(11) g.addEdge(1, 2) g.addEdge(1, 3) g.addEdge(1, 4) g.addEdge(2, 3) g.addEdge(3, 4) g.addEdge(3, 5) g.addEdge(4, 5) g.addEdge(4, 6) g.addEdge(4, 7) g.addEdge(5, 7) g.addEdge(5, 8) g.addEdge(6, 8) g.addEdge(7, 9) g.addEdge(7, 10) g.addEdge(8, 9) g.addEdge(8, 10) g.addEdge(8, 11) g.addEdge(9, 11) graphdata = [[2, 3, 4], [3], [4, 5], [5, 6, 7], [7, 8], [8], [9, 10], [9, 10, 11], [11]] # Generating all possible paths by invoking the object member function print(g.graph) g.getAllPaths(1, 11) # reusing project1 code to calculate distance for list of paths and find the shortest of all shortroute = modlist(g.finalpaths, totalpoints, coordlist) print("Shortest path using DFS Algorithm: ", shortroute) plotgraph(totalpoints, coordlist, shortroute[0])