Transform a simple directed graph to a simple undirected graph - algorithm

How would I convert a simple directed graph to a simple undirected one?
Is this possible?

Assuming: (from here)
in a simple digraph loops are disallowed. (A loop is an arc that pairs a vertex to itself.)
and: (from here)
a simple [undirected] graph is an undirected graph that has no loops (edges connected at both ends to the same vertex) and no more than one edge between any two different vertices.
I'm assuming the edges are unweighted, otherwise this can't be done without specifying how this needs to be done.
AM = Adjacency matrix
AL = Adjacency list
Remove the direction of all edges.
AM: For each edge A->B, row A, column B would already be set. Also set row B, column A.
AL: For each edge A->B, the edge already appears in A's AL, also add the edge to B's AL.
Go through all edges, removing all edges for which we already found an edge between the two vertices that are the end-points of that edge (so if A-B has already been processed, if we find another A-B (or equivalently B-A), we must remove that edge). This needs to be done since there can be multiple edges between vertices in a simple directed graph, but this can't happen in a simple undirected graph.
AM: Nothing really needs to be done since an AM forces there to only be a single edge between vertices, so we'd just be overwriting the edges.
AL: For each vertex A, for each edge A-B or B-A in its adjacency list, remove all other edges A-B or B-A in the list.

Source and details...
For each directed edge e=(x,y), add new vertices ve1,…,ve5 and replace e with the edges xve1, ve1ve2, ve1ve3, ve3ve4, ve4ve5, ve3y.
To decode, every leaf (degree-1 vertex) whose neighbour has degree 2 must be ve5 for some edge e=(x,y); its neighbour is ve4 and the other neighbour of ve4 is ve3. ve3 has a unique neighbour that has both degree 3 and is adjacent to a leaf: the neighbour is ve1 and its leaf is ve2 (if ve1 has two leaf neighbours, pick one arbitrarily to be ve2). The other neighbour of ve1 is x and the other neighbour of ve3 is y. Restore the directed edge (x,y) and delete the vertices ve1,…,ve5.

G=(V,E) is a directed graph. (a,b) \in E is a directed edge.
G' = (V, E') is an equivalent undirected graph where (a,b) is transformed as (a,b) and (b,a).
Thus, edge directed edge becomes two edge (one in each direction).

Related

Directed Weighted Graph with required vertices

Given: A directed, weighted graph G=(V, E), some of the vertices are red, some blue, and the rest white, weight Ti which is the maximum weight allowed to go from any vertex red vertex to any blue vertex.
Problem: Create an algorithm that finds a path from source node S, to target node T with least weight and which at some point goes from a red vertex to a blue vertex in at most Ti weight before reaching vertex T. The algorithm should have time complexity O(n^3)
Comments: I'm not sure how to get started on this, I figure it's some variation of Dijkstra's algorithm and I've seen some people talking about making copies of the graphs and connecting the copies but beyond that, I'm not sure what the setup of this algorithm would look like. Any help would be appreciated.
Indeed, you can use the copy-strategy as follows:
Copy the graph G to G' and to G". Label all vertices in G' as in G, but with the apostrophe. So there is an S' and a T' in G'. Similarly, S" and T" belong to G".
Let S be the start vertex, and T" be the target. As it stands now, G, G' and G" are disconnected. But add the following zero-weight, directed edges:
Edges from each red vertex r in G, to its mirror r' in G'.
Edges from each blue vertex b' in G', to its mirror b" in G".
So there are no paths from G" to G', nor from G' to G, nor from G" to G.
Now start a Dijkstra algorithm in S, with an additional data attribute for each entry in the priority queue: the weight that a path accumulated by edges in G' only. Let's call this w'. This w' plays no role in the priority of the queue. The total weight of a path determines the priority as is standard in Dijkstra's algorithm. The algorithm should not expand paths whereby w' would exceed Ti. Besides that specific limitation, all the rest of the algorithm can remain as in the standard Dijkstra algorithm.

