Programowanie 2009 Programming 2009

Podobne dokumenty
Helena Boguta, klasa 8W, rok szkolny 2018/2019

Weronika Mysliwiec, klasa 8W, rok szkolny 2018/2019

TTIC 31210: Advanced Natural Language Processing. Kevin Gimpel Spring Lecture 9: Inference in Structured Prediction

Haskell Moduły Ładowanie

ARNOLD. EDUKACJA KULTURYSTY (POLSKA WERSJA JEZYKOWA) BY DOUGLAS KENT HALL

y = The Chain Rule Show all work. No calculator unless otherwise stated. If asked to Explain your answer, write in complete sentences.

Revenue Maximization. Sept. 25, 2018

Machine Learning for Data Science (CS4786) Lecture 11. Spectral Embedding + Clustering

TTIC 31210: Advanced Natural Language Processing. Kevin Gimpel Spring Lecture 8: Structured PredicCon 2

Katowice, plan miasta: Skala 1: = City map = Stadtplan (Polish Edition)

Machine Learning for Data Science (CS4786) Lecture11. Random Projections & Canonical Correlation Analysis

Wstęp do Programowania potok funkcyjny

Compressing the information contained in the different indexes is crucial for performance when implementing an IR system

Zarządzanie sieciami telekomunikacyjnymi

Steeple #3: Gödel s Silver Blaze Theorem. Selmer Bringsjord Are Humans Rational? Dec RPI Troy NY USA

Algorytmy funkcjonalne i struktury danych

Stargard Szczecinski i okolice (Polish Edition)

Tychy, plan miasta: Skala 1: (Polish Edition)

OpenPoland.net API Documentation

Miedzy legenda a historia: Szlakiem piastowskim z Poznania do Gniezna (Biblioteka Kroniki Wielkopolski) (Polish Edition)

Karpacz, plan miasta 1:10 000: Panorama Karkonoszy, mapa szlakow turystycznych (Polish Edition)

Rozpoznawanie twarzy metodą PCA Michał Bereta 1. Testowanie statystycznej istotności różnic między jakością klasyfikatorów

Programowanie. Lista zadań nr 15. Na ćwiczenia 11, 19 i 23 czerwca 2008

Linear Classification and Logistic Regression. Pascal Fua IC-CVLab

Hard-Margin Support Vector Machines

MaPlan Sp. z O.O. Click here if your download doesn"t start automatically

Zaawansowane metody programowania. Algorytmy

Dolny Slask 1: , mapa turystycznosamochodowa: Plan Wroclawia (Polish Edition)

prowadzący dr ADRIAN HORZYK /~horzyk tel.: Konsultacje paw. D-13/325

dodatkowe operacje dla kopca binarnego: typu min oraz typu max:


EXAMPLES OF CABRI GEOMETRE II APPLICATION IN GEOMETRIC SCIENTIFIC RESEARCH

Zmiany techniczne wprowadzone w wersji Comarch ERP Altum

PROCESORY ARM TRUDNO ZNALEŹĆ PROCESORY O TAK LICZNYCH, ORYGINALNYCH, NOWYCH, POMYSŁOWYCH ROZWIĄZANIACH!

Zakopane, plan miasta: Skala ok. 1: = City map (Polish Edition)

Convolution semigroups with linear Jacobi parameters

Elementy języka Haskell

METHOD 2 -DIAGNOSTIC OUTSIDE

Algorytmy i struktury danych. Drzewa: BST, kopce. Letnie Warsztaty Matematyczno-Informatyczne

Podstawy programowania 2. Temat: Drzewa binarne. Przygotował: mgr inż. Tomasz Michno

Gradient Coding using the Stochastic Block Model

Rachunek lambda, zima

Prezentacja o Haskell u(rozdział 3 i 4)


Wybrzeze Baltyku, mapa turystyczna 1: (Polish Edition)

Arrays -II. Arrays. Outline ECE Cal Poly Pomona Electrical & Computer Engineering. Introduction

Mixed-integer Convex Representability

Proposal of thesis topic for mgr in. (MSE) programme in Telecommunications and Computer Science

