d3 Tree - drag and relocate nodes in d3 graphs is possible? - d3.js

Is it possible to use D3 Trees and draw graph. Then let the user to rearange the nodes without changing their relationships?

I used d3 Tree functions to have the location of nodes and used my current forced directed graph. This way I could keep my current functionalities, and also having more than one parent for nodes, and user could move the nodes around. However, once user moves the node, I do not let d3 to rearrange the node when nodes is expanded. I want the node stays in the location that user moved and wants.

Related

D3 v7 force graph: Can't drag nodes and links

When single node is dragged, other nodes and links don't follow along like in this graph example: https://observablehq.com/#d3/force-directed-graph
Here is the code: https://stackblitz.com/edit/js-xbmyr7?file=index.js
I have followed the 'template' solution typical of draggable graphs but am unable to understand why the rest of the graph does not move. All nodes and links are selected in the ticked function.

How many strongly connected components is there in this graph?

Consider the following graph. I can distinguish 4 strongly connected components, but they are 5.
Which one I missed? Also, can a node be shared in several components?
The 5 components are:
Top left node
Top right node
Bottom left node
Bottom right node
The rest of the nodes
What you thought of as components are not actually components, because they all can be expanded up to the 5th component from the list.
Notice that it is not possible to extend the listed components, because each of corner nodes is either unreachable from anywhere else (has only outgoing edges) or can't reach any other node (has only incoming edges). Therefore you can't add those corners to bigger component, and can't add anything to corner nodes to make them larger components.
By definition strongly connected components are largest possible (so that it's not possible to further extend them), but there's nothing about not having intersections with each other in definition. However it is easy to show that components defined that way can't have intersections.

Nodes on force graph links in d3.js

I am trying to create a force graph with nodes on the links between main nodes (O--o--O), similar to this visualization: http://www.nytimes.com/interactive/2013/02/20/movies/among-the-oscar-contenders-a-host-of-connections.html:
More specifically, I would like the links between nodes to start and stop at a main node (O), with a link node (o) at the midpoint. Is there a straightforward way to achieve this?
Generally: Create two classes or categories of nodes ("main", "midpoint") in your network data structure and conditionally set a node's radius and other style attributes based on class membership.

How to delete all related nodes in a directed graph using networkx?

I'm not sure exactly sure what the correct terminology is for my question so I'll just explain what I want to do. I have a directed graph and after I delete a node I want all independently related nodes to be removed as well.
Here's an example:
Say, I delete node '11', I want node '2' to be deleted as well(and in my own example, they'll be nodes under 2 that will now have to be deleted as well) because its not connected to the main graph anymore. Note, that node '9' or '10' should not be deleted because node '8' and '3' connect to them still.
I'm using the python library networkx. I searched the documentation but I'm not sure of the terminology so I'm not sure what this is called. If possible, I would want to use a function provided by the library than create my own recursion through the graph(as my graph is quite large).
Any help or suggestions on how to do this would be great.
Thanks!
I am assuming that the following are true:
The graph is acyclic. You mentioned this in your comment, but I'd like to make explicit that this is a starting assumption.
There is a known set of root nodes. We need to have some way of knowing what nodes are always considered reachable, and I assume that (somehow) this information is known.
The initial graph does not contain any superfluous nodes. That is, if the initial graph contains any nodes that should be deleted, they've already been deleted. This allows the algorithm to work by maintaining the invariant that every node should be there.
If this is the case, then given an initial setup, the only reason that a node is in the graph would be either
The node is in the root reachable set of nodes, or
The node has a parent that is in the root reachable set of nodes.
Consequently, any time you delete a node from the graph, the only nodes that might need to be deleted are that node's descendants. If the node that you remove is in the root set, you may need to prune a lot of the graph, and if the node that you remove is a descendant node with few of its own descendants, then you might need to do very little.
Given this setup, once a node is deleted, you would need to scan all of that node's children to see if any of them have no other parents that would keep them in the graph. Since we assume that the only nodes in the graph are nodes that need to be there, if the child of a deleted node has at least one other parent, then it should still be in the graph. Otherwise, that node needs to be removed. One way to do the deletion step, therefore, would be the following recursive algorithm:
For each of children of the node to delete:
If that node has exactly one parent: (it must be the node that we're about to delete)
Recursively remove that node from the graph.
Delete the specified node from the graph.
This is probably not a good algorithm to implement directly, though, since the recursion involved might get pretty deep if you have a large graph. Thus you might want to implement it using a worklist algorithm like this one:
Create a worklist W.
Add v, the node to delete, to W.
While W is not empty:
Remove the first entry from W; call it w.
For each of w's children:
If that child has just one parent, add it to W.
Remove w from the graph.
This ends up being worst-case O(m) time, where m is the number of edges in the graph, since in theory every edge would have to be scanned. However, it could be much faster, assuming that your graph has some redundancies in it.
Hope this helps!
Let me provide you with the python networkX code that solves your task:
import networkx as nx
import matplotlib.pyplot as plt#for the purpose of drawing the graphs
DG=nx.DiGraph()
DG.add_edges_from([(3,8),(3,10),(5,11),(7,11),(7,8),(11,2),(11,9),(11,10),(8,9)])
DG.remove_node(11)
connected_components method surprisingly doesn't work on the directed graphs, so we turn the graph to undirected, find out not connected nodes and then delete them from the directed graph
UG=DG.to_undirected()
not_connected_nodes=[]
for component in nx.connected_components(UG):
if len(component)==1:#if it's not connected, there's only one node inside
not_connected_nodes.append(component[0])
for node in not_connected_nodes:
DG.remove_node(node)#delete non-connected nodes
If you want to see the result, add to the script the following two lines:
nx.draw(DG)
plt.show()

Traversal of cyclic directed graph

I have a cyclic directed graph. Starting at the leaves, I wish to propagate data attached to each node downstream to all nodes that are reachable from that node. In particular, I need to keep pushing data around any cycles that are reached until the cycles stabilise.
I'm completely sure that this is a stock graph traversal problem. However, I'm having a fair bit of difficulty trying to find a suitable algorithm --- I think I'm missing a few crucial search keywords.
Before I attempt to write my own half-assed O(n^3) algorithm, can anyone point me at a proper solution? And what is this particular problem called?
Since the graph is cyclic (i.e. can contain cycles), I would first break it down into strongly connected components. A strongly connected component of a directed graph is a subgraph where each node is reachable from every other node in the same subgraph. This would yield a set of subgraphs. Notice that a strongly connected component of more than one node is effectively a cycle.
Now, in each component, any information in one node will eventually end up in every other node of the graph (since they are all reachable). Thus for each subgraph we can simply take all the data from all the nodes in it and make every node have the same set of data. No need to keep going through the cycles. Also, at the end of this step, all nodes in the same component contains exactly the same data.
The next step would be to collapse each strongly connected component into a single node. As the nodes within the same component all have the same data, and are therefore basically the same, this operation does not really change the graph. The newly created "super node" will inherit all the edges going out or coming into the component's nodes from nodes outside the component.
Since we have collapsed all strongly connected components, there will be no cycles in the resultant graph (why? because had there been a cycle formed by the resultant nodes, they would all have been placed in the same component in the first place). The resultant graph is now a Directed Acyclic Graph. There are no cycles, and a simple depth first traversal from all nodes with indegree=0 (i.e. nodes that have no incoming edges), propagating data from each node to its adjacent nodes (i.e. its "children"), should get the job done.

Resources