If there is a weight 3 from A->B and a weight 1 from B->A, does this mean that there are automatically 2 edges between A and B?

Regarding weighted graphs:
If there is a weight 3 from A->B and a weight 1 from B->A, does this mean that there are 2 edges between A and B? I'm 95% sure that the answer is yes, but I'd like to be certain. I'm trying to see if directed graphs with weight schemes like this are automatically multigraphs.
Thanks for your valuable input!
Marcus
As noted here
A graph with one edge from A to B and one edge from B to A, will simply be called a directed graph, but if we would have a directed graph with multiple edges from A to B, it will be called as multigraph or specifically, in our case, multidigraph.
Like, in the diagram(taken from same link).
In the first graph, there is no case when we have two edges from same origin and destination, so its a normal directed graph. But in case of second graph, there are two edges from e to d and from b to c. Thus, making it a multidigraph.
There are 2 edges between A and B, but they are not the same edge in a directed graph. There is the edge from A to B (A->B) and the edge from B to A (B->A). This does not make the graph a multigraph because those are two distinct edges.
In an undirected multigraph the source and destination nodes do not matter. The edges would no longer be (A->B) and (B->A) . They would simply each become (A, B) indicating an edge between A and B exists. If more than one edge connects any two nodes in an undirected graph, the graph becomes a multigraph.
A Direcited Multigraph must have multiple edges with the same source and destination. If there were multiple edges from A to B, then it would be a directed multigraph. However, you list two distinct directed edges. (A->B) and (B->A). These edges are not identical and so the graph you describe is not a multigraph.

Acyclic across cuts in a graph

Given we have a graph G = (V,E) and a subset F with only V in it, for each connected component S of F, add the minimum weight edge in the cut (S, V \ S) to F.
Why is it that every time the minimum weight edge is added to F, F remains acyclic?
To create cycle, you have to create edge which connects vertices which are already connected.
If you add edge between vertices that are not connected, you don't create new cycle. You connect two unconnected components. But graph remains acyclic.
To get better understanding how it works, you could represent connected component of graph as single vertex. And then, when your add edge between unconnected components, your just merge vertices.
By the way, this question is not related to weights (and MST algorithm). It's still valid without weights.

What is the difference between a directed and undirected graph

What is the difference between these fundamental types?
In drawings I see that the directed has arrows, but what exactly is meant by these arrows in the directed graph and the lack thereof in the undirected graph?
It means exactly what it sounds like. In a directed graph, direction matters. i.e. edge 2->3 means that edge is directed. There is only an edge from 2 to 3 and no edge from 3 to 2. Therefore you can go from vertex 2 to vertex 3 but not from 3 to 2.
In undirected graph 2-3 means the edge has no direction, i.e. 2-3 means you can go both from 2 to 3 and 3 to 2.
Note that in the representation of your graph, if you are using an adjacency matrix, directed 2->3 means adj[2][3]=true but adj[3][2]=false. In undirected it means adj[2][3]=adj[3][2]=true.
The difference is the same as between one directional and bidirectional streets - in directed graph, the direction matters and you can't use the edge in the other direction. An undirected graph can be simulated using a directed graph by using pairs of edges in both directions.
All of the answers so far are right.
Typically, a graph is depicted in diagrammatic form as a set of dots for the vertices, joined by lines or curves for the edges. The edges may be directed (asymmetric) or undirected (symmetric).
Imagine if the vertices represent people at a party. If there is an edge between the two people if they shake hands, then this is an undirected graph, because if person A shook hands with person B, then person B also shook hands with person A.
On the other hand, if the vertices represent people at a party, and there is an edge from person A to person B when person A knows of person B, then this graph is directed, because knowing of someone is not necessarily a symmetric relation.
Imagine graphs as a set of pumps( the circles) which can send liquid to others when are connected.In directed graphs the arrow show from where it comes and where the liquid (data) goes and in undirected graph it goes from both ways.Also a directed graph can have multiple arrows between two vertices(the pumps ) depending always on the graph.
A graph in which every edge is directed is called a Directed graph, and a graph in which every edge is undirected is called undirected graph.
In a directed graph, there is direction but in un-directed graph there is no direction.
Think in in terms of city network , where City A-> City B represents one way from City A to City B which means you can travel from City A to City B (may be through this path). It's an example of directed graph City c - City D represents the un-directed graph where you can travel in any direction
A directed graph is a graph in which edges have orientation (given by the arrowhead). This means that an edge (u, v) is not identical to edge (v, u).
An example could be nodes representing people and edges as a gift from one person to another.
An undirected graph is a graph in which edges don't have orientation (no arrowhead). This means that an edge (u, v) is identical to edge (v, u).
An example for this type of graph could be nodes representing cities and edges representing roads between cities.