SubVersion. Piotr Mikulski. SubVersion. P. Mikulski. Co to jest subversion? Zalety SubVersion. Wady SubVersion. Inne różnice SubVersion i CVS

ERASMUS + : Trail of extinct and active volcanoes, earthquakes through Europe. SURVEY TO STUDENTS.

Wstęp do Programowania potok funkcyjny

Wojewodztwo Koszalinskie: Obiekty i walory krajoznawcze (Inwentaryzacja krajoznawcza Polski) (Polish Edition)

deep learning for NLP (5 lectures)

Wykład 9 Funktory (moduły sparametryzowane)

Nazwa projektu: Kreatywni i innowacyjni uczniowie konkurencyjni na rynku pracy

Stos LIFO Last In First Out

Algorytmy i Struktury Danych. Co dziś? Drzewo decyzyjne. Wykład IV Sortowania cd. Elementarne struktury danych

Kolejka priorytetowa. Często rozważa się kolejki priorytetowe, w których poszukuje się elementu minimalnego zamiast maksymalnego.

MS Visual Studio 2005 Team Suite - Performance Tool

General Certificate of Education Ordinary Level ADDITIONAL MATHEMATICS 4037/12

Wysokość drzewa Głębokość węzła

- nawiasy kwadratowe oznaczają, że to lista

Programowanie w VB Proste algorytmy sortowania

Języki programowania Haskell

Previously on CSCI 4622

How to translate Polygons

Drzewa binarne. Drzewo binarne to dowolny obiekt powstały zgodnie z regułami: jest drzewem binarnym Jeśli T 0. jest drzewem binarnym Np.

Title: On the curl of singular completely continous vector fields in Banach spaces

Wojewodztwo Koszalinskie: Obiekty i walory krajoznawcze (Inwentaryzacja krajoznawcza Polski) (Polish Edition)

SSW1.1, HFW Fry #20, Zeno #25 Benchmark: Qtr.1. Fry #65, Zeno #67. like

Rev Źródło:

Programowanie 2009 Programming 2009

Installation of EuroCert software for qualified electronic signature

Wstęp do Programowania potok funkcyjny

Algorytmy i struktury danych Sortowanie IS/IO, WIMiIP

Roland HINNION. Introduction

Miedzy legenda a historia: Szlakiem piastowskim z Poznania do Gniezna (Biblioteka Kroniki Wielkopolski) (Polish Edition)

Podstawowe algorytmy i ich implementacje w C. Wykład 9

Aktualizacja Oprogramowania Firmowego (Fleszowanie) Microprocessor Firmware Upgrade (Firmware downloading)

Liczbę 29 możemy zaprezentować na siedem różnych sposobów:

DUAL SIMILARITY OF VOLTAGE TO CURRENT AND CURRENT TO VOLTAGE TRANSFER FUNCTION OF HYBRID ACTIVE TWO- PORTS WITH CONVERSION

Agnostic Learning and VC dimension

Pielgrzymka do Ojczyzny: Przemowienia i homilie Ojca Swietego Jana Pawla II (Jan Pawel II-- pierwszy Polak na Stolicy Piotrowej) (Polish Edition)

First-order logic. Usage. Tautologies, using rst-order logic, relations to natural language

Wstęp do programowania. Różne różności

Programowanie strukturalne. Opis ogólny programu w Turbo Pascalu

XML. 6.6 XPath. XPath is a syntax used for selecting parts of an XML document

Algorytmy i struktury danych. wykład 5

Machine Learning for Data Science (CS4786) Lecture 24. Differential Privacy and Re-useable Holdout

Struktury danych: stos, kolejka, lista, drzewo

POLITYKA PRYWATNOŚCI / PRIVACY POLICY

Instrukcja obsługi User s manual

Drzewa poszukiwań binarnych

PARADYGMATY I JĘZYKI PROGRAMOWANIA. Haskell. (w11)

USB firmware changing guide. Zmiana oprogramowania za przy użyciu połączenia USB. Changelog / Lista Zmian

Algorytmy i Struktury Danych

Zdecyduj: Czy to jest rzeczywiście prześladowanie? Czasem coś WYDAJE SIĘ złośliwe, ale wcale takie nie jest.

Angielski bezpłatne ćwiczenia - gramatyka i słownictwo. Ćwiczenie 4

