I am looking for building a hierarchical graph dynamically & programmatically.
a graph like this:
(source: graphviz.org)
I don't know how to place the nodes in a good way to avoid collisions.
Any idea of an algorithm I could use?
Related
I have a graph which is connected and the edges have weights on them. Less the weight between an edge, the more closer the adjoining vertices are. I want to divide the graph into k smaller subgraphs such that nodes in all the subgraphs are very similar.
In other words, I need to cluster the graph. Can somebody suggest clustering algorithms that are suitable for graphs and have less time comlexity(lesser than O(n^2))?
Clustering is difficult problem that in general could be solved exaclty by brute force only. So in case of efficient algorithms your have to rely on some heuristic approach. In case if you can solve your problem as a vertex clustering task, you can try something like k-means, but it could be slow on larger data sets.
For graph specifically, I would suggest using MCL algorithm on your problem. MCL seems to be efficient in a lot of cases. It uses randomized flow simulation to detect clusters in weighted/unweighted graphs. Basic idea is that flow gathers within a cluster, while links between clusters tend to be less saturated.
Suppose I have an undirected weighted connected graph. I want to group vertices that have highest edges' value all together (vertices degree). Using clustering algorithms is one way. What clustering algorithms can I consider for this task? I hope it is clear; any question for clarification, please ask. Thanks.
There are two main approach - giving your graph as an input to existing tool, or using expert knowledge you have on this graph (and its domain) in order to create a representation, and then apply machine learning methods on it.
I'll start with the second approach:
If you have only the nodes and edges (no farther data for each node), you first need to think of a representation for each node\edge. I going to explain about nodes, but it should should be similar for edges' case.
The simplest approach is to represent each node n as a connectivity vector:
Every node will be represented as n=(Ia(n),Ib(n),Ic(n),Id(n),Ie(n)), where Ii(n)=1 in case node n is a 'friend' (neighbor) of node i, and 0 otherwise. (e.g.a=(0,1,1,0,1))
Note that you can decide if a node is a friend of itself.
Second approach, which is quite similar to the first one, is to use edges' weights vector:
n=(W(a,n),W(b,n),W(c,n),W(d,n),W(e,n)) , where W(i,n) is the weight of the edge (i,n).
There are a few more ways to represent nodes, but this is enough in order to run some calculations on it.
After you have this presentation, you can start applying some clustering algorithms on it.
kmeans is considered great for this task, and sklearn has a great implementation. It has some parameters you can (and should) configure (i.e. the distance measure).
The product of kmeans, is k different non-intersecting groups of nodes.
If you want to pass you graph to an algorithm and get some measures, there are more advanced algorithms you can apply. community detection is used to find communities in a graph. Again, there is a nice python implementation in the networkxpackage.
I have a set of weighted pairwise relations between nodes which are all of the same type, like this:
A-[1]->B
A-[2]->C
B-[3]->C
B-[2]->D
E-[1]->A
I'd like to lay out this graph in such a way that makes the precedence order of the nodes relatively clear (i.e. that "flow" goes roughly from E to A to B/C/D)
I think what I need similar to a Force Layout but with the added notion of edge weight and directionality
I've looked into using neo4j's builtin viz view and d3 but they don't seem to offer what I need out of the box. Is there a standard approach to this kind of problem?
Even with Neo4j's built in viz you should be able to do:
MATCH path = (a:LabelA {id:"A"})-[:FOO*..10]->()
RETURN path
which should show you the tree starting from A
With reference to Kruskal's algorithm in Ada, I'm not sure where to start.
I'm trying to think through everything before I actually write the program, but am pretty lost as to what data structures I should be using and how to represent everything.
My original thought is to represent the full tree in an adjacency list, but reading Wikipedia the algorithm states to create a forest F (a set of trees), where each vertex in the graph is a separate tree and I'm not sure how to implement this without getting really messy quickly.
The next thing it says to do is create a set S containing all the edges in the graph, but once again I'm not sure what the best way to do this would be. I was thinking of an array of records, with a to, from and weight, but I'm lost on the forest.
Lastly, I'm trying to figure out how I would know if an edge connects two trees, but again am not sure what the best way to do all of this is.
I can see where their algorithm description would leave you confused as how to start. It left me the same way.
I'd suggest reading over the later Example section instead. That makes it pretty clear how to proceed, and you can probably come up with the data structures you would need to do it just from that.
It looks like the basic idea is the following:
Take the graph, find the shortest edge that introduces at least one new vertex, and put it in your "spanning tree".
Repeat the step above until you have every vertex.
The "create a forest part" really means: implement the pseudocode from the page Disjoint-set data structure. If you can read C++, then I have a pretty straightforward implementation here. (That implementation works, I've used it to implement Kruskal's algo myself :)
I am given a network defined by nodes and links. I have to search all loops in the network. No coordinates will be given for the nodes.
Is there any existing algorithm or library that can do this. Or can you please give me some idea how I can approach this problem? I am programming in .NET.
I draw a diagram to illustrate what I need here
Try Distance vector Routing.
This algorithm finds the shortest path to all other nodes in a network from a node.
On the assumption that your edges are not directed and that there is a maximum of one edge between nodes then a http://en.wikipedia.org/wiki/Spanning_tree Depth-first spanning tree will cover all nodes and indicate where the cycles (which is what I think you mean by loops) will occur. We use this algorithm for finding "rings" in chemical structures. There are many implementations in many languages - here's a tutorial with an applet (http://oneweb.utc.edu/~Christopher-Mawata/petersen2/lesson20.htm)
The loops are called cycles, and this answer has a lot of informations for you.