Algorithm Design | Algorithm Correctness #algorithm #algorithmdesign

EduSyl
10 Nov 202322:00

Summary

TLDRThis educational video session delves into the importance of algorithm correctness, explaining that an algorithm is considered correct if its output matches the expected result. The instructor uses a simple example involving even numbers to illustrate the concept. The video outlines three primary methods for ensuring correctness: counterexamples, mathematical induction, and loop invariants. It emphasizes the necessity of total correctness over partial correctness and provides an example of an infinite loop to highlight the importance of a stopping condition in algorithms. The session aims to help viewers understand the principles behind creating and verifying accurate algorithms.

Takeaways

  • 📘 Algorithm correctness is crucial because it ensures that the output of an algorithm matches the expected outcome.
  • 🔍 Total correctness is the goal, as it means the algorithm produces the desired output for all possible inputs, while partial correctness indicates the algorithm may only work correctly for some inputs.
  • 🔄 An infinite loop can occur if there is no increment or decrement operator within the loop, causing it to run indefinitely and not reach the stopping condition.
  • 🔢 The importance of a stopping property in loops is highlighted, as it ensures the algorithm will terminate and provide a correct output.
  • 🔑 Total correctness can be achieved by ensuring both partial correctness and the stopping property are met.
  • 🔍 Three methods for proving algorithm correctness are discussed: counterexample, mathematical induction, and loop invariant.
  • 🚫 Counterexamples serve as indirect proofs by providing an instance where a statement is false, thus disproving it.
  • 📚 Mathematical induction is a direct proof method used to prove that a statement holds for all cases, starting with a base case and then proving the inductive step.
  • 🔄 Loop invariants are conditions that are true before, during, and after the execution of a loop, ensuring that the loop behaves correctly and terminates as expected.
  • 📉 The script provides an example of a summation formula represented as a mathematical model and discusses its time complexity implications.
  • 📝 The presenter encourages students to understand the concepts of total and partial correctness, and to reach out if they have any questions or issues.

Q & A

  • What is the primary importance of algorithm correctness?

    -Algorithm correctness is crucial because it ensures that the output of an algorithm matches the expected output, which is the desired outcome for a given problem.

  • What are the two main types of algorithm correctness discussed in the script?

    -The two main types of algorithm correctness discussed are total correctness and partial correctness.

  • Why is total correctness preferred over partial correctness?

    -Total correctness is preferred because it guarantees that the algorithm consistently produces the correct output for all possible inputs, whereas partial correctness may only be true for some inputs.

  • What is an example of an incorrect algorithm output mentioned in the script?

    -An example given is a code to determine if a number is even. If the input is 10 and the output is 'odd', it is incorrect because the expected output should be 'even'.

  • What is the significance of the stopping property in the context of algorithm correctness?

    -The stopping property ensures that an algorithm will terminate after a finite number of steps and produce the correct output, preventing infinite loops.

  • Can you explain the concept of a loop invariant as discussed in the script?

    -A loop invariant is a condition that remains true before and after each iteration of a loop and upon termination of the loop. It helps in proving the correctness of an algorithm by ensuring that the loop maintains the invariant properties throughout its execution.

  • What are the three steps involved in checking a loop invariant?

    -The three steps are the initial step (ensuring the loop starts correctly), the maintenance step (ensuring the loop maintains the invariant during each iteration), and the end step or stop step (ensuring the loop stops under the correct conditions).

  • What is the purpose of using counterexamples in proving algorithm correctness?

    -Counterexamples are used to disprove a statement by providing an instance where the statement does not hold true, which is an indirect proof method.

  • How does mathematical induction differ from counterexamples in proving algorithm correctness?

    -Mathematical induction is a direct proof method where one proves the base case and then assumes the statement is true for a certain case 'n' to prove it must also be true for 'n+1'. In contrast, counterexamples use specific instances to disprove a statement.

  • What is the relationship between total correctness and the stopping property?

    -Total correctness is achieved when an algorithm is partially correct and adheres to the stopping property, which ensures the algorithm terminates with the correct output.

  • Can you provide an example of how mathematical induction is used in the script?

    -The script uses the example of proving the summation of the first 'n' integers formula. It starts with the base case (n=1), assumes the formula holds for 'n', and then proves it for 'n+1', demonstrating the direct proof method of mathematical induction.

