In the context of this exercise, when I run the program, some of the vertices are connected to themselves. Is this valid?
Yes! This is actually completely valid and can occur in graphs.
Some graphs have vertices that connect to themselves, and these connections are referred to as “loops”.
For example, some roads may have a loop on the side, allowing drivers to change their direction. The entrance of this loop would be the “vertex”, and when you go through the loop, you would end up right back where you entered, with the option to change your original direction.