0
$\begingroup$

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?

$\endgroup$
8
  • $\begingroup$ Can you share the contents of the file "friends.txt"? Or at least an representative example of its form? $\endgroup$
    – rhermans
    Commented Nov 13, 2015 at 10:03
  • 1
    $\begingroup$ hello @rhermans , I have upload the data set. $\endgroup$
    – PureLine
    Commented Nov 13, 2015 at 10:09
  • $\begingroup$ 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. $\endgroup$
    – rhermans
    Commented Nov 13, 2015 at 10:13
  • $\begingroup$ StringQ@friends give True. You have imported the data as a single String, not a table of rules. $\endgroup$
    – rhermans
    Commented Nov 13, 2015 at 10:19
  • $\begingroup$ 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. $\endgroup$
    – rhermans
    Commented Nov 13, 2015 at 10:28

1 Answer 1

2
$\begingroup$

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

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.