Python Memory Management and Tips Transcripts
Chapter: Efficient data structures
Lecture: Cycle busting part 1
0:00 Alright, let's go and change this program just a little bit to work with
0:05 our version that doesn't have cycles.
0:07 I'm gonna have this "app_no_cycles", and let's even print it out.
0:14 So we can see here, print "version with no cycles" this is the one that has
0:19 data structures that use cycles. Okay,
0:21 so what we're gonna do is we're gonna make a tiny difference here.
0:24 Let's add one more person, a person
0:25 3, Zoe, and it'll still ask
0:30 "do you want to have these people like this?"
0:33 And we can go, we're gonna change this right here,
0:37 "TODO: improve this with new data structures".
0:44 Let's go ahead and just ask whether people are friends. And then down here,
1:01 We'll just say "yes" like this,
1:03 "Yes if p1 in p2.friends" for the moment. Again,
1:10 we're going to be changing this. "else,
1:12 no". like this. Alright,
1:17 other than that, that's the same.
1:19 And we can also ask this for "is p1 a friend of p3" just
1:24 so that we have, you know,
1:25 sort of both cases covered. Alright,
1:26 let's run this thing and see how it's doing.
1:30 Alright. Create cycles? Yes.
1:33 Is Michael a friend of Sarah?
1:34 Yes. Is Michael Friend Zoe?
1:36 No. Program closing. Ah, cycles found.
1:39 Okay, that's a problem. Let's just run it one more time with no cycles.
1:42 No. No friends. We didn't create the friend relationships and no cycles and
1:47 so on. We're also not zeroing out
1:49 Zoe, so I guess we could go ahead and do that
1:51 here as well, that way you'll see them all go together.
1:57 Okay, Super. Now we've got the stage set.
2:00 How are we gonna change this?
2:02 How are we going to store them
2:03 so they don't create these references back to each other and create these cycles and all
2:07 the problems that can come from there?
2:08 Well, we're gonna use a different data structure rather than storing them on the classes
2:13 themselves, like we had indicated right there.