How to get from a recurrence relation to a time order - recurrence

So, I've been looking into reccurence relations for quicksort, and I can follow how they get to the final recurrence relation, but then they jump to a time order. For example:
Worst case: T(n) = T(n-1) + cn
Then they jump to the time order of: O(n^2)
I dont follow how they got from the recurrence relation to the time order...

You can solve this recurrence in the following manner :-
T(n)=T(n-1)+cn =T(n-1)+O(n)
T(n)=T(n-2)+c(n-1)+cn {bcoz T(n-1)=T(n-2)+c(n-1)}
T(n)=T(n-3)+c(n-2)+c(n-1)+cn {bcoz T(n-2)=T(n-3)+c(n-2)}
. . .
. . .
. . .
. . .
T(n)=T(0)+c(1+2+3....+n)
T(n)=T(0)+c(n(n+1)/2)
T(n)=T(0)+O(n^2)
Therefore it is come out of order of n^2.

Related

In general is it ok to loop if statements with goto under else?

So I have a task to be done which is to program the robot (AUBO) to pick different objects and place them in a certain order (Point A, B, C, D).
I'm using some vision system known as pim60. So if an object is detected it will go and pick and the rest of the program are waypoints to drop products. The first problem is I want it to go to the next waypoint to drop the and the second thing is, the next drop point cannot be skipped until an object is detected for that drop point.
In my own code, I wrote a rather lengthy program like this.
::LoopA::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position A
else
goto LoopA
end
::LoopB::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position B
else
goto LoopB
end
::LoopC::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position C
else
goto LoopC
end
::LoopD::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position D
else
goto LoopD
end
There is no error and the program runs as expected. However, I'm wondering if there is any better way to do it.
The only generally accepted use-case for goto is error handling, e.g. to jump forward to the cleanup code. But even for that it usually can and should be avoided.
You can probably do something like this:
-- loop B
repeat
take photo, etc.
located = ...
until(located == 1)
Drop at position B
Also, if you're repeating the same code three times, you should extract it into a function, and maybe give the position as a parameter. Or at least put the whole thing into a for loop.

Selecting cases based on values in previous cases

I've got the following list in SPSS:
Subjekt Reactiontime correct/incorrect
1 x 1
1 x 0
1 x 1
1 x 0
I now want to select all rows/cases that follow AFTER "0" (in the column correct/incorrect) because I want to compute the mean of all reactiontimes that come after "0".
How can I do that in SPSS?
One way to do this would be to add a column that keeps track of whether the prior row was equal to 0 in your correct field and then calculate the mean Reactiontime of those cases.
First let's make a variable to flag cases we want included in the average.
* set prev_correct to 0 if the prior case was 0 .
IF (LAG(correct)=0) prev_correct=0 .
* else set to -1 .
RECODE prev_correct (SYSMIS=-1) .
EXE .
Now we can calculate the mean reaction time, splitting by our new variable.
MEANS Reactiontime BY prev_correct /CELLS MEAN .
Or, if we only want to output the mean when prev_correct=0 .
TEMP .
SELECT IF prev_correct=0 .
MEANS Reactiontime /CELLS MEAN .
Here's a shorter approach (though less generic than #user45392's full process):
if lag(correct)=0 ReactiontimeAfter0=Reactiontime.
now you can just run means ReactiontimeAfter0.

How to blank all cases with a specific year in SPSS?

I have 3 datasets each from a particular year. I have already merge all 3 but I want to blank cases where year=2016. So far this is the syntax I came up with:
Do (if subyr=2016).
Recode X1 to X32 (Lowest to Highest=SYMIS)(Else=SYMIS).
End if.
You should be able to simply use
DO IF (subyr=2016) .
RECODE X1 TO X32 (ELSE=SYSMIS) .
END IF .
EXE .
If you ever wanted to code the valid values differently from the SYSMIS values, you could use
DO IF (subyr=2016) .
RECODE X1 TO X32 (LO THRU HI=0)(ELSE=SYSMIS) .
END IF .
EXE .
which would give you that flexibility. This example sets valid values to 0 and keep SYSMIS values as SYSMIS.

List still being treated as a set even after converting

So i have an instance where even after converting my sets to lists, they aren't recognized as lists.
So the idea is to delete extra columns from a data frame comparing with columns in another. I have two data frames say df_test and df_train . I need to remove columns in df_test which are not in train .
extracols = set(df_test.columns) - set(df_train.columns) #Gives cols 2b
deltd
l = [extracols] # or list(extracols)
Xdp.dropna( subset = l, how ='any' , axis = 0)
I get an error : Unhashable type set
Even on printing l it prints like a set with {} curlies.
[{set}] doesn't cast to list, it just creates a list of length 1 with your set inside it.
Are you sure that list({set}) isn't working for you? Maybe you should post more of your code as it is hard to see where this is going wrong for you.

How can I calculate the number of cases in SPSS?

I would like to calculate the last column with SPSS without turning the columns into 1.
You can use the COUNT command to do this, small example below.
DATA LIST FREE / Tool1 Tool2 Tool3.
BEGIN DATA
1 3 8
1 5 1
1 . .
2 3 .
3 . .
END DATA.
COUNT Tool# = Tool1 TO Tool3 (LOWEST THRU HIGHEST).
EXECUTE.

Resources