Wprowadzenie do programu RapidMiner, część 2 Michał Bereta 1. Wykorzystanie wykresu ROC do porównania modeli klasyfikatorów

Analysis of Movie Profitability STAT 469 IN CLASS ANALYSIS #2

Transkrypt:

Programowanie 2009 Programming 2009 Lista zadań nr 5 Problem set no. 5 Na zajęcia 31 marca 1 kwietnia 2009 Due April 1, 2009 Grupy zasadnicze Zadanie 1 (1 pkt). Zaprogramuj w Haskellu Basic groups Problem 1 (1 p). Define in Haskell a function roots :: (Double, Double, Double) -> [Double] wyznaczającą listę miejsc zerowych trójmianu kwadratowego o podanych współczynnikach. Wskazówka: typ Double należy do klasy Ord, zdefiniowano więc dla niego metodę which finds a list of roots of a quadratic function with given coefficients. Hint: the type Double belongs to the class Ord, so the method compare :: Double -> Double -> Ordering gdzie is defined for it, where data Ordering = LT EQ GT deriving (Eq, Ord, Enum, Read, Show, Bounded) W preludium standardowym zdefiniowano też A function sqrt :: (Floating a) => a -> a a typ Double należy do klasy Floating. Zadanie 2 (1 pkt). Niech is also defined in the standard prelude and the type Double belongs to the class Floating. Problem 2 (1 p). Let data Roots = No One Double Two (Double, Double) deriving Show roots :: (Double, Double, Double) -> Roots Zaprogramuj tę. Czy jest lepsza niż poprzednia? Podaj argumenty za i przeciw. Skomentuj następnie funkcje o sygnaturach: Define this function. Is it better than the previous one? Give some pros and cons. Then comment functions having the following signatures: roots :: Double -> Double -> Double -> [Double] roots :: [Double] -> [Double] Zadanie 3 (1 pkt). Nie korzystając z faktu, że typ Integer należy do klasy Show zaprogramuj Problem 3 (1 p). Not using the fact that the Integer type belongs to the Show class define a function integertostring :: Integer -> String Wskazówka: wykorzystaj Hint: use the function unfoldr :: (b -> Maybe (a, b)) -> b -> [a] 1

