graph-theory

Cliques in python

馋奶兔 提交于 2019-12-07 08:49:30
问题 I have this problem and I need help with it, this is my code: cliques=[clique for clique in nx.find_cliques(GC) if len(clique)>2] for clique in cliques: if len (clique)==3: GC.remove_edge() print "Clique to appear: ",clique #draw the graph nx.draw(GC) plt.show() first I searched in my graph to find cliques, after that I test if the clique of length 3, if its true I want to delete one edge So I can eliminate complete-graph(3). How can I do that? Thanks 回答1: I think the trickiest problem here

Dijkstras Algorithm doesn't appear to work, my understanding must be flawed

 ̄綄美尐妖づ 提交于 2019-12-07 04:49:38
问题 Here's my interpretation of how Dijkstra's algorithm as described by wikipedia would deal with the below graph. First it marks the shortest distance to all neighbouring nodes, so A gets 1 and C gets 7. Then it picks the neighbour with the current shortest path. This is A. The origin is marked as visited and will not be considered again. The shortest (and only) path from the origin to B through A is now 12. A is marked as visited. The shortest path from the origin to the Destination through B

Remove all nodes in a networkx DiGraph with in-degree and out-degree equal to 1

穿精又带淫゛_ 提交于 2019-12-07 04:12:02
问题 Say I make a DiGraph in NetworkX: import networkx as nx G = nx.DiGraph() n = ["A","B","C","D","E","F","H","I","J","K","L","X","Y","Z"] e = [("A","Z"),("Z","B"),("B","Y"),("Y","C"),("C","G"),("G","H"),("G","I"),("I","J"),("K","J"),("J","L"),("F","E"),("E","D"),("D","X"),("X","C")] G.add_nodes_from(n) G.add_edges_from(e) How would I remove all the nodes with a in-degree and an out-degree equal to 1, so that my graph would look like this?: import networkx as nx G = nx.DiGraph() n = ["A","C","F",

Connect an even number of nodes without intersection

梦想与她 提交于 2019-12-07 03:46:18
问题 I have two sets of n nodes. Now I want to connect each node from one set with another node from the other set. The resulting graph should have no intersections. I know of several sweep line algorithms (Bentley-Ottmann-Algorithm to check where intersections occur, but I couldn't find an algorithm to solve those intersections, except for a brute-force approach. Each node from one set can be connected to any other node within the other set. Any pointers to (an efficient) algorithm that solves

How to do directed graph drawing in PHP?

只愿长相守 提交于 2019-12-07 02:23:56
问题 I'm looking for a way to draw directed graphs in PHP. (as in http://upload.wikimedia.org/wikipedia/commons/0/08/Directed_acyclic_graph.png). I want it to create an image of the graph just like GD can output an image. I've googled a lot on this, but I only can find a lot of libraries for drawing graphs in general (with bars etc), not directed graphs. P.S. I've tried using dot (the linux program) via system(), but unfortunately I have no permission to do that on the server. Also, I have no

Why is greedy algorithm not finding maximum independent set of a graph?

佐手、 提交于 2019-12-07 01:27:43
问题 Given a graph G, why is following greedy algorithm not guaranteed to find maximum independent set of G: Greedy(G): S = {} While G is not empty: Let v be a node with minimum degree in G S = union(S, {v}) remove v and its neighbors from G return S I am wondering can someone show me a simple example of a graph where this algorithm fails? 回答1: I'm not sure this is the simplest example, but here is one that fails: http://imgur.com/QK3DC For the first step, you can choose B, C, D, or F since they

How to efficiently calculate triad census in undirected graph in python

☆樱花仙子☆ 提交于 2019-12-06 17:22:27
问题 I am calculating triad census as follows for my undirected network . import networkx as nx G = nx.Graph() G.add_edges_from( [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'), ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')]) from itertools import combinations #print(len(list(combinations(G.nodes, 3)))) triad_class = {} for nodes in combinations(G.nodes, 3): n_edges = G.subgraph(nodes).number_of_edges() triad_class.setdefault(n_edges, []).append(nodes) print(triad_class) It works fine

Using hypergraphs in pygraph, need verification that example is correct

坚强是说给别人听的谎言 提交于 2019-12-06 14:56:09
问题 I am trying to get my head around using hypergraphs in pygraph, the following is a simple example I inserted: hgr = hypergraph() hgr.add_nodes(["A1", "B1", "C1", "D1"]) hgr.add_nodes(["A2", "B2", "C2", "D2"]) hgr.add_nodes(["A3", "B3", "C3", "D3"]) hgr.add_nodes(["A4", "B4", "C4", "D4"]) hgr.add_hyperedge(("A1", "A2", "A3", "A4")) hgr.add_hyperedge(("B1", "B2", "B3", "B4")) hgr.add_hyperedge(("C1", "C2", "C3", "C4")) hgr.add_hyperedge(("D1", "D2", "D3", "D4")) h_dot = write(hgr) h_gvv = gv

How can I efficiently create a user graph based on transaction data using Python?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 13:43:43
I'm attempting to create a graph of users in Python using the networkx package. My raw data is individual payment transactions, where the payment data includes a user, a payment instrument, an IP address, etc. My nodes are users, and I am creating edges if any two users have shared an IP address. From that transaction data, I've created a Pandas dataframe of unique [user, IP] pairs. To create edges, I need to find [user_a, user_b] pairs where both users share an IP. Let's call this DataFrame 'df' with columns 'user' and 'ip'. I keep running into memory problems, and have tried a few different

Finding the shortest path in a graph between 2 nodes that goes through a subset of nodes

一曲冷凌霜 提交于 2019-12-06 12:18:23
I'm trying to find out an efficient way of finding the shortest path between 2 nodes in a graph with positive edge costs that goes trough a subset of nodes. More formally: Given a graph G = (U, V) where U is the set of all nodes in the graph and V is the set of all edges in the graph, a subset of U called U' and a cost function say: f : UxU -> R+ f(x, y) = cost to travel from node x to node y if there exists an edge between node x and node y or 0 otherwise, I have to find the shortest path between a source node and a target node that goes trough all the nodes in U'. The order in which I visit