Analysis of Algorithms II - PS2

21
Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution BLG 336E – Analysis of Algorithms II Practice Session 2 Atakan Aral Istanbul Technical University – Department of Computer Engineering 18.03.2014 Atakan Aral Istanbul Technical University – Department of Computer Engineering BLG 336E – Analysis of Algorithms II

Transcript of Analysis of Algorithms II - PS2

Page 1: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

BLG 336E – Analysis of Algorithms IIPractice Session 2

Atakan Aral

Istanbul Technical University – Department of Computer Engineering

18.03.2014

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 2: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Outline

1 Introduction

2 Coffee ShopProblemSolution

3 Street LightsProblemSolution

4 MST and Shortest PathTrue or False?

5 Quiz Solution

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 3: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Greedy Algorithm

An algorithm that builds a solution in small steps, choosing adecision at each step myopically [=locally, not considering whatmay happen ahead] to optimize some underlying criterion.

Produces optimum solution for some problems.Minimum spanning treeSingle-source shortest pathsHuffman trees

Produces good aproximate solutions for some otherproblems.

NP-Complete problems such as graph coloring

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 4: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Problem

You own a coffe shop that has n customers.It takes ti minutes to prepare coffee for the i th customer.i th customer’s value for you (i.e. how frequent s/he comesto your shop) is vi .If you start preparing coffee for the i th customer at time si ,you finish at fi = si + ti .All customers arrive at the same time.You can prepare one coffee at a time.There is no gap after you finish one coffee and startanother.

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 5: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Problem

You need to design an algorithm.Input: n, ti , vi

Output: A schedule (i.e. ordering of customer reqests)Aim: Minimize wait time especially for valued customers

Minimize :n∑

i=1

fi ∗ vi (1)

What is the time complexity of your algorithm?Run your algorithm for a sample input.

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 6: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

input : t [], v [],n1 for i ← 1 to n do2 w [i ,1]← v [i]/t [i]; // weight of each customer3 w [i ,2]← i ;

4 sort(w ,dec,1);5 t ← 0;6 cost ← 0;7 for j ← 1 to n do8 schedule[j]← w [j ,2];9 f [j]← t + t [schedule[j]];

10 t ← f [j];11 cost ← cost + f [schedule[j]] ∗ v [schedule[j]];

12 return schedule, f , cost

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 7: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

Both for loops take O(n) time.Complexity of the algorithm depends on sort method.Typically O(nlogn)

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 8: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

Input:t1 = 2, t2 = 3, t3 = 1v1 = 10, v2 = 2, v3 = 1

Output:Weights = 5, 0.67, 1Schedule: 1, 3, 2Finish times: 2, 3, 6Cost: 2*10 + 3*1 + 6*2 = 35

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 9: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Problem

Local goverment wants to reduce operating costs of roadlighting.Not every road will be illuminated at night.For safety, there will be at least one illuminated pathbetween all junctions.

Suggest an algorithm to optimize the road lightingWhat is the maximum saving without endangering thecitizens?

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 10: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Problem

Input contains the junctions and the roads connecting themAs well as, the cost of illuminating each road.File format is as follows:

7 110 1 70 3 51 2 81 3 91 4 7

2 4 53 4 153 5 64 5 84 6 95 6 11

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 11: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

34

1 2

5

6

5

9

11

0

6

8

5

8

9 7

15

7

g

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 12: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

34

1 2

5

6

5

9

11

0

6

8

5

8

9 7

15

7

g

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 13: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

34

1 2

5

6

5

9

11

0

6

8

5

8

9 7

15

7

g

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 14: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

34

1 2

5

6

5

9

11

0

6

8

5

8

9 7

15

7

g

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 15: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

34

1 2

5

6

5

9

11

0

6

8

5

8

9 7

15

7

g

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 16: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

34

1 2

5

6

5

9

11

0

6

8

5

8

9 7

15

7

g

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 17: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

Solution

34

1 2

5

6

5

9

11

0

6

8

5

8

9 7

15

7

Total cost: 90 – Optimized cost: 39 – Saving: 51

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 18: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

True or False?

Let G be an arbitrary connected, undirected graph with apositive distinct cost c(e) on every edge e.Let T be a MST of G. If we replace edge cost c(e) withc(e) ∗ c(e) , T must still be MST for G.Same is valid for c(e) + 5.It is also valid if negative costs are allowed.

2 4

3

MST depends only on the order of the costs, actual valuesare not important as long as order is the same.

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 19: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

True or False?

Let G be an arbitrary connected, directed graph with apositive cost c(e) on every edge e.Let P be the shortest path between node s and node t inG. If we replace edge cost c(e) with c(e)2, P must still beshortest path between node s and node t in G.Same is valid for c(e) + 5.

e

t

2

s

2

3

e

t

2

s

2

5

For shortest paths, actual values of the costs do matter.

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 20: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

True or False?

Let G be an arbitrary connected, directed graph with costc(e) on every edge e.Dijkstra algorithm finds the correct solution if some edgeshave negative costs.There always exists a solution even if Dijkstra algorithmcannot find it.

e

t

12

s

-5

10

e

t

5

s

6

10

f3

-9

There is no solution for graphs with negative cycles.

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 21: Analysis of Algorithms II - PS2

Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution

s → BLG101E → BLG102E → BLG201EBLG101E → BLG301E → BLG305EBLG102E → BLG303EBLG201E → BLG301EBLG202E → BLG301EBLG301E → BLG305EBLG303EBLG305E → BLG202E

1 Draw the graph G, showing its nodes and directed edges.2 Is this graph a DAG (directed acyclic graph)? Give the

reason for your answer.3 If G is not a DAG, how can you transform it into a DAG?

Which operation is required?4 Using the DAG course graph, produce a course program

for a student with the minimum number of semesters.

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II