z modułu List daną wzorem from the List module, defined by the formula unfoldr f b = case f b of Nothing -> [] Just (a,b) -> a : unfoldr f b gdzie where data Maybe a = Nothing Just a deriving (Eq, Ord, Read, Show) Moduł Char udostępnia The Char module exports the function inttodigit :: Int -> Char preludium standardowe oferuje the standard prelude offers the function fromenum :: (Enum a) => a -> Int a typ Integer należy do klasy Enum. Zadanie 4 (1 pkt). Zdefiniuj listę and the Integer type belongs to the Enum class. Problem 4 (1 p). Define a list nat2 :: [(Integer,Integer)] zawierającą wszystkie pary nieujemnych liczb całkowitych w kolejności określonej przez znany z dowodu tw. Cantora porządek that contains all pairs of nonnegative integers ordered by the relation known from the proof of Cantor theorem: (x 1, y 1 ) (x 2, y 2 ) x 1 + y 1 < x 2 + y 2 (x 1 + y 1 = x 2 + y 2 x 1 < x 2 ) tj. takiej, że i.e., such that nat2 = [(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(0,3),(1,2),(2,1),(3,0),... ] Wskazówka: definicja powinna zmieścić się w jednym wierszu długości mniejszej niż 45 znaków. Zauważ, że suma współrzędnych punktów leżących na tej samej przekątnej jest stała. Zadanie 5 (1 pkt). Zaprogramuj w Haskellu algorytm sortowania przez proste wstawianie. Zadanie 6 (1 pkt). Zaprogramuj w Haskellu algorytm sortowania przez proste wybieranie. Zadanie 7 (1 pkt). Zaprogramuj w Haskellu algorytm Quicksort. Grupy rozszerzone Zadanie 1 (1 pkt). Zaprogramuj w Haskellu algorytm Mergesort. Zadanie 2 (1 pkt). Wykorzystaj Hint: the definition should fit in one line shorter than 45 characters. Notice that the sum of coordinates of points laying on on the same diagonal is constant. Problem 5 (1 p). Define in Haskell the insertion sort algorithm. Problem 6 (1 p). Define in Haskell the selection sort algorithm. Problem 7 (1 p). Define in Haskell the Quicksort algorithm. Extended groups Problem 1 (1 p). Define in Haskell the Mergesort algorithm. Problem 2 (1 p). Use the function merge :: Ord a => [a] -> [a] -> [a] 2

do rozwiązania problemu 2-3-5 Dijkstry: zdefiniuj nieskończony rosnący ciąg liczb całkowitych zawierający liczbę 1 i taki, że jeśli n jest elementem tego ciągu, to są nimi też 2n, 3n i 5n. Uwaga: w odróżnieniu od funkcji wykorzystanej w algorytmie Mergesort, ta funkcja merge powinna usuwać duplikaty. Zadanie 3 (1 pkt). Rozważmy binarne drzewa poszukiwań Zaprogramuj w Haskellu następujące funkcje Zadanie 4 (1 pkt). Binarne drzewa poszukiwań służą jako efektywne reprezentacjami zbiorów skończonych. Przyjmuje się wówczas, że drzewa nie zawierają duplikatów. Zaprogramuj następujące operacje: d235 :: [Integer] to solve the Dijkstra 2-3-5 problem: define an infinite increasing sequence of integers containing 1 and such that if n belongs to that sequence, then so do 2n, 3n and 5n. Remark: in contrast to the function used in the Mergesort algorithm, this function merge should remove duplicates. Problem 3 (1 p). Consider binary search trees data Tree a = Node (Tree a) a (Tree a) Leaf Define the following functions in Haskell insert :: Ord a => a -> Tree a -> Tree a flatten :: Tree a -> [a] treesort :: Ord a => [a] -> [a] Problem 4 (1 p). Binary search trees serve as effective representations of finite sets. It is assumed that they do not contain duplicates. Define the following operations: type Set a = Tree a empty :: Set a isempty :: Ord a => Set a -> Bool singleton :: Ord a => a -> Set a fromlist :: Ord a => [a] -> Set a union :: Ord a => Set a -> Set a -> Set a intersection :: Ord a => Set a -> Set a -> Set a member :: Ord a => a -> Set a -> Bool Zadanie 5 (1 pkt). Zbiory, także nieskończone, można reprezentować w postaci ich funkcji charakterystycznych: Zdefiniuj następujące operacje: newtype FSet a = FSet (a -> Bool) Problem 5 (1 p). Sets, even infinite ones, can be represented by their characteristic functions: Define the following operations: empty :: FSet a singleton :: Ord a => a -> FSet a fromlist :: Ord a => [a] -> FSet a union :: Ord a => FSet a -> FSet a -> FSet a intersection :: Ord a => FSet a -> FSet a -> FSet a member :: Ord a => a -> FSet a -> Bool Zadanie 6 (1 pkt). Kopiec n-elementowy to drzewo binarne, w którym k-ty element (1 k n) znajduje się w wierzchołku na końcu ścieżki prowadzącej od korzenia poprzez wierzchołki zgodnie z dwójkowym rozwinięciem liczby k od najmniej znaczącej cyfry do cyfry poprzedzającej najbardziej znaczącą jedynkę. Cyfra 0 w rozwinięciu oznacza, że wybieramy lewego syna, 1 zaś że prawego. Np. 6. element kopca znajduje się w wierzchołku będącym prawym synem lewego syna. Kopiec spełnia też w każdym wierzchołku warunek kopca: etykiety synów są nie mniejsze niż etykieta danego wierzchołka. Niech 3 Problem 6 (1 p). An n-element heap is a binary tree, in which the k-th element (1 k n) is placed in a node at the end of a path leading from the root of the tree through nodes according to the binary representation of the number k from the least significant digit to a digit that precedes the most significant one. The digit 0 means that we choose the left son, 1 that the right. E.g., the 6-th element of a heap is put in the node which is the right son of the left son of the root. The heap also satisfies in every node the heap condition: the labels of the sons are not smaller than the label of the node. Let

newtype Heap a = Heap (Int, Tree a)) gdzie konstruktor Heap przechowuje kopiec i liczbę jego elementów. Zaprogramuj w Haskellu następujące funkcje where the constructor Heap stores a heap and the number of its elements. Define the following functions in Haskell insert :: Ord a => a -> Heap a -> Heap a getmin :: Ord a => Heap a -> Maybe (a, Heap a) heapsort :: Ord a => [a] -> [a] Zadanie 7 (1 pkt). Rozważmy Problem 7 (1 p). Consider the function foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) Dla jakich par funkcji f i f zachodzi równość For which pairs of functions f i f the following equation holds? unfoldr f (foldr f z xs) = xs Grupa zaawansowana Zadanie 1 (2 pkt). Rozważmy maszynę RAM przechowującą w pamięci tylko do odczytu jednokierunkową listę wiązaną długości n. Maszyna ma dodatkowo m komórek pamięci do odczytu i zapisu. Zamierzamy odwiedzić elementy tej listy w kolejności od ostatniego do pierwszego. Łatwo zauważyć, że jeśli mamy do dyspozycji m = Ω(n) dodatkowych komórek pamięci, to wystarczy odwrócić listę i przejść odwróconą listę w naturalnej kolejności. Czas działania takiego algorytmu wynosi O(n). Łatwo też znaleźć algorytm, który przechodzi listę do tyłu w stałej pamięci, ale w czasie kwadratowym. Pokaż, że dla dowolnego ustalonego k N można przejść listę pod prąd w czasie O(n 1+1/k ) używając m = O(1) dodatkowych komórek pamięci. Wskazówka: wymyśl wpierw algorytm, który zużywa m = O( n) komórek pamięci i działa w czasie O(n 3/2 ). Zadanie 2 (2 pkt). Zaprogramuj algorytm Mergesort w Prologu i Haskellu. W Prologu użyj list różnicowych, by ograniczyć kopiowanie i śmiecenie. Jak można zmniejszyć śmiecenie w Haskellu? Udowodnij, że pesymistyczny czas działania algorytmu oraz łączna liczba alokowanych komórek pamięci wynoszą O(n log n). Ile komórek pamięci jest jednocześnie żywych (patrz następne zadanie)? Jaka jest głębokość rekursji. Zadanie 3 (2 pkt). Aby opisać zarządzanie pamięcią w językach wysokiego poziomu (tutaj język wysokiego poziomu to język w którym pamięć jest zarządzana automatycznie) rozważa się zwykle pamięć o adresach z pewnego nieskończonego abstrakcyjnego zbioru A. Na adresach wolno wykonywać jedynie operacje Advanced group Problem 1 (2 p). Consider a RAM machine storing a singly linked list of length n in a readonly memory. The machine has m additional memory cells for reading and writing. We are going to visit all elements of the list from the last to the first. It is easy to observe that if we have m = Ω(n) additional memory cells it suffices to reverse the list and visit all elements of the reversed list in the natural order. The running time of this algorithm is O(n). It is as well easy to find an algorithm that traverses a list backward in constant space but in quadratic time. Show that for any constant k N it is possible to walk through a list backwards in time O(n 1+1/k ) using m = O(1) additional memory cells. Hint: first devise an algorithm that uses m = O( n) memory cells and runs in O(n 3/2 ) time. Problem 2 (2 p). Define the Mergesort algorithm in Prolog and in Haskell. Use difference lists in Prolog to mitigate copying and creation of garbage. How can you mitigate creation of garbage in Haskell? Prove that the pessimistic running time of the algorithm and the total number of allocated memory cells are O(n log n). How many memory cells are simultaneously alive (see next problem). What is the recursion depth? Problem 3 (2 p). To describe the memory management in high-level languages (here by a highlevel language we mean a language in which memory is managed automatically) one usually considers a memory with addresses taken from some infinite abstract set A. The only permitted operations on addresses are 4

