Added solutions for some basic tasks#5
Conversation
ntamas
left a comment
There was a problem hiding this comment.
Thanks for your contribution! I've added a few suggestions with possible improvements.
| for e in g.es: | ||
| union(e.source, e.target) | ||
| connected_components = dict() | ||
| for i in range(20): |
There was a problem hiding this comment.
Do not hardcode the number of vertices here.
|
|
||
| for e in g.es: | ||
| union(e.source, e.target) | ||
| connected_components = dict() |
There was a problem hiding this comment.
You can use a defaultdict from the collections module to avoid having to deal with the case when you are adding a vertex to the component for the first time.
| g = ig.Graph.Erdos_Renyi(20, m=8, directed=False, loops=False) | ||
| wcc = weakly_connected_components(g) | ||
| colors = ["aliceblue", "antiquewhite", "aqua", "aquamarine", "beige", "navyblue", "orchid", "blue", "blueviolet", "brown", "cadetblue", "chartreuse", "cornflowerblue", "gold", "darkolivegreen", "darksalmon", "firebrick", "goldenrod", "khaki"] | ||
| for a in range(len(wcc.keys())): |
There was a problem hiding this comment.
You can simply iterate over the key-value pairs of the dictionary with for key, value in wcc.items()
| for a in range(len(wcc.keys())): | ||
| i = list(wcc.keys())[a] | ||
| for j in wcc[i]: | ||
| g.vs[j]["color"] = colors[a] |
There was a problem hiding this comment.
This can be done in a single call: g.vs[wcc[i]]["color"] = colors[a] * len(wcc[i]). Also note that wcc[i] is unnecessasry if you use something like for members in wcc.values() for the iterations (it seems like you won't need the key itself).
| kneighborhood = [] | ||
| distances = g.distances(v, g.vs, weights=None, mode='all')[0] | ||
| for i in range(n): | ||
| if distances[i] == k: |
There was a problem hiding this comment.
I think this should be <= k here, not == k, but maybe @szhorvat meant something different when he originally specified the benchmark.
Nevertheless, you can simplify this greatly with a Python list comprehension. Also note that you need to exclude v itself:
kneighborhood = [i for i, d in enumerate(distances) if d <= k and i != v.index]
There was a problem hiding this comment.
Regarding distances = g.distances(v, g.vs, weights=None, mode='all')[0],
targetshould be left atNone. @ntamas In the C layer, theigraph_vss_all()path is better optimized for many functions, and will perform better than listing all vertices explicitly. I assumeNonemaps toigraph_vss_all(). What aboutg.vs? Does it map tovss_all()or to something for whichigraph_vs_is_all()is nottrue?- Generally, computing the distance to all other vertices, even those further than
ksteps away, will be quite inefficient. Using theneighborhood()method will be better here.
| if distances[i] == k: | ||
| kneighborhood.append(i) | ||
| for i in kneighborhood: | ||
| res[v.index] += g.degree(i) |
There was a problem hiding this comment.
You can query g.degree() in advance outside the for loop and just index into it. This avoids querying the degree of the same vertex multiple times if the vertex occurs in the neighborhood of many other vertices.
| for i in kneighborhood: | ||
| res[v.index] += g.degree(i) | ||
| res[v.index] /= len(kneighborhood) | ||
| res[v.index] = round(res[v.index], 2) |
There was a problem hiding this comment.
Rounding was not asked for in the original task.
| @@ -0,0 +1,13 @@ | |||
| import igraph as ig | |||
There was a problem hiding this comment.
Please specify which benchmark task this file is solving.
Added solutions for some of the basic benchmark tasks.
I hope I did this right; please let me know if any adjustments need to be made.
This is for igraph/python-igraph#469.