Algorithms Assignment 2, Exercises of Algorithms and Programming

Algorithms Assignment 2 (adjacency matrix, lossless code, divide and conquer algorithm)

Typology: Exercises

2018/2019

Uploaded on 04/20/2019

kefart
kefart ๐Ÿ‡บ๐Ÿ‡ธ

4.4

(11)

55 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 9007 - Assignment 2
Due: 14th of April 2019 at 11:59 pm
Submit your assignment via Canvas ๏ƒ  Assignments. Submission must be in pdf format, and
cannot be handwritten.
Problem 1 (30 marks)
Suppose we have a large network of computers spread across the country, which could be
connected using a network fibre links. Some of these links have already been constructed.
Each potential new link has a cost associated with constructing it. We would like to know the
cheapest way to connect all of the computers into a single network. i.e.
โ€ข V is a set of n vertices (the servers)
โ€ข E is a set of m undirected edges between vertices in V
โ€ข A โІ E is a subset of edges which have already been built
โ€ข Each remaining edge โˆˆ V\A has a cost c(e)>0
โ€ข Problem: select a subset X โІ V\A of minimal weight such that the graph (V, X โˆช A) is
connected.
Figure: The solid lines denote the fibre links which already exist, while the dashed lines
indicate potential links which could be constructed.
pf3
pf4

Partial preview of the text

Download Algorithms Assignment 2 and more Exercises Algorithms and Programming in PDF only on Docsity!

COMP 9007 - Assignment 2 Due: 14 th^ of April 2019 at 11:59 pm

Submit your assignment via Canvas ๏ƒ  Assignments. Submission must be in pdf format, and cannot be handwritten.

Problem 1 (30 marks)

Suppose we have a large network of computers spread across the country, which could be connected using a network fibre links. Some of these links have already been constructed. Each potential new link has a cost associated with constructing it. We would like to know the cheapest way to connect all of the computers into a single network. i.e.

  • V is a set of n vertices (the servers)
  • E is a set of m undirected edges between vertices in V
  • A โІ E is a subset of edges which have already been built
  • Each remaining edge โˆˆ V\A has a cost c(e)>
  • Problem: select a subset X โІ V\A of minimal weight such that the graph (V, X โˆช A) is connected.

Figure: The solid lines denote the fibre links which already exist, while the dashed lines indicate potential links which could be constructed.

Your task is to design an efficient algorithm that solves this problem. It is assumed that the graph is stored as an adjacency matrix.

Your solution must include:

(i) the optimal solution of the above graph (10 marks) (ii) a brief description of how your algorithm works and why your algorithm is correct (10 marks) (iii) analysis of the upper bound on the time complexity of your algorithm (10 marks)

Problem 2 (40 marks)

An alphabet is a finite set of symbols , and a word (over an alphabet) is a finite sequence of symbols from that alphabet. For example, 1100 is a word over the alphabet {0, 1}, and abba is a word over the alphabet {a, b, c}. If w is a word, we denote by |w| the total number of symbols in w, so |abba| = 4.

A code is a map that takes symbols from a source alphabet to words over a target alphabet. For example

C = {a โ†’ 0, b โ†’ 10, c โ†’ 110}

is a code with source alphabet {a, b, c} and target alphabet {0, 1}. Another example is Morse code which has the English alphabet (and numbers) as a source alphabet and {ยท, โˆ’} as a target alphabet.

A code naturally defines a map from words over the source alphabet to words over the target alphabet by replacing each symbol in the source word by its corresponding code word. We call this an encoding. For example, the code C above encodes the word abba as 010100. For simplicity we will write this as C(abba) = 010100. A decoding is the inverse of an encoding โ€“ that is, given a word w over the target alphabet, finding a word u over the source alphabet such that C(u) = w. Note that depending on the code there may be more than one valid decoding, or even none.

(a) Using the code C above, find a word over {0, 1} that does not have a valid decoding

(5 marks)

To overcome the difficulties highlighted in the previous question, a common approach is to use a prefix code: a code where no target code word is the prefix of another. For example, the code used at the start,

C = {a โ†’ 0, b โ†’ 10, c โ†’ 110},

is a prefix code; whereas the code

Cโ€ฒ = {a โ†’0, b โ†’10, c โ†’101}

is not because 10 is a prefix of 101.

(e) Given a source alphabet A = {a, b, c, d, e} and a target alphabet B = {0, 1}, design an

algorithm to find the prefix code C that maps A to words over B. Your answer must include the following:

(i) a brief description of the algorithm and explanation on why it works (

marks)

(ii) use the designed algorithm to give a prefix code C that maps A to words over

B (5 marks)

(Hint: use the binary tree data structure)

Problem 3 (30 marks):

Let A be an array containing n numbers (positive and negative). Develop a divide and

conquer algorithm that finds the two indices 1 โ‰ค i โ‰ค j โ‰ค n such that โˆ‘ ๐ด๐ด^ ๐‘—๐‘—๐‘–๐‘– [๐‘˜๐‘˜](the sum of the elements from i to j) is maximized. For example, in the array A = [10, โˆ’5, -6, 5, 7, โˆ’2, 4, โˆ’11], the sub-array A[4:6] has the sum 5 + 7 โˆ’ 2 + 4 = 14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the algorithm should output (3, 6). Design an algorithm to solve this problem in best possible running time.

(i) write down the pseudo code of your algorithm (20 marks). (ii) briefly justify what the running time of your algorithm is (10 marks).