Drawing directed graph represented by an array of lists - data-structures

Morning,
This was one of my exam questions for data structures and algorithms.
I have to draw a graph on paper based on below array of lists:
The problem I have encountered, is for example:
0->1->4->3
What does it exactly mean? Does it mean 0 has 3 edges to 1 and 4 and 3, or is it just a collection of edges, for example, 0 has edge to 1, 1 has edge to 4, 4 has edge to 3, and so on.
I'd appreciate any help with this.

Basically what you are talking about is called "Linked representation" in graph-theory.
0->1->4->3 means that the vertex 0 is connected to 1 and 4 and 3 in a directed way(means arrow is directed from 0 to vertex 1,vertex 4 and vertex 3 respectively).That means that there are three directed edges coming out of vertex 0 to vertices 1,4 and 3 respectively (PLEASE MIND THAT THESE EDGES ARE DIRECTED AS PER YOUR SAYING)!
Similarly,1 simply alone emphasises that there is no directed edge from vertex 1 to any other vertices.
Similarly,2->1 means that there is an edge directed from vertex 2 to vertex 1.
And,3->0->4 means that vertex 3 is directing the edges towards both vertex 0 and vertex 4.
Lastly, 4->2->0 means that there is an edge directed from vertex 4 to both vertex 2 and vertex 0 each.

Related

Can we turn any undirected graph into a directed one such that the difference between in- and out-degree of each vertex is at most 1?

Given any undirected graph G, is there always a way to add directions to its edges such that the difference between in-degree and out-degree of each vertex is not greater than 1?
Consider for instance G is defined by vertices 1, 2, and 3, and undirected edges 1--2, 1--3, and 2--3. Then, the directed version 1 -> 2, 2 -> 3, and 3 -> 1 makes the difference between in- and out-degree is 0 for all vertices.
This question is about orientations of undirected graphs.
An orientation is Eulerian if the difference between in- and out-degree of each vertex is 0. It turns out that any graph with all degrees even has an Eulerian orientation. One just has to compute an Eulerian circuit, which always exists in this case, and choose directions according to this circuit.
Using this, the same question was solved here with a clever trick: just add a new vertex and link it to all vertices of odd degree, then compute an Eulerian orientation, and remove the additional vertex!

Computing level of nodes in a graph

I want to compute the level of each node in a directed graph. I'm currently applying a depth-first search algorithm on vertices that have no incoming edges. Considering the graph below, for instance:
The expected result is:
Vertex | Level
1 | 0
2 | 1
3 | 2
4 | 1
5 | 3
6 | 4
In this particular case, if we start by applying DFS on 4, then all results for vertices 4, 3, 5 and 6 are going to be wrong, since 1 has level 0. I've tried to always consider the greatest result for each one of the nodes, so in this case the results for 3, 5, and 6 are replaced when applying DFS on 1. It works, but I can't find a way to correctly compute the level of vertex 4.
I'm working only with directed acyclic graphs.
I'm not including any code here because it is a pretty straightforward DFS implementation and I'm not struggling implementation-wise.
Any hint would be much appreciated.
You can compute the levels starting from each vertex without having an incoming edge. Then you can store the maximum value for each vertex until the end. For eg :- Vertex 3 will have values 1 and 2 when traversed from starting points vertex 1 and vertex 4 respectively. At last, you can update the vertices not having the incoming edge(number on child -1). If there's a situation where there multiple children of such a vertex, then you might want to select the child with maximum number on it for replacement and then run the algorithm from that vertex again to see if changes the numbers assigned to any of the other children.

Finding corresponding graph from given shortest paths

Given n by n points and the distance d between these points, I need to find the corresponding undirected weighted graph that would result in these distances. I tried using Prim's algorithm to find the MST, however this set is of size n-1 and does not include the n needed edges. E.g. given n by n distances
0 3 5
3 0 4
5 4 0
I need to find the corresponding edges:
1 - 2 = 3
1 - 3 = 5
2 - 3 = 4
Which results in the graph:
3
1 --------- 2
\ /
\5 /4
\ /
\ /
3
However Prim's would return only the first 2 edges since a MST doesn't contain any cycles.
One graph that would result in these distances is the graph that has an edge from every node to every other node and the length of that edge is the distance according to the matrix. (I'm not sure what you mean by unweighted directed because the example you give appears to be undirected and I'm not sure what the difference is between weights and lengths here).
Another option would be to consider the distances in increasing order, as you have done with Prim's algorithm, and, as well as checking to see if the edge is required to connect its two ends, check to see if the minimum distance between those ends in the graph reconstructed so far is the same as the distance in the matrix. If it is not, add the edge even if the ends are connected in the graph so far.

