Message Boards Message Boards

0
|
3861 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Use an an anonymous function with NestGraph?

Posted 5 years ago

Hey,

I am currently in chapter 27 of the Wolfram Language Book and can't seem to grasp the list creation and handling in the NestGraph function.

NestGraph[{2 #, 2 # + 1} &, 0, 4, VertexLabels -> All]

To understand what the above does step by step I "extracted" a part:

In[1]:= {2 #, 2 # + 1} &@{2, 3}
Out[1]= {{4, 6}, {5, 7}}

My questions:

  1. Shouldn't the output be {{4, 5}, {6, 7}}? Why is it the way it is?
  2. How does NestGraph handle the above output internally to construct the correct graph?

Thank you very much!!!

Tim

POSTED BY: Timo Kuchheuser
3 Replies

Hey! Sorry to bother you again, but I'm still having problems understanding how NestGraph[] builds the tree.

I understand the function and how it generates the lists, I just don't get how NestGraph[] traverses the lists?! Please find attached a note I created to understand the flow.

My questions (please see attached image):

  1. How does NetGraph[] figure out to go from {1,3} to {2,6}?
  2. Are the list entries I tagged with a question mark not evaluated at all?

I think I'm having problems seeing something obvious and in the end it's probably easy, but I really like to understand.

Thank you very much!

Graph Notes

POSTED BY: Timo Kuchheuser

Thank you!

POSTED BY: Timo Kuchheuser
Posted 5 years ago

The argument to the pure function is {2, 3}, which is assigned to #

2 {2, 3}
(* {4, 6} *)

2 {2, 3} + 1
(* {5, 7} *)

Compare the output of the following for different values of n

NestList[{2 #, 2 # + 1} &, 0, n]

NestGraph[{2 #, 2 # + 1} &, 0, n, VertexLabels -> All]

and you should be able to see how the graph "grows".

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract