Find minimum cost among nodes - data-structures

Suppose I have three destinations A, B, and C and associate travel cost between A to B, B to A, A to C, C to A, B to C and C to B and all are different. I have to search the particular location among A, B, and C where the other two will come and the cost will minimize. e.g., A and B can come to C location. And How it can be optimized for N location?

I would claim that this problem is in O(|V|^{2}) and you would therefore have to check the travel cost for each node. Pick a node, check the travel cost of the other n-1 nodes to this node and do this for every node while maintaining the one with the least travel cost.

Related

Find group of friends with no mutual spies and maximum value by dynamic programming

In a group of friends, each except one friend spies exactly one other friend. Every friend has some valuables, which is a positive integer. Find a group of friends with the biggest sum of valuables such that no friend spies any other friend within this group.
Example: We have the following graph for one of the possible test cases. The value above each vertex is the positive number of valuables owned by them.
The best possible group is [A,F,D,J,H] = 92 value
Looks like we can achieve the solution by ignoring the traversal through the graph and calculating the combinations of all possible groups.
Unfortunately not able to think of a dynamic programming approach or how to get started.
Give the constraints on your graph, you will always have:
one disjoint connected sub-graph that is a tree consisting of zero-or-more simple paths all branching off the friend who is not spying on anyone; and
zero-or-more disjoint connected sub-graphs where each sub-graph contains exactly one cycle and zero-or-more simple paths branching off the cycle.
To get the best sum of valuables:
Within each branching path, there can be at most two non-selected vertices between each selected vertex. (If there were three non-selected vertices then you would get a better result if the middle vertex of those three were selected; i.e. (A)->B->C->D->(E) where () is a selected vertex will always give a better result as (A)->B->(C)->D->(E)).
Within each cycle, unless blocked by a selected adjacent vertex in a branching path, there can be at most two non-selected vertices between each selected vertex. (for similar reasons to branching paths).
If you have the connected sub-graph (similar to the bottom of your example but E spies I, rather than spying nothing):
-----------
V |
C -> D -> E -> I -> J <- H
Then you can either start with the cycle and work outwards to the branching paths or from the branching paths inwards. If you consider the cycle-outwards then:
if E is selected then D, I and J cannot be and given that the minimum separation of 1 is achieved, then C and H must be selected.
if I is selected then E and J cannot be selected and H and either C or D can be selected.
if J is selected then E, I and H cannot be selected and either C or D can be selected.
if none of the vertices on the cycle are selected then H and either C or D can be selected (which, in this case, is always a strictly worse option that selecting J as well, since it does not block selection from the branching paths).
This gives possible solutions for that connected sub-graph of:
C, E, H
C, I, H
D, I, H
C, J
D, J
You can perform a similar operation for each disjoint subgraph and, for the one disjoint sub-graph containing the friend who is not spying on anyone then that friend can either be selected or not selected and each branching simple path can be evaluated independently choosing to either skip one-or-two vertices between selected vertices and finding the optimum total.
Find the solution with the highest value for the sub-graph and move on to the next sub-graph.

How to understand the random contraction algorithm?

I'm studying random contraction algorithm and could not understand how it works.
In the course it says that a graph G=(V,E) with n vertices, m edges. a minumum cut(A,B), are there are many vertices either on A's or B's sides. k=number of edges crossing (A,B).(call these edges F)
then it make the conclusion that an edge of F is contracted at some point, algorithm will not output(A,B).
Why is this? suppos there is a vertice C in A's side and a vertice D in B's side, so the edge(C,D) would belong to F, but even if C and D are to contract, we still have A and B, the algorithm can still return (A,B) right?
If G is a graph and (A, B) is a cut in the graph, then A and B are sets of nodes, not individual nodes in G. That is, there is no node in G called A or B. Rather, the nodes in G can be split apart into two groups, one of which we’ll call A and one of which we’ll call B. As a result, you can’t have the situation you’ve described.
Here’s another way to think about this. Suppose (A, B) is a minimum cut. You can then imagine that every node in the graph is painted red if it’s in the A side and blue if it’s in the B side. The randomized contraction algorithm will then find the cut (A, B) assuming that no red node is merged with a blue node. If nodes of opposite colors are merged, then the resulting cut won’t consist of all the red nodes on one side and all the blue nodes on the other.

Smallest subet of nodes that reach all nodes in DAG

Say we have a DAG comprised of a list of nodes A, B, C, D, and E.
Each node has a list of reachable nodes - for example:
A --> B, C
A --> B
D --> E
In this case, we would have to visit nodes A and D to comprehensively visit all nodes in the graph. What is the best algorithm to approach this problem in general?
Here is a linear approach:
For every node count it`s in-degree (number of edges pointing to it)
Because graph is a DAG (no cycles) we can just take all nodes with in-degree of 0 as our starting sub-set
Time Complexity(N + M) - linear in graph size
Here is an approach.
Let's say that node A is parent of node B if there is an arc from A to B.
And node C is the most-parent of node B, if it has no parent and there is a path from C to B.
Mark every node as not visited.
For every node in DAG you define it's parent.
for every node A that is not visited
Find A's most-parent MP
Mark all nodes that are reachable from MP as visited
Put MP to array
After this you'll get smallest subset of nodes that reach all nodes in DAG in array
Time compexity of algo is O(n^2)

Pathfinding algorithm constrained to one-way roads

Say I have 5 "towns": A, B, C, D and E.
Now I want to figure out a path between these towns, but I am constrained to roads, for example A only has roads to B, C and E and B only has a road to C etc.
I can't visit a town twice and the roads are only one-way. I want to "visit" all the towns. How would I programmatically solve this?
I've already tried starting at A, then seeing what roads there are and checking each option, although all my tries so far at implementing this have failed.

How can I cast this into a graph algorithm?

Given a set of nodes A and a set of nodes B, where each node in A are connected to only one node in B. Now I want to select a minimum set of nodes in A that is connected to every nodes in B, while every selected node cannot be connected to each other.
I believe there is a certain way of solving this problem maybe with matching, independent set or whatever.
Although I haven't been able to prove that my algorithm is correct, I'm still putting it here.
If the nodes in A didn't have edges between them, then this would be a bipartite graph matching problem. Right now this has become a bipartite graph matching problem with the constraint the if some node in A is selected, it may disqualify some other nodes in A.
For ease of visualisation, imagine the set of A on one sides and B on the other side, similar to the visuals drawn for Bipartite matching. Now, add another set of nodes C between A and B. The condition for adding a node in C are as follows:
If there is a node in A (say a) with no edges in A, create a new node in C (say c), join a to c, and join all the corresponding nodes of a in B to c.
If there are two nodes in A (a1 and a2) which share an edge, create a new node in C (c) and create edges from a1 to c, a2 to c, and from c to all corresponding nodes in B.
Now we can use the Max-Flow algorithm to get the desired output. We should set the node capacities of each node in C to be 1 (the way to achieve this is by creating another intermediate layer D which is a copy of C, with edge capacities equal to 1 for edges between C and D)
Any help in making the proof rigorous or any counter examples would be helpful.

Resources