Forward Edge in an Undirected Graph

CLRS - Chapter 22
Theorem 22.10
In a depth-first search of an undirected graph G, every edge of G is
either a tree edge or a back edge.
Proof Let (u,v) be an arbitrary edge of G, and suppose without loss of
generality that u.d < v.d. Then the search must discover and finish v
before it finishes u (while u is gray), since v is on u’s adjacency
list. If the first time that the search explores edge (u,v), it is in
the direction from u to v, then v is undiscovered (white) until that
time, for otherwise the search would have explored this edge already
in the direction from v to u. Thus, (u.v) becomes a tree edge. If the
search explores (u,v) first in the direction from v to u, then (u,v)
is a back edge, since u is still gray at the time the edge is first
explored.
I most certainly understand the proof; but not quite convinced with the idea of forward edges.
In the above image, there is a forward edge from the first vertex to the third vertex (first row). The first vertex is the source.
As I understand DFS(S) would include a forward vertex 1 -> 3. (I am obviously wrong, but I need somebody to set me straight!)
It looks like you didn't include the definition of "forward edge," so I'll start with the definition I learned.
Assuming u.d < v.d, DFS labels the edge (u,v) a forward edge if
when crossing the edge from u to v, v has already been marked as visited.
Because of that though, I claim that you cannot have forward edges in an undirected graph.
Assume for the sake of contradiction that it was possible. Therefore, the destination node is already marked as visited. Thus, DFS has already gone there and crossed all of the adjacent edges. In particular, you had to have already crossed that edge in the opposite direction. Thus, the edge has already been marked as a certain type of edge and thus won't be marked as a "forward edge".
Because of this, forward edges can only occur in directed graphs.
Now, just in case you mixed up "forward edges" and "tree edges", the edge you describe still isn't necessarily a tree edge. It is only a tree edge if when crossing, that was the first time you've visited the destination node. The easy way to think about it in undirected graphs is that when you traverse an edge, it is a back edge if the destination node has been reached already, and a tree edge otherwise.
I believe that what you are missing is some assumption about the order in which the algorithm would visit the different vertices.
Let's assume the algorithm visits the vertices in a lexicographic order. let's name the vertices this way:
-------
| |
S - A - B
| | |
C - D - E
In this case, the forward edges will be S->A, A->B, B->E, E->D, D->C. the rest of the edges are back edges.
Now let's rename the graph:
-------
| |
S - B - A
| | |
C - D - E
In this case, the forward edges will be S->A, A->B, B->D, D->C, D->E (note that S->A and S->B are not the same edge as in the previous example).
As you can see, the output depends on the order in which the algorithm selects the vertices. when the graph is anonymous, any output may be correct.
In the DFS tree of a general graph, there are TREE, FORWARD, BACK and CROSS edges.
In the DFS tree of an undirected graph, the would-be FORWARD edges are labeled as BACK edges.
The would-be CROSS edges are labeled as TREE edges.
In both cases, the reason is that the edges can be traversed in both directions, but you first encounter them as BACK and TREE and second time as FORWARD and maybe CROSS and they are already labeled.
In a sense, an edge is both FORWARD and BACK and can be both CROSS and TREE, but is first found as BACK and TREE, repectively.

Resources