Outlines

00:00

📚 Introduction to Algorithm Correctness

This paragraph introduces the concept of algorithm correctness, emphasizing its importance and setting the stage for a deeper discussion. It explains that an algorithm is considered correct if its output matches the expected output. The instructor uses the analogy of a simple program to check for even numbers to illustrate the point. The paragraph also touches on the idea of partial correctness, where an algorithm might be correct for some inputs but not all, and contrasts this with total correctness, which is the ultimate goal for ensuring an algorithm works as intended for all possible inputs.

05:01

🔍 Understanding Algorithm Correctness with Examples

This section delves into the specifics of algorithm correctness using an example of a loop intended to sum elements of an array. The example highlights common errors such as infinite loops due to missing increment operators and the importance of the stopping property in loops. The paragraph explains that total correctness is achieved when an algorithm is partially correct and adheres to the stopping property. It also introduces the three methods used to prove algorithm correctness: counterexamples, mathematical induction, and loop invariants, with a brief mention of other approaches like case enumeration and contradictions.

10:01

🔧 Loop Invariants and Their Role in Algorithm Correctness

The focus of this paragraph is on loop invariants, a method used to ensure that loops in an algorithm behave as expected. The instructor explains the three steps involved in maintaining a loop invariant: initialization, maintenance, and the stop step. It is emphasized that all three steps must be true for a loop to be considered invariant. The paragraph also provides an example of a loop with an incorrect initialization step, demonstrating how this can lead to incorrect results and the importance of each step in achieving total correctness.

15:05

📉 Counterexamples and Mathematical Induction in Correctness Proofs

This paragraph discusses two methods for proving algorithm correctness: counterexamples and mathematical induction. Counterexamples are used as indirect proofs to disprove a statement by providing a specific instance where the statement does not hold. The instructor uses the ceiling function as an example to illustrate this method. On the other hand, mathematical induction is a direct proof method, typically used in mathematics to prove statements that hold for all natural numbers. The paragraph briefly explains the process of induction, which involves proving a base case and then proving that if the statement holds for n, it also holds for n+1.

20:08

📝 Summary of Algorithm Correctness Methods and Their Importance

The final paragraph wraps up the session by summarizing the key points discussed in the video. It reiterates the importance of total correctness in algorithms and distinguishes it from partial correctness, which is undesirable. The paragraph also briefly explains that total correctness is achieved by combining partial correctness with the stopping property. The instructor reminds viewers of the three main methods for proving correctness: mathematical induction, counterexamples, and loop invariants. The session concludes with an invitation for viewers to ask questions if they have any, highlighting the interactive nature of the educational content.

Mindmap

Keywords

💡Algorithm Correctness

Algorithm correctness refers to the property of an algorithm that ensures it produces the expected output for a given input. In the video, it is emphasized that an algorithm is considered correct only if its current output matches the expected output. The script uses the example of a code to determine if a number is even or odd to illustrate this concept, highlighting that if the output does not match the expectation, the algorithm is not correct.

💡Expected Output

Expected output is the result that an algorithm is anticipated to produce when given a specific input. It serves as a benchmark for evaluating the correctness of an algorithm. In the script, the concept is discussed in the context of determining if an algorithm is correct by comparing the current output to the expected output, such as expecting the number 10 to be classified as even and not odd.

💡Current Output

