6
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by LuckyX2

Hey If you get it to work plz update your code here I am having the same problem can't figure out the second part. I would really appreciate if someone is able to help us out here.

Hey guys, I am working on this too. The first thing I see wrong with this code is that your for loop is running through the graph. The pseudo code says it should be running thorugh the list of neighbors for the node that is input.

Try this:

``````def DFSnodewithDeletion(graph,node)
count = 1
for i in node.neighbors:
node.neighbors.remove(i)
i.neighbors.remove(node)
count = count + DFSnodeWithDeletion(graph,i)``````

If your graph is connected I think it return the total number of nodes. Play around with it and let me know what happens.

Edited by xhaui: n/a

Ok, I found out that this code break if you use the graph like on the HW pdf. That is if the graph looks like:
A: C
C: A, F, D
F: C
D: C, B, G
B: D
G: D
E: H
H: E

Edited by xhaui: n/a

And plzz explain why it breaks b/c I am really confused with the whole thing

My code is the same code from the lecture notes, in terms of the load graph functions and everything. The aboce code break because there are 6 connected nodes in the graph, and then two nodes which are connected to eachother but no other nodes. When counting, the count should print 6, but it prints 3. If it printed 6, we could conclude that the graph is not ocnnected, because the total number of nodes is 8.

Lucky, have you gotten anywhere with this?

no

lol

i am confused of your title as seem to be knowing the number of nodes without traversing. Looks like you are looking for connectedness.

xhaui, using your bit of code, I am getting this error.

``````Intro to CS (Python)/nodecount2.py", line 81, in DFSnodeWithDeletion
for i in node.neighbors:
AttributeError: 'NoneType' object has no attribute 'neighbors'``````

where my input for node is findNode(graph,start)

Nevermind, that actually wasn't what was being input because of an oversight.

Now the problem is I'm not sure how to use the count; it's not being incremented correctly in the provided code.

Edited by LuckyX2: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.