Hi, This may be too elementary for most of you, but could you please help me with the following question? I would like to use comprehensive lists and lists of lists. Where do I start? Question: Consider a digraph with 10 vertices, labeled 1 through 10. You are given the following adjacency list representation, where we first list the vertices adjacent to vertex 1, and so on. 1*; *2; 2*; *3; 3*; *4; 4*; *5; 5*; *6; 6*; *7; 7*; *8; 8*; *9; 9*; *10; 10. a) Write code to turn the adjacency list into an incidence list and an an adjacency matrix. b) Write code to turn the incidence list into an adjacency matrix. Hint: You may find it useful to note that one incidence list representation is (1*; *1), (2*; *2), (3*; *3), (4*; *4), (5*; *5),(6*; *6), (7*; *7), (8*; *8), (9*; *9), (10*; *10), (1*; *2), (2*; *3), (3*; *4), (4*; *5), (5*; *6), (6*; *7), (7*; *8), (8*; *9), (9*; *10).
On Sunday 28 January 2007 22:45, Python Freak wrote:
Hi,
This may be too elementary for most of you, but could you please help me with the following question? I would like to use comprehensive lists and lists of lists. Where do I start?
Question:
Consider a digraph with 10 vertices, [snip]
Is this homework? If so, no problem, but personally I change the way I respond to homework questions as opposed to other things :-) If it is, consider this:
[(x,y) for x in range(y) for y in range(9) ] [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8)]
Do you see what's going on there? Regards, Michael.
On 29/01/07, Michael Sparks <ms@cerenity.org> wrote:
Is this homework?
I was about to respond with a literal answer but I thought that too. It was the mathematical terminology but especially this bit that made me suspect:
On Sunday 28 January 2007 22:45, Python Freak wrote: Hint: You may find it useful to note that one incidence list representation is...
Guys, The question I posted is taken from an exercise sheet which I will discuss with my study group -- nothing graded. I am a graduate student trying to learn Python for some application in microeconomics. As I am new to Python, I am clueless about where to start. I apologise if this caused any inconvenience -- I didn't know that you are not allowed to answer problems. Michael's response definitely helps but I would appreciate a little more help. Thanks. On 1/28/07, Zeth Green <theology@gmail.com> wrote:
On 29/01/07, Michael Sparks <ms@cerenity.org> wrote:
Is this homework?
I was about to respond with a literal answer but I thought that too. It was the mathematical terminology but especially this bit that made me suspect:
On Sunday 28 January 2007 22:45, Python Freak wrote: Hint: You may find it useful to note that one incidence list representation is...
python-uk mailing list python-uk@python.org http://mail.python.org/mailman/listinfo/python-uk
On 29/01/07, Python Freak <newtopython@gmail.com> wrote:
I am clueless about where to start.
Okay, I did economics for my first degree so we have something in common! (Although I am not sure my maths ever got that deep). There are many ways to do what you want. The first step is to mentally translate what you want to do into Python's data types. See here for a list of the basic types: http://docs.python.org/tut/node7.html Do not forget you can nest data types inside each other, so the most basic matrix is a set of lists inside a list. However once you understand what you are trying to do, it would then be worth downing tools and looking into the numpy module as it has many mathematical constructs all ready and abstracted for you. Look at http://numpy.scipy.org/ for hours of fun(?). See for example, https://networkx.lanl.gov/Reference/networkx.convert-module.html#to_numpy_ma...
Okay, how about this: I don't know how to create the adjacency list at the first place! Can you get me started please? If I start then I can make an attempt to create the incidence list and the adjacency matrix representation. Here is the question again: Consider a digraph with 10 vertices, labeled 1 through 10. You are given the following adjacency list representation, where we first list the vertices adjacent to vertex 1, and so on. 1*, *2; 2*, *3; 3*, *4; 4*, *5; 5*, *6; 6*, *7; 7*, *8; 8*, *9; 9*, *10; 10. a) Write code to turn the adjacency list into an incidence list and an an adjacency matrix. b) Write code to turn the incidence list into an adjacency matrix. On 1/28/07, Zeth Green <theology@gmail.com> wrote:
On 29/01/07, Python Freak <newtopython@gmail.com> wrote:
I am clueless about where to start.
Okay, I did economics for my first degree so we have something in common! (Although I am not sure my maths ever got that deep). There are many ways to do what you want. The first step is to mentally translate what you want to do into Python's data types.
See here for a list of the basic types: http://docs.python.org/tut/node7.html
Do not forget you can nest data types inside each other, so the most basic matrix is a set of lists inside a list.
However once you understand what you are trying to do, it would then be worth downing tools and looking into the numpy module as it has many mathematical constructs all ready and abstracted for you.
Look at http://numpy.scipy.org/ for hours of fun(?). See for example,
https://networkx.lanl.gov/Reference/networkx.convert-module.html#to_numpy_ma... _______________________________________________ python-uk mailing list python-uk@python.org http://mail.python.org/mailman/listinfo/python-uk
On 29/01/07, Python Freak <newtopython@gmail.com> wrote:
As I am new to Python, I am clueless about where to start.
There are books such as Learning Python from O'Reilly and Beginning Python from Apress. There is also the tutor mailing list for folks who want to ask questions regarding how to learn computer programming with the Python language. http://mail.python.org/mailman/listinfo/tutor The Beginners Guide has links to lots of resources: http://wiki.python.org/moin/BeginnersGuide -- Mamading Ceesay There is no business to be done on a dead planet — David Brower Another world is here - http://www.worldchanging.com/
Okay I finally got the adjancency list representation working: v = range(1,11) al=zip(v[:-1],v[1:])+[(10,)] Is that right? It works fine. How do I go from here to creating an incidence list representation and adjacency matrix? (is it possible to assign to individual items in each tuple? I guess not). [ for those of you who missed my earlier email, this is not a homework; i'm a graduate student attempting to learn python ]. On 1/28/07, Zeth Green <theology@gmail.com> wrote:
On 29/01/07, Michael Sparks <ms@cerenity.org> wrote:
Is this homework?
I was about to respond with a literal answer but I thought that too. It was the mathematical terminology but especially this bit that made me suspect:
On Sunday 28 January 2007 22:45, Python Freak wrote: Hint: You may find it useful to note that one incidence list representation is...
python-uk mailing list python-uk@python.org http://mail.python.org/mailman/listinfo/python-uk
How do I go from here to creating an incidence list representation and adjacency matrix? (is it possible to assign to individual items in each tuple? I guess not).
You're right python tuples are immutable (1,2,3), whereas python lists [1,2,3] are mutable. However which you use is really a matter of what fits your development purposes - NOT what fits your mathematical model - mathematically speaking they can both represent a (mathematical) tuple... Worthwhile checking out list comprehension syntax [1] as well - very expressive syntax (and usually, as added value, the fastest option) But in general I can't help but think, if you're actually learning python, rather than trying to get a quick computational answer to your problem, then you'll be better served by making a class to represent your underlying objects (and/or as others have suggested using appropriate libraries like numpy). [1] http://docs.python.org/tut/node7.html#SECTION007140000000000000000 Python Freak wrote:
Okay I finally got the adjancency list representation working:
v = range(1,11) al=zip(v[:-1],v[1:])+[(10,)]
Is that right? It works fine.
[ for those of you who missed my earlier email, this is not a homework; i'm a graduate student attempting to learn python ].
On 1/28/07, *Zeth Green* <theology@gmail.com <mailto:theology@gmail.com>> wrote:
On 29/01/07, Michael Sparks <ms@cerenity.org <mailto:ms@cerenity.org>> wrote: > Is this homework?
I was about to respond with a literal answer but I thought that too. It was the mathematical terminology but especially this bit that made me suspect:
> On Sunday 28 January 2007 22:45, Python Freak wrote: Hint: You may find it useful to note that one incidence list representation is... _______________________________________________ python-uk mailing list python-uk@python.org <mailto:python-uk@python.org> http://mail.python.org/mailman/listinfo/python-uk
------------------------------------------------------------------------
_______________________________________________ python-uk mailing list python-uk@python.org http://mail.python.org/mailman/listinfo/python-uk
participants (5)
-
Mamading Ceesay -
Michael Sparks -
Python Freak -
Tim Diggins -
Zeth Green