Current output is the actual result produced by an algorithm when it is executed with a given input. It is compared against the expected output to determine the correctness of the algorithm. The video script mentions that if the current output is not equivalent to the expected output, as in the example where the output incorrectly classifies 10 as odd, the algorithm is not correct.

💡Partially Correct

Partial correctness refers to a situation where an algorithm's output is partially aligned with the expected output but not entirely accurate. The script explains that if an algorithm's output matches some parts of the expected output, it can be considered partially correct, but the goal is to achieve total correctness, which means a complete match.

💡Totally Correct

Total correctness is the state of an algorithm where its output fully matches the expected output for all cases. The video emphasizes that the aim is to achieve total correctness, as exemplified by a student receiving full marks for a question answered perfectly, indicating that every part of the algorithm's output must match the expected result.

💡Infinite Loop

An infinite loop is a sequence of instructions in a program that repeats indefinitely due to a missing or incorrect loop termination condition. In the script, an example is given where a loop is supposed to sum elements of an array, but because there is no increment operator, the loop becomes infinite and does not produce the correct total sum.

💡Stopping Property

Stopping property is a characteristic of an algorithm that ensures it will terminate after a finite number of steps. The script discusses the importance of the stopping property in conjunction with partial correctness to achieve total correctness, using the infinite loop example to illustrate the absence of this property leading to incorrect algorithm behavior.

💡Loop Invariant

A loop invariant is a condition or property that remains true before and after each iteration of a loop and upon termination of the loop. The script explains that maintaining a loop invariant is crucial for ensuring the correctness of an algorithm, as it helps guarantee that the loop will produce consistent results and terminate correctly.

💡Counterexample

A counterexample is a particular instance or example that disproves a general statement or hypothesis. In the context of algorithm correctness, a counterexample can be used to show that an algorithm is not correct by demonstrating a case where it fails to produce the expected output. The script mentions counterexamples as a method to disprove a claim about an algorithm's correctness.

💡Mathematical Induction

Mathematical induction is a method of mathematical proof used to establish that a given statement is true for all positive integers. The script describes it as a direct proof method used to prove the correctness of an algorithm or a mathematical formula, where one proves the base case and then assumes the statement is true for n and proves it for n+1.

💡Partial Correctness

Partial correctness is a term used to describe an algorithm that may produce the correct result for some inputs but not necessarily for all. The script contrasts partial correctness with total correctness, stating that while an algorithm might be partially correct, the goal is to achieve total correctness by ensuring the algorithm is correct for all possible inputs.

Highlights

Introduction to the importance of algorithm correctness and its types.

Explanation of why an algorithm's output must match the expected output to be considered correct.

The concept of partial correctness versus total correctness in algorithms.

Example illustrating the difference between expected and current output in algorithm correctness.

Importance of total correctness for achieving the desired algorithmic outcome.

Discussion on the stopping property and its role in ensuring total correctness.

The role of increment operators in preventing infinite loops for algorithm correctness.

Explanation of the three-step process involving initialization, maintenance, and stopping for loop invariants.

Introduction to the methods used for proving algorithm correctness: counterexamples, induction, and loop invariants.

Counterexamples as a method of indirect proof to disprove an algorithm's correctness.

Mathematical induction as a direct proof method for establishing algorithm correctness.

The process of using mathematical induction in steps: base case, inductive hypothesis, and inductive step.

Demonstration of proving a mathematical formula using mathematical induction.

Differentiating between total correctness, which requires partial correctness plus stopping property.

Practical applications of algorithm correctness in real-world programming scenarios.

The significance of loop invariants in ensuring consistent and correct algorithm behavior.

Summary of the session's learning objectives and an invitation for questions from the audience.

Transcripts

play00:01

hello students welcome to edus today's

play00:04

session is all about to read the

play00:06

algorithm correctness here we will

play00:09

discuss why algorithm correctness is

play00:11

important and how many types of

play00:13

correctness of algorithms are there and

play00:16

what are the methods are getting used to

play00:19

