I am working with graph data defined as 2d array of edges. I.e.
JavaScript
x
6
1
[[1, 0],
2
[2, 5],
3
[1, 5],
4
[3, 4],
5
[1, 4]]
6
Defines a graph, all elements define a node id, there are no self loops, it is directed, and no value in a column exists in the other column.
Now to the question, I need to select all edges where both ‘nodes’ occur more than once in the list. How do I do that in a quick way. Currently I am iterating over each edge and looking at the nodes individually. It feels like a really bad way to do this.
Current dumb/slow solution
JavaScript
1
11
11
1
edges = []
2
for edge in graph:
3
src, dst = edge[0], edge[1]
4
# Check src for existance in col 1 & 2
5
src_fan = np.count_nonzero(graph == src, axis=1).sum()
6
dst_fan = np.count_nonzero(graph == dst, axis=1).sum()
7
8
if(src_fan >= 2 and dst_fan >= 2):
9
# Add to edges
10
edges.append(edge)
11
I am also not entirely sure this way is even correct…
Advertisement
Answer
JavaScript
1
15
15
1
# Obtain the unique nodes and their counts
2
3
from_nodes, from_counts = np.unique(a[:, 0], return_counts = True)
4
to_nodes, to_counts = np.unique(a[:, 1], return_counts = True)
5
6
# Obtain the duplicated nodes
7
8
dup_from_nodes = from_nodes[from_counts > 1]
9
dup_to_nodes = to_nodes[to_counts > 1]
10
11
# Obtain the edge whose nodes are duplicated
12
13
graph[np.in1d(a[:, 0], dup_from_nodes) & np.in1d(a[:, 1], dup_to_nodes)]
14
Out[297]: array([[1, 4]])
15