









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document presents a number of modules, each containing a coherent set of questions and exercises. Pick any module you deem interesting!
Typology: Exercises
1 / 15
This page cannot be seen from the preview
Don't miss anything!










This document presents a number of modules, each containing a coherent set of questions and exercises. Pick any module you deem interesting! If you don’t like it, or it’s too easy or too difficult, just switch to another. Each module has a difficulty rating, from 1 (easy) to 3 (difficult) stars. This difficulty is based on my estimate of how much experience one has with algorithms (and computer science in general). Each module also indicates the language used. The code can be found here: github.com/lbel/icsc2018. To run the software you need at least the following:
Python Python 2.7, 3.4 or newer. You can find an installa- tion of Python 2.7.5 on CentOS 7 at lxplus7.cern.ch, under /usr/bin/python.
C++ A C++ compiler supporting C++11, such as GCC 4.8 or newer. You can find an installation of GCC 4.8.5 on CentOS 7 at lxplus7.cern.ch, under /usr/bin/g++. This should be referenced automatically by the Makefiles.
Java JDK version 1.8 or newer. This is also installed on lxplus7, and will be picked up by the scripts.
BFS is very similar to DFS, except it exhausts all the children of the current node before moving on to their children. Another way to see it is that it traverses a tree level-by-level (from left to right in the ASCII visualisation provided by the script).
Exercises
2 ∗ Java collections (Java)
Java has very good out-of-the-box support for all the data structures dis- cussed in the lectures. In this exercise, you will verify their complex- ity by running large numbers of operations on them. We’ll be using the java.util.Collection interface, which is an interface common to all java collections. The advantage of this is that it supports similar operations through the same interface. For example, adding an object to a set and pushing an object to the back of an array both use the same method add.
3 ∗ Recursion and stack overflow (Python)
Recursive implementations can simplify many algorithms, such as the im- plementation of the Fibonacci sequence given in recursion/recursion.py. This script takes n as an argument and outputs the nth^ Fibonacci number. The recursion lies in the fact that the method fib repeatedly calls itself. This module is about inefficiencies and stack overflows (no, not the website).
Exercises
This implementation is rather terrible. We can speed it up a lot by using a cache.
Exercises
When running code, the computer keeps track of all the called methods, so that it knows how to proceed once the current method returns. This is
done in a buffer with a fixed size. That size is large enough that normally this causes no problems, but if we go very deep – as a recursive algorithm very well may – it is definitely possible to hit the limit. Let’s fix the code!
Exercises
You can find a cached implementation of the recursive algorithm in recursion/recursion_cache.py, and a solution of the final exercise in recursion/recursion_solution.py.
Exercises
An example implementation with decent (but not perfect) performance can be found in bloom/bloom_solution.py.
5 ∗∗ Dijkstra’s algorithm (Python)
In this module, you will implement Dijkstra’s algorithm. A framework containing a Graph data structure and several test cases can be found in dijkstra/dijkstra.py. Everything below the method called dijkstra are test cases. The first is trivial, and the second is the one presented in the lectures.
Questions
Next, let’s implement the actual algorithm! A skeleton implementation has been set up in the method dijkstra.
6 ∗ ∗ ∗ Linked lists (C++)
Linked lists were presented in the lectures as an alternative to array-based se- quential containers. This exercise explores some of their properties, strengths and weaknesses. The code relies heavily on C++14 features – don’t hesitate to ask if you have any questions about that! The folder linked_list contains two source files: linked_list.h fea- tures an almost compelete (singly-)linked list implementation, and main.cxx has some code to test it. There’s also a small shell script to compile the code.
Exercises
Now look at the linked list implementation. It features a lot of methods that coincide with the definitions of std::vector, including support for iter- ation (which is already used in the program to print the elements). However, there is a lot of duplicate and inefficient code.
Exercises