correct an algorithm or to find

play00:21

correctness inside an algorithm we will

play00:23

be discussing that one in the next uh

play00:27

slides so here correctness of an algor

play00:30

them is important as we discussed in the

play00:32

previous video those who are new to this

play00:35

video please watch the previous video

play00:37

here correctness is important if the

play00:40

current output if the current output of

play00:43

your algorithm is equivalent to the

play00:48

expected

play00:49

output expected output then only you can

play00:52

say your algorithm is desired algorithm

play00:56

or correct algorithm like you know the

play00:59

same same example I I'll say if you know

play01:02

you have written a code to find whether

play01:04

a number is even or not in that time if

play01:08

uh if you're putting a number is a uh 10

play01:12

that time if that you know as for the

play01:15

expectations you know 10 is even if your

play01:18

code is giving 10 is odd so your current

play01:21

output which is odd is not equal to the

play01:24

expected output which is even in that

play01:27

case even is not equivalent to odd that

play01:30

what happens you can say it is not

play01:34

meeting with the uh expected output so

play01:38

that's why it is not correct if some

play01:40

part are getting matched so you can say

play01:43

it is partially

play01:45

correct okay if everything is getting

play01:47

match you can say totally correct

play01:49

suppose you can say you have given a you

play01:52

know answer given an answer for a

play01:54

question which is totally correct that

play01:57

you know so you know it's two marks that

play02:00

you got out of two out of two is given

play02:03

by the teachers to you okay that is

play02:05

totally correct sometimes what the

play02:09

students are doing like to get 0.5 so at

play02:12

least give us5.5 means they know5 will

play02:15

be one okay but5 you are actually going

play02:18

for uh you know arguing with the faculty

play02:21

so please give us five because you are

play02:23

partially correct you know you're

play02:25

partially correct you're not fully

play02:26

correct so in that case what happened

play02:29

like the correct is somehow you meeting

play02:31

with the expected output suppose instead

play02:34

of getting with uh suppose 1 2 3 4 like

play02:37

up to 10 suppose you would need to find

play02:39

instead of getting up to 10 you are

play02:41

getting 1 2 3 like uh up to again you

play02:45

are repeating with 2 1 2 1 like this

play02:48

suppose you you know infite Loop you can

play02:51

up to three it is running somehow you're

play02:54

matching with the expected one but it is

play02:56

not totally correct the partially

play02:58

correct answer we are actually not

play03:00

needing an algorithm is partially

play03:02

correct is not our Target the Target is

play03:05

to get total correct why because we need

play03:07

a desired algorithm that is why we

play03:10

should get the totally correct answer so

play03:14

the then what why total correctness is

play03:17

needed that you understood now total

play03:19

correct if you want to find what are the

play03:22

structures that you have to find uh

play03:24

before that we should have to discuss

play03:26

one example through which you can easily

play03:29

understand what is happening now so this

play03:31

is an algorithm we have written or you

play03:34

can say it's a sudut where array is

play03:36

given and length is given where sum is

play03:39

decided as zero because no need to keep

play03:41

the garbage value that's why it is

play03:42

written as zero so I is z it started

play03:45

from zero okay so length which is we

play03:49

defined it has given there sum equal to

play03:52

sum plus a RR like each element will be

play03:56

added here it's a you can say the code

play03:59

it is WR is to add all the numbers

play04:02

inside the r indexes okay we are adding

play04:04

the numbers or digits inside the r

play04:06

indexes okay but one more thing you can

play04:10

remember your while condition is correct

play04:13

not an issue but within that sum is

play04:15

equal to Su plus a r i the I which is

play04:19

zero is there but there is no increment

play04:22

operator if there is no increment or

play04:24

decrement at least operator that time

play04:27

the while loop will not run the while

play04:29

loop Loop will be okay throughout you

play04:32

know it's a infinite Loop rather than

play04:34

going to stop it we have you know

play04:37

