0% found this document useful (1 vote)
185 views

Python Questions

This document describes tasks for a workshop on algorithms and programming fundamentals in Python. Students are asked to implement recursive functions for problems like Euclid's algorithm, list reversal, and palindrome checking. They are also asked to implement depth-first search and breadth-first search graph traversal algorithms, including modifications to stop at goal vertices and return only the path to the goal.

Uploaded by

Alireza Kafaei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
185 views

Python Questions

This document describes tasks for a workshop on algorithms and programming fundamentals in Python. Students are asked to implement recursive functions for problems like Euclid's algorithm, list reversal, and palindrome checking. They are also asked to implement depth-first search and breadth-first search graph traversal algorithms, including modifications to stop at goal vertices and return only the path to the goal.

Uploaded by

Alireza Kafaei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FIT1045/1053 Algorithms and Programming Fundamentals in

Python – Workshop 08.

Objectives
After this workshop, you should be able to:
ˆ solve simple problems by recursive functions

ˆ implement and understand the difference between depth-first and breadth-first search

MARKS: Marks for this workshop will be determined by correctness of implementation as well as meaningful
attempt to solve the conceptual tasks as specified in the instructions.

Task 1: Recursive Problem Solving


Part A: Euclid’s Algorithm
Re-implement Euclid’s algorithm using a recursive function gcd(a, b).

Part B: List Inversion


Write a recursive function reverse(lst) that computes the reversion of lst. For example:
>>> reverse ([1 , 2 , 3 , 4])
[4 , 3 , 2 , 1]
>>> reverse ([10 , 11 , 12 , 13 , 14])
[14 , 13 , 12 , 11 , 10]
>>> reverse ([1])
[1]
>>> reverse ([])
[]

Part C: Palindromes
Write a recursive function is pal(string) to check if a string is palindrome. A palindrome string is a string
that reads the same form either direction. For example:
>>> is_pal ( ' aa ' )
True
>>> is_pal ( ' aabb ' )
False
>>> is_pal ( ' aba ' )
True

Task 2: Graph Traversals


Warm-Up
Re-implement the functions dfs traversal and bfs traversal from the lecture. Then import the provided
module graphs and familiarise yourself with its functionality to visualise traversals in grid graphs. For example,
in your interactive session you can start by trying out the following:

1
>>> import graphs
>>> g = graphs.ex_tree
>>> graphs.print_grid_traversal(g, 10, [0, 1, 2, 12])
000---001---002 ***---*** ***---*** ***---***---***
| | | | |
***---***---003---***---***---***---***---***---*** ***
| | | | | |
***---***---***---***---*** *** *** *** *** ***
| | | | |
*** *** ***---***---***---*** ***---*** ***---***
| | | |
*** ***---*** *** ***---***---***---***---***---***
>>> graphs.print_grid_traversal(g, 10, bfs_traversal(g, 0))
000---001---002 008---013 019---026 033---038---042
| | | | |
007---004---003---005---009---014---020---027---034 045
| | | | | |
015---010---006---011---017 021 028 035 039 048
| | | | |
022 016 012---018---024---031 036---040 043---046
| | | |
029 023---030 025 032---037---041---044---047---049
>>> graphs.print_grid_traversal(g, 10, dfs_traversal(g, 0))
000---001---002 046---047 044---045 039---040---041
| | | | |
049---048---003---025---026---027---029---033---035 042
| | | | | |
022---018---004---016---017 028 030 034 036 043
| | | | |
023 019 005---006---008---015 031---032 037---038
| | | |
024 020---021 007 009---010---011---012---013---014
Do you recognise the dfs and bfs patterns in the printed traversal order? Try to predict what will happen if
you start the traversal in a different vertex than 0. Test your prediction by visualising the traversals with the
above function.

Part A: Goal vertices


Modify the functions dfs traversal and bfs traversal such that they accept an optional third parameter
goals with default value an empty collection (set() or []). The behaviour of both traversal functions should
be such that the traversal should be stopped as soon as one of the goal vertices is visited. For example:
>>> graphs.print_grid_traversal(g, 10, bfs_traversal(g, 0, {12}))
000---001---002 ***---*** ***---*** ***---***---***
| | | | |
***---***---003---***---***---***---***---***---*** ***
| | | | | |
***---***---***---***---*** *** *** *** *** ***
| | | | |
*** *** ***---***---***---*** ***---*** ***---***
| | | |
*** ***---*** *** ***---***---***---***---***---***
Can you find inputs (by varying the input graph, the starting vertex and/or the goal vertices) such that the bfs
traversal is longer than the dfs traversal and vice versa? You can use the functionality from the graphs module
to generate other graphs and optionally add/remove individual edges by modifying the adjacency matrices.

Part B: Path to goal


Write new functions dfs path and bfs path that take the same arguments as dfs traversal and bfs traversal
but that, instead of returning the entire traversal, they only return the path from the start vertex to the goal
vertex that has been reached. For example:

2
>>> print_grid_traversal(g, 10, dfs_traversal(g, 0, {9, 39}))
000---001---002 ***---*** ***---*** ***---***---***
| | | | |
***---***---003---025---026---027---029---033---035 ***
| | | | | |
022---018---004---016---017 028 030 034 036 ***
| | | | |
023 019 005---006---008---015 031---032 037---038
| | | |
024 020---021 007 009---010---011---012---013---014
>>> print_grid_traversal(g, 10, dfs_path(g, 0, {9, 39}))
000---001---002 ***---*** ***---*** ***---***---***
| | | | |
***---***---003---004---005---006---007---008---009 ***
| | | | | |
***---***---***---***---*** *** *** *** 010 ***
| | | | |
*** *** ***---***---***---*** ***---*** 011---012
| | | |
*** ***---*** *** ***---***---***---***---***---***
Can you find inputs (by varying the input graph, the starting vertex and/or the goal vertices) such that the bfs
path is shorter than the dfs path (although the corresponding dfs traversal is shorter than the bfs traversal)?
Discuss your findings in a short text in the doc string at the end of your module file. Include
visualisations of your discovered example inputs by copying the corresponding output of the
print grid traversal function.

You might also like