YouTube2Text

Understanding the Time Complexity of an Algorithm — Transcript

by Neso Academy · 3,175 words · 475 segments · language en · Watch on YouTube

Full transcript

  1. 0:01[Music]
  2. 0:06we are done with the chapter of
  3. 0:08asymptotic notations where we discussed
  4. 0:11different types of asymptotic notations
  5. 0:14and the problems based on it now from
  6. 0:18this lecture onwards we are starting a
  7. 0:20new chapter where we will discuss how to
  8. 0:24find the time complexity and the space
  9. 0:27complexity of algorithms involving
  10. 0:30Loops in this lecture we will first
  11. 0:34understand the time complexity in great
  12. 0:37details and then through an example we
  13. 0:40will understand how to analyze the time
  14. 0:43complexity of an algorithm in involving
  15. 0:46Loops so let's get started with this
  16. 0:49lecture and let's see what are the
  17. 0:52topics the first topic of this lecture
  18. 0:55is priori versus posterior analysis
  19. 0:59recap we will first get the quick recap
  20. 1:02of priori and posterior
  21. 1:05analysis and then we will understand CPU
  22. 1:08computations and Main memory space we
  23. 1:11will understand these two terms in great
  24. 1:14depth and then we will understand the
  25. 1:17time complexity and finally through an
  26. 1:20example we will understand how to
  27. 1:23analyze an algorithm and that to an
  28. 1:26algorithm which involves Loops so let's
  29. 1:29get started and let's get the quick
  30. 1:31recap of the priori versus posteriori
  31. 1:36analysis we know the difference between
  32. 1:38priori and posterior analysis from our
  33. 1:42previous
  34. 1:43chapters let's get the quick recap of
  35. 1:46priori and posterior
  36. 1:48analysis in case of priori analysis we
  37. 1:52estimate time and memory space required
  38. 1:56by an algorithm before executing it on
  39. 2:00the system but in case of posterior
  40. 2:03analysis we calculate time and memory
  41. 2:07space required by an algorithm after
  42. 2:11executing it on the
  43. 2:13system here in case of priori analysis
  44. 2:17we estimate time and memory space this
  45. 2:19means we do not calculate the actual
  46. 2:22time and memory space required by an
  47. 2:25algorithm and we estimate time and
  48. 2:27memory space before exit executing our
  49. 2:30algorithm on the
  50. 2:32system so before executing the algorithm
  51. 2:36we estimate time and memory space it
  52. 2:39takes in case of posterior analysis we
  53. 2:43calculate the actual time and memory
  54. 2:46space required by an algorithm after
  55. 2:48executing it on the system so
  56. 2:51posteriority analysis depends upon the
  57. 2:54system priori analysis does not depend
  58. 2:57upon the system because we are analyzing
  59. 3:00the time and memory space required by an
  60. 3:03algorithm before executing it on the
  61. 3:06system in case of priori analysis our
  62. 3:10focus is on priori analysis because
  63. 3:13priori analysis is the easiest and it is
  64. 3:16the most practical
  65. 3:18analysis so keeping this in mind we will
  66. 3:21use priori analysis to analyze our
  67. 3:24algorithms now how do we estimate time
  68. 3:27and memory space is the main question
  69. 3:31the estimation of time is same as the
  70. 3:34estimation of total number of CPU
  71. 3:38computations an algorithm takes and
  72. 3:41estimation of memory space means
  73. 3:45estimation of main memory
  74. 3:47space we will understand how do we
  75. 3:50estimate main memory space later but in
  76. 3:53this lecture we will understand how to
  77. 3:56estimate time or in other words how to
  78. 3:59estimate
  79. 4:00the total number of CPU computations an
  80. 4:03algorithm needs to
  81. 4:06execute now what is the meaning of CPU
  82. 4:10computations let's understand the
  83. 4:12meaning of CPU computations and Main
  84. 4:15memory space first after this we will
  85. 4:18learn how to analyze an algorithm or in
  86. 4:21other words how to estimate time and
  87. 4:24memory space required by an algorithm so
  88. 4:28now we will understand the meaning of
  89. 4:32CPU computations and Main memory
  90. 4:36space so what is the meaning of CPU
  91. 4:40computation a CPU computation refers to
  92. 4:43a task performed by the CPU or
  93. 4:47instruction executed by the CPU so CPU
  94. 4:51computation refers to a task which is
  95. 4:55performed by the CPU at any point of
  96. 4:58time or it refers to an instruction
  97. 5:01executed by the CPU in simpler terms so
  98. 5:05the meaning of CPU computation is an
  99. 5:08instruction executed by the CPU now what
  100. 5:12is the meaning of main memory space main
  101. 5:16memory space is used to temporarily
  102. 5:19store data and instructions that CPU
  103. 5:22needs for quick access during program
  104. 5:27execution one thing is clear that our
  105. 5:30focus is on CPU or Central Processing
  106. 5:33Unit whenever a CPU performs a task we
  107. 5:37call it a CPU computation or we can say
  108. 5:40whenever an instruction is executed by
  109. 5:43the CPU we call it a CPU
  110. 5:46computation and we are focusing on the
  111. 5:49main memory space because main memory or
  112. 5:52random access memory which we also call
  113. 5:54RAM is required by the CPU for quick
  114. 5:59access of the data and instructions
  115. 6:02stored in it main memory space is of
  116. 6:06concern to us and we are also concerned
  117. 6:09with the total number of CPU
  118. 6:12computations required by an algorithm so
  119. 6:15these are the two terms which I hope are
  120. 6:18completely clear to you the meaning of
  121. 6:21CPU computation is an instruction
  122. 6:23executed by the CPU and Main memory
  123. 6:26space is the memory space required by
  124. 6:29the CPU for quick access of data and
  125. 6:33instructions during program
  126. 6:36execution now as we have understood the
  127. 6:39meaning of CPU computations and Main
  128. 6:41memory space we are in the state to
  129. 6:45understand the time
  130. 6:47complexity so what is time
  131. 6:50complexity time complexity refers to the
  132. 6:55estimation of total CPU
  133. 6:58computations record IR ired to execute
  134. 7:01an algorithm we just understood the
  135. 7:04meaning of CPU
  136. 7:05computations we are not bothering about
  137. 7:08main memory space at this moment later
  138. 7:11we will understand how to estimate main
  139. 7:14memory space but right now our focus is
  140. 7:17to understand the time complexity and
  141. 7:20time complexity is the estimation of
  142. 7:24total CPU computations required to
  143. 7:27execute an algorithm now the main
  144. 7:30question is how do we estimate total CPU
  145. 7:35computations we now know time complexity
  146. 7:38of an algorithm is equal to total number
  147. 7:42of CPU computations but the question is
  148. 7:45how do we know the total number of CPU
  149. 7:47computations of an
  150. 7:50algorithm we can calculate the total
  151. 7:53number of CPU computations by using the
  152. 7:56method which we call the frequ quency
  153. 8:00count
  154. 8:01method according to this method we
  155. 8:04calculate the sum of frequency count of
  156. 8:08each instruction of an
  157. 8:11algorithm so frequency count method is
  158. 8:14the method in which we calculate the sum
  159. 8:17of frequency count of each instruction
  160. 8:20of an
  161. 8:21algorithm so total number of CPU
  162. 8:24computations is equal to the sum of
  163. 8:27frequency count of each instruction of
  164. 8:31an algorithm now what is the meaning of
  165. 8:33frequency count frequency count refers
  166. 8:37to the number of times an instruction is
  167. 8:41executed so in order to calculate the
  168. 8:44time complexity of an algorithm we
  169. 8:47calculate the sum of frequency count of
  170. 8:50each instruction of an algorithm and
  171. 8:53frequency count refers to the number of
  172. 8:56times an instruction is executed so by
  173. 9:00seeing an algorithm you can calculate
  174. 9:02its time
  175. 9:04complexity and now we will understand
  176. 9:07how to do this so now we're going to
  177. 9:10take a simple example algorithm and
  178. 9:14through that algorithm we will
  179. 9:16understand how to calculate the time
  180. 9:20complexity and that to using the
  181. 9:22frequency count method so now let's move
  182. 9:26to the next topic where we will consider
  183. 9:28a simple example algorithm to understand
  184. 9:32the time complexity
  185. 9:34properly here is the example algorithm
  186. 9:38here I have written the algorithm in C
  187. 9:41like syntax because C like syntax is
  188. 9:44simpler to understand and many students
  189. 9:47might already know C programming
  190. 9:50language so it is my assumption that you
  191. 9:52are familiar with at least one
  192. 9:54programming language if not C if you
  193. 9:57know C programming language AG then it
  194. 10:00is great but if you don't know C
  195. 10:02programming language you can still
  196. 10:04follow along there is no
  197. 10:07issue this algorithm is written in C
  198. 10:10like syntax and I've mentioned algo here
  199. 10:14to indicate that this is not a c program
  200. 10:18this is an algorithm this algorithm is
  201. 10:23non-executable this algorithm is written
  202. 10:25for us to understand how to analyze the
  203. 10:28time
  204. 10:30complexity so now we are going to
  205. 10:32analyze the time complexity of this
  206. 10:35algorithm using the formula which we
  207. 10:37just saw we are going to use the
  208. 10:40frequency count method to calculate the
  209. 10:43time complexity of this algorithm so we
  210. 10:46will do the priori analysis of this
  211. 10:49algorithm this means we will estimate
  212. 10:52the time complexity of this algorithm
  213. 10:55through the frequency count method now
  214. 10:57let's proceed and let's do this the job
  215. 11:00of this algorithm is to calculate the
  216. 11:02sum of N elements of the list
  217. 11:06a so n represents number of elements and
  218. 11:11a represents list of n
  219. 11:14elements here in this algorithm the
  220. 11:17first instruction is sum equal to0 this
  221. 11:22instruction tells CPU to assign zero to
  222. 11:25variable sum here only one operation is
  223. 11:29per formed hence this is just a single
  224. 11:31instruction and this instruction is
  225. 11:34executed only once so the frequency
  226. 11:38count of this instruction is
  227. 11:40one now why is that the case we know the
  228. 11:44meaning of frequency count frequency
  229. 11:46count refers to the number of times an
  230. 11:50instruction is
  231. 11:52executed this instruction is executed
  232. 11:56once here we have just one assignment
  233. 11:59that's it only one operation and it is
  234. 12:03executed only once therefore the
  235. 12:05frequency count of this instruction is
  236. 12:08one and let's assume that one
  237. 12:11instruction takes one unit of time
  238. 12:14therefore this instruction takes one
  239. 12:17unit of time now if you want to know the
  240. 12:20time complexity of this algorithm we
  241. 12:23need to calculate the sum of frequency
  242. 12:25count of each instruction in this
  243. 12:28algorithm
  244. 12:29so let's put one here here we will
  245. 12:32calculate the sum of frequency count of
  246. 12:35each instruction the frequency count of
  247. 12:38this instruction is one and this is the
  248. 12:41reason why I have written one here now
  249. 12:44what is the next
  250. 12:45instruction this is the for Loop and I
  251. 12:49hope you already know the meaning of for
  252. 12:51Loop for Loop allows us to execute
  253. 12:55instructions a certain number of times
  254. 12:59this Loop will allow us to execute this
  255. 13:03instruction which is part of the for
  256. 13:05Loop a certain number of
  257. 13:08times in this for Loop we have this
  258. 13:11instruction I equal to
  259. 13:131 this is an assignment instruction and
  260. 13:18this instruction will be executed only
  261. 13:21once because this represents the
  262. 13:24initialization of I so clearly the
  263. 13:28frequency count of I = 1 is 1 and
  264. 13:33therefore the time it takes is 1 unit
  265. 13:38and hence one can be added in this sum
  266. 13:42now what's the next instruction the next
  267. 13:44instruction is I less than or equal to
  268. 13:48n what can we say about this instruction
  269. 13:51what is the frequency count of this
  270. 13:55instruction I want you to pause this
  271. 13:57video and I want you think about this
  272. 14:00what is the frequency count of this
  273. 14:04instruction so pause the video
  274. 14:08now I hope you're done okay so what's
  275. 14:12the Frequency count of this instruction
  276. 14:15the frequency count of this instruction
  277. 14:18is n + 1 and hence it will take n+ 1
  278. 14:23units of time now why is this the case
  279. 14:27you might have given the answer as n and
  280. 14:31you are quite close but n is not the
  281. 14:34correct
  282. 14:36answer n + 1 is the correct answer why
  283. 14:41let's find out we know here we are
  284. 14:44checking a condition we are checking is
  285. 14:47I less than or equal to n if it is the
  286. 14:50case that I is less than or equal to n
  287. 14:54then this statement sum equal to sum +
  288. 14:57AI will be exec Ute
  289. 14:59it then I is incremented and the
  290. 15:03condition is checked once again so we
  291. 15:05know the initial value of I I is 1 one
  292. 15:09is compared with n this is the first
  293. 15:12condition checking first we are checking
  294. 15:15is 1 less than or equal to n if true we
  295. 15:18will get inside and execute the
  296. 15:21statement then I is incremented I
  297. 15:23becomes 2 then we will check this
  298. 15:26condition once again is 2 less than or
  299. 15:29equal to n this is the second condition
  300. 15:32check then we will go inside execute the
  301. 15:35statement I is again incremented it
  302. 15:38becomes three then three is compared
  303. 15:41with n this is the third time the
  304. 15:44condition is
  305. 15:46checked and this will continue and then
  306. 15:49we will compare n with n eventually I
  307. 15:52becomes n and we will compare n with n
  308. 15:56but we know n is equal to it is not less
  309. 16:00than n still the condition is true and
  310. 16:04hence the statement inside the for Loop
  311. 16:07that is Su equal to Su plus a I will be
  312. 16:10executed and I is incremented by one we
  313. 16:14know I is n and n ++ is equal to n + 1
  314. 16:19so the next condition check is n + 1
  315. 16:23less than or equal to
  316. 16:24n can we say n + 1 is less than or equal
  317. 16:28to n
  318. 16:29no right n + 1 is not less than or equal
  319. 16:34to n it is greater than n therefore at
  320. 16:38this point this condition becomes false
  321. 16:41hence we will go outside this
  322. 16:44Loop we are done with this loop at this
  323. 16:48point how many times this instruction is
  324. 16:51executed a total of n + 1
  325. 16:55times therefore there are n + 1
  326. 16:59comparisons and hence we can say the
  327. 17:02frequency count of this instruction is n
  328. 17:05+ 1 and this is the reason why this
  329. 17:09instruction will take n + 1 units of
  330. 17:13time so now we can add n + one here I
  331. 17:17hope this point is clear now what can we
  332. 17:21say about this instruction
  333. 17:23i++ how many times do you think this
  334. 17:26instruction will execute
  335. 17:29let's try to understand this now this
  336. 17:32instruction will execute based on this
  337. 17:36condition if this condition is satisfied
  338. 17:39this means if this condition is true
  339. 17:42then this instruction will execute if
  340. 17:45this condition is false then this
  341. 17:47instruction will not execute why am I
  342. 17:50saying this let's now try to understand
  343. 17:53this the initial value of I is 1 let's
  344. 17:56say 1 is less than n this condition is
  345. 17:59true if this condition is true we will
  346. 18:02go inside the for Loop and execute the
  347. 18:05statement after execution of the
  348. 18:08statement I ++ will execute so it is
  349. 18:12clear when this condition is satisfied
  350. 18:16i++ will
  351. 18:17execute but what when this condition
  352. 18:20becomes false if this condition is false
  353. 18:24then this statement sum equal to sum
  354. 18:26plus AI will not execute and hence this
  355. 18:30instruction will also not execute
  356. 18:33because i++ can only execute after
  357. 18:37execution of the
  358. 18:39statement so one thing is clear that
  359. 18:42when this condition is true I ++ will
  360. 18:45execute if this condition is false then
  361. 18:48i++ will not
  362. 18:50execute now let's use this idea to
  363. 18:54understand how many times this
  364. 18:56instruction will execute the initial
  365. 18:59value of I is 1 and I'm assuming 1 is
  366. 19:02less than n therefore the condition is
  367. 19:05true after execution of the statement I
  368. 19:08is incremented by 1 this means I ++ is
  369. 19:13executed for I =
  370. 19:161 after execution of this instruction I
  371. 19:20becomes 2 after I becomes 2 this
  372. 19:24condition will again
  373. 19:25checked let's say 2 is also less than n
  374. 19:29the condition is once again satisfied as
  375. 19:32this condition is satisfied I ++ will
  376. 19:35definitely execute so for I equal to 2
  377. 19:40also this instruction will
  378. 19:43execute what about I = to 3 let's say
  379. 19:46for I equal to 3 also this condition is
  380. 19:48true then it is surely the case that I
  381. 19:52++ will execute and hence for I = 3 also
  382. 19:56I ++ will execute
  383. 19:59similarly for i = 4 also this
  384. 20:02instruction will execute again assuming
  385. 20:05the same thing that 4 is less than n now
  386. 20:08let's say this process continues up to I
  387. 20:11= to n when I becomes n then n is
  388. 20:17compared with n as this condition is
  389. 20:20satisfied I ++ will again execute
  390. 20:24therefore for I = 1 to n this in
  391. 20:28instruction will definitely execute
  392. 20:31after I equal to n this instruction will
  393. 20:34execute and hence I becomes n + 1 we
  394. 20:37know that this condition will be checked
  395. 20:40once again for I = to n + 1 but this
  396. 20:44time this condition is not satisfied
  397. 20:46because n + 1 is neither less than n nor
  398. 20:49it is equal to n as we know the
  399. 20:53condition is not satisfied therefore I
  400. 20:56++ will not execute this time so for i =
  401. 21:00n + 1 this instruction will not execute
  402. 21:04only for i = 1 up to n this instruction
  403. 21:10will
  404. 21:10execute and this clearly shows that i++
  405. 21:14will execute n times not n + 1 times
  406. 21:20hence the frequency count of this
  407. 21:22instruction is n and the amount of time
  408. 21:25this instruction will take is n n
  409. 21:29units so now we got to know that this
  410. 21:33instruction will take n units of time so
  411. 21:37now we can add in here for this
  412. 21:41instruction now what about this
  413. 21:43statement sum equal to sum plus
  414. 21:46AI this is one statement but in this
  415. 21:50statement we have a total of two
  416. 21:53instructions here we are performing the
  417. 21:55addition operation and we are also
  418. 21:58assigning the result of sum plus AI to
  419. 22:02sum so there are a total of two
  420. 22:05instructions now how many times do you
  421. 22:08think this statement will
  422. 22:10execute this entire statement will
  423. 22:12execute n times because of the same
  424. 22:15reason which we saw in case of I
  425. 22:19++ the execution of this statement also
  426. 22:22depends upon this
  427. 22:24condition hence when this condition is
  428. 22:28true then this statement will execute
  429. 22:30when this condition is false then this
  430. 22:32statement will not execute so it is
  431. 22:35clear the number of times this statement
  432. 22:37will execute is also
  433. 22:40n but here we have a total of two
  434. 22:43instructions therefore the total time
  435. 22:46required to execute the statement is 2N
  436. 22:51units the frequency count of the
  437. 22:53statement is 2
  438. 22:55N so now we can add 2 N here here as
  439. 22:59well now what can we say about this
  440. 23:01statement return sum this is just one
  441. 23:05instruction and hence it will take one
  442. 23:07unit of time so we can add one here for
  443. 23:11this
  444. 23:12instruction now here we can calculate
  445. 23:15the total frequency count of this
  446. 23:19algorithm this is equal to 4n + 4
  447. 23:23because 2 n + n + n is equal to 4N
  448. 23:281 + 1 + 1 + 1 is equal to 4 so we got 4
  449. 23:33n + 4 as 4 n is the dominating term
  450. 23:38therefore big of n is the time
  451. 23:42complexity of this
  452. 23:45algorithm we can use different notations
  453. 23:47here to represent the time complexity
  454. 23:51but I'm using the biger notation which
  455. 23:53is also correct so the time required to
  456. 23:56solve this algorithm is Big of n which
  457. 24:00is the linear time as the size of the
  458. 24:04input grows the time required to solve
  459. 24:07this algorithm grows
  460. 24:10linearly it is Big go of
  461. 24:13n now we know what is the time
  462. 24:15complexity of this algorithm and we got
  463. 24:18to know this by knowing the frequency
  464. 24:21count of each instruction of the
  465. 24:23algorithm in this way we can calculate
  466. 24:26the time complexity of any algorithm so
  467. 24:30I hope the idea is completely clear with
  468. 24:33this we are done with all the topics of
  469. 24:36this lecture and hence we are done with
  470. 24:38this
  471. 24:39lecture okay friends this is it for now
  472. 24:42thank you for watching this presentation
  473. 24:44I will see you in the next one
  474. 24:47[Applause]
  475. 24:50[Music]

About this transcript

This page contains the full transcript of Understanding the Time Complexity of an Algorithm by Neso Academy, generated from the public captions YouTube serves with the video. The transcript has 3,175 words across 475 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.