infinite Loop like it will check yes

play04:39

then it will be printing some then

play04:41

whether it will check yes then it will

play04:43

be printing because there is no such

play04:45

increment like how do we say it will

play04:47

stop if if the condition will be I is

play04:51

greater than equal to length either in a

play04:55

different way also if I is greater than

play04:57

length okay

play05:00

I is greater than Len or I is equal to

play05:04

Ln then only we can stop so that is why

play05:06

I'm just writing it if I is greater than

play05:09

equal to l then only we can stop so to

play05:11

reach to that condition we cannot do

play05:14

without any operator which is increment

play05:17

operator you can say okay though

play05:19

increment operator we do not have in

play05:21

that case it is considered as the

play05:24

stopping property in one case it could

play05:27

not be infinite Loop if length is is

play05:29

considered as uh zero but length could

play05:32

not be zero okay if you have written

play05:34

length is zero that time it will stop

play05:36

not an issue one time it will run and it

play05:37

will stop but if you have written one as

play05:41

length means one index is there in that

play05:43

case what happens like it has to be one

play05:46

is always greater than zero okay that

play05:48

time it will be you know condition like

play05:51

it gives you the result but not the

play05:54

correct result because there are

play05:56

suppose uh four indices 0 1 2 3 so there

play06:01

are four indices in that indexes what

play06:04

you are doing like 1 2 3 are the values

play06:07

are being there so sum would be or you

play06:10

can say

play06:12

sum would be 1 + 2 + 3 + 4

play06:18

okay what would be considered as 10 okay

play06:23

sum should be 10 but instead of getting

play06:26

10 so in this case suppose you are

play06:29

writing

play06:30

length I start mov zero and length

play06:34

is 4 in that case it will be checking

play06:38

the condition but the first only will

play06:41

give you the correct answer sum equal to

play06:44

sum plus a r r i a r i means sum will be

play06:48

zero then one so that will be your

play06:51

answer like we are going to far from the

play06:53

10 also like it is infinite Loop though

play06:57

we are going far from it so we might get

play07:00

the correct answers as 10 but we are far

play07:02

from it okay because it's infinite Loop

play07:05

sometimes though it is not stopping so

play07:07

we are getting partially correct answers

play07:10

it is wrong okay it's not totally

play07:12

correct answers we getting so to stop it

play07:15

we have to use this operator which we

play07:19

discussed now so uh you can say total

play07:26

correct is equal to the mathema model

play07:29

you can

play07:32

say partial

play07:35

correct plus it should be partially

play07:37

correct plus stopping property or stop

play07:41

property it has to be

play07:43

followed okay if both are there then

play07:46

only we can say we can expect total

play07:49

correct answer it should be a partially

play07:51

correct answers means infinite Loop is

play07:53

there and stop property should be

play07:55

followed well then only we can say it is

play07:58

totally correct correct okay so here it

play08:01

is written as you can see uh here Stops

play08:06

Plus correct output is considered as

play08:08

totally correct answers or total

play08:09

correctness you can say

play08:12

okay

play08:14

now there are uh

play08:17

three uh you can say methods are getting

play08:19

used one is counter example another is

play08:22

induction or which is mathematical

play08:24

induction third one is Loop invariant

play08:26

and other approaches are there but this

play08:28

is not inside the Corman anywhere but

play08:31

you can remember by cases enumerations

play08:33

also by contradictions by contrapositive

play08:36

this could be done but we should

play08:38

remember counter examples induction

play08:40

method or mathematical inductions or

play08:42

Loop invariant so Loop invariant is you

play08:45

can if the loop is not getting

play08:48

consistent okay so if you are not

play08:50

getting the consistent result definitely

play08:53

there will be a problem that's why we

play08:55

focusing on Loop in valant and the

play08:56

number of statements it is is running

play09:00

that we know uh the statements will give

play09:02

you the correct output as one time it is

