Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

comp9123

Assignment 4

s2 2022

Problem 1. (20 points) Consider the following edge weighted undirected graph:

C

 

 

H

2

Your task is to run Dijkstra’s algorithm, Prim’s algorithm, and Kruskal’s algo- rithm on this graph and indicate their outputs:

a) Indicate the order in which the edges are added when running Dijkstra’s

[points] [points]

c) Indicate the order in which the edges are added by Prim’s algorithm, starting

at B.                                                                                                   [points]

d) What is the cost of the MST computed by Prim’s algorithm?            [points]

e) Indicate the order in which the edges are added by Kruskal’s algorithm.     [points]

f)  What is the cost of the MST computed by Kruskal’s algorithm?       [工 points]

(You do not have to explain your answer.)

Problem 2. (10 points) Consider the following instance of the fractional knapsack problem on 9 items:

Your task is to:

a) Compute the set of items, along with the fraction in which they are used, with maximum total benefit of weight at most W = 21 and also give the total benefit of this set.                                                                              [points]

 

Benefit

Weight

Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 Item 7 Item 8 Item 9

6

8

5

12

6

3

8

2

7

17

4

7

13

25

19

5

13

26

b) List the order in which the items are considered by the greedy algorithm from the lecture that returns the optimal solution.                                    [points]

(You do not have to explain your answer.)

Problem 3. (15 points)

Given an array of n (distinct) numbers A, an index 1  ≤ i  ≤ n − 2 is a local minimum if A[i] < A[i + 1] and A[i] < A[i − 1]. Your task is to design an algorithm that finds a local minimum in a given array A.

a) Give an algorithm with time complexity O(n).

b) Give a (better) algorithm with time complexity O(log n).

c) Analyse the time complexity of the second algorithm.

You do not need to prove the correctness of your algorithms. Hint for b): divide-and-conquer.

[points] [points] [points]

Problem 4. (15 points) Consider the following problem: you are given a set of n people, and need to divide them in two groups (not necessarily of equal size). But there is a catch:  some people really do not like each other, and refuse to be in the same group: specifically, you are also given a list of m conditions of the form “person i hates person j”(for 1 ≤ i, j ≤ n), and you must make sure that people who hate each other are not put in the same group.

Your task is to design an algorithm which, given n people and a set of m con- straints of that sort, determines if there exists a way to divide them in two groups while satisfying all the constraints. The algorithm must run in time O(n + m).

a) Provide an algorithm to solve this problem..

b) Analyse the time complexity of the algorithm.

c) Prove correctness of the algorithm.

[points] [points] [points]

Hint: transform the problem into a problem about graphs. You can use anything seen during the lectures and tutorials without justification.

Advice on how to do the assignment

• Assignments should be typed and submitted as pdf (no pdf containing text as images, no handwriting).

• Start by typing your student ID at the top of the first page of your submission. Do not type your name.

• Submit only your answers to the questions. Do not copy the questions.

• When asked to give a plain English description, describe your algorithm as you would to a friend over the phone, such that you completely and un- ambiguously describe your algorithm, including all the important (i.e., non- trivial) details. It often helps to give a very short (1-2 sentence) description of the overall idea, then to describe each step in detail. At the end you can also

include pseudocode, but this is optional.

• In particular, when designing an algorithm or data structure, it might help you (and us) if you briefly describe your general idea, and after that you might want to develop and elaborate on details. If we don’t see/understand your general idea, we cannot give you marks for it.

• Be careful with giving multiple or alternative answers.  If you give multiple answers, then we will give you marks only for "your worst answer", as this indicates how well you understood the question.

• Some of the questions are very easy (with the help of the slides or book). You can use the material presented in the lecture or book without proving it. You do not need to write more than necessary (see comment above).

• When giving answers to questions, always prove/explain/motivate your an- swers.

• When giving an algorithm as an answer, the algorithm does not have to be given as (pseudo-)code.

• If you do give (pseudo-)code, then you still have to explain your code and your ideas in plain English.

• Unless otherwise stated, we always ask about worst-case analysis, worst case running times, etc.

• As done in the lecture, and as it is typical for an algorithms course, we are interested in the most efficient algorithms and data structures.

• If you use further resources (books, scientific papers, the internet,...)  to for- mulate your answers, then add references to your sources and explain it in your own words. Only citing a source doesn’t show your understanding and will thus get you very few (if any) marks. Copying from any source without

reference is considered plagiarism.