Graph data representation in text form - data-structures

What would be the best way to visualize DAG's nodes and edges in a text form/file? The node can be referred by its name.

A good way that always worked fine for me is this:
In the first line put all node names (maybe also the total number of nodes).
After that, at each line put first the node name and then all nodes the current node has a directed edge.
Example:
A B C D
A B C
B A D
C D
D A
That works of course if all edge weights are equal (or none exist).
If you also want to use weights, one possible solution would be like this:
Example:
A B C D
A B(3) C(4)
etc

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.

Finding node-dispoint paths from A to B

This is the problem I got stumped on the exam:
Given a directed graph G = (V,E), and let A and B be partition of V(union of A and B is V and they are disjoint). I want to find a set of paths such that:
1) Every path contains exactly one node in A
2) Each node in A is the first node of corresponding path
3) For each path, the last node is in B
4) All paths are node-disjoint
Given A,B, and G=(B,E), I have to design a poly-time algorithm to decide whether such a set of paths exist.
The observation I made is that if I have a cut (A,B) and then look at the number of arcs going from A to B, then the number of arcs must be at least |A| for such a set of paths to exist. Also, every node in A must be adjacent to at least one of nodes in B. Also, once a path crosses the cut from A to B, you can't cross from B to A because of the constraint (1), but what's difficult is how to find node-disjoint paths among the vertices in B... How should I approach this?

Graph check if path exist between three nodes

The problem is following we have to find path from A to C that goes thru node B or following example graph A-G-F-B-L-C.
Now to implement from A to C is easy using BFS, but I don't know how to make sure that this path passes thru B ?
By 'path' you probably mean 'simple path' - a path with no repeating vertices.
First, ensure that A, B, and C are connected.
An A-...-B-..-C path exists iff:
There is no cut-vertex that splits A, B, and C into 3 different components
A is not a cut-vertex that splits B and C into different components
C is not a cut-vertex that splits B and A into different components
First run a bfs to reach the intermediate node, then run a bfs from that intermediate node to your desired goal node.

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.

Find the all chain of one to one node in graph

I go the un-directed graph G an my goal is to find all the possible chains longest than N of nodes in a one to one relation.
For example:
In the the next Graph the "chains" of length more than 2 of nodes in one to one relation are:
- d -> e -> f -> g
- c -> k -> l -> m
So what is the best approach or algorithm to solve this problem ?
If you want to find all paths so that each vertex in it has a degree <=2, then the simple approach may be as follows.
Remove all vertices with degree >2 from your graph. You are left with a graph with each vertex having a degree <=2. It is easy to prove that every connected component of such a graph is either a simple way, either a simple loop, and it is easy to distinguish them (for example, running a DFS from one node and seeing whether you ever return to it).
So, every component that is a path is a path you look for. Every component that is a loop is also a path you look for, or can be easily converted to such a path by removing an edge or a vertex, depending on whether you allow a loop as the needed path.

Resources