play09:05

running but Loop there might be a

play09:08

problem in a conditions so whether it is

play09:10

stopping or not it it has to be stopped

play09:13

with the correct answer that's why Loop

play09:15

will have some problem that's why we are

play09:16

focusing on Loop invariant first time

play09:18

how it is running like this is your

play09:21

initial steps next how many times it is

play09:23

running like you you are in a

play09:25

maintenance step once maintenance is

play09:27

okay also you have to check whether it

play09:29

is stopping or not there are three steps

play09:32

first is initial steps next is

play09:33

maintenance step last is your end step

play09:37

or you can say the stop step you can

play09:40

have to check if three will be true then

play09:43

you can say Loop is invariant or Loop

play09:45

inent holds okay Loop inent holds you

play09:48

can say if only one step is uh holding

play09:52

only Loop andant you cannot say every

play09:54

every sh or all the you know Loop

play09:58

element ments would be inv variant here

play10:00

it is not holding because only one is

play10:02

not enough suppose initial is okay not

play10:05

an issue suppose for Loop you are saying

play10:07

for I = 1 and I is less than 10 I ++

play10:13

your initial will be you have to check I

play10:15

value you have initialized it you have

play10:17

to check whether one is less than 10 or

play10:19

not if it is done you can enter into the

play10:21

statements written there once it is done

play10:24

initial part is over like you have

play10:26

installed the SE if you are having a

play10:28

problem like air condition you need to

play10:30

install installation is done next you

play10:33

paying money for the

play10:35

maintenance maintenance will be from it

play10:38

will be running until the condition will

play10:41

hold okay like you paying the

play10:45

maintenance without know initializing or

play10:48

without installing AC you are not paying

play10:50

the money like you are not paying the

play10:51

maintenance fee that that is how it is

play10:54

running okay so first step is

play10:55

initialization second step is a

play10:57

maintenance you're checking it checking

play10:59

it how much time like until 9 is greater

play11:04

than 10 like up to this you can have

play11:07

expir date before the expired date okay

play11:09

up to that we can have the food we can

play11:11

have to use the maintenance everything

play11:13

would be maintained well but if you have

play11:16

to check if it is not working then we

play11:18

are throwing the products clear so where

play11:21

the I value like it is 10 10 is not

play11:24

equivalent to 10 which is you can say if

play11:28

I value which we said if it is equal to

play11:31

10 it will stop and if I is greater than

play11:34

10 then it will stop this stop is end

play11:38

version of it or stop property of it

play11:40

which is the third one where you have to

play11:43

check the loop is invariant or not okay

play11:46

so it's a though Loop is a problem so

play11:48

that's why we are focusing that one next

play11:51

is counter example which is said as

play11:53

indirect proof and uh induction which is

play11:56

mathematical induction is direct proof

play12:00

so counter example everyone when you are

play12:03

quarreling with a person okay with an

play12:06

example I'm telling you whenever you are

play12:08

quarreling okay that time nobody in the

play12:11

earth is

play12:13

saying this is my fault nobody is

play12:16

accepting that fault of thems that's why

play12:19

the coring will be the growth of the

play12:21

coring would be any know more than that

play12:24

more than the you can say super polom

play12:26

okay it's it's a super polom if nobody

play12:29

is understanding that fault their own

play12:31

fault everyone is aling no I'm not doing

play12:35

you have done so like this the counter

play12:37

example is like this so whenever uh uh

play12:40

you know have the problem is given there

play12:42

you can take a random value you have to

play12:44

fix it you have to disprove or prove it

play12:47

basically we are going to disprove it

play12:49

which is easy for us because we people

play12:51

are actually not maining like not

play12:53

thinking no we are culprit we are always

play12:56

saying in front of us who is there he is

play12:58

a culprit or see is the con like this is

play13:00

what a counter example is so you can say

play13:03

to prove or disprove it so X Y within

play13:06

