python tsp travelling salesman undirected graph - python

In other posts Networkx was suggested as "my friend". But there doesn't seem to be a ready to use function for a certain solution for the TSP problem.
i.e. Creating undirected graphs in Python
I have an undirected graph, the suggested solutions are all related to directed graphs, and I want to know a short tour to visit all nodes using the available edges.
(also, the tsp with directed graphs I could not find in the documentation of networkx)
Does anybody did something like this for an undirected graph or should I modify solutions for directed graphs with infinit costs for unconnected nodes?
edit: I am learning: Actually, as the graph is unweighted (or 'all weights' are the same), and not every node is connected to all other nodes, I just need to find a cycle in the graph containing all the nodes. When that cycle does not exist, nodes may be repeated (so, it is not a cycle anymore...). There are no isolated groups (there is a path from each node to another). I think that this is not the salesman problem?!
Thanks for your feedback so far (when milliseconds start to matter, I will install a photofinish :) )

If you already have code for directed graphs, I would just convert your undirected graph. Replace each undirected edge with two directed edges, one in each direction, preserving the edge weight.

Related

Steiner Tree for Directed Graphs

I was using steiner_tree approximation algorithm in NetworkX library. While feeding it a directed graph it showed me an error that
NetworkXNotImplemented: not implemented for directed type.
I also tried to convert the following graph to an undirected type, and feed the same. But I'm losing some information like edge directions.
How can I extract Steiner Tree on a Directed Graph?
You cannot get a solution to the directed Steiner tree problem from the undirected one. I would suggest to write out your directed graph and solve it with specialized software for directed Steiner tree problems. I don't know any
python package that is able to do that, the only software I know is:
https://scipjack.zib.de/

Having a directed graph in pyhton (networkx), is it possible to create a subgraph according to a specific condition on the node attributes?

I have a twitter data set that i want to analyze,after creating a directed graph(the nodes are twitter users, and edge from a to b means a follows b)
how i created the graph :
I want to make a sub graph that contains only male users and edges between them, and another one containing only female users and edges between them.
thank you.
I am working on a similar problem, and I think there are two fitting stack answers:
Select nodes and edges form networkx graph with attributes
partition graph into sungraphs based on node's attribute NetworkX
The preliminary requirement is that you add attributes to your nodes and edges according to what you need to use later
Not sure if this is satisfactory, but maybe it'll help a little.
PS. If you've already solved it, paste your solution here so others can see :)

Python NetworkX finding an specific subgraph

Assume that we turned a UML diagram into a directed graph with labeled edges (edges has labels like "Association" or "Generalization"). Is there any way to find a specific subgraph, say finding the graph of the Factory design pattern in this graph using NetworkX APIs?

How can I find the number of unique undirected edges of a graph using networkx?

I have a directed bigraph that contains directed and undirected edges.
networkx_Graph = nx.read_adjlist('graph.txt', create_using=nx.DiGraph())
I was able to find the number of directed edges using: len(list(networkx_Graph.in_edges(data=False)))
But I am trying to find the number of undirected edges.
This is quite easy using the python package snap, but I am not finding anything like this in networkx's documentation?
What is the networkx equivalent of snap.CntUniqDirEdges()?
You have to convert the graph to an undirected graph the calculate the size. NetworkX does not have a function that does this in one shot like snap does.
networkx_Graph.to_undirected().size()

Graphviz: how to insert two new linked nodes and minimize edge crossings?

I have the following graph :
As you can see, there are two natural clusters. I would like to figure out a way to separate these clusters into two graphs.
The key step, of course, is to compute the right split. I would like to insert two nodes n1 & n2, link them e(n1, n2), and move them around, minimizing the number of edge crossings (of course fixing all nodes/edges exactly where they are).
Can anyone offer any help here? I don't think graphviz has anything that enables me to do it.
I think you mingle two different tasks here: the one is Analysis of a graph, the other one is Visualization of the same.
Graphviz, as the name suggests, is a tool for visualization of graphs. Visualization can take many forms, typically one tries to "make it look good" by having those nodes close to each other that are connected, thus reducing the visual edge lengths. One can utilize some spring- or gravitational model to calculate optimal positions for all nodes. Other options include circular- or shell-layouts.
A certain visualization should not be the basis for the analysis of a graph. Graph properties, like average shortest path length or clustering coefficient, are independent of any visualization.
You say you want to "minimize the number of edge crossings". The number of edge crossings is a property of your visualization, not of your graph! It probably changes each time you let graphviz calculate the layout, even if the graph is unchanged. Who says that 2d is the only possible representation of your graph? Add just one dimension, and you won't have any edge crossing.
I'd recommend to concentrate on graph analysis. I don't know if you're aware of NetworkX. They have dozens of Algorithms to analyze your graph. Maybe the clustering and clique sections are of interest to you.

Categories