find the source of reconvergent fanouts in a DAG - algorithm

if we have graph like:
the rencovergent nodes are nodes 10 and 11.
by reconvergent nodes i mean the parents of that node come from a common source node.
in the given graph the parents of node 10 are nodes 7, 8 and they both have node 6 as source node(source of reconvergence)
and for node 11 the source node would be node 4.
my question is if we know which nodes are reconvergent nodes in the DAG how could we find the source nodes of their parents?
for my graph structure i use adjacency list representation available at geekforgeeks
i've tried this:
if(Node is reconvergent_node)
for each parents of Node
Do DFS(0, parents[i]) // depth first search from primary source till we reach the parent node
if the paths from 0 to parents[i] (which extracted from DFS) have common node mark that node as source node
is this algorithm correct? although i have not get the right results yet...
if it's correct i thinks it's limited to reconvergent nodes with max of two parents.
what if we have reconvergent_node which has five parents such that parents[1,2] have common source and parents[3,4,5] have different source node?
what should i do in this case?
any suggestion are welcome.
thank you

Related

how to merge tree nodes into one node

Hopefully, my question is not duplicated.
I would like to know if there exists such algorithm which merges some nodes in a tree to a new tree so the node in the new tree consists of some nodes in the old tree?
In order to explain my idea, I drew a graph to explain the question.
Input: A original tree.
Output: A new tree. There are following conditions with which the new tree must be satisfied:
The number of nodes in the new tree should be a fixed number k.
Each node in the new tree must consists of nodes in the original tree. For example, the node A in the second graph contains node 1,3, and 4 of the first graph. Node D in the secod graph contains nodes 9,12, and 13 in the first graph.
if one node of the original tree is contained in a node of the new tree, it cannot appear in another node of the new tree.
The nodes in the new tree are not necessarily have to be a subtree of the original tree. For example, node C in the second graph contains 6,7,and 10 of the first graph, It is not a subtree of the original graph. Because both node 6 and node 7 in the original graph connect to the nodes in the dotted area of A in the original tree, So they could be grouped in the node C of the second graph.
Currently, I just want the original tree can be converted to a new tree that has a K number of nodes and meets above conditions. For a given tree, there are many solutions. For example, graph 3 and graph 4 illustrate another solution for the original tree. It also has 4 nodes.
You need an addition condition or desired property of your output, otherwise it is quite trivial:
Starting from leaf nodes, copy K - 1 nodes to the output: B = {11}, C = {12}, D = {13}
Group all other nodes into a single K-th node: A = {1,2,3,4,5,6,7,8,9,10}

Recommmend a proper data structure

I have an algorithm problem that needs binary tree structure similar to a binary tree. but the difference is that it may have nodes apart from the original tree independently.
And each node has three types. The first type is to point out starting node and only one exists. The second type is to point out connecting node and of course, and the last type is to point out a leaf node. Each edge has a cost to traverse to its bottom node.
Which data structure is good for me to cost to reach each node?
UPDATE
OK, I questioned this with data-structure tag so that I want to avoid to explain what the problem is. But inevitably, I explain about the problem because of lack of my explaination and my poor English.
I have nodes lists and edges with costs. There is a starting node(root node), nodes where will be located in the middle of a tree and leaf nodes are the destination for my program to traverse starting from a root node. But some of the leaf nodes may be ignored depending on the value in it. It is not important anyway. I have to calculate all leaf nodes' cost to reach its node from the root node and get the maximum value for them. Now, The problem is to adjust the cost value in edges for all other leaf nodes to have the same total cost with the maximum cost. But the sum of the adjust values has to be the minumum.

Proper traversal of undirected graph using depth first search?

I've got an undirected graph that I need to traverse using depth first search.
The excel chart below shows each node has been marked after traversal in the marked column, and the edgeTo column shows which node brought us to that node. For example, we got to node 1 from node 5, we got to node 2 from node 7, etc.
My question is for node 6 and 8, since they are separated from the main graph, how do I properly traverse it? My guess is that I start at 6 and go to 8, but since 6 will already have been visited at that point, I do not go back to 6 from 8. Hence row 6 is left blank in the edgeTo column.
Am I correct? Is my chart correct?
Depth first search is basically used to find a path between two nodes in a graph. The graph of your example is disconnected, i.e. there exist two nodes in your graph such that no path in your graph has those nodes as endpoints.
6 and 8 are obviously nodes that belong to a different subgraph and therefore you can't find a path between 0 and 8 and the DFS will return IMPOSSIBLE or No path found. Apart from that your chart is correct.

Create a DAG given leaf nodes

Assume we have a DAG with a single leaf Node leafN. Each node is an object containing list of parent nodes. Node class can be something like class Node(Object, List[Node].
How to construct a DAG from leafN?
For e.g., given leaf node leafD with two parents, leafD(Object#123, List(leafC(leafA(null)), leafB(leafA(null)))) it's DAG will be:
leafA
/ \
leafB leafC
\ /
leafD
i.e., leafA(leafB(leafD(null)), leafC(leafD(null))) (I am ignoring the objects from every node for clarity.)
In short we have a leaf node with parent pointers which themselves have parent pointers and finally, after applying an algorithm, we want a root node with pointers to children nodes which further have pointers to children nodes.
Code ain't required, algorithm or links to any will suffice.
This is pretty simple, once you realise that notionally only the direction of the edges in the DAG has changed.
Instead of the Parent ---> Child node relationship, you have a Child ---> Parent relationship.
As such, all you need to do is construct a DAG from the given relationships, assuming that the Child node is actually the parent, and the parent nodes are childs.
So you end up with
leafD
↙ ↘
leafB leafC
↘ ↙
leafA
Now, all you need to do is reverse the edges in the DAG.
This is pretty simple, One way to do it would be to walk through the DAG, and get all the edge relationships, and then reverse them one by one.
Another would be to do this recursively using level order traversal.
Check this question for more solutions around how to reverse a DAG.
All you need to do is to put your nodes into some data structure that allows you to find a node easily. If your integer values in your node are sequential (or close to it) and start at a low number then you can use an array and just put the node with integer i in position i of the array (This makes including the integer in the node somewhat redundant).
This is all explained well on this site (but note that in their implementation the list is of the edges to, not the edges from) http://algs4.cs.princeton.edu/42directed/

how to find the number of roads from node A to node B in a directed graph?

I'm given a graph which can have more than one arch between 2 nodes.
Example :
4 nodes
1->2
2->3
3->4
3->4
1->4
Which is the optim way to find the number of the roads from node A to node B ?
The answer for the example is 3 : 1->2->3->4 ; 1->2->3->4 and 1->4
Limit for nodes and archs are 1 000 000
I'm only thinking at a brute force algorithm.
Any other ideas?
Edit:
graph is acyclic
If the graph is cyclic then the result is +infinity, since you can run in a cycle as often as you like.
One idea that might work on an acyclic directed graph:
Order all nodes in a way so that for any two connected nodes the parent node comes always before the child node
Assign 0 to all nodes
Assign 1 to the start node
Iterate over the nodes in that order starting with the start node and do
If the node is the end node you're done
Foreach connection starting on this node(i.e. it is the parent) do
Add the value of the current node to the child node
The number assigned to the end node is your desired result
Ordering in the nodes isn't trivial though. But I'm sure you can find an algorithm for that, since it's a common problem when using DAGs.
There is no optimal way. This Problem is a NP Complete problem. http://en.wikipedia.org/wiki/Feedback_vertex_set
You can only find good solutions

Resources