0

Situation

I'd like analyse my web site with CommunityGraphPlot as follows:

Step 1: Define a function to scrape the all the webpages from expected website.

netScrape[url_] :=
  Union[Flatten[Thread[# -> Import[#, "Hyperlinks"]]& /@ Last/@ url]]

Step 2: Define a function to crawl the topology, down to the depth I want to dig.

webCrawler[rootUrl_, depth_] := 
  Flatten[Rest[NestList[netScrape, {"" -> rootUrl}, depth]]]

Step 3: crawl the website.

netFriends = webCrawler["http://bitwan.science", 2] 

Step 4: Plot the community.

CommunityGraphPlot[netFriends]

Debug

I ran the above code, step1 through step4. However, I get a warning saying

A graph object is expected at position 1 in EdgeList ...

Checking the data type of netFriends with netFriends // FullForm, confirms it is a list of rules.

I refer to Mathematica documentation on CommunityGraphPlot. I find it says that it's legal when using

CommunityGraphPlot[{v1 -> v2, v3 -> v4}, ... ]

My Question

I am confused by this situation. As the netFriends is a list of rules, why doesn't CommunityGraphPlot[netFriends] work?

Can anyone tell me why my codes produced the error I am seeing?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
PureLine
  • 1,310
  • 7
  • 20
  • Can you share the contents of the file "friends.txt"? Or at least an representative example of its form? – rhermans Nov 13 '15 at 10:03
  • 1
    hello @rhermans , I have upload the data set. – PureLine Nov 13 '15 at 10:09
  • Thanks, BTW ?dl=1 in the Dropbox URL for automatic download. I edited it for you. Please keep that file there forever, as this question will be visited by other users in the future. – rhermans Nov 13 '15 at 10:13
  • StringQ@friends give True. You have imported the data as a single String, not a table of rules. – rhermans Nov 13 '15 at 10:19
  • Your question may be put on-hold as it seems to be off-topic, i.e it arises from a simple mistake (syntax error, incorrect capitalization, spelling mistake) and is unlikely to help any future visitors, or else it is easily found in the documentation. Don't be discouraged by that cleaning-up policy. Your future good questions are welcome. Learn about common pitfalls here. – rhermans Nov 13 '15 at 10:28
  • hi @rhermans . My original problem wasn't caused by misused data type. I have post my original problem. – PureLine Nov 15 '15 at 09:35
  • You have changed the question completely, I answered the original one, not the new one. Probably you should have asked a new question. – rhermans Nov 17 '15 at 10:20
  • @rhermans you are right. now i will closed this post. Thanks your help very much. But, I've no idea how to close it.. – PureLine Nov 18 '15 at 02:15

1 Answers1

2

The problem in your code is that you have imported the data as a single String, not as table of rules as you expected. See that by evaluating StringQ[friends].

One solution is this:

friends = Import[
  "https://www.dropbox.com/s/hq8twoz2ct99kzr/friends.txt?dl=1"
 ,"Table"
 ]

CommunityGraphPlot[Rule[#1, #3] & @@@ friends]

Mathematica graphics

rhermans
  • 36,518
  • 4
  • 57
  • 149