What Is Advance Algorithm Analysis
What Is Advance Algorithm Analysis
Advanced Algorithms
MSCS / MSIT - Fall 2021
Department of CS & IT, University of
Sargodha
1
Agenda
• What is Solution?
• Algorithms vs Heuristics
• Optimization Problems
• What is Time Complexity?
• About the Course
• subject to
• For each uncolored vertex, determine whether it has an edge to any vertex already
colored with the new color.
• If there is no such edge, color the present vertices with the new color.
1 5 2
4
Two colors used
(optimum) 44
• For input size n the worst-case running time is O(nk) for some constant k
1 5 Cost(12 3 4 5 6) = 17
5
1 3
Cost(13 2 6 5 4) = 13
3 3
4 4
2
5
Cost(14 5 6 2 3) = 12
3 2
5
2
1 Time complexity of naïve solution?
6
5 6
• Model the layout of the museum as follows: vertices are rooms and an
edge between two vertices means that those two rooms are connected.
• Now, assuming that a guard standing in a room has full view of the
adjacent rooms, find where we should place guards so that all rooms
are guarded at the least cost (i.e. fewest number of guards).
Utilities
• Model the map as follows: Each vertex represents a state and there is an edge
between vertices if the corresponding states share a border.
• Then, the problem becomes: color the vertices of a planar graph so that
adjacent vertices have different colors.
• It has been shown that for a planar graph four colors suffice to color the graph.
• Then, count how many steps are used for an input of that size:
• A step is an elementary operation such as
• +, <, =, A[i]
• N = 10 => 53 steps
• N = 100 => 503 steps
• N = 1,000 => 5003 steps
• N = 1,000,000 => 5,000,003 steps
f(N) = O(g(N))
200000
150000
100000
50000
0
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3
1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
100n2 10 40 90 16 25 36 49 64 81 10 12 14 16 19 22 25 28 32 36 40 44 48 52 57 62 67 72 78 84 90 96 1E 1E 1E
5n3 5 40 13 32 62 10 17 25 36 50 66 86 10 13 16 20 24 29 34 40 46 53 60 69 78 87 98 1E 1E 1E 1E 2E 2E 2E
20000
10000
0
-10000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33
-20000
-30000
-40000
-50000
-60000
-70000
-80000
-90000
0.05 N2 = O(N2)
Time (steps)
3N = O(N)
Input (size)
N = 60
f2(n)
f3(n)
F(n)
f4(n)
• Important:
• Big-O is not a function!
• Never read = as "equals“
• Examples:
5N + 3 = O(N)
37N5 + 7N2 - 2N + 1 = O(N5)
int j,k;
for (j=0; j < N; j++)
for (k=0; k < j; k++)
sum += k+j;