Nov-09-2021, 07:17 PM
Ahh, I understand your confusion now. It is clever, and I guess potentially confusing.
You are correct that roots starts out as an empty list the first time through the "for s in S:" loop.
roots = [], s = {2, 3, 4, 5}
Since roots is empty, s is not going to be a subset of any of the sets in roots and is appended to roots.
roots = [{2, 3, 4, 5}], s = {4, 5, 6}
Second time through the outer loop roots contains {2, 3, 4, 5}. s is not a subset of {2, 3, 4, 5}, so it is appended to roots.
roots = [{2, 3, 4, 5}, {4, 5, 6}] s = {2, 3}
Third time through the outer loop {2, 3} is a subset of {2, 3, 4, 5}, so it is not appended to roots.
Do follow how this goes now? Each time through the outer loop we test if s is a subset of any of the sets in root. If s is a subset of one of the sets in root, it is not appended to root. If s is not a subset of any set in root, s is appended to root.
You are correct that roots starts out as an empty list the first time through the "for s in S:" loop.
roots = [], s = {2, 3, 4, 5}
Since roots is empty, s is not going to be a subset of any of the sets in roots and is appended to roots.
roots = [{2, 3, 4, 5}], s = {4, 5, 6}
Second time through the outer loop roots contains {2, 3, 4, 5}. s is not a subset of {2, 3, 4, 5}, so it is appended to roots.
roots = [{2, 3, 4, 5}, {4, 5, 6}] s = {2, 3}
Third time through the outer loop {2, 3} is a subset of {2, 3, 4, 5}, so it is not appended to roots.
Do follow how this goes now? Each time through the outer loop we test if s is a subset of any of the sets in root. If s is a subset of one of the sets in root, it is not appended to root. If s is not a subset of any set in root, s is appended to root.