mervebilgin / DFS-and-BFS-Comparison-in-Python-

The comparison will be done on the basis of length of the final path, length of the search path and the execution time plus the discussion on the memory complexit.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DFS and BFS Comparison in Python

The comparison will be done on the basis of length of the final path, length of the search path and the execution time plus the discussion on the memory complexit.

BFS (Breath First Search):

from BFSfile import BFS

Breadth-First Search is a recursive algorithm to search all the vertices of a graph or a tree. BFS in python can be implemented by using data structures like a dictionary and lists. Breadth-First Search in tree and graph is almost the same.

BFS BFS2

DFS (Depth First Search):

from DFSFile import DFS

Traversal means that visiting all the nodes of a graph which can be done through Depth-first search or Breadth-first search in python. Depth-first traversal or Depth-first Search is an algorithm to look at all the vertices of a graph or tree data structure.

DFS1 DFS2

Pyamaze:

from pyamaze import maze, agent, COLOR, textLabel

The module pyamaze is created for the easy generation of random maze and apply different search algorithm efficiently. The main idea of this module, pyamaze, is to assist in creating customizable random mazes and be able to work on that, like applying the search algorithm with much ease.

Timeit:

from timeit import timeit

Python timeit() is a method in Python library to measure the execution time taken by the given code snippet. The Python library runs the code statement 1 million times and provides the minimum time taken from the given set of code snippets.

+ from BFSfile import BFS
+ from DFSFile import DFS
+ from pyamaze import maze, agent, COLOR, textLabel
+ from timeit import timeit
+ m=maze(15,20) 
# I am creating a maze of size 15 by 20
# m.CreateMaze(loopPercent=100)
# loopercent=100 meaning the maximum number of loops in the maze path
#Internally the last cell i.e. the last row and last column cell is set as the start cell. 
+ m.CreateMaze(3,5,loopPercent=100, theme='light') 
# My goal row and column (3,5)
# With loopPercent we can create a maze with multiple paths.
# loopPercent=100, means it will maximize the number of multiple paths
# and here i have called the function bfs and dfs 
# first is the search path than the reverse path from the goal to the start and the second is the path from start to goal
+ searchPath,dfsPath,fwdDFSPath=DFS(m)
+ bSearch,bfsPath,fwdBFSPath=BFS(m)
# but will simply display the sizes of the two such spaces and then i can easily compare those, 
# in these four lines we are displaying the two such space lengths of the two algorithms and also the length of the final paths calculated by two algorithms
+ textLabel(m,'DFS Path Length',len(fwdDFSPath)+1)
+ textLabel(m,'BFS Path Length',len(fwdBFSPath)+1)
+ textLabel(m,'DFS Search Length',len(searchPath)+1)
+ textLabel(m,'BFS Search Length',len(bSearch)+1)
# Here we are simulating an agent travelling on the path calculated by the algorithms first a blue colored agent will follow the path caculated by bfs and 
# then small size red colored agent will traverse the path calculated by dfs
+ a=agent(m,footprints=True,color=COLOR.blue,filled=True)
+ b=agent(m,footprints=True,color=COLOR.red)
# footprints:I used it to be able to visualize the full path when the agent moves through the maze.
+ m.tracePath({a:fwdBFSPath},delay=100)
+ m.tracePath({b:fwdDFSPath},delay=100)
# Here we are calculating the time taken by the two algorithms to compute the final path for that we are using the time it  function of the time it module 
# for that we need to provide the statement we wnat to execute as a string
+ t1=timeit(stmt='DFS(m)',number=1000,globals=globals())
+ t2=timeit(stmt='BFS(m)',number=1000,globals=globals())
+ t1=timeit(stmt='DFS(m)',number=1000,globals=globals())
+ t2=timeit(stmt='BFS(m)',number=1000,globals=globals())
#timeit: provides the minimum time taken from the given set of code snippets
# Then we need to provide the complete global space of this program to the time it function it is needed 
# because the statement provided here will get exacuted in the time it module and the statement is exacuting function or bfs 
# which are defined in this file so the definition of all variables and functions as tetra are passed to the time it module and then here we are displaying those times
+ textLabel(m,'DFS Time',t1)
+ textLabel(m,'BFS Time',t2)
- finally is the run function to run the simulation before i run this program, DFS doesnt guarentee to provide the shortest path while the BFS quarentees to provide the shortest path
+ m.run()
# the function run to run the simulation

Adsız tasarım (1)

About

The comparison will be done on the basis of length of the final path, length of the search path and the execution time plus the discussion on the memory complexit.


Languages

Language:Python 100.0%