def k_skip_graph(G,cover,k):

        H = nx.Graph()
	s1=time.time()
	for node in cover:
		
		G2 = share.vincity(G,node,k)
		SPT = share.SPT(G2,node)
		SPTD = nx.single_source_dijkstra_path_length(SPT,node)
		tmp = [node]
		picked = []
		while(len(tmp)>0):
			x = tmp.pop()
			picked.append(x)
			for u in SPT.neighbors(x):
				if u in picked:
					continue
				if u in cover and u != node:
					H.add_edge(node,u)
					H[node][u]['weight'] = SPTD[u]
				else:
					tmp.insert(0,u)
		
	#	print '..............................................'
        t1 = time.time()
	print '%.4f sec -- kG construct finished' %(t1-s1)        
        return H
def sampling(G,cent,hop,cover):
	
	G2 = share.vincity(G,cent,hop)
	sp = nx.single_source_dijkstra(G2,cent)
	for path in sp[1].values():
		if len(path) >= hop:
			if len(set(path) & set(cover))==0:
				return True
	return False
def local_query_edit(G,start,end,cover,k):

	G2 = share.vincity(G,start,k)
	sp = nx.single_source_dijkstra(G2,start)

	n_set = {}
	loc_dist = -1	
	if end in sp[1]:
		loc_dist = sp[0][end]

	for path in sp[1].values():
		tmp = list(set(path[1:])&set(cover))
		if len(tmp)>0:
			tmp = tmp[0]
			if tmp not in n_set:
				n_set[tmp] = sp[0][tmp]
	return [n_set,loc_dist]
def k_skip_graph(G,cover,k):
	cover_tmp = [i for i in cover]
	H = nx.Graph()

	while(len(cover_tmp) >0):
		cent = cover_tmp[0]
		cover_tmp.remove(cent)
		dist={}; dist[cent]=[0,0,0]
		picked = []
		while( len(dist) >0):
			u = share.pick_min_Key_edit(dist)
			G2 = share.vincity(G,u,k)
			picked.append(u)
			if u in cover and u != cent and dist[u][2] == 0:
				H.add_edge(cent,u)
				H[cent][u]['weight']=dist[u][0]
				dist[u][2] = 1 
				
			if dist[u][1] < k:
				share.find_neighbors_edit2(G2,u,dist,picked)
			del dist[u]
	return H