Simple way to represent force/flow directed, weighted graph? - d3.js

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

Related

Which graph visualization library is suitable for static node and edges with text?

I would like to implement a graph in Java. I defined class Node and class Edge, and the graph is represented with a list of Node, and for each node, a list of edges starting from the node is defined as a field of the node. Now I would like to use some Java library to visualize the graph.
Currently I have tried graphviz-java. The result meets my requirement but the edges and nodes are not placed well. See the following graph below:
I created the graph with the API it provided like node(xxx), addLink() through traversing the node list of my graph instead of generating the raw dot file text.
The key code of creating the graph is as follows:
for (LiteralNode litNode : literalNodeArrayList)
{
MutableNode mutableNode = mutNode(litNode.getNodeElement());
for (Edge e : litNode.getEdgeList()) {
MutableNode endNode = mutNode(e.endNode.getNodeElement());
mutableNode.addLink(to(endNode.port(Compass.NORTH)).add(Arrow.VEE));
}
mutableNodes.add(mutableNode);
g.add(mutableNode);
}
It's basically simple traverse with loops. The node and edges are dynamically changed according to different cases.
I would like the node and edges adjust the location to make the graph display better. I have also tried using json file and d3js to visualize the graph but I found those libraries focus too much on displaying huge amount of nodes and their distributions, which I don't need. I like the structure graphviz provided with some stratification. So I would like to get some recommendation.
I mentioned the json file because if no Java library is available, I might as well try some javascript library like d3js and only use java as a backend tool to generate json file I need to input. So the recommendation is not limited to Java.
All graphviz attributes (https://graphviz.gitlab.io/_pages/doc/info/attrs.html) are supported.
Node distances are set with "ranksep" and "nodesep".
To force nodes on the same level, add a subgraph with "rank=same" and the nodes that should be aligned.
Here's a simple example:
Graph g = graph()
.graphAttr().with(attr("ranksep", .1), attr("nodesep", .1))
.with(graph().graphAttr().with(Rank.SAME).with(node("a"), node("c")))
.with(node("a").link("b"), node("b").link("c"));
The graph looks good to me. What do you mean by "I would like the node and edges adjust the location to make the graph display better."?

Graph implementation adjacency list vs set

After reading about how to implement a graph it seems I have basically two options:
Matrix
Adjacency list
In order to decide which implementation to use this post can be useful.
When an adjacency list is used to implement a graph the cost to know if there is an edge between two nodes may take linear time (for those nodes connected to all nodes).
That make me wonder: Why not to use a HashSet instead of a linked list in order to keep the neighbors of a node?
This will give us constant time to know if there is an edge between two nodes.
I'm sure must be a disadvantage using a Set instead of Linked list but I can't see it.
I think "list" is just a generic name. I've used a Set and it works perfectly well.
There is no specific reason to use a list instead of a set, here go through this link - Graph using set
Hope this helps!

Algorithm best placement of nodes in hierarchical graph

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?

What are good ways of organizing directed graph data?

Here's my situation. I have a graph that has different sets of data being added at different times. For example, set1 might have a few thousand nodes and then set2 comes in later and we apply business logic to create edges from set1 to set2(and disgard any Vertices from set1 that do not have edges to set2). Then at a later point, we get set3, set4, and so on and the same process applies between each set and its previous set.
Question, what's the best way to organize this? What I did before was name the nodes set1-xx, set2-xx,etc.. The problem I faced was when I was trying to run analytics between the current set and the previous set I would have to run a loop through the entire graph and look for all the nodes that started with 'setx'. It took a long time as the graph grew, so I thought of another solution which was to create a node called 'set1' and have it connected to all nodes for that particular set. I am testing it but I was wondering if there way a more efficient way or a build in way of handling data structures like this? Is there a way to somehow segment data like this?
I think a general solution would be application but if it helps I'm using neo4j(so any specific solution to that database would be good as well).
You have a very special type of a directed graph, called a layered graph.
The choice of the data structure depends primarily on the expected graph density (how many nodes from a previous set/layer are typically connected to a node in the current set/layer) and on the operations that you need to perform on it most of the time. It is definitely a good idea to have each layer directly represented by a numeric index (that is, the outermost structure will be an array of sets/layers), and presumably you can also use one array of vertices per layer. However, the list of edges per vertex (out only, or in and out sets of edges depending on whether you ever traverse the layers backward) may be any of the following:
Linked list of vertex identifiers; this is good if the graph is very sparse and edges are often added/removed.
Sorted array of vertex identifiers; this is good if the graph is quite sparse and immutable.
Array of booleans, indexed by vertex identifiers, determining whether a given vertex is or is not linked by an edge from the current vertex; this is good if the graph is dense.
The "vertex identifier" can take many forms. For example, it can be an index into the array of vertices on the next layer.
Your second solution is what I would do- create a setX node and connect all nodes belonging to that set to setX. That way your data is partitioned and it is easier to query.

Defining Invalid Paths on a Graph

We are working on a project that a regular directed graph is suitable for most cases. However on our graph we want to invalidate some paths. For example if our graph is:
A->B
A->D
B->C
D->C
Then A->B->C is a valid path but A->D->C is not. We could define invalid paths somewhere and do a validation check every time but this cause an important performance issue since our application highly depends on the graph.
So, is there a special data structure or algorithm for this type situation?
Thanks
You could store a mapping at each node of from:list(to) so you know that a path coming from A can not go to C because it isn't in the list of nodes it can go to from C. If you ahve a depth greater than 1, the tuple of nodes leading up to it can be a key instead of that one node. This is a lot like how eBGP works for internet routing.
On a different note, you will need to do a check no matter what if you decide to use a data structure like you're describing. Either that, or store multiple graphs for each situation.

Resources