I have an undirected fully connected graph. I have a start node and there is no end node. My nodes and edges don't have weights. Any path on the graph has some value. I can only know this value after going in this entire path. Path length is limited, but it can be very large.
For example:
It might be that:
The value of the path node1 -> node2 is 1
The value of the path node1 -> node3 is 1
The value of the path node1 -> node2 -> node3 is 2
The value of the path node1 -> node3 -> node2 is 20
My goal is to find some path with small value without traversing all possible paths. The smallest the value, the better.
I cannot use any familiar shortest-path algorithm like Dijkstra or Floyd-Warshall since i don't have an end node nor known weights in advance.
I was thinking on the following simple algorithm:
Starting from the start node, go to some other node. Evaluate the path.
From the next node, go to some other node. Evaluate the path. If the path's value is larger than before, go back and try another node.
Is there a known algorithm for that problem?
Thanks
Related
Let's say that we use Dijkstra algorithm and now we know the shortest distance between source node and every node from the graph.
What should I do to know which nodes was visited for every distance?
Whenever you find a shorter path to a node, and therefore decrease its distance, you should also record the node's predecessor. That's the node who's edge list you are enumerating when you discover the new distance.
When you're done, you can then backtrack from any node through the chain of predecessors to find all the nodes on the shortest path to it.
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
Can Breadth First Search be used on Directed Acyclic Graph?
For example, you start with the root node (say it has 3 connected nodes, edges all pointing towards them from the root), following BFS, you visit the first connected node from the root following the directed edge, and you got to come back to the root node and visit the second connected node if it were an undirected graph, but you can't in the case of directed graph, so I assume BFS cannot be used on Directed Acyclic Graph?
Also,a line of nodes as such 1 -> 2 -> 3 -> 4 can be considered as a Directed Acyclic Graph, correct?
Thank
Short answer : Yes
Here is a video example. (This is no Adv, I don't know the guy)
Here is some written courses and examples.
To respond more precisely to your question, if there is no edge from a given node pointing to a root then you don't "have to" go back to the root, like others said in the comments.
The fact that the graph has cycles or not is not important because in BFS algorithms a node is supposed to be marked when you visit it. The mark is then used to not enqueue the node a second time (i.e to visit it only once), so the cycle is some how broken.
Just check the pseudo-code on Wikipedia.
An yes the "line of node" you mentioned is a DAG, but a very special one.
P.S : Sorry to dig up this question but I asked it to myself, searched the internet, saw it, keep searching and find some answers, so I thought it could help somebody else in the future.
1 -> 2 -> 3 -> 4
is a DAG
BFS means breath fast search. if u start bfs from a node u, every node which are reachable from u will be found but those nodes that are not reachable from u are not found.
example G (V,E ) a graph
v={1,2,3,4}
E={(1,2),(1,3),(4,1)}
if u run bfs from node 1 ,node 2 and 3 will be discovered
but 4 will be undiscovered
but if u run bfs from 4 every node will be discovered
So if u know the topological sorting of the DAG u can run bfs from the nodes in the topological order every node will be discovered and their level will be calculated correctly.
Suppose there are 3 target nodes in a graph.
A vertex-disjoint path means there is not any same node except the end nodes during the path.
For any one single node, say node i, how to find all vertex-disjoint paths from node i to the three target nodes?
You can solve this problem by reducing it to a max-flow problem in an appropriately-constructed graph. The idea is as follows:
Split each node v in the graph into to nodes: vin and vout.
For each node v, add an edge of capacity one from vin to vout.
Replace each other edge (u, v) in the graph with an edge from uout to vin of capacity 1.
Add in a new dedicated destination node t.
For each of the target nodes v, add an edge from vin to t with capacity 1.
Find a max-flow from sout to t. The value of the flow is the number of node-disjoint paths.
The idea behind this construction is as follows. Any flow path from the start node s to the destination node t must have capacity one, since all edges have capacity one. Since all capacities are integral, there exists an integral max-flow. No two flow paths can pass through the same intermediary node, because in passing through a node in the graph the flow path must cross the edge from vin to vout, and the capacity here has been restricted to one. Additionally, this flow path must arrive at t by ending at one of the three special nodes you've identified, then following the edge from that node to t. Thus each flow path represents a node-disjoint path from the source node s to one of the three destination nodes. Accordingly, computing a max-flow here corresponds to finding the maximum number of node-disjoint paths you can take from s to any of the three destinations.
Hope this helps!
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