Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP108 Time Complexity of Pseudo Code. Example 1 sum = 0 for i = 1 to n do begin sum = sum + A[i] end output sum O(n)

Similar presentations


Presentation on theme: "COMP108 Time Complexity of Pseudo Code. Example 1 sum = 0 for i = 1 to n do begin sum = sum + A[i] end output sum O(n)"— Presentation transcript:

1 COMP108 Time Complexity of Pseudo Code

2 Example 1 sum = 0 for i = 1 to n do begin sum = sum + A[i] end output sum O(n)

3 Example 2 sum = 0 for i = 1 to n do sum = sum + A[i] output sum product = 1 for i = 1 to n do product = product * A[i] output product O(n)

4 Example 3 sum = 0 for i = 1 to n do begin for j = 1 to n do begin sum = sum + i * j end output sum O(n 2 )

5 Example 4 sum = 0 for i = 1 to n do begin for j = 1 to m do begin sum = sum + i * j end output sum O(nm)

6 Example 5 sum = 0 for i = 1 to n do for j = 1 to n do for k = 1 to n do sum = sum + i * j * k output sum O(n 3 )

7 Example 6 i = 1 while i <= n do begin output i i = i * 2 end i = n while i > 1 do begin output i i = i / 2 end O(log n)

8 Example 7 i = 1 while i <= n do begin j = 1 while j <= n do begin output i*j j = j * 2 end i = i + 1 end O(n log n)

9 Graph - Example 1 // Input: a graph G=(V,E) with n vertices and m edges V' =  while V' ≠ V do begin pick a new vertex v in V \ V' V' = V'  {v} end O(n)

10 Graph - Example 2 // Input: a graph G=(V,E) with n vertices and m edges V' = V while V' ≠  do begin pick a new vertex v in V' V' = V' \ {v} end O(n)

11 Graph - Example 3 // Input: a graph G=(V,E) with n vertices and m edges E' =  while E' ≠ E do begin pick a new edge e in E \ E' E' = E'  {e} end O(m)

12 Graph - Example 4 // Input: a graph G=(V,E) with n vertices and m edges E' = E while E' ≠  do begin pick a new edge e in E' E' = E' \ {e} end O(m)

13 Graph - Example 5 // Input: a graph G=(V,E) with n vertices and m edges V' =  for each vertex v in V do begin unmark v end while V' ≠ V do begin pick an unmarked v in V mark v V' = V'  {v} end O(n)

14 Graph - Example 6 // Input: a graph G=(V,E) with n vertices and m edges V' =  while V' ≠ V do begin pick a new vertex v in V \ V' V' = V'  {v} for every neighbour u of v do begin output information of u end O(n 2 )

15 Graph - Example 7 // Input: a graph G=(V,E) with n vertices and m edges V' =  for each vertex v in V do begin initalise information of v end while V' ≠ V do begin pick a new vertex v in V \ V' V' = V'  {v} for every neighbour u of v do begin output information of u end O(n 2 )

16 Graph - Example 8 // Input: a graph G=(V,E) with n vertices and m edges E' =  while E' ≠ E do begin pick a new edge e=(u,v) in E \ E' E' = E'  {e} for every vertex w in V do begin if w is a neighbour of u or neighbour of v then output information of w end O(nm)


Download ppt "COMP108 Time Complexity of Pseudo Code. Example 1 sum = 0 for i = 1 to n do begin sum = sum + A[i] end output sum O(n)"

Similar presentations


Ads by Google