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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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
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
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
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
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
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
Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution
Atakan Aral Istanbul Technical University – Department of Computer Engineering
BLG 336E – Analysis of Algorithms II
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
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
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
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
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
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
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
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
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
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
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
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
Introduction Coffee Shop Street Lights MST and Shortest Path Quiz Solution