graph-theory

Find how many connected groups of nodes in a given adjacency matrix

Find how many connected groups of nodes in a given adjacency matrix Question: I have a list of lists, each list is a node and contains the edges to other nodes. e.g [[1, 1, 0], [1, 1, 0], [0, 0, 1]] The node has a 1 when it refers to its own position, as well …

Total answers: 3

printing all the edges of a graph in an adjacency matrix in python

printing all the edges of a graph in an adjacency matrix in python Question: How do you print the all the edges of a graph with a given adjacency matrix in python? for example, if 0 is adjacent to 3 and 8, it should print: 0 3 0 8 without repetition I’ve been using Bfs …

Total answers: 4

deceptively simple implementation of topological sorting in python

deceptively simple implementation of topological sorting in python Question: Extracted from here we got a minimal iterative dfs routine, i call it minimal because you can hardly simplify the code further: def iterative_dfs(graph, start, path=[]): q = [start] while q: v = q.pop(0) if v not in path: path = path + [v] q = …

Total answers: 5

Spectral Clustering a graph in python

Spectral Clustering a graph in python Question: I’d like to cluster a graph in python using spectral clustering. Spectral clustering is a more general technique which can be applied not only to graphs, but also images, or any sort of data, however, it’s considered an exceptional graph clustering technique. Sadly, I can’t find examples of …

Total answers: 3

NetworkX find root_node for a particular node in a directed graph

NetworkX find root_node for a particular node in a directed graph Question: Suppose I have a directed graph G in Network X such that: G has multiple trees in it Every node N in G has exactly 1 or 0 parent’s. For a particular node N1, I want to find the root node of the …

Total answers: 4

Get all edges linked to a given node in a networkx graph

Get all edges linked to a given node in a networkx graph Question: Just wondering if there is convenient networkx function that returns a list of edges connected to a given node (or nodes) (e.g. my_node_name) in a graph (e.g. G). I can do it this way: edlist=[] for ed in G.edges(): if ‘my_node_name’ in …

Total answers: 2

Combine (join) networkx Graphs

Combine (join) networkx Graphs Question: Say I have two networkx graphs, G and H: G=nx.Graph() fromnodes=[0,1,1,1,1,1,2] tonodes=[1,2,3,4,5,6,7] for x,y in zip(fromnodes,tonodes): G.add_edge(x,y) H=nx.Graph() fromnodes=range(2,8) tonodes=range(8,14) for x,y in zip(fromnodes,tonodes): H.add_edge(x,y) What is the best way to join the two networkx graphs? I’d like to preserve the node names (note the common nodes, 2 to 7). …

Total answers: 3

How do I run graphx with Python / pyspark?

How do I run graphx with Python / pyspark? Question: I am attempting to run Spark graphx with Python using pyspark. My installation appears correct, as I am able to run the pyspark tutorials and the (Java) GraphX tutorials just fine. Presumably since GraphX is part of Spark, pyspark should be able to interface it, …

Total answers: 3

NoSQL Solution for Persisting Graphs at Scale

NoSQL Solution for Persisting Graphs at Scale Question: I’m hooked on using Python and NetworkX for analyzing graphs and as I learn more I want to use more and more data (guess I’m becoming a data junkie :-). Eventually I think my NetworkX graph (which is stored as a dict of dict) will exceed the …

Total answers: 3

How do weighted edges affect PageRank in networkx?

How do weighted edges affect PageRank in networkx? Question: I’m playing around with networkx (graph library in python) and I found documentation saying the PageRank algorithm takes edge weights into account when scoring, but I was wondering if larger edge weights were better or lower weights better? Asked By: Lostsoul || Source Answers: Shortly, large …

Total answers: 2