20

Click here to load reader

Analysis of Algorithms II - PS3

Embed Size (px)

Citation preview

Page 1: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

BLG 336E – Analysis of Algorithms IIPractice Session 3

Atakan Aral

Istanbul Technical University – Department of Computer Engineering

25.03.2014

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 2: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Outline

1 Dijkstra AlgorithmProblemSolution

2 Street LightsProblemSolution

3 MST and Shortest PathTrue or False?

4 Median of Two DB’sProblemSolution

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 3: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Problem

[Midterm 2013] Compute the shortest path from node 4 tonode 1 in the following undirected graph using the greedyalgorithm we learned in the class.

35

2

46

12

79 3

5

4

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 4: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Solution

35

2

46

12

79 3

5

4

d(4) : {∞,∞,∞,0,∞}, s : {4}d(4) : {∞,2,∞,0,7}s : {4,2}d(4) : {6,2,11,0,5}, s : {4,2,5}d(4) : {6,2,10,0,5}, s : {4,2,5,1}d(4) : {6,2,10,0,5}, s : {4,2,5,1,3}

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 5: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Problem

Local government 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 6: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 7: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 8: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 9: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 10: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 11: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 14: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 15: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

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 16: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Problem

You are interested in analyzing some hard-to-obtain datafrom two separate databases A and B.Each database contains n numerical values and you mayassume that no two values are the same.Determine the median of this set of 2n values

Only way you can access these values is through queriesto the databases: In a single query, you can specify avalue i to one of the two databases, and the chosendatabase will return the i th smallest value that it contains.Since queries are expensive, you would like to computethe median using as few queries as possible.Give an algorithm that finds the median value using atmost O(logn) queries.

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 17: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Solution

Formulation

Let k = dn/2e then A(k) and B(k) are the medians ofdatabases A and B. We are looking for C(n) where C is themerged database.

If A(k) < B(k) then:B(k) > A(1),A(2), ...,A(k)B(k) > B(1),B(2), ...,B(k − 1)

B(k) is greater than at least 2k − 1 elements.B(k) ≥ C(n) so no need to considerB(k + 1),B(k + 2), ...,B(n)

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 18: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Solution

Formulation

Let k = dn/2e then A(k) and B(k) are the medians ofdatabases A and B. We are looking for C(n) where C is themerged database.

If A(k) < B(k) then:A(k) < B(k),B(k + 1), ...,B(n)A(k) < A(k + 1),A(k + 2), ...,A(n)

A(k) is less than at least 2n − 2k − 1 elements.A(k) ≤ C(n) so no need to considerA(1),A(2), ...,A(k − 1)

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 19: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Solution

Otherwise, if A(k) > B(k) then:A(k) > B(1),B(2), ...,B(k)A(k) > A(1),A(2), ...,A(k − 1)

A(k) ≥ C(n) so no need to considerA(k + 1),A(k + 2), ...,A(n)Similarly,

B(k) < A(k),A(k + 1), ...,A(n)B(k) < B(k + 1),B(k + 2), ...,B(n)

B(k) ≤ C(n) so no need to consider B(1),B(2), ...,B(k −1)

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II

Page 20: Analysis of Algorithms II - PS3

Dijkstra Algorithm Street Lights MST and Shortest Path Median of Two DB’s

Solution

median: n,a = 0,b = 01 if n == 1 then2 return min(A(1),B(1))3 end4 k = dn/2e;5 if A(a + k) < B(b + k) then6 return median(k ,a + bn/2c,b)7 end8 else9 return median(k ,a,b + bn/2c)

10 end

Array size is halved at each recursion. So the complexity isO(logn).

Atakan Aral Istanbul Technical University – Department of Computer Engineering

BLG 336E – Analysis of Algorithms II