# How to Convert a Directed Graph to an Undirected Graph (Adjacency Matrix) [closed]

Given an adjacency matrix, what is an algorithm/pseudo-code to convert a directed graph to an undirected graph without adding additional vertices (does not have to be reversable)?

similar question here

## closed as unclear what you're asking by David Richerby, Tom van der Zanden, Juho, cody, D.W.♦Apr 30 '16 at 21:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Convert in what way, with what properties? It's not at all clear what you're looking for in the resulting undirected graph. – David Richerby Apr 20 '16 at 6:13
• @DavidRicherby Any way is fine and just use the general properties of an adjacency matrix... Resulting undirected graph should be an adjacency matrix; I'm not sure what you mean really? – Ibrahim Apr 23 '16 at 22:59
• OK, fine. Convert any directed graph to the undirected graph with three vertices and no edges. Not acceptable? So it turns out that "any way" is not fine. So what are you looking for? You want to convert a digraph X into an undirected graph Y but you've not said anything at all about how Y should related to X. – David Richerby Apr 24 '16 at 16:38
• @DavidRicherby lets say each edge in a directed graph has 1 arrow pointing to some vertex, so the edges are 1way; so what I was asking is basically how to make the edges 2way. – Ibrahim Apr 28 '16 at 7:48
• What do you mean by "how to make the edges 2-way"? If you're asking how to write a program to do that, the question is off-topic because we don't do programming here. If you're not asking about programming, I can't see what you are asking. Just make every edge 2-way by adding the edge $(y,x)$ for every edge $(x,y)$, if it's not already there. – David Richerby Apr 28 '16 at 14:59

Note that for an undirected graph, the adjacency matrix is symmetric, i.e. A[i,j] == A[j,i].

From this, we can see that we can simply compute the new value of A[i,j] = A[j,i] depending if A[i,j] or A[j,i] is set. Assuming the graph is unweighted, we can do:

for i from 0 to n-1
for j from 0 to i
if A[i,j] == 1 OR A[j,i] == 1
A[i,j] = A[j,i] = 1
else
A[i,j] = A[j,i] = 0


Note that we only have to consider 1 + 2 + 3 + ... + n-1 entries since the resultant adjacency matrix is symmetric.

If we have a weighted graph, we now have the problem of which edge weight to take as the new undirected graph edge weight. For example, if w(2,5) = 5 but w(5,2) = 10, the resultant edge weight is ambiguous. However, this is enough for you to figure out what else you need from here.

• So for a weighted graph it would just be if A[i,j] !=0 { A[j,i] == A[i,j] } and vice versa? (given there is a maximum of 1 edge between any pair of vertices) – Ibrahim Apr 23 '16 at 23:07
• @Ibrahim Yes that's correct, in this case, you can just set A[i, j] = A[j, i] = max(A[i, j], A[j, i]), assuming you have no negative weight edges/you use 0 to represent the absence of an edge in the adjacency matrix. – Irvin Lim Apr 24 '16 at 3:56