this sing function whether it is

play13:09

equivalent to sing of X Plus sing of Y

play13:12

or not to check

play13:14

that so we have taken x value as like 1X

play13:18

two y value is 1x two you can check the

play13:21

LHS and rhs side of it okay in LHS it is

play13:25

there X + Y which is having sing

play13:28

function you can say 1 by 2 which is the

play13:31

value of x and 1 by 2 which the value of

play13:34

y with the ceiling function that you

play13:37

know sing function will be picking the

play13:39

next value of it if there is a decimal

play13:42

here is no decimal so the same value

play13:44

will be considered as one as a ciling

play13:47

function here and rhs side is like x

play13:51

with a ciling plus y with a ceiling is

play13:53

there you can say 1X 2 with the ceiling

play13:57

plus again 1x2 with the ceiling is there

play14:00

which is 1 + 1 is 2 like five5 the sing

play14:04

will be the next round value or next

play14:07

whole number will be written so that's

play14:10

why it is two where LHS is not

play14:14

equivalent to rhs here you are

play14:17

disproving it okay by taking any example

play14:20

that's why it is said to be indirect

play14:22

proof you are not do doing it directly

play14:24

you to taking any any of this okay so

play14:27

like this every positive integer is sum

play14:30

of two squares of integers it is written

play14:33

you can take the example of three also

play14:35

here in the third example it is written

play14:38

so for all X for all X and for all y XY

play14:42

is greater than equal to uh X so that

play14:47

you can take any value of x here the

play14:50

value of x is min-1 value of 3 is y is 3

play14:55

so X into Y is - 3 because -1 into 3 is

play15:00

- 3 so - 3 is not always or equal to

play15:05

greater than equal to the X okay which

play15:08

is said to be minus one though it is not

play15:11

equal you have dis it this is how the

play15:13

counter example is working okay now

play15:17

proving of mathematical inductions

play15:19

maximum of you have done this in know uh

play15:22

intermediate level or might be 10th

play15:24

level that time you have already seen so

play15:26

a summation is given like we have in

play15:28

this mathematical model which is uh this

play15:31

summation in indirect way if you can ask

play15:34

me in mathematical way you can say it's

play15:36

a for Loop where I is starting from one

play15:40

here with it has there written I equal

play15:42

to 1 okay so I will be starting from one

play15:45

I will be ending with this one so I will

play15:48

be less than n or equal to you can it is

play15:50

sumission then the value it is running

play15:53

is though it is one like adding of the

play15:56

thing you can say i++ it is a

play15:58

mathematical representation of a for

play16:00

Loop basically we are using okay so and

play16:04

also though though you can say it is

play16:07

running for n Square time the time

play16:09

complexity in that case what you have to

play16:11

say again one for Loop the same for Loop

play16:14

you can say uh running for two times if

play16:17

you say the the time complexity of this

play16:20

summation is how much time like if

play16:21

you're following this one so you have to

play16:23

use another for Loop for it where the

play16:26

same condition is running for two times

play16:29

the time complexity could be uh for this

play16:33

is n² but it will be read by you the

play16:35

next videos don't worry okay so it's

play16:38

just an you know discussion that we have

play16:41

done next you focus like the

play16:44

mathematical induction the mathematical

play16:46

induction is needed to prove it directly

play16:50

whether it is falling or not so for that

play16:53

first case you have to take the best

play16:56

solution like you know from starting

play16:58

from a simple one like you can check

play17:01

with the one the N value you have to

play17:03

take as one once you have taken with one

play17:06

then you have to go for the proof okay

play17:08

so this is how the first step is step

play17:11

one says step

play17:13

one less or you can say

play17:17

U smaller

play17:22

value will be taken as input okay so

play17:26

here you can start with one if you want

play17:28

to take five also as so you can check

play17:31

that one whether 15 the side is

play17:34

equivalent to that one or not okay so

play17:36

