bipartite

Detect if a graph is bipartite using union find (aka disjoint sets)

不想你离开。 提交于 2019-12-02 14:35:52
I'm doing a problem on Spoj that basically reduces to detecting if a graph is bipartite. I'm trying to just color the graph using dfs, but it is too slow. Some guy comments this No bfs, no dfs, no bipartie graph. Simple Union-Find Set would make it (with speed, indeed). Hint #1: Cycle of even length does not affect the divisibility by 2 ( wow, so many i in a word ) of length of paths between two node. Hint #2: (SPOILER) let dist[i] be the distance of a path from i to parent[i]. update it with the find and union function. It can be improved to make dist a bool array. Could someone explain what

Simplest way to plot changes in ranking between two ordered lists in R?

ぃ、小莉子 提交于 2019-11-29 02:04:05
I'm wondering if there is an easy way to plot the changes in position of elements between 2 lists in the form of a directed bipartite graph in R. For example, list 1 and 2 are vectors of character strings, not necessarily containing the same elements: list.1 <- c("a","b","c","d","e","f","g") list.2 <- c("b","x","e","c","z","d","a") I would like to generate something similar to: I've had a slight bash at using the igraph package, but couldn't easily construct what I would like, which I imagine and hope shouldn't be too hard. Cheers. Here is a simple function to do what you want. Essentially it

Hopcroft–Karp algorithm in Python

人走茶凉 提交于 2019-11-29 01:09:28
问题 I am trying to implement the Hopcroft Karp algorithm in Python using networkx as graph representation. Currently I am as far as this: #Algorithms for bipartite graphs import networkx as nx import collections class HopcroftKarp(object): INFINITY = -1 def __init__(self, G): self.G = G def match(self): self.N1, self.N2 = self.partition() self.pair = {} self.dist = {} self.q = collections.deque() #init for v in self.G: self.pair[v] = None self.dist[v] = HopcroftKarp.INFINITY matching = 0 while

How to create a bipartite graph in GraphX

荒凉一梦 提交于 2019-11-28 12:45:54
I am able to build a graph using a vertexRDD and an edgeRDD via the GraphX API, no problem there. i.e.: val graph: Graph[(String, Int), Int] = Graph(vertexRDD, edgeRDD) However, I don't know where to start if I want to use two separate vertexRDD's instead of just one (a bipartite graph). Fore example, a graph containing shopper and product vertices. My question is broad so I'm not expecting a detailed example, but rather a hint or nudge in the right direction. Any suggestions would be much appreciated. eliasah For example to model users and products as a bipartite graph we might do the

Bipartite graph in NetworkX

早过忘川 提交于 2019-11-28 09:58:12
B.add_nodes_from(a, bipartite=1) B.add_nodes_from(b, bipartite=0) nx.draw(B, with_labels = True) plt.savefig("graph.png") I am getting the following figure. How can I make it look like a proper bipartite graph? You could do something like this, to draw nodes from each partition at a particular x coordinate: X, Y = bipartite.sets(B) pos = dict() pos.update( (n, (1, i)) for i, n in enumerate(X) ) # put nodes from X at x=1 pos.update( (n, (2, i)) for i, n in enumerate(Y) ) # put nodes from Y at x=2 nx.draw(B, pos=pos) plt.show() The key is creating the dict for the the nx.draw pos parameter,

Simplest way to plot changes in ranking between two ordered lists in R?

拜拜、爱过 提交于 2019-11-27 16:24:38
问题 I'm wondering if there is an easy way to plot the changes in position of elements between 2 lists in the form of a directed bipartite graph in R. For example, list 1 and 2 are vectors of character strings, not necessarily containing the same elements: list.1 <- c("a","b","c","d","e","f","g") list.2 <- c("b","x","e","c","z","d","a") I would like to generate something similar to: I've had a slight bash at using the igraph package, but couldn't easily construct what I would like, which I imagine

How to create a bipartite graph in GraphX

人走茶凉 提交于 2019-11-27 07:15:47
问题 I am able to build a graph using a vertexRDD and an edgeRDD via the GraphX API, no problem there. i.e.: val graph: Graph[(String, Int), Int] = Graph(vertexRDD, edgeRDD) However, I don't know where to start if I want to use two separate vertexRDD's instead of just one (a bipartite graph). Fore example, a graph containing shopper and product vertices. My question is broad so I'm not expecting a detailed example, but rather a hint or nudge in the right direction. Any suggestions would be much

Bipartite graph in NetworkX

别来无恙 提交于 2019-11-27 02:48:31
问题 B.add_nodes_from(a, bipartite=1) B.add_nodes_from(b, bipartite=0) nx.draw(B, with_labels = True) plt.savefig("graph.png") I am getting the following figure. How can I make it look like a proper bipartite graph? 回答1: You could do something like this, to draw nodes from each partition at a particular x coordinate: X, Y = bipartite.sets(B) pos = dict() pos.update( (n, (1, i)) for i, n in enumerate(X) ) # put nodes from X at x=1 pos.update( (n, (2, i)) for i, n in enumerate(Y) ) # put nodes from