graph-theory

How to remove cycles in an unweighted directed graph, such that the number of edges is maximised?

随声附和 提交于 2020-04-07 14:59:10
问题 Let G be an unweighted directed graph containing cycles. I'm looking for an algorithm which finds/creates all acyclic graphs G', composed of all vertices in G and a subset of edges of G, just small enough to make G' acyclic. More formal: The desired algorithm consumes G and creates a set of acyclic graphs S, where each graph G' in S satisfies following properties: G' contains all vertices of G. G' contains a subset of edges of G, such that G' is acyclic. The number of edges of G' is maximised

Group connected graphs in pandas DF

送分小仙女□ 提交于 2020-04-06 08:37:32
问题 I have a pandas DF where each column represent a node and two columns an edge, as following: import pandas as pd df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'], 'node2': ['4', '13', '25', '38', '208', '300']}) All Nodes are Undirected, i.e. you can get from one to the other undirected_graph I would like to group them into all connected groupes (Connectivity), as following: df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'], 'node2': ['4', '13', '25', '38', '208',

How to detect a cycle in a directed graph with Python?

旧时模样 提交于 2020-03-08 09:28:10
问题 I have some input like: [('A', 'B'),('C', 'D'),('D', 'C'),('C', 'D')] . I want to look for if the existence of a cycle in a directed graph represented by this edgeList. I read a discussion: https://www.geeksforgeeks.org/detect-cycle-in-a-graph/, however it has some errors when the case is: g = Graph(3) g.addEdge('A', 'B') g.addEdge('B', 'C') g.addEdge('C', 'A') Its result is 'Graph has no cycle'. This is clearly wrong. Can you help me to solve this problem? 回答1: Using the networkx library, we

Is it possible to store graphs hbase? if so how do you model the database to support a graph structure?

人走茶凉 提交于 2020-02-20 05:09:38
问题 I have been playing around with using graphs to analyze big data. Its been working great and really fun but I'm wondering what to do as the data gets bigger and bigger? Let me know if there's any other solution but I thought of trying Hbase because it scales horizontally and I can get hadoop to run analytics on the graph(most of my code is already written in java), but I'm unsure how to structure a graph on a nosql database? I know each node can be an entry in the database but I'm not sure

Getting connected components of graph in Prolog

早过忘川 提交于 2020-02-03 18:59:29
问题 I'm struggling with logic programming. I have a this problem and I hope some of you can help me with it. Discontinous graph is represented by facts in this way: h(0,1). h(1,2). h(3,4). h(3,5). So there is two separate graph components. I would like all the separate components on the output represented by a list. So if there is three separate components in the graph, there will be three lists. For the given example above, the expected output is [[0,1,2],[3,4,5]] . 回答1: Using iwhen/2 we can

How do I enumerate all *maximal* cliques in a graph using networkx + python?

纵然是瞬间 提交于 2020-01-30 12:56:48
问题 If you look at https://en.wikipedia.org/wiki/Clique_problem, you'll notice there is a distinction between cliques and maximal cliques. A maximal clique is contained in no other clique but itself. So I want those clique, but networkx seems to only provide: networkx.algorithms.clique.enumerate_all_cliques(G) So I tried a simple for loop filtering mechanism (see below). def filter_cliques(self, cliques): # TODO: why do we need this? Post in forum... res = [] for C in cliques: C = set(C) for D in

Optimize connection between nodes in a graph [duplicate]

亡梦爱人 提交于 2020-01-25 20:58:56
问题 This question already has answers here : Connect nodes to maximize total edge weight (5 answers) Closed 2 years ago . I am working on a problem which could be reduced to a graph optimization problem as below. A set of colored nodes are given. A set of rules are given about the cost contribution from the nodes. Ex. If a red node is not connected, cost is 100 If a red node connects to red node, cost is 10 If a red node connects to a blue node, cost is 20 Any node can have only 4 connections at

All possible path in a graph

折月煮酒 提交于 2020-01-25 08:18:05
问题 Given a graph G(V, E) , a source vertex s and destination vertex d , the problem is to find all possible paths from s to d where G may contain loops and cycles. I want to get all simple paths, no cycle is allowed. What would be the complexity of this problem? 回答1: This problem is NP-hard, since its output may have an exponential size w.r.t its input. Finding the longest path between two points is already NP-hard (reduction to hamiltonian path problem), so finding all of them is as well. You

How to return an iterator over the keys of a HashMap from a trait implementation?

夙愿已清 提交于 2020-01-24 22:05:52
问题 I'm trying to build a simple graph library in Rust. There is a trait Graph that any graph must implement. This trait has only one function at the moment, nodes , which allows iteration of the graph's nodes using a for-in loop. An implementation of Graph , MapGraph , is a lightweight wrapper around a HashMap . MapGraph must implement the Graph trait method nodes . I'm having problems getting this to work. Here's the code for Graph : pub trait Graph<N> { fn nodes(&self) -> Box<dyn Iterator<Item

Updating Shortest path distances matrix if one edge weight is decreased

本小妞迷上赌 提交于 2020-01-23 08:17:05
问题 We are given a weighed graph G and its Shortest path distance's matrix delta. So that delta(i,j) denotes the weight of shortest path from i to j (i and j are two vertexes of the graph). delta is initially given containing the value of the shortest paths. Suddenly weight of edge E is decreased from W to W'. How to update delta(i,j) in O(n^2)? (n=number of vertexes of graph) The problem is NOT computing all-pair shortest paths again which has the best O(n^3) complexity. the problem is UPDATING