new : (N A) A lookup : A N (N A) gdzie new(x 1,..., x n ) rezerwuje nową komórkę pamięci i inicjuje ją krotką (x 1,..., x n ), zaś lookup(a, i) ujawnia i-ty składnik krotki przechowywanej w komórce o adresie a. (W językach imperatywnych mamy dodatkowo operację where new(x 1,..., x n ) allocates a fresh memory cell and initializes its contents with a tuple (x 1,..., x n ), and lookup(a, i) returns the i-th component of a tuple stored in a memory cell addressed with a. (In imperative languages one also has the operation update : A (N A) () zmieniającą zawartość komórki pamięci.) Poza pamięcią maszyna posiada też ustaloną liczbę rejestrów. Pamięć i rejestry można przedstawić w postaci grafu skierowanego, którego wierzchołkami są rejestry i komórki pamięci, a krawędzie prowadzą od rejestrów i komórek zawierających adresy do komórek o tych adresach. Komórka pamięci jest żywa, jeśli jest osiągalna w tym grafie z pewnego rejestru maszyny. W przeciwnym razie komórka jest martwa. Maszynę z abstrakcyjnymi adresami tłumaczymy do rzeczywistej maszyny RAM, której komórki pamięci są adresowane liczbami naturalnymi. Udowodnij, że jeśli pewien algorytm działa na maszynie z abstrakcyjnymi adresami w czasie t i w każdym momencie jego działania jest nie więcej niż m żywych komórek pamięci, to jego wykonanie na maszynie RAM można tak przeplatać operacjami usuwania nieużytków, by całkowity czas działania wyniósł O(t), a w każdym momencie obliczenia potrzebna pamięć maszyny RAM nie przekraczała O(m). Zatem analizując złożoność algorytmów możemy przyjąć, że komputer dysponuje nieograniczoną pamięcią, a nieużytki nie są usuwane. Takie założenie nie zmienia bowiem klasy złożoności algorytmu. Np. algorytm Mergesort zaprogramowany w Prologu lub Haskellu rezerwuje Θ(n log n) komórek pamięci (w odróżnieniu od programu imperatywnego, który działa w pamięci Θ(n)), ale w każdej chwili jego działania żywe jest jedynie O(n) komórek. Można go wiec przetłumaczyć na maszynę RAM działającą w pamięci O(n) i w czasie O(n log n), jak w przypadku imperatywnym. Zadanie 4 (2 pkt). Zaprogramuj w Prologu algorytm sortowania list, który działa w czasie oczekiwanym O(n log n) i alokuje łącznie O(n) komórek pamięci. Czy potrafisz ten algorytm zaprogramować w czystym Prologu (bez odcięć, porównywania wskaźników itp.)? Czy potrafisz zaprogramować algorytm, który działa w pesymistycznym czasie O(n log n) i alokuje O(n) komórek pamięci? Czy możesz to samo zrobić w Haskellu? that modifies the contents of memory cells.) Besides memory the machine has also a fixed number of registers. The memory and registers can be presented in the form of a directed graph, in which vertices stand for memory cells and registers, and edges lead from register and memory cells containing addresses to cells having those addresses. A memory cell is alive if it is reachable from some register in this graph. Otherwise the memory cell is dead. The abstract pointer machine is translated to a real RAM machine, in which memory cells are addressed with natural numbers. Prove that if an algorithm runs on the abstract pointer machine in time t and in every moment of its computation the number of alive memory cells does not exceed m, then its execution on the RAM machine can be interleaved with operations of garbage collection so that the total running time is O(t), and the amount of needed memory cells does not exceed O(m) in every moment of the computation. Hence if we analyze the complexity of algorithms we can assume that an infinite memory is available for a computer and garbage is not removed, because this assumption does not change the complexity class of an algorithm. For example the Mergesort algorithm programmed in Prolog or Haskell allocates Θ(n log n) memory cells (in contrast to an imperative program which runs in Θ(n) memory), but in every moment of computation there are only O(n) alive memory cells. Hence it is possible to translate such program to the RAM machine that runs in O(n) memory and O(n log n) time, as in the imperative case. Problem 4 (2 p). Define in Prolog a list sorting algorithm that runs in O(n log n) expected time and allocates O(n) memory cells in total. Can you define such an algorithm in pure Prolog (without cuts, pointer comparison etc.)? Can you define an algorithm with pessimistic running time O(n log n) which allocates O(n) memory cells? Can you do the same in Haskell? 5