訪問したノードの数が 1000 になるまで、ノードから開始して BFS 内のすべてのユーザーをたどって、slideshare.net グラフをクロールしています。次の方法で BFS を実行します。
from urllib.request import urlopen
from collections import deque
import sys
import json
import codecs
import csv
import io
import hashlib
import time
import xml.etree.ElementTree as etree
queue = deque(["himanshutyagi11"])
while len_visitedset < 1ooo:
vertex = queue.pop()
if vertex in visited:
continue
visited.add(vertex)
length_visited = len(visited)
print(vertex,length_visited)
crawl(vertex)
crawl() は、ここで説明されているように、slideshare api クエリを 作成する関数です。(api 登録時に指定された) 私の shared_secret と api_key を使用してクエリ ペイロードを作成し、クエリを送信し、変数 'response に保存されている XML 応答を解析します。 '。解析後、現在のノードの連絡先をキューに追加します。
request_timestamp = int(time.time())
request_hash_string = shared_secret+str(request_timestamp)
request_hash_value = hashlib.sha1(request_hash_string.encode('utf-8')).hexdigest()
request_url = 'https://www.slideshare.net/api/2/get_user_contacts?username_for='+username+'&api_key='+api_key+'&hash='+request_hash_value+'&ts='+str(request_timestamp)
response = etree.parse(urlopen(request_url)).getroot()
# Append all the adjacent nodes of this user to the queue.
for child in response:
friend_name = child[0].text
queue.appendleft(friend_name)
edge_file = open('non_anonymized.csv','a')
for child in response:
f_name = child[0].text # Name of friend is stored in variable 'f_name'
edge_file.write(username+','+f_name+'\n') # username is the name of user currently being crawled
edge_file.close()
クロール中に、グラフ内のすべてのエッジを含む edgelist.csv ファイルも作成します。このファイルは問題ないようです。また、degree()、in_degree()、average_clustering() などの他の関数も正常に動作しているようです。
次に、networkx を使用して 1000 ノードのグラフを作成します。しかし、次の関数を使用してこのグラフの直径を見つけようとすると:
diameter = nx.diameter(graph)
上記のコードでは、グラフの直径を見つけることができません。これは何も返さず、プログラムはこの行で動かなくなります。何が起こっているのかについての洞察はありますか?私のものは接続されたグラフです。関数を使用して無向に変換していto_undirected()
ます。有向グラフで実行するのに疲れて、次のエラーが発生しました
networkx.exception.NetworkXError: Graph not connected: infinite path length
私の質問は、クロールに BFS を使用しているため、どうすれば切断できるかということです。
Python 3.4
Networkx 1.9.1