Give an efficient greedy algorithm that finds an optimal vertex cover for a tree in linear time

I'm trying to work on this problem...
Below mentioned is one algorithm..i figured out..
Input a graph
select a vertex with highest degree of matching with all the other nodes.
Remove the edges that are incident on this node.
Add the selected vertex and its edge to a set X.
Return X
Where X returns the minimum set of vertices that are required for a vertex cover.Is this way correct...?
Thanks
To select a vertex with highest degree can't guarantee to give the best solution. For example,
you have a tree with 7 vertices, edges are listed as follows:
1 2 // (1,2) is connected.
1 3
1 4
2 5
3 6
4 7
The minimum vertex cover is {2,3,4}, however, based on you greedy approach, you will choose {1} first, then you will choose at least 3 vertices to covered the left 3 edges.
Indeed, there is a greedy algorithm to solve the vertex cover problem for a tree, that is you find a leaf at each step (since the input is a tree, you can always find such leaf unless there is no edge left), then select the neighbor of the leaf to the vertex cover set X. Return X as the minimum vertex cover when the graph is empty. The complexity is O(E) when E = V-1 so that we can say it is a linear solution.

What are the articulation points of this directed graph?

5 nodes in this directed graph.
Edges:
1 -> 2
2 -> 3
2 -> 4
4 -> 5
(Graphical image : http://i.imgur.com/hafBv.jpg )
Am I correct in thinking that the articulation points are node 2 and 4 ?
(If you remove node 2 or node 4, the graph becomes disconnected)
But the definition I've seen everywhere says something similar to:
a node u is an articulation point, if for every child v of u, there is no back edge from v to a node higher in the DFS tree than u.
How does this work for a directed graph? For example, Node 3 does not have a back edge to a node higher in the DFS tree than 2. Does this classify Node 3 as an articulation point? But its removal does not cause the graph to be broken into 2 or more pieces (That is my layman definition of an articulation node).
Disclaimer: My memory is vague.
Directed graphs have three kinds of connectedness.
"Strongly connected" if there is a path from every vertex to every other vertex,
"Connected" if there is a path between any two nodes, but not in both directions.
"Weakly connected" if the graph is only connected if the arcs are replaced with undirected arcs.
eg
1->2 , 2->3 , 3->1
Strongly connected, you can get from every node to every other node
1->2 , 2->3
You can't get from 3 to 1 but you can from 1 to 3 so it's connected
1->2 , 3->2
There is no way to get from 1 to 3 or from 3 to 1, so it's only weakly connected.
What nodes are articulation points depends on what kind of connectedness you are considering.
Your graph is only weakly connected since there's no way to get from 3 to 4 or from 4 to 3.
Which would mean that the only way it makes sense to talk about articulation points is if you treat the arcs as undirected. In which case 2 and 4 would be the articulation points, as you said.
Articulation point should always have children and parent. This is something that is missing from your definition and makes nodes 1 and 3 articulation points whilst they are not.
Also note that in your reasoning for node 3 you consider the node itself not its children. If you apply the definition carefully you will see that you should consider the children instead. In your case - empty set and, with the extended by me definition, 3 is no longer articulation point.
Node 3 does not have a back edge to a node higher in the DFS tree than 2
Node 3 doesnt have children, so it cannot be an articulation point (from def).
If we put this definition in the context of a directed tree, then the articular points are all the points except the root and the leaf node
but when we follow the method we used to solve the undirected graph we get the respected (num,low) values for nodes are node-1(1,1)
2 (2,2) ,node 3 (3,3), node 4(4,4).node 5(5,5).
so following the def of articulation point and finding the node with 2 children
and satisfying the rule (low(child)>=num(node))
we get only the node 2 so that is the articulation point .
but the theory can be applied because it is still a connected graph i.e where every node is reachable but when we are finding we had to take care of tree and back edges and the rest is same as that of undirected graph.
The definition of articulation is not as clear as in case of undirected graphs. However if we choose two particular nodes: source and sink and call any point that must occur on the path from source to sink articulation point, the definition is clear.
I call them unavoidable points and have got simple solution for finding them on my GitHub

Resources