you have to check it it is okay now okay

play17:40

next for this mathematical induction as

play17:43

we told to prove that one we have

play17:45

checked it one one side and equal to we

play17:48

have written is one so it is there and

play17:51

next we have to in step two in step two

play17:54

step one we have checked with a smaller

play17:56

value next in step two we assume

play17:59

that it run for already run for end

play18:02

times and it we got correct answer next

play18:05

we have to

play18:06

prove with n + one time why because if

play18:10

the next step is true then definitely

play18:12

the previous step could be true so to

play18:15

say previous step is true we are

play18:17

assuming let us take it is okay next we

play18:20

have to prove the uh plus one or the

play18:23

next step of the N or might be anything

play18:26

which you have taken so the here uh I

play18:28

kept for the left hand side and right

play18:30

hand side as an example so here we know

play18:33

the sumission is all about to add the

play18:35

thing so we have just added it 1 2 3 up

play18:39

to n it has to run and here we kept as

play18:42

it is then here what exactly we have to

play18:44

do by writing

play18:47

this n + 1 okay we just add to Next Step

play18:52

because this is true for us this is what

play18:55

a true that we said true if it is

play18:59

running for n + one time so what we have

play19:02

to do we have to just run for one n +

play19:05

one time which is you can say instead of

play19:07

one you put n + 1 okay here also you can

play19:11

put instead of n you can put to n + 1

play19:14

because you are saying n is uh true okay

play19:18

n is true you are saying now you are

play19:22

proving you are proving n + 1 will be

play19:25

true so though you are proving n + 1 is

play19:27

true you have to put

play19:29

the N +1 in place of n okay so here this

play19:32

is the answer next to you know to equate

play19:35

this you are getting the answer as like

play19:37

1 + 1 - one will be cut as zero then up

play19:40

to this you got the answer as you know

play19:44

which is uh the proof that you said like

play19:47

it is true that that's why you are

play19:48

writing it so uh I kept it on bracket

play19:51

here you have written as the answer that

play19:53

you have already proven this is correct

play19:56

and you kept as you and here also no

play19:59

need to do anything so you keep the

play20:02

right hand side and other way here when

play20:05

you are actually doing it like you have

play20:07

to add it whenever you are adding the

play20:11

total will be looking like this so I'm

play20:13

just writing it here

play20:19

okay done so which will be written like

play20:22

this if you add this only one like here

play20:26

two will be there under this where you

play20:29

can write aners like this

play20:32

okay and then you have to take common of

play20:34

n + one once you have taken common here

play20:37

LHS equivalent to rhs

play20:41

okay though it happens so you can say we

play20:44

have

play20:45

proven uh like the the pro the the

play20:49

formula is getting proved for n + 1 so

play20:53

though we proved it for n + one so we

play20:57

can say

play20:58

n is also true this is how the

play21:01

mathematical induction works okay so

play21:04

mathematical induction is a direct proof

play21:06

without mathematics we can know say

play21:08

whether it is true or not so in this way

play21:10

we

play21:12

said the methods we are using in our

play21:17

correctness is mathematical induction

play21:20

which is a direct proof counter example

play21:22

which is indirect proof any values you

play21:25

keeping and third one is loop invariant

play21:28

which you actually studied and during

play21:31

the slides and correctness are two types

play21:35

one is total correctness another is

play21:36

partial correctness partial correctness

play21:39

we don't want for getting total

play21:41

correctness uh is partial correctness

play21:43

plus to property this is how our to

play21:46

today's session is all about I hope it

play21:50

is understood by you if any problem you

play21:53

can message below okay and thank you for

play21:56

watching have a good day

Rate This

5.0 / 5 (0 votes)

Related Tags
Algorithm CorrectnessEducational ContentComputer ScienceProgramming ConceptsCorrectness ProofsMathematical InductionLoop InvariantsCounterexamplesCoding TutorialTechnical Learning